feat(backend): add cookie parser and CSRF protection middleware
This commit is contained in:
parent
4028cebb63
commit
13f372390b
@ -2,6 +2,8 @@ import { NestFactory } from '@nestjs/core';
|
||||
import { ValidationPipe } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
|
||||
import * as cookieParser from 'cookie-parser';
|
||||
import * as csurf from 'csurf';
|
||||
import { AppModule } from './app.module';
|
||||
|
||||
async function bootstrap() {
|
||||
@ -17,8 +19,34 @@ async function bootstrap() {
|
||||
}),
|
||||
);
|
||||
|
||||
// Configuration CORS selon l'environnement
|
||||
// Configure cookie parser
|
||||
app.use(cookieParser());
|
||||
|
||||
// Get environment configuration
|
||||
const environment = configService.get<string>('NODE_ENV', 'development');
|
||||
|
||||
// Configure CSRF protection
|
||||
if (environment !== 'test') { // Skip CSRF in test environment
|
||||
app.use(csurf({
|
||||
cookie: {
|
||||
httpOnly: true,
|
||||
sameSite: 'strict',
|
||||
secure: environment === 'production'
|
||||
}
|
||||
}));
|
||||
|
||||
// Add CSRF token to response
|
||||
app.use((req, res, next) => {
|
||||
res.cookie('XSRF-TOKEN', req.csrfToken?.() || '', {
|
||||
httpOnly: false, // Client-side JavaScript needs to read this
|
||||
sameSite: 'strict',
|
||||
secure: environment === 'production'
|
||||
});
|
||||
next();
|
||||
});
|
||||
}
|
||||
|
||||
// Configuration CORS selon l'environnement
|
||||
const frontendUrl = configService.get<string>('FRONTEND_URL', 'http://localhost:3001');
|
||||
|
||||
if (environment === 'development') {
|
||||
|
Loading…
x
Reference in New Issue
Block a user