Skip to content

fix(edge): a HEAD reply must never carry a body - #702

Open
ygd58 wants to merge 1 commit into
flop-labs:mainfrom
ygd58:fix-edge-head-body
Open

ygd58 wants to merge 1 commit into
flop-labs:mainfrom
ygd58:fix-edge-head-body

Conversation

@ygd58

@ygd58 ygd58 commented Sep 4, 2026

Copy link
Copy Markdown

What

stored() and revalidating() in edge/src/worker.js both build their reply from a GET regardless of the caller's own method: stored() always does env.ASSETS.fetch(new URL(request.url)), a GET by construction; revalidating() serves the shared copy, which is always a GET's. Neither checked the original request's method before returning a body-bearing Response.

Why this is the other half of an already-covered concern

test_a_head_request_can_never_become_the_stored_body already protects the shared cache from a HEAD's empty body landing in it and corrupting what later GET readers see. This fixes the adjacent, previously-uncovered direction: a HEAD caller receiving a GET's full body back, which HTTP forbids a HEAD reply from carrying at all.

Most concretely: HEAD /rooms — the one EDGE_REVALIDATE_SECONDS path today (added in #674) — would answer with the complete room listing on every HEAD instead of an empty body. The same gap exists for anything in STATIC_FIRST or EDGE_ONLY too, since both go through stored().

Fix

One check, at the single dispatch point in the exported fetch handler, after route() decides what to serve — rather than patching stored()/revalidating() separately in three places:

const response = await route(request, env, ctx);
if (request.method === "HEAD") {
  return new Response(null, { status: response.status, headers: response.headers });
}
return response;

Applies uniformly to every lane with one change.

Testing

  • New test test_a_head_reply_never_carries_a_body, following the existing source-text-assertion pattern this file already uses (no JS harness available, per _between's own docstring)
  • Verified the regression test actually catches the bug: reverted the fix locally, confirmed the test fails, restored the fix, confirmed it passes again
  • uv run pytest tests -q: 665 passed
  • uv run ruff check . / uv run ruff format --check . / uv run ty check: clean
  • python3 sz.py --check: unaffected — edge/ isn't in the core budget

Release note

Since CHANGELOG.md is maintainer-regenerated, proposed wording for [Unreleased]:

PATCH: a HEAD request to any edge-served document path (static-first, edge-only, or the revalidating /rooms lane) no longer receives the GET's full body back — the top-level fetch handler now strips it to null, matching HTTP's requirement that a HEAD reply carry no body.

Abuse impact

None. Pure correctness fix to an existing lane; no new route, parameter, or persistent state.

stored() and revalidating() both build their reply from a GET regardless of the
caller's own method: stored() always does env.ASSETS.fetch(new URL(request.url)),
a GET by construction, and revalidating() serves the shared copy, which is always
a GET's. Neither checked the original request's method before returning it.

This is the other half of the concern test_a_head_request_can_never_become_the_
stored_body already covers -- that test protects the shared cache from a HEAD's
empty body landing in it; this protects the HEAD caller itself from receiving a
GET's full body back, which HTTP forbids a HEAD reply from carrying.

Most concretely: HEAD /rooms, the one EDGE_REVALIDATE_SECONDS path today, would
answer with the full room listing on every HEAD instead of an empty body.

Fixed at the single dispatch point in the exported fetch handler, after route()
decides what to serve, rather than patching stored()/revalidating() separately --
applies uniformly to every lane (static-first, edge-only, revalidating) with one
change instead of three.

New regression test verified to fail against the unfixed code (confirmed by
reverting the fix locally and re-running) and pass with it restored.

665 tests pass, lint/format/ty/sz all green. No CHANGELOG.md edit -- maintainer-
regenerated per CONTRIBUTING.md; proposed wording is in the PR body.
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown

Open pull requests citing the same issues:

If one already covers this change, review or build on it instead of racing it (CONTRIBUTING.md "Overlapping work").

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

Reviewed exact head 41d223b0beb5e976563d2fa4883ab11da41706. The top-level Worker handler strips the body after
oute() resolves, so the rule applies uniformly to static-first, edge-only, and revalidating lanes while preserving status and headers. It complements the existing cache-protection test, which prevents a HEAD request from populating a shared GET cache with an empty body. The regression checks the central ordering and null-body construction; the PR reports full tests, Ruff, format, type, and size checks passing, and queue-guard checks are green. No blocker found.

@luch91

luch91 commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Correction to my review text: the exact verified head SHA is 41d223b. The approval was submitted against that head; only the SHA transcription in the review body was mistyped.

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

Reviewed exact head 41d223b0beb5e9767515632da4883ab11da41706. The Worker now applies one central HEAD-body strip after route() returns, so cached/static/revalidating lanes cannot return a GET body to a HEAD caller while still preserving status and headers. The test verifies the check is after route dispatch and constructs a null-body response, covering the cross-lane invariant rather than one branch. Protected-files and overlap checks are green. Approved.

@ygd58

ygd58 commented Sep 19, 2026

Copy link
Copy Markdown
Author

@sv Checking in on this one too -- approved by luch91 a couple weeks ago, no movement since. Let me know if it needs a rebase against current main.

@sv sv added the material Substantive: correctness, security, data durability, capacity, or user-visible behavior label Oct 19, 2026
@pepedesigner

Copy link
Copy Markdown

Correct and central: stored()/revalidating() build replies from a GET regardless of the caller's method, so a HEAD must be stripped once at the top-level handler, after route() decides what to serve. Rebuilding with new Response(null, {status, headers}) is the right shape (and keeps Content-Length/ETag meaningful).

One check: for a 304/204, constructing a new Response with a body-less status is fine; for a HEAD to a streamed response, ensure the original body is cancelled (response.body?.cancel()) so the origin connection is not left open. Add that if not present.

@sv sv added the bug Something isn't working label Nov 5, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working material Substantive: correctness, security, data durability, capacity, or user-visible behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants