feat: add some security
This commit is contained in:
+44
-8
@@ -4,10 +4,13 @@ import { I18nValidationExceptionFilter, I18nValidationPipe } from 'nestjs-i18n';
|
||||
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
|
||||
import { ResponseInterceptor } from './common/interceptors/response.interceptor';
|
||||
import helmet from 'helmet';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
|
||||
app.setGlobalPrefix('v1');
|
||||
|
||||
app.useGlobalPipes(
|
||||
new I18nValidationPipe({
|
||||
whitelist: true,
|
||||
@@ -32,24 +35,57 @@ async function bootstrap() {
|
||||
|
||||
app.useGlobalInterceptors(new ResponseInterceptor());
|
||||
|
||||
const configService = app.get(ConfigService);
|
||||
|
||||
const origins = configService.get<string[]>('app.corsOrigins');
|
||||
const subdomain = configService.get<string>('app.corsSubdomain');
|
||||
|
||||
console.log(subdomain, origins);
|
||||
|
||||
app.enableCors({
|
||||
origin: ['http://localhost:5173'],
|
||||
origin: (origin: string, callback: any) => {
|
||||
if (!origin) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
|
||||
return callback(null, true);
|
||||
}
|
||||
|
||||
if (origins?.includes(origin)) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
|
||||
return callback(null, true);
|
||||
}
|
||||
|
||||
try {
|
||||
const hostname = new URL(origin).hostname;
|
||||
|
||||
if (hostname === subdomain || hostname.endsWith(`.${subdomain}`)) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
|
||||
return callback(null, true);
|
||||
}
|
||||
} catch {
|
||||
/* empty */
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
||||
callback(new Error('Not allowed by CORS'));
|
||||
},
|
||||
credentials: true,
|
||||
});
|
||||
|
||||
app.use(helmet());
|
||||
|
||||
const config = new DocumentBuilder()
|
||||
.setTitle('Borna API')
|
||||
.setDescription('Borna API Documentation')
|
||||
.setTitle('Heala API')
|
||||
.setDescription('Heala API Documentation')
|
||||
.setVersion('1.0')
|
||||
.setLicense('MIT', 'https://opensource.org/license/mit')
|
||||
.addServer('http://localhost:3000')
|
||||
.build();
|
||||
.setLicense('MIT', 'https://opensource.org/license/mit');
|
||||
|
||||
const document = SwaggerModule.createDocument(app, config);
|
||||
const swaggerServers = configService.get<string[]>('app.swaggerServers');
|
||||
|
||||
SwaggerModule.setup('api/v1', app, document);
|
||||
swaggerServers?.forEach((server) => config.addServer(server));
|
||||
|
||||
const document = SwaggerModule.createDocument(app, config.build());
|
||||
|
||||
SwaggerModule.setup('v1/docs', app, document);
|
||||
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user