Skip to content

fix: skip compression for 206 and Content-Range responses#402

Merged
mcollina merged 1 commit into
fastify:mainfrom
naveentehrpariya:fix-compress-range-responses
Jun 14, 2026
Merged

fix: skip compression for 206 and Content-Range responses#402
mcollina merged 1 commit into
fastify:mainfrom
naveentehrpariya:fix-compress-range-responses

Conversation

@naveentehrpariya

Copy link
Copy Markdown
Contributor

Summary

A 206 Partial Content response carries a Content-Range header whose byte offsets describe the original, unencoded payload. When @fastify/compress compresses such a response, the body shrinks but Content-Range still describes the pre-compression byte range — a range client stitching parts together gets corrupted data.

The same class of bug was recently fixed in hono/compress (honojs/hono#5023) and is tracked in expressjs/compression (#277).

Changes

index.js — two new guards in the noCompress check:

// don't compress partial content: Content-Range describes unencoded byte offsets
(reply.statusCode === 206) ||
(reply.getHeader('Content-Range') !== undefined) ||

test/global-compress.test.js — new Range Responses describe block with two tests:

  1. it should not compress 206 Partial Content responses
  2. it should not compress responses carrying a Content-Range header

Test

npm test
# 0 failures — new tests pass, all existing tests pass

Checklist

  • Tests added
  • All tests pass (npm test → 0 failures)

A 206 Partial Content response carries a Content-Range header whose
byte offsets refer to the original, unencoded payload. Compressing the
body invalidates those offsets, producing data that range clients
cannot safely reassemble.

Add two guards to the noCompress check:
- reply.statusCode === 206
- reply.getHeader('Content-Range') !== undefined

Mirrors the fix applied to @hono/compress (#5023) and the pending PR
against expressjs/compression (fastify#277).

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina merged commit 496e22a into fastify:main Jun 14, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants