Skip to content
Myr

fix(compress): set Vary: Accept-Encoding on negotiated responses (#5137)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(compress): set Vary: Accept-Encoding on compressed responses The compress middleware negotiates gzip/deflate from the request's Accept-Encoding header but never advertises Vary: Accept-Encoding on the response. A shared cache can then store the compressed body and serve it to a client that did not send a compatible Accept-Encoding, breaking that client. Add Accept-Encoding to the response's Vary header when compressing, appending to any existing Vary value and avoiding duplicates. * fix(compress): set Vary: Accept-Encoding on identity responses too Move the Vary handling ahead of the encoding negotiation so that a response left uncompressed — because Accept-Encoding was absent or offered nothing the middleware supports — is also marked as varying on Accept-Encoding. RFC 9110 lists Accept-Encoding "or lack thereof" as a determining factor, so without this a shared cache can reuse the identity response for a client that does accept gzip. The helper now takes the Context and writes through ctx.header(), which recreates the response when a handler returned a fetch() response. On Node that recreation inherits the immutable header guard from the original response, so the write can still throw; fall back to rebuilding the response around a fresh Headers instance in that case. * refactored * remove the node.js test * ci: apply automated fixes ---------

authored by Arham Amin · committed by GitHub · parent 8a0b18f · Browse files · 2 files+101 added−0 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: Yusuke Wada <yusuke@kamawada.com>
  • Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

Diff stats

2 files+101 added−0 removed

  • tests:+87 added−0 removed
  • other:+14 added−0 removed

2 files+101 added−0 removed

modified:src/middleware/compress/index.test.ts+87 added
257 unmodified lines
258 expect(res.headers.get('ETag')).toBeNull()259 })260 })261262 describe('Vary Header', () => {263 const app = new Hono()264 app.use('*', compress())265 app.get('/no-vary', (c) => {266 c.header('Content-Type', 'text/plain')267 c.header('Content-Length', '1024')268 return c.text('a'.repeat(1024))269 })270 app.get('/existing-vary', (c) => {271 c.header('Content-Type', 'text/plain')272 c.header('Content-Length', '1024')273 c.header('Vary', 'Cookie')274 return c.text('a'.repeat(1024))275 })276 app.get('/already-accept-encoding', (c) => {277 c.header('Content-Type', 'text/plain')278 c.header('Content-Length', '1024')279 c.header('Vary', 'Accept-Encoding')280 return c.text('a'.repeat(1024))281 })282 app.get('/wildcard-vary', (c) => {283 c.header('Content-Type', 'text/plain')284 c.header('Content-Length', '1024')285 c.header('Vary', '*')286 return c.text('a'.repeat(1024))287 })288 app.get('/not-compressible', (c) => {289 c.header('Content-Type', 'image/png')290 c.header('Content-Length', '1024')291 return c.body('a'.repeat(1024))292 })293294 it('should set Vary: Accept-Encoding when compressing', async () => {295 const res = await app.request('/no-vary', {296 headers: { 'Accept-Encoding': 'gzip' },297 })298 expect(res.headers.get('Content-Encoding')).toBe('gzip')299 expect(res.headers.get('Vary')).toBe('Accept-Encoding')300 })301302 it('should append Accept-Encoding to an existing Vary header', async () => {303 const res = await app.request('/existing-vary', {304 headers: { 'Accept-Encoding': 'gzip' },305 })306 expect(res.headers.get('Content-Encoding')).toBe('gzip')307 expect(res.headers.get('Vary')).toBe('Cookie, Accept-Encoding')308 })309310 it('should not duplicate Accept-Encoding when Vary already lists it', async () => {311 const res = await app.request('/already-accept-encoding', {312 headers: { 'Accept-Encoding': 'gzip' },313 })314 expect(res.headers.get('Content-Encoding')).toBe('gzip')315 expect(res.headers.get('Vary')).toBe('Accept-Encoding')316 })317318 it('should leave Vary: * unchanged', async () => {319 const res = await app.request('/wildcard-vary', {320 headers: { 'Accept-Encoding': 'gzip' },321 })322 expect(res.headers.get('Content-Encoding')).toBe('gzip')323 expect(res.headers.get('Vary')).toBe('*')324 })325326 it('should set Vary on an identity response when Accept-Encoding is absent', async () => {327 const res = await app.request('/no-vary')328 expect(res.headers.get('Content-Encoding')).toBeNull()329 expect(res.headers.get('Vary')).toBe('Accept-Encoding')330 })331332 it('should set Vary on an identity response when no offered encoding is accepted', async () => {333 const res = await app.request('/no-vary', {334 headers: { 'Accept-Encoding': 'br' },335 })336 expect(res.headers.get('Content-Encoding')).toBeNull()337 expect(res.headers.get('Vary')).toBe('Accept-Encoding')338 })339340 it('should not add a Vary header when the response is not eligible for compression', async () => {341 const res = await app.request('/not-compressible', {342 headers: { 'Accept-Encoding': 'gzip' },343 })344 expect(res.headers.get('Content-Encoding')).toBeNull()345 expect(res.headers.get('Vary')).toBeNull()346 })347 })348349 describe('contentTypeFilter', () => {350 describe('RegExp', () => {351 const app = new Hono()352 app.use('*', compress({ contentTypeFilter: /^application\/json/, threshold: 0 }))
183 unmodified lines
modified:src/middleware/compress/index.ts+14 added