Skip to content

Commit 68965b5

Browse files
committed
feat: restrict CORS to configured allowed origins
1 parent 392c8a3 commit 68965b5

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

backend/src/app.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,20 @@ function sendError(res: Response, status: number, message: string, code?: string
1010
export function createApp() {
1111
const app = express();
1212

13-
app.use(cors());
13+
const allowedOrigins = (process.env.ALLOWED_ORIGINS ?? "http://localhost:3000")
14+
.split(",")
15+
.map(o => o.trim());
16+
17+
app.use(cors({
18+
origin: (origin, callback) => {
19+
// Allow requests with no origin (curl, Postman, server-to-server)
20+
if (!origin || allowedOrigins.includes(origin)) {
21+
callback(null, true);
22+
} else {
23+
callback(new Error("Not allowed by CORS"));
24+
}
25+
},
26+
}));
1427
app.use(express.json());
1528

1629
app.get("/health", (_req, res) => {

0 commit comments

Comments
 (0)