Skip to content

Commit a2a1155

Browse files
KoyamaSoheihimanshiLt
authored andcommitted
examples: fix path traversal in downloads example
closes expressjs#4120
1 parent c9564b5 commit a2a1155

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

examples/downloads/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66

77
var express = require('../../');
88
var path = require('path');
9+
var resolvePath = require('resolve-path')
10+
911
var app = module.exports = express();
1012

13+
// path to where the files are stored on disk
14+
var FILES_DIR = path.join(__dirname, 'files')
15+
1116
app.get('/', function(req, res){
1217
res.send('<ul>' +
1318
'<li>Download <a href="/files/notes/groceries.txt">notes/groceries.txt</a>.</li>' +
@@ -20,7 +25,7 @@ app.get('/', function(req, res){
2025
// /files/* is accessed via req.params[0]
2126
// but here we name it :file
2227
app.get('/files/:file(*)', function(req, res, next){
23-
var filePath = path.join(__dirname, 'files', req.params.file);
28+
var filePath = resolvePath(FILES_DIR, req.params.file)
2429

2530
res.download(filePath, function (err) {
2631
if (!err) return; // file sent

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"multiparty": "4.2.2",
7676
"nyc": "15.1.0",
7777
"pbkdf2-password": "1.2.1",
78+
"resolve-path": "1.4.0",
7879
"should": "13.2.3",
7980
"supertest": "6.2.2",
8081
"vhost": "~3.0.2"

test/acceptance/downloads.js

+8
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,12 @@ describe('downloads', function(){
3636
.expect(404, done)
3737
})
3838
})
39+
40+
describe('GET /files/../index.js', function () {
41+
it('should respond with 403', function (done) {
42+
request(app)
43+
.get('/files/../index.js')
44+
.expect(403, done)
45+
})
46+
})
3947
})

0 commit comments

Comments
 (0)