Skip to content

Commit 7e91e6a

Browse files
committed
feat(cors): add CORS support with configurable allowed origins
1 parent 379a1ec commit 7e91e6a

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

.env.template

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ FEEDBACK_URL_LINK=
3232

3333
# frame-ancestors attribute of CSP. Separate multiple values with a space
3434
FRAME_ANCESTORS=
35+
36+
# Allowed CORS origins (comma-separated). Example: https://app.example.com,https://admin.example.com
37+
# Leave empty to use POST_LOGIN_REDIRECT as the default allowed origin
38+
ALLOWED_CORS_ORIGINS=

package-lock.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@apollo/client": "3.14.0",
2929
"@fastify/autoload": "6.3.1",
3030
"@fastify/cookie": "11.0.2",
31+
"@fastify/cors": "^11.1.0",
3132
"@fastify/env": "5.0.3",
3233
"@fastify/helmet": "13.0.2",
3334
"@fastify/http-proxy": "11.3.0",

server.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Fastify from 'fastify';
22
import FastifyVite from '@fastify/vite';
3+
import cors from '@fastify/cors';
34
import helmet from '@fastify/helmet';
45
import { fileURLToPath } from 'node:url';
56
import path from 'node:path';
@@ -65,6 +66,29 @@ const fastify = Fastify({
6566
logger: true,
6667
});
6768

69+
fastify.register(cors, {
70+
origin: isLocalDev
71+
? true // Allow all origins in local development
72+
: (origin, callback) => {
73+
// In production, validate against allowed origins
74+
// @ts-ignore
75+
const allowedOrigins = fastify.config.ALLOWED_CORS_ORIGINS
76+
? // @ts-ignore
77+
fastify.config.ALLOWED_CORS_ORIGINS.split(',').map((o) => o.trim())
78+
: // @ts-ignore
79+
[fastify.config.POST_LOGIN_REDIRECT]; // Fallback to POST_LOGIN_REDIRECT
80+
81+
console.log('Allowed Origin:', allowedOrigins, !origin || allowedOrigins.includes(origin));
82+
if (!origin || allowedOrigins.includes(origin)) {
83+
callback(null, true);
84+
} else {
85+
callback(new Error(`Origin ${origin} not allowed by CORS policy`), false);
86+
}
87+
},
88+
methods: ['GET', 'HEAD', 'POST', 'PATCH', 'DELETE'],
89+
credentials: true, // Required for cookie-based sessions
90+
});
91+
6892
Sentry.setupFastifyErrorHandler(fastify);
6993
await fastify.register(envPlugin);
7094

server/config/env.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const schema = {
2929
FEEDBACK_SLACK_URL: { type: 'string' },
3030
FEEDBACK_URL_LINK: { type: 'string' },
3131
FRAME_ANCESTORS: { type: 'string' },
32+
ALLOWED_CORS_ORIGINS: { type: 'string' },
3233
BFF_SENTRY_DSN: { type: 'string' },
3334
FRONTEND_SENTRY_DSN: { type: 'string' },
3435
FRONTEND_SENTRY_ENVIRONMENT: { type: 'string' },

0 commit comments

Comments
 (0)