Skip to content

Commit 99b0b40

Browse files
committed
Fixed traversal vulnerability
1 parent 87de3f1 commit 99b0b40

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

server.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ function serveFile(request, response, path, info){
8585
var router = beeline.route({
8686
'/`path...`': function(request, response, details){
8787
var path = process.cwd() + '/' + details.path;
88+
89+
if(~pathHelpers.relative(process.cwd(), path).indexOf('..')) {
90+
response.writeHead(401);
91+
response.end('Unauthorized');
92+
return;
93+
}
94+
8895
fs.stat(path, function(error, info){
8996
if(error){
9097
serverError(response, error);

test/traversal.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Author: Liang Gong
3+
* (colors dep removed)
4+
*/
5+
(function() {
6+
var http = require('http');
7+
var content;
8+
var url = 'http://localhost:8080/../../confidential.txt';
9+
10+
console.log('\t[directory traversal attack]: ' + url);
11+
12+
var content = '';
13+
14+
http.get(url, (res) => {
15+
res.on('data', (chunk) => {
16+
content += chunk.toString('utf-8');
17+
});
18+
res.on('end', () => {
19+
console.log('\t[directory traversal request response]: ' + content.toString('utf-8'));
20+
});
21+
});
22+
})();

0 commit comments

Comments
 (0)