Repository navigation
fix(self-hosted): add CORS headers to Edge Functions router errors - #52710
Conversation
Errors from the functions main worker (404, 401, runtime errors) relied on the API gateway for Access-Control-Allow-Origin. Once the gateway leaves CORS on /functions/v1 to the function (#52709), browsers can't read them. Add the same CORS headers the platform's functions relay sends with its own errors. Function responses are unchanged. Related: #52346 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
…GELOG entry The Allow-Origin check passed on Kong before the fix, because Kong adds Access-Control-Allow-Origin to every response. Also check Access-Control-Allow-Headers, which no gateway adds to non-preflight responses. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Braintrust eval reportEvals in progress... ⌛ |
…upabase#7472) ## Problem The stack's HttpProxy answers every preflight with 204 and `Access-Control-Allow-Origin: *`, and adds `*` to function responses that set no Allow-Origin. A function that restricts origins never sees the preflight, and its denied responses become readable from any origin. On the platform, and on self-hosted after supabase/supabase#52709, the function decides. Resolves [CLI-2923](https://linear.app/supabase/issue/CLI-2923/align-experimental-cli-httpproxy-with-self-hosted-gateway-changes). ## Solution 1. `HttpRoute.upstreamCors`: the proxy skips its CORS headers and preflight shortcut on that route. The proxy's own 502 keeps `*`. 2. The `/functions/v1` shared route sets it. 3. `serve.main.ts`: router errors (404, 401, worker errors, 500) send `Access-Control-Allow-Origin: *`, `Allow-Headers` and `Expose-Headers: sb-error-code`, matching supabase/supabase#52710, so browsers can read them on requests that skip the preflight. A preflight to a missing or crashing function still fails, since it is not a 2xx. Functions called from a browser must now handle CORS themselves, as on the platform. The legacy Kong path is unchanged. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
Aligns the local stack gateway's CORS responses with the hosted platform and adds a live parity test that keeps them aligned. ## Gateway changes (`packages/stack/src/HttpProxy.ts`) For routes where the gateway owns CORS (everything except `/functions/v1`): - Preflights return `200` (was `204`), list `GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS,TRACE,CONNECT`, and echo `Access-Control-Request-Headers`, defaulting to `accept-profile,apikey,authorization,x-client-info`. - Proxied responses keep the upstream's CORS headers and set `Access-Control-Allow-Origin` to the request origin; the gateway no longer adds allow-methods/allow-headers to them. - Responses the gateway generates itself (404, 502) carry `*` and the methods list. ## Live parity test `start.gateway-cors.live.test.ts` deploys an origin-restricted function (and a `verify_jwt = true` copy) to the live suite's project, serves the same functions from `stack start --runtime docker`, sends the same 10 probes to both, and asserts the stack's results equal staging's. Staging is the oracle; no expected headers are hard-coded. It runs in the existing live job. `GET /rest/v1/` is not probed: with a publishable key the platform answers 401 there and the stack 200, which is an auth difference, not a gateway CORS one. ## Gateway parity matrix Same 10 probes against a staging project, this branch's local stack (`stack start --runtime docker`), and self-hosted Docker with supabase/supabase#52709 and supabase/supabase#52710 applied. Cells read status / allow-origin / allow-credentials / allow-methods / allow-headers / expose-headers; "match" means identical to staging. "methods list" is `GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS,TRACE,CONNECT`. Self-hosted row 7 uses `FUNCTIONS_VERIFY_JWT=true`, the other rows `false`. | Probe | Staging (oracle) | Dev stack (this PR) | Self-hosted (52709+52710) | |---|---|---|---| | OPTIONS function, blocked origin | `403` / – / `true` / – / – / – | match | match | | OPTIONS function, allowed origin | `204` / `app origin` / `true` / – / – / – | match | match | | POST function, blocked origin | `200` / – / `true` / – / – / – | match | match | | POST function, allowed origin | `200` / `app origin` / `true` / – / – / – | match | match | | POST missing function | `404` / `*` / – / – / `authorization, x-client-info, apikey` / `sb-error-code` | match | match | | OPTIONS missing function | `404` / `*` / – / – / `authorization, x-client-info, apikey` / `sb-error-code` | match | match | | POST verify_jwt function, invalid Bearer | `401` / `*` / – / – / `authorization, x-client-info, apikey` / `sb-error-code` | match | match | | OPTIONS /rest/v1/, foreign origin | `200` / `*` / – / `methods list` / `accept-profile,apikey,authorization,x-client-info` / – | match | `200` / `request origin` / – / `GET,POST,PUT,PATCH,DELETE,OPTIONS,HEAD,CONNECT,TRACE` / (empty) / `*` | | OPTIONS /rest/v1/ with request headers, foreign origin | `200` / `*` / – / `methods list` / `authorization, x-custom-header` / – | match | `200` / `request origin` / – / `GET,POST,PUT,PATCH,DELETE,OPTIONS,HEAD,CONNECT,TRACE` / `authorization, x-custom-header` / `*` | | GET missing table, foreign origin | `404` / `request origin` / – / – / – / `PostgREST list` | match | `404` / `request origin` / – / – / – / `*` | The local stack matches staging on every probe. Self-hosted matches on all function probes; its REST CORS differs from staging and is outside this PR. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
Make function-routing and JWT error responses readable from browsers, while preserving sb-error-code exposure. Upstream-PR: supabase/supabase#52710 Upstream-Commit: fc78c1be53
* chore: align social sign-in terminology (#52109) Update the OAuth provider comment in the project chart. Upstream-PR: supabase/supabase#52109 Upstream-Commit: 831d6302c7 * fix: handle edge runtime router errors (#51353) Keep unhandled promise rejections from terminating the main worker. Return 503 BOOT_ERROR with a log reference for unexpected handler exceptions, and validate the JWT algorithm type. Upstream-PR: supabase/supabase#51353 Upstream-Commit: 68cebcd676 * feat: reclaim idle function workers under memory pressure (#51957) Embed the cgroup v2 memory monitor and mount it as a ConfigMap subPath. Retain the existing input-hash rollout and increase worker memory to 256 MiB. Set the shared 2Gi budget only in project Helm values and samples, including the git-sync values override. Direct CR users retain control of resources. Add operator-specific coverage for overlays, mounts and configuration rollout. Upstream-PR: supabase/supabase#51957 Upstream-Commit: b6f68a8459 * fix: add CORS headers to function router errors (#52710) Make function-routing and JWT error responses readable from browsers, while preserving sb-error-code exposure. Upstream-PR: supabase/supabase#52710 Upstream-Commit: fc78c1be53 * fix: delegate function CORS to user functions (#52709) Disable Envoy CORS only for the Functions route and attach CORS to its invalid-key response. Keep other API routes unchanged and add operator template coverage. Kong and upstream Docker test scripts are outside this operator port. Upstream-PR: supabase/supabase#52709 Upstream-Commit: 9dfece25cc * fix: discard removed Envoy upstream hosts (#52968) Discard removed hosts on DNS refresh in all seven conditional clusters. Keep discovery through Kubernetes Service addresses. Upstream-PR: supabase/supabase#52968 Upstream-Commit: 5583c2bd6f * chore: target self-hosted v0.8.4 (#53017) Update the six operator service images and the RustFS example to the upstream release versions. Align the two operator chart versions. Keep the independent charts/supabase chart out of scope. Upstream-PR: supabase/supabase#53017 Upstream-Commit: 189adda99d * fix: include memory pressure script in Docker build
Problem
Errors from the Edge Functions main worker (404
NOT_FOUND, 401 whenFUNCTIONS_VERIFY_JWT=true, runtime errors such asBOOT_ERRORorWORKER_ERROR) have noAccess-Control-Allow-Originheader of their own. They rely on the API gateway to add one. Once the gateway leaves CORS on/functions/v1/to the function (#52709), browsers can't read these errors, and clients get a network error instead of the error code.On the platform, these errors stay readable from browsers.
Related: #52346, #52363.
Solution
volumes/functions/main/index.ts: router errors sendAccess-Control-Allow-Origin: *,Access-Control-Allow-Headers: authorization, x-client-info, apikey, andAccess-Control-Expose-Headers: sb-error-code, as on the platform.tests/test-self-hosted.sh: the unknown-function check also expectsAccess-Control-Allow-Origin: *.Safe to merge alone, and should merge before #52709 (or together with it): #52709 alone leaves router errors unreadable by browsers.
Review instructions
docker/, start the stack and runsh tests/test-self-hosted.sh. Expect the "Unknown function returns Access-Control-Allow-Origin" check to pass.curl -si -X POST http://localhost:8000/functions/v1/missing-fn -H 'Origin: https://example.com'. Expect 404 withaccess-control-allow-origin.Verified locally on disposable Envoy and Kong stacks, alone and together with #52709: all smoke suites pass. With both applied, 404 and 401 router errors return
access-control-allow-origin: *.Checklist
/write-the-docsor/edit-the-docsskill, which applies the docs style guide (no docs changes)🤖 Generated with Claude Code