Skip to content

Commit

Permalink
Merge pull request senchalabs#480 from aheckmann/malformedURI
Browse files Browse the repository at this point in the history
malformedURIs return 400
  • Loading branch information
tj committed Feb 23, 2012
2 parents 480e6bb + 9a20b6c commit d939271
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
22 changes: 21 additions & 1 deletion lib/middleware/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ exports = module.exports = function static(root, options){

exports.mime = mime;

/**
* decodeURIComponent.
*
* Allows V8 to only deoptimize this fn instead of all
* of send().
*
* @param {String} path
* @api private
*/

function decode(path){
try {
return decodeURIComponent(path);
} catch (err) {
return err;
}
}

/**
* Attempt to tranfer the requested file to `res`.
*
Expand Down Expand Up @@ -103,9 +121,11 @@ var send = exports.send = function(req, res, next, options){

// parse url
var url = parse(options.path)
, path = decodeURIComponent(url.pathname)
, path = decode(url.pathname)
, type;

if ('URIError: URI malformed' == path) return next(utils.error(400));

// null byte(s)
if (~path.indexOf('\0')) return next(utils.error(400));

Expand Down
10 changes: 9 additions & 1 deletion test/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ describe('connect.static()', function(){
})
})

describe('malformedURIs', function(){
it('should respond with 400', function(done){
app.request()
.get('/%')
.expect(400, done)
});
})

// TODO: node bug
// describe('on ENAMETOOLONG', function(){
// it('should next()', function(done){
Expand All @@ -179,4 +187,4 @@ describe('connect.static()', function(){
// .expect(404, done);
// })
// })
})
})

0 comments on commit d939271

Please sign in to comment.