From dff17e87e8efb1d6bf4efdd1b120d48d5d676f09 Mon Sep 17 00:00:00 2001 From: NgoQuocViet2001 Date: Sat, 12 Sep 2026 22:22:48 +0700 Subject: [PATCH 1/4] fix(res.sendFile): treat the app etag setting as a default, not an override #6073 wired the application etag setting into res.sendFile to close #2294. The assignment is unconditional, so it also overwrites an etag the caller passed in -- and writes the setting onto the caller's own options object. send documents etag as an option and res.sendFile documents that other options are passed along to it, but this one could not get through: with the app default on, { etag: false } still sent an ETag, and with the app setting disabled, { etag: true } still sent none. Assign only when the caller left it undefined, which keeps #2294's behaviour for callers that pass no etag. --- lib/response.js | 8 ++++++-- test/res.sendFile.js | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/response.js b/lib/response.js index bc672434890..73475169292 100644 --- a/lib/response.js +++ b/lib/response.js @@ -398,8 +398,12 @@ res.sendFile = function sendFile(path, options, callback) { // create file stream var pathname = encodeURI(path); - // wire application etag option to send - opts.etag = this.app.enabled('etag'); + // wire application etag option to send, as a default only: every other + // option is passed along to send untouched, and overriding this one made + // send's documented `etag` option unreachable through res.sendFile + if (opts.etag === undefined) { + opts.etag = this.app.enabled('etag'); + } var file = send(req, pathname, opts); // transfer diff --git a/test/res.sendFile.js b/test/res.sendFile.js index 16eea79761e..0e4f1c6cf7a 100644 --- a/test/res.sendFile.js +++ b/test/res.sendFile.js @@ -92,6 +92,28 @@ describe('res', function(){ } }); + it('should let an explicit etag option turn the ETag off', function (done) { + var app = createApp(path.resolve(fixtures, 'name.txt'), { etag: false }); + + request(app) + .get('/') + .expect(handleHeaders) + .expect(200, done); + + function handleHeaders (res) { + assert(res.headers.etag === undefined); + } + }); + + it('should let an explicit etag option turn the ETag on', function (done) { + var app = createApp(path.resolve(fixtures, 'name.txt'), { etag: true }).disable('etag'); + + request(app) + .get('/') + .expect('ETag', /^(?:W\/)?"[^"]+"$/) + .expect(200, done); + }); + it('should 404 for directory', function (done) { var app = createApp(path.resolve(fixtures, 'blog')); From 16671ee2799eb5949c482d33792438883a3bf130 Mon Sep 17 00:00:00 2001 From: NgoQuocViet2001 Date: Tue, 20 Oct 2026 14:25:09 +0700 Subject: [PATCH 2/4] refactor(res.sendFile): use ??= for the etag default Restore the original one-line comment. An explicit `etag: null` now falls back to the app setting as well, same as undefined. --- lib/response.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/response.js b/lib/response.js index 73475169292..6ecaa71fbfc 100644 --- a/lib/response.js +++ b/lib/response.js @@ -398,12 +398,8 @@ res.sendFile = function sendFile(path, options, callback) { // create file stream var pathname = encodeURI(path); - // wire application etag option to send, as a default only: every other - // option is passed along to send untouched, and overriding this one made - // send's documented `etag` option unreachable through res.sendFile - if (opts.etag === undefined) { - opts.etag = this.app.enabled('etag'); - } + // wire application etag option to send + opts.etag ??= this.app.enabled('etag'); var file = send(req, pathname, opts); // transfer From 80318c47bbda6af3f69f4b6bbefb8aa7bfd82673 Mon Sep 17 00:00:00 2001 From: NgoQuocViet2001 Date: Fri, 23 Oct 2026 10:42:51 +0700 Subject: [PATCH 3/4] fix(res.sendFile): set the etag default on a copy of the options When the caller passes no etag, the app setting was written onto their own options object. Set it on an object that inherits from it instead, the same way res.download layers its headers. A spread copy would drop the options res.download passes through its prototype (root, dotfiles, maxAge). --- lib/response.js | 1 + test/res.sendFile.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/response.js b/lib/response.js index 6ecaa71fbfc..e855d5abf7d 100644 --- a/lib/response.js +++ b/lib/response.js @@ -398,6 +398,7 @@ res.sendFile = function sendFile(path, options, callback) { // create file stream var pathname = encodeURI(path); + opts = Object.create(opts); // wire application etag option to send opts.etag ??= this.app.enabled('etag'); var file = send(req, pathname, opts); diff --git a/test/res.sendFile.js b/test/res.sendFile.js index 0e4f1c6cf7a..007a10b533b 100644 --- a/test/res.sendFile.js +++ b/test/res.sendFile.js @@ -114,6 +114,20 @@ describe('res', function(){ .expect(200, done); }); + it('should not modify the options object', function (done) { + var options = {}; + var app = createApp(path.resolve(fixtures, 'name.txt'), options).disable('etag'); + + request(app) + .get('/') + .expect(utils.shouldNotHaveHeader('ETag')) + .expect(200, function (err) { + if (err) return done(err); + assert.deepStrictEqual(options, {}); + done(); + }); + }); + it('should 404 for directory', function (done) { var app = createApp(path.resolve(fixtures, 'blog')); From 1f8b4928c843f4bcae5910e791f4f8aefe3e8348 Mon Sep 17 00:00:00 2001 From: NgoQuocViet2001 Date: Wed, 28 Oct 2026 15:07:55 +0700 Subject: [PATCH 4/4] test(res.sendFile): simplify options check --- test/res.sendFile.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/test/res.sendFile.js b/test/res.sendFile.js index 007a10b533b..82f46baffae 100644 --- a/test/res.sendFile.js +++ b/test/res.sendFile.js @@ -121,11 +121,8 @@ describe('res', function(){ request(app) .get('/') .expect(utils.shouldNotHaveHeader('ETag')) - .expect(200, function (err) { - if (err) return done(err); - assert.deepStrictEqual(options, {}); - done(); - }); + .expect(() => assert.deepStrictEqual(options, {})) + .expect(200, done); }); it('should 404 for directory', function (done) {