Skip to content

fix(self-hosted): add CORS headers to Edge Functions router errors - #52710

Merged
aantti merged 2 commits into
masterfrom
fix/self-hosted-functions-cors-errors
Nov 4, 2026
Merged

aantti merged 2 commits into
masterfrom
fix/self-hosted-functions-cors-errors

Conversation

@aantti

@aantti aantti commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Errors from the Edge Functions main worker (404 NOT_FOUND, 401 when FUNCTIONS_VERIFY_JWT=true, runtime errors such as BOOT_ERROR or WORKER_ERROR) have no Access-Control-Allow-Origin header 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

  1. volumes/functions/main/index.ts: router errors send Access-Control-Allow-Origin: *, Access-Control-Allow-Headers: authorization, x-client-info, apikey, and Access-Control-Expose-Headers: sb-error-code, as on the platform.
  2. Function responses are unchanged: the function's own CORS headers apply.
  3. tests/test-self-hosted.sh: the unknown-function check also expects Access-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

  1. From docker/, start the stack and run sh tests/test-self-hosted.sh. Expect the "Unknown function returns Access-Control-Allow-Origin" check to pass.
  2. Run curl -si -X POST http://localhost:8000/functions/v1/missing-fn -H 'Origin: https://example.com'. Expect 404 with access-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

  • I have read CONTRIBUTING.md
  • If I wrote a new docs topic or edited an existing topic, I used the /write-the-docs or /edit-the-docs skill, which applies the docs style guide (no docs changes)

🤖 Generated with Claude Code

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]>
@vercel

vercel Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
design-system Ready Ready Preview Nov 3, 2026 7:43pm UTC
docs Ready Ready Preview, v0 Nov 3, 2026 7:43pm UTC
kb Ready Ready Preview Nov 3, 2026 7:43pm UTC
studio-self-hosted Ready Ready Preview Nov 3, 2026 7:43pm UTC
studio-staging Ready Ready Preview Nov 3, 2026 7:43pm UTC
ui-library Ready Ready Preview Nov 3, 2026 7:43pm UTC
zone-www-dot-com Ready Ready Preview, v0 Nov 3, 2026 7:43pm UTC
1 Skipped Deployment
Project Deployment Actions Updated
studio Ignored Ignored Nov 3, 2026 7:43pm UTC

Request Review

@aantti aantti added the self-hosted Related to self-hosted Supabase label Nov 3, 2026
@aantti
aantti requested review from a team as code owners November 3, 2026 15:35
@aantti
aantti requested a review from kanadgupta November 3, 2026 15:35
@aantti aantti self-assigned this Nov 3, 2026
@aantti
aantti removed the request for review from kanadgupta November 3, 2026 15:38
@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Walkthrough

Router-generated function and authentication errors now include shared CORS headers. The self-hosted smoke test checks that an unknown-function response includes Access-Control-Allow-Origin: *.

Changes: Router error CORS

Layer / File(s) Summary
Shared error response CORS
docker/volumes/functions/main/index.ts, docker/tests/test-self-hosted.sh
Function and authentication error responses now use shared CORS headers. The unknown-function smoke test checks for a wildcard Access-Control-Allow-Origin header.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Possibly related issues

Possibly related PRs

  • supabase/supabase#50618 — It adds structured router and runtime errors in the same error-response implementation.
  • supabase/supabase#52363 — It changes gateway CORS handling for /functions/v1, the path affected by this PR’s router error headers.
  • supabase/supabase#51353 — It changes getFunctionErrorResponse and authentication error handling, which this PR also updates.

Suggested labels: edge functions

Suggested reviewers: avallete, luizfelmach

Merge Risk: Minimal · up to 65d86

The change is ready to merge; no concrete correctness or integration risk remains established.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title "fix(self-hosted): add CORS headers to Edge Functions router errors" names the main change. The changeset adds Access-Control-Allow-Origin, Access-Control-Allow-Headers, and Access-Control-E…
Description check Passed The description follows the repository template. It has Problem and Solution sections with clear context, linked related PRs, and a numbered Review instructions section with concrete curl and smoke-te…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "fix(self-hosted): add CORS headers to Edge Functions router errors" names the main change. The changeset adds Access-Control-Allow-Origin, Access-Control-Allow-Headers, and Access-Control-Expose-Headers to the main worker's router error responses (404 NOT_FOUND, 401 JWT failures, and runtime errors), and updates the smoke test to match. The title is short, specific, and follows the conventional-commit style.

Full details: Description check

Explanation

The description follows the repository template. It has Problem and Solution sections with clear context, linked related PRs, and a numbered Review instructions section with concrete curl and smoke-test steps. The Checklist is present. The preview-links and additional-context sections are HTML comments in the template, so omitting them is expected. The description explains the required merge ordering with #52709 and reports local verification results.

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 2 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.
Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 93c4c75 and 65d86eb.

Files selected for processing: 2
  • docker/CHANGELOG.md
  • docker/tests/test-self-hosted.sh

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

…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]>
@aantti
aantti merged commit fc78c1b into master Nov 4, 2026
49 of 50 checks passed
@aantti
aantti deleted the fix/self-hosted-functions-cors-errors branch November 4, 2026 11:31
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Braintrust eval report

Evals in progress... ⌛

TFSebben pushed a commit to TFSebben/supabase_cli that referenced this pull request Nov 4, 2026
…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]>
kallebysantos pushed a commit to kallebysantos/supabase-cli that referenced this pull request Nov 5, 2026
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]>
luizfelmach added a commit to supabase-community/supabase-kubernetes that referenced this pull request Nov 11, 2026
Make function-routing and JWT error responses readable from browsers,
while preserving sb-error-code exposure.

Upstream-PR: supabase/supabase#52710
Upstream-Commit: fc78c1be53
luizfelmach added a commit to supabase-community/supabase-kubernetes that referenced this pull request Nov 12, 2026
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

self-hosted Related to self-hosted Supabase

Projects

Development

Successfully merging this pull request may close these issues.

2 participants