-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add Helmet security headers and restrict CORS to known origin #116
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 | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,9 +2,11 @@ import 'reflect-metadata'; | |||||||||||||||||||||||||||||||||
| import { NestFactory } from '@nestjs/core'; | ||||||||||||||||||||||||||||||||||
| import { AppModule } from './app.module'; | ||||||||||||||||||||||||||||||||||
| import { Logger, ValidationPipe } from '@nestjs/common'; | ||||||||||||||||||||||||||||||||||
| import { ConfigService } from '@nestjs/config'; | ||||||||||||||||||||||||||||||||||
| import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; | ||||||||||||||||||||||||||||||||||
| import * as figlet from 'figlet'; | ||||||||||||||||||||||||||||||||||
| import * as dotenv from 'dotenv'; | ||||||||||||||||||||||||||||||||||
| import helmet from 'helmet'; | ||||||||||||||||||||||||||||||||||
| import { HttpExceptionFilter } from './common/filters/http-exception.filter'; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| dotenv.config(); | ||||||||||||||||||||||||||||||||||
|
|
@@ -13,14 +15,22 @@ async function bootstrap() { | |||||||||||||||||||||||||||||||||
| const app = await NestFactory.create(AppModule); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Application configuration | ||||||||||||||||||||||||||||||||||
| const port = process.env.PORT || 3001; | ||||||||||||||||||||||||||||||||||
| const appName = process.env.APP_NAME || 'STATION BACKEND'; | ||||||||||||||||||||||||||||||||||
| const configService = app.get(ConfigService); | ||||||||||||||||||||||||||||||||||
| const port = configService.get<string>('PORT') || 3001; | ||||||||||||||||||||||||||||||||||
| const appName = configService.get<string>('APP_NAME') || 'STATION BACKEND'; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // ASCII Art for Application Name | ||||||||||||||||||||||||||||||||||
| console.log(figlet.textSync(appName, { horizontalLayout: 'full' })); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Enable CORS (if needed for APIs) | ||||||||||||||||||||||||||||||||||
| app.enableCors(); | ||||||||||||||||||||||||||||||||||
| // Security headers — must be registered before other middleware | ||||||||||||||||||||||||||||||||||
| app.use(helmet()); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
| app.use(helmet()); | |
| app.use( | |
| helmet({ | |
| contentSecurityPolicy: { | |
| directives: { | |
| defaultSrc: [`'self'`], | |
| baseUri: [`'self'`], | |
| objectSrc: [`'none'`], | |
| scriptSrc: [`'self'`, `'unsafe-inline'`], | |
| styleSrc: [`'self'`, `'unsafe-inline'`], | |
| imgSrc: [`'self'`, 'data:', 'https:'], | |
| fontSrc: [`'self'`, 'https:', 'data:'], | |
| }, | |
| }, | |
| }), | |
| ); |
Copilot
AI
Apr 12, 2026
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.
ALLOWED_ORIGIN is optional here (configService.get(...) can return undefined). If it’s missing/misconfigured, the CORS middleware will fall back to its defaults, which undermines the goal of restricting origins (and can also break credentialed CORS). Make this a required startup config (e.g., use getOrThrow, or explicitly check and throw before calling enableCors) and consider trimming/validating the value (URI, no trailing slash).
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
cookie-parser(and@types/cookie-parser) andjoiare added as dependencies here, but there are no usages in the backend codebase. If they’re not required for this PR, please remove them to avoid dependency bloat; if they are intended for env validation / cookie auth, wire them up in code (e.g., ConfigModule validation schema forALLOWED_ORIGIN, orapp.use(cookieParser())) within the same change.