Skip to content

Commit 8f07028

Browse files
arhxamyusukebeautofix-ci[bot]
authored
fix(compress): set Vary: Accept-Encoding on negotiated responses (#5137)
* 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 --------- Co-authored-by: Yusuke Wada <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 8a0b18f commit 8f07028

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

‎src/middleware/compress/index.test.ts‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,93 @@ describe('Compress Middleware', () => {
259259
})
260260
})
261261

262+
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+
})
293+
294+
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+
})
301+
302+
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+
})
309+
310+
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+
})
317+
318+
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+
})
325+
326+
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+
})
331+
332+
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+
})
339+
340+
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+
})
348+
262349
describe('contentTypeFilter', () => {
263350
describe('RegExp', () => {
264351
const app = new Hono()

‎src/middleware/compress/index.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ const selectEncoding = (
4343
return best?.encoding
4444
}
4545

46+
const varyAcceptEncodingRegExp = /(?:^|,)\s*accept-encoding\s*(?:,|$)/i
47+
4648
/**
4749
* Compress Middleware for Hono.
4850
*
@@ -101,6 +103,18 @@ export const compress = (options?: CompressionOptions): MiddlewareHandler => {
101103
return
102104
}
103105

106+
// From here on the representation is selected from the request's Accept-Encoding,
107+
// so caches must key on that header. This is done before the negotiation result is
108+
// known, because an identity response served for a missing or unsupported
109+
// Accept-Encoding is just as much a negotiated representation: RFC 9110 lists
110+
// Accept-Encoding "or lack thereof" as a determining factor. Without this, a shared
111+
// cache could reuse the identity response for a client that does accept gzip.
112+
// https://www.rfc-editor.org/rfc/rfc9110#field.vary
113+
const current = ctx.res.headers.get('Vary')
114+
if (current !== '*' && !(current && varyAcceptEncodingRegExp.test(current))) {
115+
ctx.header('Vary', current ? `${current}, Accept-Encoding` : 'Accept-Encoding')
116+
}
117+
104118
const accepted = ctx.req.header('Accept-Encoding')
105119
const encoding = selectEncoding(accepted, candidates)
106120
if (!encoding || !ctx.res.body) {

0 commit comments

Comments
 (0)