| name | edge-function-cors-hardener |
| description | Implementa CORS restrictivo en Edge Functions de Supabase. Usa cuando veas Access-Control-Allow-Origin con *, seguridad de CORS, o tarea 2.2 del PLAN_MEJORAS.md. |
| allowed-tools | Read, Write, Edit, Bash, mcp_supabase |
Edge Function CORS Hardener
Skill para asegurar Edge Functions de Supabase con CORS restrictivo.
Problema
// INSEGURO: permite cualquier origen
'Access-Control-Allow-Origin': '*'
Solución
Crear módulo compartido con orígenes permitidos.
Archivos a Crear/Modificar
1. Crear supabase/functions/_shared/cors.ts
const ALLOWED_ORIGINS = [
'https://fullcolor.com.ec',
'https://www.fullcolor.com.ec',
'https://cotizador.fullcolor.com.ec',
Deno.env.get('ALLOWED_ORIGIN'),
].filter(Boolean) as string[]
export function getCorsHeaders(req: Request): HeadersInit {
const origin = req.headers.get('origin')
const allowedOrigin = ALLOWED_ORIGINS.includes(origin ?? '')
? origin
: ALLOWED_ORIGINS[0]
return {
'Access-Control-Allow-Origin': allowedOrigin ?? ALLOWED_ORIGINS[0],
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
}
}
export function corsResponse(req: Request): Response {
return new Response(null, {
status: 204,
headers: getCorsHeaders(req)
})
}
2. Actualizar Edge Functions
import { getCorsHeaders, corsResponse } from '../_shared/cors.ts'
Deno.serve(async (req) => {
// Manejar preflight
if (req.method === 'OPTIONS') {
return corsResponse(req)
}
// ... lógica de la función ...
return new Response(JSON.stringify(data), {
headers: { ...getCorsHeaders(req), 'Content-Type': 'application/json' }
})
})
Edge Functions a Actualizar
supabase/functions/generate-pdf/index.tssupabase/functions/send-email/index.tssupabase/functions/upsert-lead/index.ts
Instrucciones
- Crear
_shared/cors.ts - Actualizar cada Edge Function
- Configurar variable
ALLOWED_ORIGINen Supabase para desarrollo - Desplegar con
mcp_supabase_deploy_edge_function - Probar desde frontend
Verificación
- Abrir DevTools > Network
- Verificar que requests tienen el header correcto
- Intentar desde origen no permitido (debe fallar)