Upgrade `content-disposition` (#7233)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Upgrade content-disposition * Better backward compat * chore: upgrade content-disposition to 2.0.1 and update History.md * test: add case for encoding latin1 file names with ASCII fallback in attachment * test: add cases for quoting invalid file names and handling empty file names in attachment ---------
authored by Blake Embrey · committed by GitHub · parent b3004cb · Browse files · 6 files·+72 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Sebastian Beltran <bjohansebas@gmail.com>
Diff stats
6 files+72 added−22 removed
- tests:+57 added−17 removed
- other:+15 added−5 removed
Provenance
- Commit
59e205a- Tree
63d34a8- Author
Blake Embrey <hello@blakeembrey.com>- Committer
GitHub <noreply@github.com>- Parent
b3004cb
6 files changed+72 added−22 removed
modified:lib/response.js+6 added-4 removed
29 unmodified lines30 var cookie = require('cookie');31 var send = require('send');32 var extname = path.extname;33 var resolve = path.resolve;34+var basename = path.basename;35 var vary = require('vary');36 const { Buffer } = require('node:buffer');37 38 /**415 unmodified lines454 }455 456 // set Content-Disposition when file is sent457 var headers = {457- 'Content-Disposition': contentDisposition(name || path)458+ 'Content-Disposition': contentDisposition.create(basename(name || path))459 };460 461 // merge user-provided headers462 if (opts && opts.headers) {139 unmodified lines602 * @public603 */604 605 res.attachment = function attachment(filename) {606+ const name = filename !== undefined ? basename(filename) : undefined;605- if (filename) {606- this.type(extname(filename));607+ if (name) {608+ this.type(extname(name));609 }610 609- this.set('Content-Disposition', contentDisposition(filename));611+ this.set('Content-Disposition', contentDisposition.create(name));612 613 return this;614 };615 434 unmodified linesmodified:test/acceptance/downloads.js+2 added-2 removed
13 unmodified lines141516171818192021222324252627272829303116 unmodified lines13 unmodified lines describe('GET /files/notes/groceries.txt', function () { it('should have a download header', function (done) { request(app) .get('/files/notes/groceries.txt') .expect('Content-Disposition', 'attachment; filename="groceries.txt"') .expect('Content-Disposition', 'attachment; filename=groceries.txt') .expect(200, done) }) })
describe('GET /files/amazing.txt', function(){ it('should have a download header', function(done){ request(app) .get('/files/amazing.txt') .expect('Content-Disposition', 'attachment; filename="amazing.txt"') .expect('Content-Disposition', 'attachment; filename=amazing.txt') .expect(200, done) }) })
16 unmodified linesmodified:test/res.attachment.js+41 added-1 removed
29 unmodified lines303132333434353637383940414243444546474849505152535455565758596061626364656621 unmodified lines88899091929394959697989910010110210310410510610710810910 unmodified lines29 unmodified lines });
request(app) .get('/') .expect('Content-Disposition', 'attachment; filename="image.png"', done); .expect('Content-Disposition', 'attachment; filename=image.png', done); })
it('should quote file names that are not a valid token', function(done){ var app = express();
app.use(function(req, res){ res.attachment('/path/to/my report.png'); res.send('foo'); });
request(app) .get('/') .expect('Content-Disposition', 'attachment; filename="my report.png"', done); })
it('should handle an empty file name', function(done){ var app = express();
app.use(function(req, res){ res.attachment(''); res.send('foo'); });
request(app) .get('/') .expect('Content-Disposition', 'attachment; filename=""', done); })
it('should set the Content-Type', function(done){ var app = express();
app.use(function(req, res){21 unmodified lines .expect('Content-Disposition', 'attachment; filename="???.txt"; filename*=UTF-8\'\'%E6%97%A5%E6%9C%AC%E8%AA%9E.txt') .expect(200, done); })
it('should encode latin1 file names with an ASCII fallback and filename* param', function(done){ var app = express();
app.use(function(req, res){ res.attachment('/locales/café.txt'); res.send('coffee'); });
request(app) .get('/') .expect('Content-Disposition', 'attachment; filename="caf?.txt"; filename*=UTF-8\'\'caf%C3%A9.txt') .expect(200, done); })
it('should set the Content-Type', function(done){ var app = express();
app.use(function(req, res){10 unmodified linesmodified:test/res.download.js+14 added-14 removed
22 unmodified lines23 24 request(app)25 .get('/')26 .expect('Content-Type', 'text/html; charset=utf-8')27- .expect('Content-Disposition', 'attachment; filename="user.html"')27+ .expect('Content-Disposition', 'attachment; filename=user.html')28 .expect(200, '<p>{{user.name}}</p>', done)29 })30 31 it('should accept range requests', function (done) {34 unmodified lines66 67 request(app)68 .get('/')69 .expect('Content-Type', 'text/html; charset=utf-8')70- .expect('Content-Disposition', 'attachment; filename="document"')70+ .expect('Content-Disposition', 'attachment; filename=document')71 .expect(200, done)72 })73 })74 8 unmodified lines83 84 request(app)85 .get('/')86 .expect('Content-Type', 'text/html; charset=utf-8')87- .expect('Content-Disposition', 'attachment; filename="user.html"')87+ .expect('Content-Disposition', 'attachment; filename=user.html')88 .expect(200, cb);89 })90 91 describe('async local storage', function () {20 unmodified lines112 113 request(app)114 .get('/')115 .expect('Content-Type', 'text/plain; charset=utf-8')116- .expect('Content-Disposition', 'attachment; filename="name.txt"')116+ .expect('Content-Disposition', 'attachment; filename=name.txt')117 .expect(200, 'tobi', cb)118 })119 120 it('should persist store on error', function (done) {40 unmodified lines161 162 request(app)163 .get('/')164 .expect(200)165- .expect('Content-Disposition', 'attachment; filename=".name"')165+ .expect('Content-Disposition', 'attachment; filename=.name')166 .expect('Cache-Control', 'public, max-age=14400')167 .expect(utils.shouldHaveBody(Buffer.from('tobi')))168 .end(done)169 })88 unmodified lines258 259 request(app)260 .get('/')261 .expect(200)262- .expect('Content-Disposition', 'attachment; filename="user.html"')262+ .expect('Content-Disposition', 'attachment; filename=user.html')263 .end(done)264 })265 266 it('should be ignored case-insensitively', function (done) {9 unmodified lines276 277 request(app)278 .get('/')279 .expect(200)280- .expect('Content-Disposition', 'attachment; filename="user.html"')280+ .expect('Content-Disposition', 'attachment; filename=user.html')281 .end(done)282 })283 })284 })10 unmodified lines295 296 request(app)297 .get('/')298 .expect(200)299- .expect('Content-Disposition', 'attachment; filename="name.txt"')299+ .expect('Content-Disposition', 'attachment; filename=name.txt')300 .expect(utils.shouldHaveBody(Buffer.from('tobi')))301 .end(done)302 })303 8 unmodified lines312 313 request(app)314 .get('/')315 .expect(200)316- .expect('Content-Disposition', 'attachment; filename="name.txt"')316+ .expect('Content-Disposition', 'attachment; filename=name.txt')317 .expect(utils.shouldHaveBody(Buffer.from('tobi')))318 .end(done)319 })320 45 unmodified lines366 367 request(app)368 .get('/')369 .expect('Content-Type', 'text/html; charset=utf-8')370- .expect('Content-Disposition', 'attachment; filename="document"')370+ .expect('Content-Disposition', 'attachment; filename=document')371 .expect(200, cb);372 })373 })374 10 unmodified lines385 request(app)386 .get('/')387 .expect(200)388 .expect('Content-Type', 'text/html; charset=utf-8')389- .expect('Content-Disposition', 'attachment; filename="document"')389+ .expect('Content-Disposition', 'attachment; filename=document')390 .end(cb)391 })392 393 it('should allow options to res.sendFile()', function (done) {8 unmodified lines402 403 request(app)404 .get('/')405 .expect(200)406- .expect('Content-Disposition', 'attachment; filename="document"')406+ .expect('Content-Disposition', 'attachment; filename=document')407 .expect('Cache-Control', 'public, max-age=14400')408 .expect(utils.shouldHaveBody(Buffer.from('tobi')))409 .end(done)410 })14 unmodified lines425 request(app)426 .get('/')427 .expect(200)428 .expect('Content-Type', 'text/x-custom')429- .expect('Content-Disposition', 'attachment; filename="document"')429+ .expect('Content-Disposition', 'attachment; filename=document')430 .end(done)431 })432 433 it('should be ignored case-insensitively', function (done) {11 unmodified lines445 request(app)446 .get('/')447 .expect(200)448 .expect('Content-Type', 'text/x-custom')449- .expect('Content-Disposition', 'attachment; filename="document"')449+ .expect('Content-Disposition', 'attachment; filename=document')450 .end(done)451 })452 })453 })34 unmodified linesmodified:History.md+8 added
17 unmodified lines18 ```19 20 * The default error handler now logs the full error object instead of only its stack trace, so nested details such as `Error.cause` and library-specific properties (e.g. Sequelize's `parent`/`original`) are no longer swallowed - by [@Nitin-Mohapatra](https://github.com/Nitin-Mohapatra) in [#6464](https://github.com/expressjs/express/pull/6464)21 22+* Upgrade `content-disposition` to `^2.0.0`, which changes the `Content-Disposition` header emitted by `res.download()`, `res.attachment()`, and `res.sendFile()`: file names that are valid HTTP tokens are no longer wrapped in quotes. This is equivalent per RFC 6266, but applications asserting on the exact header bytes should update their expectations - by [@blakeembrey](https://github.com/blakeembrey) in [#7233](https://github.com/expressjs/express/pull/7233)23+24+ ```js25+ res.attachment('user.html');26+ // before -> Content-Disposition: attachment; filename="user.html"27+ // after -> Content-Disposition: attachment; filename=user.html28+ ```29+30 ## ⚡ Performance31 32 * Avoid duplicate Content-Type header processing in `res.send()` when sending string responses without an explicit Content-Type header - by [@bjohansebas](https://github.com/bjohansebas) in [#6991](https://github.com/expressjs/express/pull/6991)33 3871 unmodified linesmodified:package.json+1 added-1 removed
32 unmodified lines33 ],34 "dependencies": {35 "accepts": "^2.0.0",36 "body-parser": "^2.2.1",37- "content-disposition": "^1.0.0",37+ "content-disposition": "^2.0.1",38 "content-type": "^2.0.0",39 "cookie": "^0.7.1",40 "cookie-signature": "^1.2.1",41 "debug": "^4.4.0",58 unmodified lines