Skip to content

feat(cache): decompress Content-Encoding on the S3 origin (#804) - #806

Merged
thiras merged 3 commits into
mainfrom
feat/s3-content-encoding-decompress-804
Jun 4, 2026
Merged

thiras merged 3 commits into
mainfrom
feat/s3-content-encoding-decompress-804

Conversation

@thiras

@thiras thiras commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #804.

The S3 origin previously rejected any non-identity Content-Encoding outright, while the HTTP origin transparently decompresses gzip/zstd. This left a real functional gap between the two first-class origin backends — operators on S3/R2/B2 had to pre-store canonical bytes or front their bucket with an HTTP CDN that strips encoding.

This PR reuses the HTTP origin's decode path on the S3 backend so encoded objects decode to canonical bytes before the engine's BLAKE3 verify runs, with the same strict/auto DecompressMode semantics.

Changes

  • New shared helper crates/cache/src/origin/decompress.rs (pub(crate)):
    • decode_stream — the gzip/zstd decoder sandwich, generic over the input byte stream so both backends share it.
    • resolve_encoding — collapses identity / known / unknown / strict-rejection into a single decision.
    • classify_encoding and typed_decoder_error (moved out of http.rs).
  • http.rs — refactored to call the shared helper. One intentional behavior change (see "Behavior change" below): compressed responses now report size_hint: None instead of the encoded Content-Length.
  • s3.rs — added a decompress: DecompressMode field (default Auto) and a with_decompress_mode builder; replaced the upfront encoding-reject with resolve_encoding + decode_stream.
  • Config wiring — decompress flows through the wire S3OriginConfig → ResolvedS3Config → resolve_s3_origin (defaults to Auto) → runtime build_origin S3 arm, mirroring the HTTP origin's knob.
[cache.origin]
kind = "s3"
bucket = "decdn-blobs"
region = "us-east-1"
decompress = "auto"   # default; "strict" refuses any non-identity encoding

Tests

  • s3_origin.rs integration tests: gzip decode, zstd decode, Strict rejects a known encoding, unknown encoding (br) → permanent error, identity passthrough.
  • config resolver tests: S3 defaults to Auto, S3 strict round-trips.
  • decompress.rs unit tests for the resolve_encoding policy table.

Behavior change (HTTP origin) — latent #804 bug fixed on a second backend

This is not a pure S3 extraction: the refactor also changes the HTTP origin's size_hint for compressed bodies from the encoded Content-Length to None. That fixes a latent instance of the #804 bug that existed on HTTP too — a compressed body whose encoded length fit under cache.buffered_max_bytes but decoded above it was routed into the buffer/drain path, whose cap (buffered_max_bytes) is applied to the decoded stream, and was falsely rejected as BlobTooLarge. Reporting None forces compressed bodies onto the streaming path, capped at max_blob_bytes by count_and_cap_stream. Pinned by a new HTTP regression test (http_origin_compressed_above_buffer_threshold_succeeds) — the twin of the S3 cache_engine_s3_compressed_above_buffer_threshold_succeeds. Also called out in CHANGELOG.md.

Default change (S3): the S3 backend's default flips from reject-all-Content-Encoding to decompress = "auto". Safe by construction (BLAKE3 verify over canonical bytes ⇒ a mis-decode fails closed as a hash mismatch), but operators who relied on the old blanket rejection should set decompress = "strict". Documented in CHANGELOG.md.

Notes

  • The decompressed-side max_bytes cap remains enforced by the engine's count_and_cap_stream, so a small compressed payload that decodes to a huge blob still fails fast at the engine seam (decompression-bomb protection preserved).
  • The pre-stream Content-Length > max_bytes short-circuit on compressed bodies still fires first on both backends (an encoded length over the blob cap already implies a decoded overrun under sane ratios); only the size_hint handed to the engine changed.
  • Strict/unknown-encoding rejections carry an operator runbook hint (store canonical bytes, or set decompress = "auto" for gzip/zstd).

Validation

cargo build, cargo clippy --tests, cargo fmt --check, and cargo nextest run -p decdn-cache -p decdn-common -p decdn-node all pass (476 + 441 tests).

The S3 origin previously rejected any non-identity Content-Encoding,
while the HTTP origin transparently decodes gzip/zstd. Reuse the HTTP
decode path on S3 so encoded objects decode to canonical bytes before
the engine's BLAKE3 verify, with the same strict/auto DecompressMode
semantics.

- Extract the shared gzip/zstd decoder, encoding classification, and a
  new resolve_encoding policy helper into origin/decompress.rs; refactor
  HttpOrigin to use it (behavior unchanged).
- Add a configurable `decompress` knob to the S3 origin config, wired
  through ResolvedS3Config and the runtime build_origin S3 arm,
  defaulting to Auto.

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings June 3, 2026 21:32

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request centralizes Content-Encoding handling by introducing a shared decompress module for both HTTP and S3 origin backends. It enables transparent decompression of gzip and zstd compressed bodies from S3 origins, mirroring the existing behavior of the HTTP origin, and exposes a decompress configuration option for S3. Since there are no review comments, I have no additional feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR closes #804 by bringing Content-Encoding decompression parity to the S3 origin backend, matching the HTTP origin’s existing DecompressMode::{Auto, Strict} semantics so that gzip/zstd-encoded objects decode to canonical bytes before the cache engine’s BLAKE3 verification.

Changes:

  • Introduces a shared origin::decompress helper to centralize encoding classification, strict/auto policy, and the gzip/zstd decode pipeline.
  • Refactors HttpOrigin to use the shared helper (intended behavior unchanged) and updates S3Origin to transparently decode gzip/zstd in Auto mode while preserving Strict rejection behavior.
  • Wires an optional decompress knob through config resolution and runtime origin construction; adds integration/unit tests and updates the changelog.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.

Show a summary per file
File Description
crates/node/src/runtime/mod.rs Passes resolved S3 decompress mode into the constructed S3Origin; updates runtime tests for the new resolved field.
crates/common/src/config/types.rs Adds decompress option to S3 origin config with documented semantics aligned to HTTP origin.
crates/common/src/config/resolved.rs Extends ResolvedS3Config with a resolved decompress mode (default Auto).
crates/common/src/config/mod.rs Wires S3 decompress through resolver, adds resolver tests, and updates existing config tests’ struct literals.
crates/cache/tests/s3_origin.rs Adds S3 integration tests covering gzip/zstd decode, strict rejection, unknown encoding rejection, and identity passthrough behavior.
crates/cache/src/origin/s3.rs Implements S3-side decode via shared resolve_encoding + decode_stream; adds decompress field and builder method.
crates/cache/src/origin/mod.rs Exposes the new shared decompress module within the origin subsystem.
crates/cache/src/origin/http.rs Refactors HTTP origin to delegate encoding policy/decoder plumbing to the shared helper.
crates/cache/src/origin/decompress.rs New shared module implementing resolve_encoding and decode_stream plus unit tests for the policy table.
CHANGELOG.md Updates documentation to reflect S3 Content-Encoding handling parity and the new configuration knob.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions

github-actions Bot commented Jun 3, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 87.12% (was 87.06%, ▲ +0.06%) — 27798/31908 lines

File Coverage Lines Δ
crates/cache/src/origin/decompress.rs 98.78% 81/82 new
crates/cache/src/origin/http.rs 92.07% 151/164 ▼ 1.35%
crates/cache/src/origin/mod.rs 75.68% 28/37 ● 0.00%
crates/cache/src/origin/s3.rs 93.26% 346/371 ▲ +1.80%
crates/common/src/config/mod.rs 96.28% 4815/5001 ▼ 0.01%
crates/common/src/config/resolved.rs 100.00% 13/13 ● 0.00%
crates/node/src/runtime/mod.rs 44.43% 522/1175 ▲ +0.24%
History
Commit Total Δ vs base
297ab12 86.65% ▲ +0.03%
45d73f8 86.68% ▲ +0.06%
55b1ef0 (HEAD) 87.12% ▲ +0.06%

…alsely rejected

Both the HTTP and S3 origins advertised the encoded Content-Length as
size_hint for compressed bodies. When that encoded length fit under
buffered_max_bytes, the engine took the buffer/drain path, whose cap
(buffered_max_bytes) is applied to the *decoded* stream — falsely
rejecting an in-bounds blob (decoded size between buffered_max_bytes and
max_blob_bytes) as BlobTooLarge. Inert for S3 until #804 made it
decompress; S3 is the common production origin, so the bug is now live.

Hand None for compressed bodies so they always take the streaming path,
where count_and_cap_stream checks the decoded running total against the
correct max_blob_bytes. The pre-stream advertised > max_bytes
short-circuit still fires first.

Add S3 failure-path tests at parity with the HTTP suite, exercising the
S3-specific ByteStream::into_async_read -> ReaderStream -> decode_stream
seam: truncated gzip -> Permanent DecompressionFailed (no retry burned),
decompression bomb -> BlobTooLarge via the decoded cap, BLAKE3 verify
over decoded bytes, plus a regression test for the size_hint fix.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@alpergundogdu alpergundogdu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review: request changes

The core engineering is sound — the extraction into decompress.rs is a faithful, byte-identical move of the error-classification logic; the S3 decoded stream genuinely streams through the same count_and_cap_stream cap seam (verified: size_hint: None for compressed ⇒ never drained ⇒ engine running-total cap applies); and the bomb/truncation/verify-over-canonical cases are tested specifically on the S3 SDK body adapter. Two things to address before merge.

High — "behavior-unchanged refactor" silently changes (and fixes) the HTTP path, untested

crates/cache/src/origin/http.rs:379-394: on main, HTTP reports size_hint: advertised_len even for compressed bodies; this PR changes it to None. That is a real behavior change to a second backend, not the pure extraction the description claims. It happens to be a correct latent-bug fix — the same #804 bug (encoded length under buffered_max_bytes routes a large decoded body into the drain path, which caps at buffered_max_bytes and falsely raises BlobTooLarge) existed on HTTP too, masked because the existing HTTP test only crosses 2 MiB < 4 MiB. But the fix ships untested on HTTP — the new buffer-threshold regression test was added for S3 only.

  • Please (a) call out the HTTP size_hint change explicitly in the PR description / CHANGELOG, and (b) add the HTTP twin of cache_engine_s3_compressed_above_buffer_threshold_succeeds (encoded < 4 MiB, decoded ~8 MiB, < 16 MiB blob cap) to pull_through.rs so the HTTP fix is pinned against regression. Optionally split the HTTP fix into its own commit.

Medium — strict-mode rejection lost its operator runbook hint

crates/cache/src/origin/s3.rs:510-515: the old reject message guided operators to "store canonical bytes or front the bucket with an HTTP CDN that strips encoding." The new UnsupportedEncoding context says what failed but not what to do, and fetch_with_strict_mode_rejects_gzip_encoding was loosened to drop the "canonical bytes" wording. Either enrich the strict-rejection context with the actionable hint (store canonical bytes / set decompress = "auto"), or confirm the runbook was updated. Lower urgency since strict is opt-in.

Backward-compat note (not a defect)

Default flips from reject-all-encoding to Auto/decompress. Safe — BLAKE3 verify runs over canonical bytes, so a mis-decode fails closed as HashMismatch — but worth one CHANGELOG sentence framing it as a default change for operators who relied on the old rejection as a guard.

…book hint

Address Alper's review on #806:

- High: the "behavior-unchanged" refactor also fixed a latent #804 bug on
  the HTTP origin (compressed size_hint advertised_len -> None). Pin it with
  the HTTP twin of the S3 buffer-threshold regression test, and call out the
  HTTP change + S3 default flip in the CHANGELOG.
- Medium: restore the operator runbook hint on the S3 strict/unknown-encoding
  rejection (store canonical bytes, or set decompress = "auto"); re-tighten
  the strict-mode test to pin the wording.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@thiras
thiras merged commit b25f3c2 into main Jun 4, 2026
19 checks passed
@thiras
thiras deleted the feat/s3-content-encoding-decompress-804 branch June 4, 2026 12:32
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.

cache: S3 origin does not support Content-Encoding decompression (HTTP origin does)

4 participants