@@ -164,8 +164,8 @@ Mount.prototype.getCacheOptions = function (opt) {
164
164
return c
165
165
}
166
166
167
- // get a path from a url
168
- Mount . prototype . getPath = function ( u ) {
167
+ // get the path component from a URI
168
+ Mount . prototype . getUriPath = function ( u ) {
169
169
var p = url . parse ( u ) . pathname
170
170
171
171
// Encoded dots are dots
@@ -179,8 +179,7 @@ Mount.prototype.getPath = function (u) {
179
179
180
180
// Make sure it starts with a slash
181
181
p = p . replace ( / ^ \/ / , '/' )
182
-
183
- if ( p . match ( / [ \/ \\ ] \. \. [ \/ \\ ] / ) ) {
182
+ if ( ( / [ \/ \\ ] \. \. ( [ \/ \\ ] | $ ) / ) . test ( p ) ) {
184
183
// traversal urls not ever even slightly allowed. clearly shenanigans
185
184
// send a 403 on that noise, do not pass go, do not collect $200
186
185
return 403
@@ -202,8 +201,12 @@ Mount.prototype.getPath = function (u) {
202
201
u = u . substr ( this . url . length )
203
202
if ( u . charAt ( 0 ) !== '/' ) u = '/' + u
204
203
205
- p = path . join ( this . path , u )
206
- return p
204
+ return u
205
+ }
206
+
207
+ // get a path from a url
208
+ Mount . prototype . getPath = function ( u ) {
209
+ return path . join ( this . path , u )
207
210
}
208
211
209
212
// get a url from a path
@@ -223,25 +226,25 @@ Mount.prototype.serve = function (req, res, next) {
223
226
224
227
// querystrings are of no concern to us
225
228
if ( ! req . sturl )
226
- req . sturl = url . parse ( req . url ) . pathname
229
+ req . sturl = this . getUriPath ( req . url )
227
230
228
- var p = this . getPath ( req . sturl )
231
+ // don't allow dot-urls by default, unless explicitly allowed.
232
+ // If we got a 403, then it's explicitly forbidden.
233
+ if ( req . sturl === 403 || ( ! this . opt . dot && ( / ( ^ | \/ ) \. / ) . test ( req . sturl ) ) ) {
234
+ res . statusCode = 403
235
+ res . end ( 'Forbidden' )
236
+ return true
237
+ }
229
238
230
239
// Falsey here means we got some kind of invalid path.
231
240
// Probably urlencoding we couldn't understand, or some
232
241
// other "not compatible with st, but maybe ok" thing.
233
- if ( ! p ) {
242
+ if ( typeof req . sturl !== 'string' || req . sturl == '' ) {
234
243
if ( typeof next === 'function' ) next ( )
235
244
return false
236
245
}
237
246
238
- // don't allow dot-urls by default, unless explicitly allowed.
239
- // If we got a 403, then it's explicitly forbidden.
240
- if ( p === 403 || ! this . opt . dot && req . sturl . match ( / ( ^ | \/ ) \. / ) ) {
241
- res . statusCode = 403
242
- res . end ( 'Forbidden' )
243
- return true
244
- }
247
+ var p = this . getPath ( req . sturl )
245
248
246
249
// now we have a path. check for the fd.
247
250
this . cache . fd . get ( p , function ( er , fd ) {
0 commit comments