-
Notifications
You must be signed in to change notification settings - Fork 0
Add configurable CORS handling to proxy-metrics and proxy-health functions #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,9 +11,23 @@ | |||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.49.1' | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const corsHeaders = { | ||||||||||||||||||||||||||||||||||
| 'Access-Control-Allow-Origin': '*', | ||||||||||||||||||||||||||||||||||
| 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type', | ||||||||||||||||||||||||||||||||||
| const DEFAULT_ALLOWED_ORIGIN = 'https://pronto-talk-suite.lovable.app' | ||||||||||||||||||||||||||||||||||
| const ALLOWED_ORIGINS = new Set( | ||||||||||||||||||||||||||||||||||
| (Deno.env.get('PROXY_HEALTH_ALLOWED_ORIGINS') | ||||||||||||||||||||||||||||||||||
| ?? `${DEFAULT_ALLOWED_ORIGIN},http://localhost:5173,http://127.0.0.1:5173`) | ||||||||||||||||||||||||||||||||||
| .split(',') | ||||||||||||||||||||||||||||||||||
| .map((origin) => origin.trim()) | ||||||||||||||||||||||||||||||||||
| .filter(Boolean), | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| function getCorsHeaders(req?: Request) { | ||||||||||||||||||||||||||||||||||
| const requestOrigin = req?.headers.get('origin') ?? '' | ||||||||||||||||||||||||||||||||||
| const allowOrigin = ALLOWED_ORIGINS.has(requestOrigin) ? requestOrigin : DEFAULT_ALLOWED_ORIGIN | ||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This fallback makes the allowlist non-authoritative: even if Useful? React with 👍 / 👎. |
||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||
| 'Access-Control-Allow-Origin': allowOrigin, | ||||||||||||||||||||||||||||||||||
| 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type', | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
| 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type', | |
| 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type', | |
| 'Access-Control-Allow-Methods': 'GET, OPTIONS', |
Copilot
AI
Apr 27, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same fallback logic as proxy-metrics (ALLOWED_ORIGINS.has(requestOrigin) ? requestOrigin : DEFAULT_ALLOWED_ORIGIN) makes the default origin effectively always allowed, even if PROXY_HEALTH_ALLOWED_ORIGINS is configured to exclude it. If the goal is a configurable allowlist, consider making the default origin configurable too, or omitting Access-Control-Allow-Origin for disallowed origins.
| const allowOrigin = ALLOWED_ORIGINS.has(requestOrigin) ? requestOrigin : DEFAULT_ALLOWED_ORIGIN | |
| return { | |
| 'Access-Control-Allow-Origin': allowOrigin, | |
| 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type', | |
| Vary: 'Origin', | |
| } | |
| const headers: Record<string, string> = { | |
| 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type', | |
| Vary: 'Origin', | |
| } | |
| if (requestOrigin && ALLOWED_ORIGINS.has(requestOrigin)) { | |
| headers['Access-Control-Allow-Origin'] = requestOrigin | |
| } | |
| return headers |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,13 +19,26 @@ import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.45.0' | |||||||||||||||||||||||||||||||||||||||||||||
| const SUPABASE_URL = Deno.env.get('SUPABASE_URL')! | ||||||||||||||||||||||||||||||||||||||||||||||
| const SERVICE_ROLE_KEY = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')! | ||||||||||||||||||||||||||||||||||||||||||||||
| const SCRAPE_TOKEN = Deno.env.get('PROXY_METRICS_TOKEN') ?? '' | ||||||||||||||||||||||||||||||||||||||||||||||
| const DEFAULT_ALLOWED_ORIGIN = 'https://pronto-talk-suite.lovable.app' | ||||||||||||||||||||||||||||||||||||||||||||||
| const ALLOWED_ORIGINS = new Set( | ||||||||||||||||||||||||||||||||||||||||||||||
| (Deno.env.get('PROXY_METRICS_ALLOWED_ORIGINS') | ||||||||||||||||||||||||||||||||||||||||||||||
| ?? `${DEFAULT_ALLOWED_ORIGIN},http://localhost:5173,http://127.0.0.1:5173`) | ||||||||||||||||||||||||||||||||||||||||||||||
| .split(',') | ||||||||||||||||||||||||||||||||||||||||||||||
| .map((origin) => origin.trim()) | ||||||||||||||||||||||||||||||||||||||||||||||
| .filter(Boolean), | ||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const PROM_HEADERS = { | ||||||||||||||||||||||||||||||||||||||||||||||
| 'Content-Type': 'text/plain; version=0.0.4; charset=utf-8', | ||||||||||||||||||||||||||||||||||||||||||||||
| 'Cache-Control': 'no-store', | ||||||||||||||||||||||||||||||||||||||||||||||
| 'Access-Control-Allow-Origin': '*', | ||||||||||||||||||||||||||||||||||||||||||||||
| 'Access-Control-Allow-Headers': 'authorization, content-type', | ||||||||||||||||||||||||||||||||||||||||||||||
| 'Access-Control-Allow-Methods': 'GET, OPTIONS', | ||||||||||||||||||||||||||||||||||||||||||||||
| function getPromHeaders(req?: Request) { | ||||||||||||||||||||||||||||||||||||||||||||||
| const requestOrigin = req?.headers.get('origin') ?? '' | ||||||||||||||||||||||||||||||||||||||||||||||
| const allowOrigin = ALLOWED_ORIGINS.has(requestOrigin) ? requestOrigin : DEFAULT_ALLOWED_ORIGIN | ||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||
| 'Content-Type': 'text/plain; version=0.0.4; charset=utf-8', | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+31
to
+35
|
||||||||||||||||||||||||||||||||||||||||||||||
| 'Cache-Control': 'no-store', | ||||||||||||||||||||||||||||||||||||||||||||||
| 'Access-Control-Allow-Origin': allowOrigin, | ||||||||||||||||||||||||||||||||||||||||||||||
| 'Access-Control-Allow-Headers': 'authorization, content-type', | ||||||||||||||||||||||||||||||||||||||||||||||
| 'Access-Control-Allow-Methods': 'GET, OPTIONS', | ||||||||||||||||||||||||||||||||||||||||||||||
| Vary: 'Origin', | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+33
to
+41
|
||||||||||||||||||||||||||||||||||||||||||||||
| const allowOrigin = ALLOWED_ORIGINS.has(requestOrigin) ? requestOrigin : DEFAULT_ALLOWED_ORIGIN | |
| return { | |
| 'Content-Type': 'text/plain; version=0.0.4; charset=utf-8', | |
| 'Cache-Control': 'no-store', | |
| 'Access-Control-Allow-Origin': allowOrigin, | |
| 'Access-Control-Allow-Headers': 'authorization, content-type', | |
| 'Access-Control-Allow-Methods': 'GET, OPTIONS', | |
| Vary: 'Origin', | |
| } | |
| const headers: Record<string, string> = { | |
| 'Content-Type': 'text/plain; version=0.0.4; charset=utf-8', | |
| 'Cache-Control': 'no-store', | |
| 'Access-Control-Allow-Headers': 'authorization, content-type', | |
| 'Access-Control-Allow-Methods': 'GET, OPTIONS', | |
| Vary: 'Origin', | |
| } | |
| if (requestOrigin && ALLOWED_ORIGINS.has(requestOrigin)) { | |
| headers['Access-Control-Allow-Origin'] = requestOrigin | |
| } | |
| return headers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function introduces its own allowed-origin parsing and CORS header construction, but the codebase already provides
getCorsHeaders(req)/handleCors(req)insupabase/functions/_shared/validation.ts(with origin validation, localhost/preview handling, and security headers). Consider reusing/extending the shared helper to avoid CORS behavior diverging across edge functions.