Skip to content

fix(worker): no request waits on the Access key fetch of another request - #73

Merged
wojciechszyjka merged 2 commits into
mainfrom
fix/access-keys-request-scope
Nov 30, 2026
Merged

wojciechszyjka merged 2 commits into
mainfrom
fix/access-keys-request-scope

Conversation

@wojciechszyjka

@wojciechszyjka wojciechszyjka commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

The same fault #72 fixed for the Run data, in the Access key set of WP09 (#68): no request of a Worker isolate waits on the fetch of the Access keys that another request started.

Cause

remoteKeys in src/worker/access.ts is one per team domain and isolate (src/worker/access-entry.ts). It kept one pending promise for the fetch of https://<team>/cdn-cgi/access/certs, and every request that missed a kid awaited it.

In workerd, a request that awaits a promise another request's I/O settles goes on in that request's I/O context, and its next I/O is refused ("Cannot perform I/O on behalf of a different request"). If the request that started the fetch was cancelled, the others could also wait forever.

What changed

  • The isolate keeps the parsed keys and the time of the last fetch, never a fetch.

  • Who fetches: the request that misses a kid fetches for itself, at most once per JWKS_REFRESH_MS (10 s, as before).

  • A fetch already in flight: a request that misses a kid while another request's fetch is in flight does not await it. It waits on a timer of its own, polling every JWKS_POLL_MS (25 ms) for up to JWKS_WAIT_MS (3 s), for the keys that fetch leaves.

  • A fetch that never settles stops counting as in flight after that time, and the request refuses the assertion. The default fetch has the same time limit (AbortSignal.timeout(JWKS_WAIT_MS)), so "in flight" and "given up" agree.

  • Only the newest fetch writes its result (a generation number), so a fetch that settles late never puts back older keys.

  • A failed fetch keeps nothing: the old keys stay. The next request may try again JWKS_RETRY_MS (1 s) after the failure settled. The wait doubles for each failure in a row, up to the 10 s window. A made-up kid can still start at most one fetch per window while the team domain answers.

  • Key rotation is unchanged: a kid miss after the window fetches again.

  • Regression tests, test/worker/access/keys-request-scope.test.ts, in the style of request-scope.test.ts:

    • a request that misses a kid while another request's fetch is pending;
    • a fetch that never settles;
    • a failed fetch;
    • key rotation;
    • the backoff after failures;
    • a late fetch.

    The first three fail on main's code (d20a7df). The backoff and late-fetch tests fail on this PR's first commit (096b571). The rotation test is a guard.

Evidence (owner's Mac, 30 Nov 2026)

  • Unit tests at the head (8b1776f): test/worker/access/**, 71 passed (65 before, plus the 6 new).
  • Live refusal walk: node test/worker/access/live.ts against wrangler dev of node test/worker/matrix-build.ts indexed, at 096b571 and again at 8b1776f, gives 1940 of 1940 refused: 194 paths (191 built routes, 0 assets) x 5 attempts x GET, POST.
    • With the placeholder Access vars of wrangler.worker.jsonc, the gate refuses before any key is fetched.
    • So no live request reaches remoteKeys there. The unit tests carry the fetch behaviour.
  • yarn check: green.
  • yarn test at 8b1776f: 3128 passed and 22 failed, in the same 5 files that fail on main in this sandbox (test/validate/workflow-run, test/rentgen/golden, test/proxy/check-sh, test/receipts/presets-r2, test/receipts/harness), which refuses mktemp and the npm registry.

@wojciechszyjka

Copy link
Copy Markdown
Collaborator Author

🤖 om-auto-review-pr started a review of this PR (autofix on). Lock held until the review completes.

@wojciechszyjka wojciechszyjka left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

🤖 om-auto-review-pr: review of 8b1776f. Verdict: approve (posted as a comment, since an author cannot approve their own PR). The PR is left for the coordinating session to merge.

An independent reviewer found no security defect: the isolate keeps only the parsed keys and three numbers, and no key outside the latest applied set is accepted. Its findings are fixed in 8b1776f.

Majors (fixed):

  • After a failure, the retry was 1 s after the fetch started. It is now JWKS_RETRY_MS after the failure settled, doubled for each failure in a row up to the 10 s window.
  • A fetch slower than the wait could refuse requests that the old code let through. The default fetch now has the same 3 s time limit as the waiting requests.

Minors (fixed):

  • A fetch that settled late could overwrite newer keys. Only the newest fetch (a generation number) writes now.
  • The poll loop is now capped by count as well as by the clock.
  • There are tests for the backoff and for the late fetch. Both fail on the PR's first commit.

Validation at 8b1776f:

  • test/worker/access/**: 71 passed.
  • Live refusal walk: 1940 of 1940 refused.
  • yarn check: green.
  • yarn test: the same 22 sandbox-bound failures as main.

@wojciechszyjka

Copy link
Copy Markdown
Collaborator Author

🤖 om-auto-review-pr completed: APPROVED (as a comment on the author's own PR). Lock released.

@wojciechszyjka
wojciechszyjka merged commit 76b0cf2 into main Nov 30, 2026
5 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.

1 participant