@@ -6,19 +6,41 @@ var EventEmitter = require('events').EventEmitter,
6
6
mkdirp = require ( 'mkdirp' ) ,
7
7
_ = require ( 'lodash' ) ;
8
8
9
- module . exports = function ( options ) {
9
+ var upload_directory = null ;
10
+ var FileInfo = null ;
11
+ var username = null ;
12
+
13
+ function get_upload_directory ( ) {
14
+ return upload_directory ;
15
+ }
16
+
17
+ function validatePath ( relativePath , fileName ) {
18
+
19
+ relativePath = unescape ( relativePath ) ;
20
+ fileName = unescape ( fileName ) ;
10
21
11
- var FileInfo = require ( './fileinfo' ) (
12
- _ . extend ( {
13
- baseDir : options . uploadDir
14
- } , _ . pick ( options , 'minFileSize' , 'maxFileSize' , 'acceptFileTypes' ) )
15
- ) ;
22
+ var fullPath = relativePath + fileName ;
23
+ //Check for .. in relative path
24
+ var pathReg1 = / .* \. \. .* / ;
25
+ //Check that the fileName doesn't contain / or \
26
+ var pathReg2 = / ( .* ( \/ | \\ ) .* ) / ;
27
+ //Further validation on the name mostly ensures characters are alphanumeric
28
+ var pathReg3 = / ^ ( [ a - z A - Z 0 - 9 _ . ] | - ) * $ / ;
29
+
30
+ return ! ( pathReg1 . exec ( relativePath )
31
+ || pathReg2 . exec ( fileName )
32
+ || ! pathReg3 . exec ( fileName )
33
+ || pathReg1 . exec ( fullPath ) ) ;
34
+ }
35
+
36
+ module . exports = function ( options ) {
16
37
17
38
var UploadHandler = function ( req , res , callback ) {
18
39
EventEmitter . call ( this ) ;
19
40
this . req = req ;
20
41
this . res = res ;
21
42
this . callback = callback ;
43
+ username = req . session . user_id ;
22
44
} ;
23
45
require ( 'util' ) . inherits ( UploadHandler , EventEmitter ) ;
24
46
@@ -31,23 +53,6 @@ module.exports = function (options) {
31
53
} ;
32
54
33
55
UploadHandler . prototype . get = function ( ) {
34
- this . noCache ( ) ;
35
- var files = [ ] ;
36
- fs . readdir ( options . uploadDir ( ) , _ . bind ( function ( err , list ) {
37
- _ . each ( list , function ( name ) {
38
- var stats = fs . statSync ( options . uploadDir ( ) + '/' + name ) ,
39
- fileInfo ;
40
- if ( stats . isFile ( ) ) {
41
- fileInfo = new FileInfo ( {
42
- name : name ,
43
- size : stats . size
44
- } ) ;
45
- this . initUrls ( fileInfo ) ;
46
- files . push ( fileInfo ) ;
47
- }
48
- } , this ) ;
49
- this . callback ( files ) ;
50
- } , this ) ) ;
51
56
} ;
52
57
53
58
UploadHandler . prototype . post = function ( ) {
@@ -74,6 +79,7 @@ module.exports = function (options) {
74
79
form . uploadDir = options . tmpDir ;
75
80
form
76
81
. on ( 'fileBegin' , function ( name , file ) {
82
+ console . log ( "Begin file... " ) ;
77
83
tmpFiles . push ( file . path ) ;
78
84
var fileInfo = new FileInfo ( file ) ;
79
85
fileInfo . safeName ( ) ;
@@ -82,6 +88,20 @@ module.exports = function (options) {
82
88
self . emit ( 'begin' , fileInfo ) ;
83
89
} )
84
90
. on ( 'field' , function ( name , value ) {
91
+
92
+ if ( ! validatePath ( value , "" ) ) {
93
+ return ;
94
+ }
95
+ md5h = require ( 'MD5' ) ,
96
+ app_dir = require ( '../../../config' ) . app_dir ;
97
+ upload_directory = app_dir + '/users/' + md5h ( username ) + value ;
98
+
99
+ FileInfo = require ( './fileinfo' ) (
100
+ _ . extend ( {
101
+ baseDir : function ( ) { return get_upload_directory ( ) ; }
102
+ } , _ . pick ( options , 'minFileSize' , 'maxFileSize' , 'acceptFileTypes' ) )
103
+ ) ;
104
+
85
105
if ( name === 'redirect' ) {
86
106
redirect = value ;
87
107
}
@@ -99,33 +119,33 @@ module.exports = function (options) {
99
119
if ( options . imageTypes . test ( fileInfo . name ) ) {
100
120
_ . each ( options . imageVersions , function ( value , version ) {
101
121
// creating directory recursive
102
- if ( ! fs . existsSync ( options . uploadDir ( ) + '/' + version + '/' ) )
103
- mkdirp . sync ( options . uploadDir ( ) + '/' + version + '/' ) ;
122
+ if ( ! fs . existsSync ( get_upload_directory ( ) + '/' + version + '/' ) )
123
+ mkdirp . sync ( get_upload_directory ( ) + '/' + version + '/' ) ;
104
124
105
125
counter ++ ;
106
126
var opts = options . imageVersions [ version ] ;
107
127
imageMagick . resize ( {
108
128
width : opts . width ,
109
129
height : opts . height ,
110
- srcPath : options . uploadDir ( ) + '/' + fileInfo . name ,
111
- dstPath : options . uploadDir ( ) + '/' + version + '/' + fileInfo . name ,
130
+ srcPath : get_upload_directory ( ) + '/' + fileInfo . name ,
131
+ dstPath : get_upload_directory ( ) + '/' + version + '/' + fileInfo . name ,
112
132
customArgs : opts . imageArgs || [ '-auto-orient' ]
113
133
} , finish ) ;
114
134
} ) ;
115
135
}
116
136
}
117
137
118
- if ( ! fs . existsSync ( options . uploadDir ( ) + '/' ) )
119
- mkdirp . sync ( options . uploadDir ( ) + '/' ) ;
138
+ if ( ! fs . existsSync ( get_upload_directory ( ) + '/' ) )
139
+ mkdirp . sync ( get_upload_directory ( ) + '/' ) ;
120
140
121
141
counter ++ ;
122
- fs . rename ( file . path , options . uploadDir ( ) + '/' + fileInfo . name , function ( err ) {
142
+ fs . rename ( file . path , get_upload_directory ( ) + '/' + fileInfo . name , function ( err ) {
123
143
if ( ! err ) {
124
144
generatePreviews ( ) ;
125
145
finish ( ) ;
126
146
} else {
127
147
var is = fs . createReadStream ( file . path ) ;
128
- var os = fs . createWriteStream ( options . uploadDir ( ) + '/' + fileInfo . name ) ;
148
+ var os = fs . createWriteStream ( get_upload_directory ( ) + '/' + fileInfo . name ) ;
129
149
is . on ( 'end' , function ( err ) {
130
150
if ( ! err ) {
131
151
fs . unlinkSync ( file . path ) ;
@@ -157,16 +177,6 @@ module.exports = function (options) {
157
177
} ;
158
178
159
179
UploadHandler . prototype . destroy = function ( ) {
160
- var self = this ,
161
- fileName = path . basename ( decodeURIComponent ( this . req . url ) ) ;
162
-
163
- fs . unlink ( options . uploadDir ( ) + '/' + fileName , function ( ex ) {
164
- _ . each ( options . imageVersions , function ( value , version ) {
165
- fs . unlink ( options . uploadDir ( ) + '/' + version + '/' + fileName ) ;
166
- } ) ;
167
- self . emit ( 'delete' , fileName ) ;
168
- self . callback ( ! ex ) ;
169
- } ) ;
170
180
} ;
171
181
172
182
UploadHandler . prototype . initUrls = function ( fileInfo ) {
0 commit comments