mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 10:51:13 -05:00
Centralize CORS configuration
Consolidate CORS handling by introducing a shared supabase/functions/_shared/cors.ts and migrate edge functions to import from it. Remove inline cors.ts usage across functions, standardize headers (including traceparent and x-request-id), and prepare for environment-aware origins.
This commit is contained in:
@@ -1,62 +1,10 @@
|
||||
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
|
||||
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'
|
||||
import { getAllowedOrigin, getCorsHeaders } from '../_shared/cors.ts'
|
||||
import { edgeLogger, startRequest, endRequest } from '../_shared/logger.ts'
|
||||
import { rateLimiters, withRateLimit } from '../_shared/rateLimiter.ts'
|
||||
import { formatEdgeError } from '../_shared/errorFormatter.ts'
|
||||
|
||||
// Environment-aware CORS configuration
|
||||
const getAllowedOrigin = (requestOrigin: string | null): string | null => {
|
||||
// If no origin header, it's not a CORS request (same-origin or server-to-server)
|
||||
if (!requestOrigin) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const environment = Deno.env.get('ENVIRONMENT') || 'development';
|
||||
|
||||
// Production allowlist - configure via ALLOWED_ORIGINS environment variable
|
||||
// Format: comma-separated list of origins, e.g., "https://example.com,https://www.example.com"
|
||||
const allowedOriginsEnv = Deno.env.get('ALLOWED_ORIGINS') || '';
|
||||
const allowedOrigins = allowedOriginsEnv.split(',').filter(origin => origin.trim());
|
||||
|
||||
// In development, only allow localhost and Replit domains - nothing else
|
||||
if (environment === 'development') {
|
||||
if (
|
||||
requestOrigin.includes('localhost') ||
|
||||
requestOrigin.includes('127.0.0.1') ||
|
||||
requestOrigin.includes('.repl.co') ||
|
||||
requestOrigin.includes('.replit.dev')
|
||||
) {
|
||||
return requestOrigin;
|
||||
}
|
||||
// Origin not allowed in development - log and deny
|
||||
edgeLogger.warn('CORS origin not allowed in development mode', { origin: requestOrigin });
|
||||
return null;
|
||||
}
|
||||
|
||||
// In production, only allow specific domains from environment variable
|
||||
if (allowedOrigins.includes(requestOrigin)) {
|
||||
return requestOrigin;
|
||||
}
|
||||
|
||||
// Origin not allowed in production - log and deny
|
||||
edgeLogger.warn('CORS origin not allowed in production mode', { origin: requestOrigin });
|
||||
return null;
|
||||
};
|
||||
|
||||
const getCorsHeaders = (allowedOrigin: string | null): Record<string, string> => {
|
||||
// If no allowed origin, return empty headers (no CORS access)
|
||||
if (!allowedOrigin) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return {
|
||||
'Access-Control-Allow-Origin': allowedOrigin,
|
||||
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
|
||||
'Access-Control-Allow-Methods': 'GET, POST, DELETE, OPTIONS',
|
||||
'Access-Control-Allow-Credentials': 'true',
|
||||
};
|
||||
};
|
||||
|
||||
// Helper to create authenticated Supabase client
|
||||
const createAuthenticatedSupabaseClient = (authHeader: string) => {
|
||||
const supabaseUrl = Deno.env.get('SUPABASE_URL')
|
||||
|
||||
Reference in New Issue
Block a user