|
3 | 3 | var after = require('after');
|
4 | 4 | var Buffer = require('safe-buffer').Buffer
|
5 | 5 | var express = require('..');
|
| 6 | +var path = require('path') |
6 | 7 | var request = require('supertest');
|
7 | 8 | var utils = require('./support/utils')
|
8 | 9 |
|
| 10 | +var FIXTURES_PATH = path.join(__dirname, 'fixtures') |
| 11 | + |
9 | 12 | describe('res', function(){
|
10 | 13 | describe('.download(path)', function(){
|
11 | 14 | it('should transfer as an attachment', function(done){
|
@@ -178,6 +181,77 @@ describe('res', function(){
|
178 | 181 | .end(done)
|
179 | 182 | })
|
180 | 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_PATH |
| 192 | + }) |
| 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_PATH |
| 209 | + }) |
| 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_PATH |
| 229 | + }) |
| 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_PATH |
| 245 | + }) |
| 246 | + }) |
| 247 | + |
| 248 | + request(app) |
| 249 | + .get('/') |
| 250 | + .expect(403) |
| 251 | + .expect(utils.shouldNotHaveHeader('Content-Disposition')) |
| 252 | + .end(done) |
| 253 | + }) |
| 254 | + }) |
181 | 255 | })
|
182 | 256 |
|
183 | 257 | describe('on failure', function(){
|
|
0 commit comments