Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/gilad61/gzippo
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgco committed Jun 20, 2012
2 parents 90cc076 + 4877231 commit a708d94
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/staticGzip.js
Expand Up @@ -96,6 +96,8 @@ exports = module.exports = function staticGzip(dirPath, options){
if (!dirPath) throw new Error('You need to provide the directory to your static content.'); if (!dirPath) throw new Error('You need to provide the directory to your static content.');
if (!contentTypeMatch.test) throw new Error('contentTypeMatch: must be a regular expression.'); if (!contentTypeMatch.test) throw new Error('contentTypeMatch: must be a regular expression.');


dirPath = path.normalize(dirPath);

return function staticGzip(req, res, next){ return function staticGzip(req, res, next){
var url, filename, contentType, acceptEncoding, charset; var url, filename, contentType, acceptEncoding, charset;


Expand Down Expand Up @@ -134,6 +136,14 @@ exports = module.exports = function staticGzip(dirPath, options){
}); });
} }


function forbidden(res) {
var body = 'Forbidden';
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Length', body.length);
res.statusCode = 403;
res.end(body);
};

if (req.method !== 'GET' && req.method !== 'HEAD') { if (req.method !== 'GET' && req.method !== 'HEAD') {
return next(); return next();
} }
Expand All @@ -145,7 +155,11 @@ exports = module.exports = function staticGzip(dirPath, options){
return next(); return next();
} }


filename = path.join(dirPath, url.pathname.substring(prefix.length)); filename = path.normalize(path.join(dirPath, url.pathname.substring(prefix.length)));
// malicious path
if (0 != filename.indexOf(dirPath)){
return forbidden(res);
}


contentType = mime.lookup(filename); contentType = mime.lookup(filename);
charset = mime.charsets.lookup(contentType, 'UTF-8'); charset = mime.charsets.lookup(contentType, 'UTF-8');
Expand Down

0 comments on commit a708d94

Please sign in to comment.