Skip to content

feat(server): exact-origin CORS for the HTTP API - #826

Merged
samuv merged 2 commits into
sdk/02-problem-detailsfrom
sdk/03-cors
Aug 31, 2026
Merged

samuv merged 2 commits into
sdk/02-problem-detailsfrom
sdk/03-cors

Conversation

@samuv

@samuv samuv commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #825 → #823 → #822. Review those first.

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 reach mecated even 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.com matched by suffix also admits evil-example.com and example.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:

Origin Why it's dangerous
https://evil-app.example.com different subdomain
https://app.example.com.attacker.net allowed origin as a prefix of a hostile domain
https://notapp.example.com hostname merely ending with the allowed one
http://app.example.com same host, different scheme
https://app.example.com:8443 same host, different port
https://APP.example.com case variant
null the opaque origin any sandboxed page can present

Ordering is load-bearing: CORS wraps outside auth

A browser preflight is an unauthenticated OPTIONS request — 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_PreflightDoesNotInvokeHandler wraps a sentinel handler and asserts it is never 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 goes on every response the middleware sees — including refusals and requests with no Origin. The response depends on the Origin header, 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.com parses 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 real Origin. 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.

* and null are 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-Headers echoes 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 in serve. It's 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 — I moved it there specifically rather than raise the gocyclo threshold to accommodate a new branch.

Acceptance criteria

AC Test
3.1 exact origin allowed TestSDKServerEnablers_Scenario3_ExactOriginAllowed
3.2 near misses rejected TestSDKServerEnablers_Scenario3_NearMissOriginsRejected
3.3 no wildcard-with-credentials TestADR_0244_NoWildcardWithCredentials
3.4 preflight doesn't invoke the handler TestSDKServerEnablers_Scenario3_PreflightDoesNotInvokeHandler
3.5 default off is unchanged TestSDKServerEnablers_Scenario3_DefaultOffUnchanged
— startup validation is strict TestSDKServerEnablers_Scenario3_OriginValidationIsStrict

Docs

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.

Verification

task lint 0 issues across all modules. go test -race ./... green on root and engine. docs/lint green. mecademo prints a full offline session.

Scope note

cmd/mecak8s also mounts the HTTP handler and does not get --cors-origins here — the plan scopes Scenario 3 to mecated, and adding an unrequested flag to the k8s binary is scope creep. Easy follow-up if wanted.

Refs #821.

🤖 Generated with Claude Code

@jhrozek jhrozek 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 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)

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.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

samuv added a commit that referenced this pull request Aug 31, 2026
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]>
samuv added a commit that referenced this pull request Aug 31, 2026
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]>
samuv added a commit that referenced this pull request Aug 31, 2026
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]>
samuv added a commit that referenced this pull request Aug 31, 2026
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]>
samuv added a commit that referenced this pull request Aug 31, 2026
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]>
samuv added 2 commits August 31, 2026 12:31
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]>
@samuv
samuv merged commit fa72990 into main Aug 31, 2026
8 of 27 checks passed
@samuv
samuv deleted the sdk/03-cors branch August 31, 2026 10: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.

2 participants