Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

examples: defend from privilege elevation #4120

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion examples/downloads/index.js
Original file line number Diff line number Diff line change
@@ -6,8 +6,13 @@

var express = require('../../');
var path = require('path');
var resolvePath = require('resolve-path')

var app = module.exports = express();

// path to where the files are stored on disk
var FILES_DIR = path.join(__dirname, 'files')

app.get('/', function(req, res){
res.send('<ul>' +
'<li>Download <a href="/files/notes/groceries.txt">notes/groceries.txt</a>.</li>' +
@@ -20,7 +25,7 @@ app.get('/', function(req, res){
// /files/* is accessed via req.params[0]
// but here we name it :file
app.get('/files/:file(*)', function(req, res, next){
var filePath = path.join(__dirname, 'files', req.params.file);
var filePath = resolvePath(FILES_DIR, req.params.file)

res.download(filePath, function (err) {
if (!err) return; // file sent
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -75,6 +75,7 @@
"multiparty": "4.2.2",
"nyc": "15.1.0",
"pbkdf2-password": "1.2.1",
"resolve-path": "1.4.0",
"should": "13.2.3",
"supertest": "6.2.2",
"vhost": "~3.0.2"
8 changes: 8 additions & 0 deletions test/acceptance/downloads.js
Original file line number Diff line number Diff line change
@@ -36,4 +36,12 @@ describe('downloads', function(){
.expect(404, done)
})
})

describe('GET /files/../index.js', function () {
it('should respond with 403', function (done) {
request(app)
.get('/files/../index.js')
.expect(403, done)
})
})
})