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
Provenance
- Commit
0def9bb- Tree
3941266- Author
Tommaso Tofacchi <tofacchitommaso@gmail.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
4847d0e
3 files changed+78 added−1 removed
modified:lib/response.js+3 added-1 removed
580 unmodified lines581 opts = Object.create(opts)582 opts.headers = headers583 584 // Resolve the full path for sendFile585- var fullPath = resolve(path);585+ var fullPath = !opts.root586+ ? resolve(path)587+ : path588 589 // send file590 return this.sendFile(fullPath, opts, done)591 };568 unmodified linesmodified:test/res.download.js+74 added
1 unmodified line2 3 var after = require('after');4 var Buffer = require('safe-buffer').Buffer5 var express = require('..');6+var path = require('path')7 var request = require('supertest');8 var utils = require('./support/utils')9 10+var FIXTURES_PATH = path.join(__dirname, 'fixtures')11+12 describe('res', function(){13 describe('.download(path)', function(){14 it('should transfer as an attachment', function(done){15 var app = express();164 unmodified lines180 .expect('Content-Disposition', 'attachment; filename="document"')181 .end(done)182 })183 })184+185+ describe('with "root" option', function () {186+ it('should allow relative path', function (done) {187+ var app = express()188+189+ app.use(function (req, res) {190+ res.download('name.txt', 'document', {191+ root: FIXTURES_PATH192+ })193+ })194+195+ 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+ })202+203+ it('should allow up within root', function (done) {204+ var app = express()205+206+ app.use(function (req, res) {207+ res.download('fake/../name.txt', 'document', {208+ root: FIXTURES_PATH209+ })210+ })211+212+ 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+ })219+220+ it('should reject up outside root', function (done) {221+ var app = express()222+223+ app.use(function (req, res) {224+ var p = '..' + path.sep +225+ path.relative(path.dirname(FIXTURES_PATH), path.join(FIXTURES_PATH, 'name.txt'))226+227+ res.download(p, 'document', {228+ root: FIXTURES_PATH229+ })230+ })231+232+ request(app)233+ .get('/')234+ .expect(403)235+ .expect(utils.shouldNotHaveHeader('Content-Disposition'))236+ .end(done)237+ })238+239+ it('should reject reading outside root', function (done) {240+ var app = express()241+242+ app.use(function (req, res) {243+ res.download('../name.txt', 'document', {244+ root: FIXTURES_PATH245+ })246+ })247+248+ request(app)249+ .get('/')250+ .expect(403)251+ .expect(utils.shouldNotHaveHeader('Content-Disposition'))252+ .end(done)253+ })254+ })255 })256 257 describe('on failure', function(){258 it('should invoke the callback', function(done){31 unmodified linesmodified:History.md+1 added
1 unreleased2 ==========3 4+ * 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