-
Notifications
You must be signed in to change notification settings - Fork 7
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
PS-703 : Added CORS and helmet to middle ware for security #16
base: main
Are you sure you want to change the base?
Changes from 3 commits
09af75b
fb4859c
9ee37bc
ea31670
d5819fd
ca36ef7
3a4e18b
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 | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,11 +1,35 @@ | ||||||||||||||||||||||||||||||||
import { NestFactory } from '@nestjs/core'; | ||||||||||||||||||||||||||||||||
import helmet from 'helmet'; | ||||||||||||||||||||||||||||||||
import { AppModule } from './app.module'; | ||||||||||||||||||||||||||||||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; | ||||||||||||||||||||||||||||||||
import * as dotenv from 'dotenv'; | ||||||||||||||||||||||||||||||||
import { ConfigService } from '@nestjs/config'; | ||||||||||||||||||||||||||||||||
import { ForbiddenException } from '@nestjs/common'; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
async function bootstrap() { | ||||||||||||||||||||||||||||||||
dotenv.config(); // Load environment variables from .env file | ||||||||||||||||||||||||||||||||
const app = await NestFactory.create(AppModule); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
const configService = app.get(ConfigService); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
const corsOriginList = configService | ||||||||||||||||||||||||||||||||
.get<string>('CORS_ORIGIN_LIST') | ||||||||||||||||||||||||||||||||
?.split(','); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
if (!corsOriginList || corsOriginList.length === 0) { | ||||||||||||||||||||||||||||||||
throw new Error('CORS_ORIGIN_LIST is not defined or empty'); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
if (corsOriginList[0] !== '*' && !validateCorsOriginList(corsOriginList)) { | ||||||||||||||||||||||||||||||||
throw new Error('Invalid CORS_ORIGIN_LIST'); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
const corsOptions = { | ||||||||||||||||||||||||||||||||
origin: corsOriginList, // Array of allowed origins | ||||||||||||||||||||||||||||||||
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE', // Specify allowed methods | ||||||||||||||||||||||||||||||||
credentials: true, // Allow credentials (cookies, authorization headers) | ||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
const config = new DocumentBuilder() | ||||||||||||||||||||||||||||||||
.setTitle('Middleware APIs') | ||||||||||||||||||||||||||||||||
.setDescription('The Middlware service') | ||||||||||||||||||||||||||||||||
|
@@ -17,8 +41,40 @@ async function bootstrap() { | |||||||||||||||||||||||||||||||
.build(); | ||||||||||||||||||||||||||||||||
const document = SwaggerModule.createDocument(app, config); | ||||||||||||||||||||||||||||||||
SwaggerModule.setup('api/swagger-docs', app, document); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
app.use((req, res, next) => { | ||||||||||||||||||||||||||||||||
const origin = req.headers.origin; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
if (corsOriginList.includes(origin) || corsOriginList[0] === '*') { | ||||||||||||||||||||||||||||||||
res.setHeader('Access-Control-Allow-Origin', origin); | ||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||
throw new ForbiddenException('Origin not allowed'); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
res.setHeader('Access-Control-Allow-Credentials', 'true'); | ||||||||||||||||||||||||||||||||
res.header( | ||||||||||||||||||||||||||||||||
'Access-Control-Allow-Methods', | ||||||||||||||||||||||||||||||||
'GET,HEAD,PUT,PATCH,POST,DELETE', | ||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||
next(); | ||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||
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. Remove redundant custom CORS middleware. The custom CORS middleware implementation conflicts with the Consider removing the custom middleware and relying solely on Apply this diff to remove the custom middleware: - app.use((req, res, next) => {
- const origin = req.headers.origin;
-
- if (corsOriginList.includes(origin) || corsOriginList[0] === '*') {
- res.setHeader('Access-Control-Allow-Origin', origin);
- } else {
- throw new ForbiddenException('Origin not allowed');
- }
- res.setHeader('Access-Control-Allow-Credentials', 'true');
- res.header(
- 'Access-Control-Allow-Methods',
- 'GET,HEAD,PUT,PATCH,POST,DELETE',
- );
- next();
- }); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
app.enableCors(corsOptions); | ||||||||||||||||||||||||||||||||
app.use(helmet()); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
await app.listen(4000, () => { | ||||||||||||||||||||||||||||||||
console.log(`Server middleware on port - 4000`); | ||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
function validateCorsOriginList(corsOriginList: string[]): boolean { | ||||||||||||||||||||||||||||||||
return corsOriginList.every((origin) => { | ||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||
new URL(origin); | ||||||||||||||||||||||||||||||||
return true; | ||||||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||||||
return false; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
bootstrap(); |
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.
Potential issue with empty
corsOriginList
.If
CORS_ORIGIN_LIST
is undefined or empty, accessingcorsOriginList[0]
could lead to an error.Add a check to handle cases where
corsOriginList
is undefined or empty.Apply this diff:
📝 Committable suggestion