Skip to content
Myr

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

6 files+72 added−22 removed

modified:lib/response.js+6 added-4 removed
29 unmodified lines
30var cookie = require('cookie');31var send = require('send');32var extname = path.extname;33var resolve = path.resolve;34var basename = path.basename;35var vary = require('vary');36const { Buffer } = require('node:buffer');3738/**
415 unmodified lines
454 }455456 // set Content-Disposition when file is sent457 var headers = {457 'Content-Disposition': contentDisposition(name || path)458 'Content-Disposition': contentDisposition.create(basename(name || path))459 };460461 // merge user-provided headers462 if (opts && opts.headers) {
139 unmodified lines
602 * @public603 */604605res.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 }610609 this.set('Content-Disposition', contentDisposition(filename));611 this.set('Content-Disposition', contentDisposition.create(name));612613 return this;614};615
434 unmodified lines
modified:test/acceptance/downloads.js+2 added-2 removed
modified:test/res.attachment.js+41 added-1 removed
modified:test/res.download.js+14 added-14 removed
22 unmodified lines
2324 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 })3031 it('should accept range requests', function (done) {
34 unmodified lines
6667 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 lines
8384 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 })9091 describe('async local storage', function () {
20 unmodified lines
112113 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 })119120 it('should persist store on error', function (done) {
40 unmodified lines
161162 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 lines
258259 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 })265266 it('should be ignored case-insensitively', function (done) {
9 unmodified lines
276277 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 lines
295296 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 lines
312313 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 lines
366367 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 lines
385 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 })392393 it('should allow options to res.sendFile()', function (done) {
8 unmodified lines
402403 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 lines
425 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 })432433 it('should be ignored case-insensitively', function (done) {
11 unmodified lines
445 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 lines
modified:History.md+8 added
17 unmodified lines
18 ```1920* 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)2122* 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)2324 ```js25 res.attachment('user.html');26 // before -> Content-Disposition: attachment; filename="user.html"27 // after -> Content-Disposition: attachment; filename=user.html28 ```2930## ⚡ Performance3132* 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 lines
modified:package.json+1 added-1 removed
32 unmodified lines
33 ],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