fix(security): remove localhost CORS origin, consolidate CORS in proxy#4658
fix(security): remove localhost CORS origin, consolidate CORS in proxy#4658waleedlatif1 wants to merge 1 commit into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
️✅ There are no secrets present in this pull request anymore.If these secrets were true positive and are still valid, we highly recommend you to revoke them. 🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request. |
PR SummaryMedium Risk Overview This removes static CORS headers from Reviewed by Cursor Bugbot for commit 3a8349d. Configure here. |
Greptile SummaryThis PR fixes a production CORS misconfiguration where
Confidence Score: 3/5The reflected-origin form route path needs Vary: Origin and the double CORS header application needs resolution before this is production-safe for form embeds. The reflected-origin form route path sets Access-Control-Allow-Origin dynamically but omits Vary: Origin, a cache-poisoning vector for any CDN in front of the app. addCorsHeaders is also still called inside the form route handler while the middleware sets the same headers, risking duplicate Access-Control-Allow-Origin values that browsers reject. apps/sim/proxy.ts (applyCorsHeaders, buildPreflightResponse, and the /api/form branch of resolveApiCorsPolicy) and apps/sim/app/api/form/[identifier]/route.ts (addCorsHeaders calls that now overlap with middleware)
|
| Filename | Overview |
|---|---|
| apps/sim/proxy.ts | New CORS policy engine added; reflects Origin for form routes without Vary: Origin (cache-poisoning risk), and returns early before security filtering/analytics for all /api/ paths. |
| apps/sim/next.config.ts | Removes build-time CORS headers for /api/* routes, replacing them with runtime proxy logic; retains non-CORS headers (COEP/COOP) for workflow execute and other routes. |
| apps/sim/app/api/form/[identifier]/route.ts | OPTIONS handler removed (moved to proxy); POST/GET still call addCorsHeaders internally, creating potential duplicate CORS headers alongside the new middleware layer. |
| docker/app.Dockerfile | Drops build-time NEXT_PUBLIC_APP_URL ARG/ENV since CORS origin is now resolved at runtime from the environment variable. |
| apps/sim/app/api/files/utils.ts | Removes createOptionsResponse helper now that preflight is handled centrally in the proxy. |
| apps/sim/app/api/mcp/copilot/route.ts | OPTIONS handler removed; MCP copilot CORS is now handled by the proxy policy table, preserving the same wildcard-origin policy. |
Sequence Diagram
sequenceDiagram
participant Browser
participant Proxy as proxy.ts (middleware)
participant Handler as Route Handler
Browser->>Proxy: "OPTIONS /api/* (preflight)"
Proxy->>Proxy: resolveApiCorsPolicy(request)
Proxy-->>Browser: 204 + CORS headers (short-circuit)
Browser->>Proxy: GET/POST /api/form/[id]
Proxy->>Proxy: resolveApiCorsPolicy reflect Origin
Proxy->>Handler: NextResponse.next() + CORS headers
Handler->>Handler: addCorsHeaders() still called internally
Handler-->>Proxy: Response + CORS headers again
Proxy-->>Browser: Merged response potential duplicate CORS headers
Reviews (1): Last reviewed commit: "fix(security): remove localhost CORS ori..." | Re-trigger Greptile
Move all /api/* CORS handling from next.config.ts to proxy.ts so the runtime can resolve allowed origin per-request instead of baking it at build time (which produced "Access-Control-Allow-Origin: http://localhost:3000" with credentials:true in production). - proxy.ts: per-route CORS policy table covering auth, MCP, form, and workflow execute endpoints; OPTIONS preflight short-circuit; Vary: Origin when origin is not '*'; form routes defer to route handler's addCorsHeaders to avoid double-setting - next.config.ts: drop all /api/* Access-Control-Allow-* headers; keep COEP/COOP/CSP - deployment.ts: addCorsHeaders sets Vary: Origin alongside reflected Allow-Origin - Dockerfile: drop NEXT_PUBLIC_APP_URL build placeholder (Zod has skipValidation:true; build path doesn't read it) - Remove 8 dead OPTIONS handlers and their preflight tests now that the proxy handles preflight uniformly
d8b6cfa to
e168fab
Compare
Summary
/api/*CORS handling fromnext.config.tstoproxy.tsso allowed origin is resolved per-request at runtime instead of baked at build time (which producedAccess-Control-Allow-Origin: http://localhost:3000withcredentials: truein production)NEXT_PUBLIC_APP_URLbuild placeholder from Dockerfile (Zod hasskipValidation: true; build path doesn't read it)Type of Change
Testing
Tested manually
Checklist