Skip to content

fix(api): accept a document ETag with a compression suffix in If-Match - #759

Merged
pikann merged 2 commits into
Paca-AI:masterfrom
msammu07:fix/api-if-match-encoded-etag
Nov 15, 2026
Merged

pikann merged 2 commits into
Paca-AI:masterfrom
msammu07:fix/api-if-match-encoded-etag

Conversation

@msammu07

Copy link
Copy Markdown
Contributor

Summary

Fixes a 400 on document writes that send back the ETag they received, when the request goes through the bundled gateway. Found while testing the next parts of #747 against the full stack; it is on master since #751.

What went wrong

  1. The gateway compresses responses (encode gzip in the Caddyfiles). Caddy appends the content coding to a strong ETag, so a document's "<hash>-<meta>" reaches the client as "<hash>-<meta>-gzip".
  2. The client sends that tag back unchanged in If-Match.
  3. The API's ETag parser required exactly 16 hex characters after the first -, so it answered 400 BAD_REQUEST ("If-Match must be a document content hash or ETag…").

If-None-Match was not affected through the gateway, because Caddy removes the suffix from that header itself. The API tests did not catch this because they talk to the API directly.

The fix

parseEntityTag accepts one content-coding suffix after the meta tag (gzip, br, zstd or deflate, any case) and drops it. If-Match reads the same content hash as before, and If-None-Match compares the same whole tag, with or without the suffix.

Still rejected: a suffix without a meta tag (<hash>-gzip), an unknown suffix, an empty suffix and two suffixes. If-Match answers 400 for those as before.

No change to the ETag the API sends, to the gateway config or to any client.

Type of Change

  • Bug fix

Testing

  • services/api, CI's commands in a Linux container: unit tests with -race and ./test/e2e/... pass.
    • if_match_hash_test.go: table cases for each accepted coding, upper case, weak and bare forms, and each rejected shape.
    • test/e2e/doc_content_hash_test.go: a write with the suffixed tag succeeds, a stale suffixed tag gets 412 VERSION_CONFLICT, and If-None-Match with the suffixed tag gets 304.
  • apps/e2e: new tests/docs/etag-through-gateway.spec.ts reads a document through the gateway and sends the returned ETag back in If-Match. It passes on a stack built from this branch. A direct request with the -gzip tag gave 400 on master and gives 200 here.
  • Browser specs (tests/docs and tests/realtime, chromium) on that stack: 215 passed, 6 skipped, 1 failed. The failure is docs.spec.ts:640 (edit own comment, uses Control+a on macOS), which fails the same way on master.

Checklist

  • The change is focused and scoped.
  • Related documentation is updated: none needed (code comments only).

A proxy that compresses the response appends the content coding to a
strong ETag. The bundled Caddy gateway (encode gzip) turns
"<hash>-<meta>" into "<hash>-<meta>-gzip", and a client that sent that
tag back in If-Match got 400 BAD_REQUEST.

parseEntityTag now accepts one gzip, br, zstd or deflate suffix (any
case) after the meta tag and drops it, so If-Match reads the same
content hash and If-None-Match compares the same whole tag. A suffix
without a meta tag, an empty, unknown or repeated suffix stay invalid.

@pullfrog pullfrog Bot 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.

ℹ️ No critical issues. One suggestion on the new gateway spec.

Reviewed changes

  • parseEntityTag accepts one coding suffix — if_match_hash.go now splits the segment after the meta tag and accepts gzip, br, zstd, deflate in any case, dropping the suffix from both the returned content hash and the whole tag. Unknown, empty and repeated suffixes, and a suffix with no meta tag, stay invalid.
  • Both preconditions share the change — ifMatchHash (doc PATCH and doc PUT by-path) reads the same content hash, and etagMatches (both document GETs) compares the unsuffixed tag. Caddy appends the coding only to strong ETags and strips it from If-None-Match, so the suffix only reaches the API on If-Match; tolerating it on the read side too is consistent with the PR's stated contract.
  • Unit coverage — if_match_hash_test.go adds accept/reject tables for If-Match and If-None-Match, including upper-case, weak, bare, stale and malformed shapes.
  • API-level e2e — doc_content_hash_test.go covers the suffixed current tag (200), a stale suffixed tag (412 VERSION_CONFLICT) and a suffixed If-None-Match (304).
  • Gateway-level spec — etag-through-gateway.feature / .spec.ts read a compressed document through Caddy and round-trip its ETag into If-Match, following the managed-copies.spec.ts prefix and cleanup conventions.

Verified locally: the handler unit tests pass, go vet ./test/e2e/ compiles the new e2e test, gofmt -l is clean. I also checked Caddy's encode handler source: it appends -<encodingName> to strong ETags and strips it from If-None-Match only, which matches the premise. No security change — If-Match already accepted a bare content hash, so ignoring the coding leaves the precondition no weaker.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot 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.

✅ No new issues found.

Reviewed changes

Reviewed the one commit since the prior pullfrog review (df22eb0 → 523fc79), which replaces the gateway spec's vacuous ETag check with a real assertion.

  • Gateway spec now proves the coding suffix — apps/e2e/tests/docs/etag-through-gateway.spec.ts requires the document read's ETag to match "<64-hex hash>-<16-hex meta>-gzip" instead of merely being truthy, so the spec fails on a stack where the gateway does not append the content coding — the gap the prior review flagged.

I checked the two external contracts the assertion rests on. Playwright 1.64.1's APIRequestContext.fetch seeds accept-encoding: gzip,deflate,br, overwrites it with the spec's gzip, and exposes response.rawHeaders untouched (only the body is decompressed), so read.headers().etag is the raw suffixed tag. Caddy's encode handler rewrites a strong ETag to <tag>-<encoding> only when it actually encodes, and all three bundled Caddyfiles enable gzip alone, so -gzip is the only suffix this stack can produce. docETag is a 64-hex content hash plus a 16-hex meta tag, matching the pattern.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pikann pikann 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.

LGTM! Thank you for the contribution! 🚀

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.

[Feature] Docs: efficient pull/push/content-check for coding agents (change feed, ETags, upsert by path, partial reads)

2 participants