Skip to content

fix: answer CORS preflight requests for the broadcast API - #2423

Open
hsusul wants to merge 2 commits into
supabase:mainfrom
hsusul:fix/broadcast-cors-preflight
Open

hsusul wants to merge 2 commits into
supabase:mainfrom
hsusul:fix/broadcast-cors-preflight

Conversation

@hsusul

@hsusul hsusul commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Summary

  • adds OPTIONS routes for /api/broadcast and /api/broadcast/:topic/events/:event, answered by Corsica with origins: "*" and allow_headers: :all
  • adds router tests for the preflight on both endpoints

Why

The broadcast REST endpoints pipe through :open_cors (Corsica, origins: "*", from #693), but Phoenix runs a pipeline only after a route matches. There is no OPTIONS route, so a browser's preflight gets Phoenix.Router.NoRouteError (404) with no CORS headers, and the browser never sends the POST. Corsica's docs call out this case. Even if the pipeline ran, allow_headers defaults to [], so a preflight asking for apikey, authorization or content-type would still be refused.

This doesn't show up behind a gateway that answers preflights itself (Kong in the hosted and self-hosted stacks). It does for a browser app calling standalone Realtime from another origin, for example the local dev setup.

The POST routes and their pipelines are unchanged. The tenant and auth plugs don't run for OPTIONS. passthrough_non_cors_requests: true makes these OPTIONS-only routes answer 200 when there's no Origin header instead of leaving the conn unsent.

Validation

  • both new tests failed on origin/main (ce84f7d7) with 404 and no route found for OPTIONS /api/broadcast...
  • with the fix: test/realtime_web/router_test.exs 2 passed. Each run on its own: broadcast_controller_test.exs 13, broadcast_single_controller_test.exs 34, openapi_controller_test.exs 2, page_controller_test.exs 7, plugs/auth_tenant_test.exs 14, plugs/assign_tenant_test.exs 4 passed
  • mix format --check-formatted, MIX_ENV=test mix credo --strict on the changed files, git diff --check
  • mix compile --force --warnings-as-errors, MIX_ENV=test mix compile --warnings-as-errors and mix format --check-formatted with the CI-pinned Elixir 1.19.5 / OTP 28 (elixir:1.19.5-otp-28 container)

I didn't run the full suite or dialyzer locally.

This branch has not been deployed

No deployments
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