Skip to content

Commit

Permalink
Fixed static() trailing backslash DoS vector. Closes #452
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jan 7, 2012
1 parent a02c338 commit 2b0e8d6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
7 changes: 1 addition & 6 deletions lib/middleware/limit.js
Expand Up @@ -31,11 +31,6 @@ module.exports = function limit(bytes){
? parseInt(req.headers['content-length'], 10)
: null;

// deny the request
function deny() {
req.destroy();
}

// self-awareness
if (req._limit) return next();
req._limit = true;
Expand All @@ -46,7 +41,7 @@ module.exports = function limit(bytes){
// limit
req.on('data', function(chunk){
received += chunk.length;
if (received > bytes) deny();
if (received > bytes) req.destroy();
});

next();
Expand Down
9 changes: 9 additions & 0 deletions lib/middleware/static.js
Expand Up @@ -198,7 +198,16 @@ var send = exports.send = function(req, res, next, options){
function callback(err) { done || fn(err); done = true }
req.on('close', callback);
req.socket.on('error', callback);
stream.on('error', callback);
stream.on('end', callback);
} else {
stream.on('error', function(err){
if (res.headerSent) {
console.error(err.stack);
} else {
next(err);
}
});
}
});
};
8 changes: 8 additions & 0 deletions test/static.js
Expand Up @@ -161,6 +161,14 @@ describe('connect.static()', function(){
})
})

describe('when a trailing backslash is given', function(){
it('should 500', function(done){
app.request()
.get('/todo.txt\\')
.expect(500, done);
})
})

// TODO: node bug
// describe('on ENAMETOOLONG', function(){
// it('should next()', function(done){
Expand Down

0 comments on commit 2b0e8d6

Please sign in to comment.