Repository navigation
feat(server): exact-origin CORS for the HTTP API - #826
Conversation
jhrozek
left a comment
There was a problem hiding this comment.
Reviewed with a security-focused pass verifying every claim in the PR description directly against the code (not just trusting the write-up): 204-with-no-headers on refusal, unconditional Vary: Origin, the wildcard/null/case-sensitivity refusals, byte-exact matching, zero-cost when unset, and the outside-auth wrapping order in main.go. All confirmed. One low-severity test-coverage nit below — approving.
| // its own headers it may send. A fixed list would silently break | ||
| // any client that needs a header we did not predict. | ||
| if reqHeaders := r.Header.Get("Access-Control-Request-Headers"); reqHeaders != "" { | ||
| w.Header().Set("Access-Control-Allow-Headers", reqHeaders) |
There was a problem hiding this comment.
This header-echo path is correct on inspection — allowed is computed once before the preflight branch, and this line is only reachable inside if allowed, so there's no path where headers get echoed to an unvalidated origin. But unlike the methods/origin/status assertions, nothing in cors_test.go actually sends Access-Control-Request-Headers and checks it comes back echoed in Access-Control-Allow-Headers. Worth a test alongside the near-miss table, given that's the real spec for this file.
There was a problem hiding this comment.
Added in fcd85ba — TestADR_0244_PreflightEchoesRequestedHeaders, three cases: an allowed origin gets its exact request echoed, a refused origin gets nothing, and a preflight requesting no headers gets no header rather than an empty or invented one.
Your framing is why it was worth doing even though the code reads correctly: both failure modes here are enforced by a browser and invisible to a Go test client, so nothing in-process would have noticed a regression. The allowed case deliberately asks for x-mecatl-idempotency-key alongside the obvious two — a hardcoded list would still pass a test that only ever asked for content-type.
It also asserts Vary includes Access-Control-Request-Headers, which the echo makes load-bearing: without it a shared cache can serve one request's allowed-header set to a request that asked for a different one.
Addresses the #826 review. The near-miss table asserts methods, origin, and status, but nothing ever sent Access-Control-Request-Headers to see what came back — so the header-echo branch, which is the actual spec of that part of the middleware, had no test at all. The code was correct on inspection (allowed is computed once before the preflight branch and the echo is only reachable inside if allowed), but that is exactly the kind of correctness that a later refactor breaks silently. Both failure modes are enforced by a BROWSER and invisible to a Go test client: a fixed allow-list instead of an echo breaks any client needing a header we did not predict, and echoing to an unvalidated origin tells an attacker's page which headers it may send. Neither shows up as a failing request in-process. Three cases: an allowed origin gets its exact request echoed, a refused origin gets nothing, and a preflight that requests no headers gets no header rather than an empty or invented one. The allowed case deliberately asks for x-mecatl-idempotency-key alongside the obvious two — a hardcoded list would still pass a test that only ever asked for content-type. It also asserts Vary includes Access-Control-Request-Headers, which the echo makes load-bearing: without it a shared cache can serve one request's allowed-header set to a request that asked for a different one. Gates: go test green on internal/adapter/server. Refs #821. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Signed-off-by: Samuele Verzi <[email protected]>
Addresses the #826 review. The near-miss table asserts methods, origin, and status, but nothing ever sent Access-Control-Request-Headers to see what came back — so the header-echo branch, which is the actual spec of that part of the middleware, had no test at all. The code was correct on inspection (allowed is computed once before the preflight branch and the echo is only reachable inside if allowed), but that is exactly the kind of correctness that a later refactor breaks silently. Both failure modes are enforced by a BROWSER and invisible to a Go test client: a fixed allow-list instead of an echo breaks any client needing a header we did not predict, and echoing to an unvalidated origin tells an attacker's page which headers it may send. Neither shows up as a failing request in-process. Three cases: an allowed origin gets its exact request echoed, a refused origin gets nothing, and a preflight that requests no headers gets no header rather than an empty or invented one. The allowed case deliberately asks for x-mecatl-idempotency-key alongside the obvious two — a hardcoded list would still pass a test that only ever asked for content-type. It also asserts Vary includes Access-Control-Request-Headers, which the echo makes load-bearing: without it a shared cache can serve one request's allowed-header set to a request that asked for a different one. Gates: go test green on internal/adapter/server. Refs #821. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Signed-off-by: Samuele Verzi <[email protected]>
Addresses the #826 review. The near-miss table asserts methods, origin, and status, but nothing ever sent Access-Control-Request-Headers to see what came back — so the header-echo branch, which is the actual spec of that part of the middleware, had no test at all. The code was correct on inspection (allowed is computed once before the preflight branch and the echo is only reachable inside if allowed), but that is exactly the kind of correctness that a later refactor breaks silently. Both failure modes are enforced by a BROWSER and invisible to a Go test client: a fixed allow-list instead of an echo breaks any client needing a header we did not predict, and echoing to an unvalidated origin tells an attacker's page which headers it may send. Neither shows up as a failing request in-process. Three cases: an allowed origin gets its exact request echoed, a refused origin gets nothing, and a preflight that requests no headers gets no header rather than an empty or invented one. The allowed case deliberately asks for x-mecatl-idempotency-key alongside the obvious two — a hardcoded list would still pass a test that only ever asked for content-type. It also asserts Vary includes Access-Control-Request-Headers, which the echo makes load-bearing: without it a shared cache can serve one request's allowed-header set to a request that asked for a different one. Gates: go test green on internal/adapter/server. Refs #821. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Signed-off-by: Samuele Verzi <[email protected]>
Addresses the #826 review. The near-miss table asserts methods, origin, and status, but nothing ever sent Access-Control-Request-Headers to see what came back — so the header-echo branch, which is the actual spec of that part of the middleware, had no test at all. The code was correct on inspection (allowed is computed once before the preflight branch and the echo is only reachable inside if allowed), but that is exactly the kind of correctness that a later refactor breaks silently. Both failure modes are enforced by a BROWSER and invisible to a Go test client: a fixed allow-list instead of an echo breaks any client needing a header we did not predict, and echoing to an unvalidated origin tells an attacker's page which headers it may send. Neither shows up as a failing request in-process. Three cases: an allowed origin gets its exact request echoed, a refused origin gets nothing, and a preflight that requests no headers gets no header rather than an empty or invented one. The allowed case deliberately asks for x-mecatl-idempotency-key alongside the obvious two — a hardcoded list would still pass a test that only ever asked for content-type. It also asserts Vary includes Access-Control-Request-Headers, which the echo makes load-bearing: without it a shared cache can serve one request's allowed-header set to a request that asked for a different one. Gates: go test green on internal/adapter/server. Refs #821. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Signed-off-by: Samuele Verzi <[email protected]>
Addresses the #826 review. The near-miss table asserts methods, origin, and status, but nothing ever sent Access-Control-Request-Headers to see what came back — so the header-echo branch, which is the actual spec of that part of the middleware, had no test at all. The code was correct on inspection (allowed is computed once before the preflight branch and the echo is only reachable inside if allowed), but that is exactly the kind of correctness that a later refactor breaks silently. Both failure modes are enforced by a BROWSER and invisible to a Go test client: a fixed allow-list instead of an echo breaks any client needing a header we did not predict, and echoing to an unvalidated origin tells an attacker's page which headers it may send. Neither shows up as a failing request in-process. Three cases: an allowed origin gets its exact request echoed, a refused origin gets nothing, and a preflight that requests no headers gets no header rather than an empty or invented one. The allowed case deliberately asks for x-mecatl-idempotency-key alongside the obvious two — a hardcoded list would still pass a test that only ever asked for content-type. It also asserts Vary includes Access-Control-Request-Headers, which the echo makes load-bearing: without it a shared cache can serve one request's allowed-header set to a request that asked for a different one. Gates: go test green on internal/adapter/server. Refs #821. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Signed-off-by: Samuele Verzi <[email protected]>
PR 3 of the #821 server-enabler stack. Implements Scenario 3 of docs/acceptance/sdk-server-enablers.md (AC3.1-AC3.5) per ADR 0244. There was no CORS support at all, so a browser could not reach mecated even in local development. --cors-origins grants access one EXACT origin at a time; empty (the default) installs no middleware and responses are byte-identical to before. Exact means exact: byte equality after parse, no wildcard, no suffix match, no subdomain match. Suffix matching is the classic CORS bug — an allowlist of "example.com" matched by suffix also admits "evil-example.com" and "example.com.attacker.net" — and this HTTP API can START AGENT RUNS, so a wrong match is not information disclosure but arbitrary action taken with the victim's credentials. The near-miss table in the tests is the real specification: different subdomain, allowed-origin-as-prefix, different scheme, different port, trailing slash, case variant, and "null" are each asserted refused. ORDERING IS LOAD-BEARING: CORS wraps OUTSIDE auth. A browser preflight is an unauthenticated OPTIONS request — the CORS spec forbids sending credentials on it — so a policy installed inside the auth middleware would 401 every preflight and cross-origin access would never work at all. Wrapping outside is safe because a preflight is answered from headers alone: it never reaches a handler, never touches a session, never returns data. Pinned by TestSDKServerEnablers_Scenario3_PreflightDoesNotInvokeHandler, which wraps a sentinel handler and asserts it is not invoked. A refused preflight answers 204 with no CORS headers rather than 403: the absent headers are what make the browser block the real request, and matching the allowed status means a page cannot probe allowlist membership. Vary: Origin is set on EVERY response the middleware sees, including refusals and requests with no Origin. The response depends on the Origin header, so a cache that does not vary on it could serve one origin's CORS headers to another. Validation is strict and happens at startup, where the operator is present: "*" and "null" are refused by name (this policy sends credentials, and wildcard-with-credentials is forbidden), as is any origin with a path, query, fragment, or userinfo. The wildcard-host case was a real gap the tests caught — "https://*.example.com" PARSES as a valid literal hostname, so it was accepted and would then never match anything: a silently dead allowlist entry indistinguishable from a working one, which is the exact failure this validation exists to prevent. Preflight Access-Control-Allow-Headers echoes the requested headers rather than publishing a fixed list. That is safe precisely BECAUSE the origin has already passed exact-match allowlisting — we are telling an already-trusted origin which of its own headers it may send — and a fixed list would silently break any client needing a header we failed to predict. CORS construction lives in buildEdge alongside TLS and bearer auth rather than in serve. It is edge configuration in the same sense (it decides who may reach the listeners), it fails startup through the ONE error path serve already handles, and it keeps serve's cyclomatic complexity at its existing bound instead of raising the lint threshold to accommodate a new branch. user-docs: --cors-origins and --deployment-id added to the mecated flag table, a "Browsers and CORS" section explaining exact matching and pointing production deployments at a same-origin BFF, and a cross-link from the gRPC/HTTP page. task site:build passes. Gates: task lint 0 issues all modules; go test -race green on root and engine; docs/lint green; mecademo prints a full offline session. Refs #821. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Signed-off-by: Samuele Verzi <[email protected]>
Addresses the #826 review. The near-miss table asserts methods, origin, and status, but nothing ever sent Access-Control-Request-Headers to see what came back — so the header-echo branch, which is the actual spec of that part of the middleware, had no test at all. The code was correct on inspection (allowed is computed once before the preflight branch and the echo is only reachable inside if allowed), but that is exactly the kind of correctness that a later refactor breaks silently. Both failure modes are enforced by a BROWSER and invisible to a Go test client: a fixed allow-list instead of an echo breaks any client needing a header we did not predict, and echoing to an unvalidated origin tells an attacker's page which headers it may send. Neither shows up as a failing request in-process. Three cases: an allowed origin gets its exact request echoed, a refused origin gets nothing, and a preflight that requests no headers gets no header rather than an empty or invented one. The allowed case deliberately asks for x-mecatl-idempotency-key alongside the obvious two — a hardcoded list would still pass a test that only ever asked for content-type. It also asserts Vary includes Access-Control-Request-Headers, which the echo makes load-bearing: without it a shared cache can serve one request's allowed-header set to a request that asked for a different one. Gates: go test green on internal/adapter/server. Refs #821. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Signed-off-by: Samuele Verzi <[email protected]>
What
PR 3 of 9 in the #821 server-enabler stack. Implements Scenario 3 of
docs/acceptance/sdk-server-enablers.md— AC3.1–AC3.5 — per ADR 0244.Adds
mecated --cors-origins. There was no CORS support at all before, so a browser could not reachmecatedeven in local development.Exact means exact
Byte equality after parse. No wildcard, no suffix match, no subdomain match.
Suffix matching is the classic CORS bug — an allowlist of
example.commatched by suffix also admitsevil-example.comandexample.com.attacker.net. This HTTP API can start agent runs, so a wrong match isn't information disclosure, it's arbitrary action taken with the victim's credentials.The near-miss table in the tests is the real specification. Each of these is asserted refused:
https://evil-app.example.comhttps://app.example.com.attacker.nethttps://notapp.example.comhttp://app.example.comhttps://app.example.com:8443https://APP.example.comnullOrdering is load-bearing: CORS wraps outside auth
A browser preflight is an unauthenticated
OPTIONSrequest — the CORS spec forbids sending credentials on it. A policy installed inside the auth middleware would 401 every preflight and cross-origin access would never work at all.Wrapping outside is safe because a preflight is answered from headers alone: it never reaches a handler, never touches a session, never returns data.
TestSDKServerEnablers_Scenario3_PreflightDoesNotInvokeHandlerwraps a sentinel handler and asserts it is never invoked.A refused preflight answers
204with no CORS headers rather than403. The absent headers are what make the browser block the real request, and matching the allowed status means a page cannot probe allowlist membership.Vary: Origingoes on every response the middleware sees — including refusals and requests with noOrigin. The response depends on theOriginheader, so a cache that doesn't vary on it could serve one origin's CORS headers to another.A real gap the tests caught
https://*.example.comparses as a valid URL with the literal hostname*.example.com, so my first validator accepted it — as an entry that could then never match any realOrigin. A silently dead allowlist entry, indistinguishable from a working one, which is the exact failure this validation exists to prevent. Now refused by name, with an error saying why.*andnullare likewise refused explicitly rather than falling out of the parse: they're what an operator is most likely to try, and the refusal should explain that this policy sends credentials and wildcard-with-credentials is forbidden.Two judgement calls worth reviewing
Preflight
Access-Control-Allow-Headersechoes the requested headers instead of publishing a fixed list. Safe precisely because the origin already passed exact-match allowlisting — we're telling an already-trusted origin which of its own headers it may send — and a fixed list would silently break any client needing a header we failed to predict. Push back if you'd rather pin a list.CORS construction lives in
buildEdge, alongside TLS and bearer auth, not inserve. It's edge configuration in the same sense (it decides who may reach the listeners), it fails startup through the one error pathservealready handles, and it keepsserve's cyclomatic complexity at its existing bound — I moved it there specifically rather than raise thegocyclothreshold to accommodate a new branch.Acceptance criteria
TestSDKServerEnablers_Scenario3_ExactOriginAllowedTestSDKServerEnablers_Scenario3_NearMissOriginsRejectedTestADR_0244_NoWildcardWithCredentialsTestSDKServerEnablers_Scenario3_PreflightDoesNotInvokeHandlerTestSDKServerEnablers_Scenario3_DefaultOffUnchangedTestSDKServerEnablers_Scenario3_OriginValidationIsStrictDocs
user-docs:--cors-originsand--deployment-idadded to themecatedflag table, a Browsers and CORS section explaining exact matching and pointing production deployments at a same-origin BFF, and a cross-link from the gRPC/HTTP page.task site:buildpasses.Verification
task lint0 issues across all modules.go test -race ./...green on root and engine.docs/lintgreen.mecademoprints a full offline session.Scope note
cmd/mecak8salso mounts the HTTP handler and does not get--cors-originshere — the plan scopes Scenario 3 tomecated, and adding an unrequested flag to the k8s binary is scope creep. Easy follow-up if wanted.Refs #821.
🤖 Generated with Claude Code