-
Notifications
You must be signed in to change notification settings - Fork 1
Restrict CORS to allowed origins #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Example environment variables for the Awesome Overcut monorepo | ||
| # ------------------------------------------------------------- | ||
| # Copy this file to `.env` (or another env file) and adjust values as needed. | ||
|
|
||
| # Comma-separated list of allowed origins for CORS | ||
| CORS_ALLOWED_ORIGINS=http://localhost:3000,http://localhost:4200 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,18 @@ import { | |
| const { PORT = 3000 } = process.env; | ||
|
|
||
| async function main() { | ||
| const app = await NestFactory.create(AppModule, { cors: true }); | ||
| const app = await NestFactory.create(AppModule); | ||
|
|
||
| const allowedOrigins = | ||
| process.env.CORS_ALLOWED_ORIGINS?.split(',').map(o => o.trim()) ?? [ | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
| 'http://localhost:3000', | ||
| 'http://localhost:4200', | ||
| ]; | ||
|
|
||
| app.enableCors({ | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Security-critical behaviour changed, but no automated test verifies that disallowed origins are rejected or that wildcards are never re-enabled. Please add an integration test that spins up the app with/without |
||
| origin: allowedOrigins, | ||
| credentials: true, | ||
| }); | ||
|
|
||
| app.setGlobalPrefix("api"); | ||
| app.useGlobalPipes( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "name": "awesome-overcut-root", | ||
| "private": true, | ||
| "scripts": { | ||
| "lint:cors": "! grep -R --line-number -- '{ cors: true }' apps/*/src/main.ts || (echo 'Error: Disallowed permissive CORS usage found.' && exit 1)" | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The grep guard is fragile: it matches only the exact substring with spaces/braces, so variants like |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Including active localhost origins in the example file may lead teams to copy the value unmodified into production, unintentionally exposing the API. Consider commenting this line out and providing it as guidance instead, e.g.
# CORS_ALLOWED_ORIGINS=http://example.com, so production environments start locked-down by default.