Skip to content

Commit

Permalink
feat: change allowed origins from env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
AlejandroAkbal committed Feb 16, 2024
1 parent c62d1cf commit e8028b7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ NODE_ENV=

PORT=

ALLOWED_ORIGIN=r34.app

JWT_SECRET=
JWT_EXPIRATION_TIME=30m

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
"author": "Alejandro Akbal",
"main": "dist/main.js",
"scripts": {
"start": "node dist/main",
"build": "nest build",
"debug": "nest start --debug --watch",
"dev": "nest start --watch",
"debug": "nest start --debug --watch",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"start": "nest start",
"test": "jest",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
Expand Down Expand Up @@ -47,6 +47,7 @@
"axios": "^1.6.7",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"lodash": "^4.17.21",
"nest-raven": "^10.0.1",
"passport": "^0.7.0",
"passport-jwt": "^4.0.1",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 33 additions & 29 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,49 @@
import { ValidationPipe } from '@nestjs/common'
import { CorsOptions } from '@nestjs/common/interfaces/external/cors-options.interface'
import { NestFactory } from '@nestjs/core'
import { ConfigService } from '@nestjs/config'
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify'
import {ValidationPipe} from '@nestjs/common'
import {CorsOptions} from '@nestjs/common/interfaces/external/cors-options.interface'
import {NestFactory} from '@nestjs/core'
import {ConfigService} from '@nestjs/config'
import {FastifyAdapter, NestFastifyApplication} from '@nestjs/platform-fastify'
import helmet from '@fastify/helmet'
import * as Sentry from '@sentry/node'
import { AppModule } from './app.module'
import {AppModule} from './app.module'
import {escapeRegExp} from 'lodash'

async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter())
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter())

const configService: ConfigService = app.get(ConfigService)
const configService: ConfigService = app.get(ConfigService)

// Sentry
Sentry.init({
enabled: configService.get<boolean>('SENTRY_ENABLED') || false,
dsn: configService.get<string>('SENTRY_DSN')
// Sentry
Sentry.init({
enabled: configService.get<boolean>('SENTRY_ENABLED') || false,
dsn: configService.get<string>('SENTRY_DSN')

// ignoreErrors: ['NoContentException', 'MethodNotAllowedException'],
})
// ignoreErrors: ['NoContentException', 'MethodNotAllowedException'],
})

app.register(helmet)
app.register(helmet)

const corsOptions: CorsOptions = {
origin: configService.get<string>('NODE_ENV') === 'development' ? true : /r34\.app$/,
credentials: true
}
const allowedOrigin = configService.get<string>('ALLOWED_ORIGIN')
const allowedOriginRegex = new RegExp(escapeRegExp(allowedOrigin) + '$')

app.enableCors(corsOptions)
const corsOptions: CorsOptions = {
origin: configService.get<string>('NODE_ENV') === 'development' ? true : allowedOriginRegex,
credentials: true
}

app.useGlobalPipes(
new ValidationPipe({
transform: true, // Transform to DTO type
// transformOptions: { enableImplicitConversion: true },
app.enableCors(corsOptions)

whitelist: true, // Remove unnecessary properties
forbidNonWhitelisted: true // Sends "property <property> should not exist." error
})
)
app.useGlobalPipes(
new ValidationPipe({
transform: true, // Transform to DTO type
// transformOptions: { enableImplicitConversion: true },

whitelist: true, // Remove unnecessary properties
forbidNonWhitelisted: true // Sends "property <property> should not exist." error
})
)

await app.listen(configService.get<number>('PORT'), '0.0.0.0')
await app.listen(configService.get<number>('PORT'), '0.0.0.0')
}

bootstrap()

0 comments on commit e8028b7

Please sign in to comment.