Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/core/supabase-js/src/cors.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/**
* Canonical CORS configuration for Supabase Edge Functions
*
* This module exports CORS headers that stay synchronized with the Supabase SDK.
* When new headers are added to the SDK, they are automatically included here,
* preventing CORS errors in Edge Functions.
* This module exports CORS headers kept in sync with the headers the Supabase
* SDK sends. Using it in Edge Functions prevents CORS preflight errors when the
* SDK adds new headers — update SUPABASE_HEADERS whenever the SDK starts
* sending one (enforced by the unit tests).
*
* @example Basic usage
* ```typescript
Expand Down Expand Up @@ -34,13 +35,19 @@
* - apikey: Project API key
* - content-type: Standard HTTP content type
* - x-retry-count: Retry attempt number sent by postgrest-js on retried requests
* - traceparent: W3C trace context sent when `tracePropagation` is enabled
* - tracestate: Vendor-specific trace data sent alongside traceparent
* - baggage: W3C baggage sent alongside traceparent
*/
const SUPABASE_HEADERS = [
'authorization',
'x-client-info',
'apikey',
'content-type',
'x-retry-count',
'traceparent',
'tracestate',
'baggage',
].join(', ')

/**
Expand Down
29 changes: 23 additions & 6 deletions packages/core/supabase-js/test/unit/cors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,29 @@ describe('CORS Module', () => {
it('should include all Supabase custom headers', () => {
const allowedHeaders = corsHeaders['Access-Control-Allow-Headers']

// All 10 Supabase headers must be present
expect(allowedHeaders).toContain('authorization')
expect(allowedHeaders).toContain('x-client-info')
expect(allowedHeaders).toContain('apikey')
expect(allowedHeaders).toContain('content-type')
expect(allowedHeaders).toContain('x-retry-count')
// Pin the exact list: when the SDK starts sending a new header, this
// test must fail until the header is added to SUPABASE_HEADERS —
// otherwise browser preflights break for Edge Functions using corsHeaders.
expect(allowedHeaders.split(', ').sort()).toEqual(
[
'authorization',
'x-client-info',
'apikey',
'content-type',
'x-retry-count',
'traceparent',
'tracestate',
'baggage',
].sort()
)
})

it('should include the trace context headers sent when tracePropagation is enabled', () => {
const allowedHeaders = corsHeaders['Access-Control-Allow-Headers']

expect(allowedHeaders).toContain('traceparent')
expect(allowedHeaders).toContain('tracestate')
expect(allowedHeaders).toContain('baggage')
})

it('should include all HTTP methods including OPTIONS', () => {
Expand Down
Loading