Skip to content
Myr

Add "root" option to res.download

Before adoption: this commit came in with the history the project adopted, so no landing records it.

fixes #4834 closes #4855

authored by Tommaso Tofacchi · committed by Douglas Christopher Wilson · parent 4847d0e · Browse files · 3 files+78 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+78 added−1 removed

  • tests:+74 added−0 removed
  • other:+4 added−1 removed

3 files+78 added−1 removed

modified:lib/response.js+3 added-1 removed
580 unmodified lines
581 opts = Object.create(opts)582 opts.headers = headers583584 // Resolve the full path for sendFile585 var fullPath = resolve(path);585 var fullPath = !opts.root586 ? resolve(path)587 : path588589 // send file590 return this.sendFile(fullPath, opts, done)591};
568 unmodified lines
modified:test/res.download.js+74 added
1 unmodified line
23var after = require('after');4var Buffer = require('safe-buffer').Buffer5var express = require('..');6var path = require('path')7var request = require('supertest');8var utils = require('./support/utils')910var FIXTURES_PATH = path.join(__dirname, 'fixtures')1112describe('res', function(){13 describe('.download(path)', function(){14 it('should transfer as an attachment', function(done){15 var app = express();
164 unmodified lines
180 .expect('Content-Disposition', 'attachment; filename="document"')181 .end(done)182 })183 })184185 describe('with "root" option', function () {186 it('should allow relative path', function (done) {187 var app = express()188189 app.use(function (req, res) {190 res.download('name.txt', 'document', {191 root: FIXTURES_PATH192 })193 })194195 request(app)196 .get('/')197 .expect(200)198 .expect('Content-Disposition', 'attachment; filename="document"')199 .expect(utils.shouldHaveBody(Buffer.from('tobi')))200 .end(done)201 })202203 it('should allow up within root', function (done) {204 var app = express()205206 app.use(function (req, res) {207 res.download('fake/../name.txt', 'document', {208 root: FIXTURES_PATH209 })210 })211212 request(app)213 .get('/')214 .expect(200)215 .expect('Content-Disposition', 'attachment; filename="document"')216 .expect(utils.shouldHaveBody(Buffer.from('tobi')))217 .end(done)218 })219220 it('should reject up outside root', function (done) {221 var app = express()222223 app.use(function (req, res) {224 var p = '..' + path.sep +225 path.relative(path.dirname(FIXTURES_PATH), path.join(FIXTURES_PATH, 'name.txt'))226227 res.download(p, 'document', {228 root: FIXTURES_PATH229 })230 })231232 request(app)233 .get('/')234 .expect(403)235 .expect(utils.shouldNotHaveHeader('Content-Disposition'))236 .end(done)237 })238239 it('should reject reading outside root', function (done) {240 var app = express()241242 app.use(function (req, res) {243 res.download('../name.txt', 'document', {244 root: FIXTURES_PATH245 })246 })247248 request(app)249 .get('/')250 .expect(403)251 .expect(utils.shouldNotHaveHeader('Content-Disposition'))252 .end(done)253 })254 })255 })256257 describe('on failure', function(){258 it('should invoke the callback', function(done){
31 unmodified lines
modified:History.md+1 added
1unreleased2==========34 * Add "root" option to `res.download`5 * Deprecate string and non-integer arguments to `res.status`6 * Ignore `Object.prototype` values in settings through `app.set`/`app.get`7 * Support proper 205 responses using `res.send`8
3525 unmodified lines