diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index 5da1b31..709e275 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -1,3 +1,4 @@ +import { Module, MiddlewareConsumer, NestModule } from '@nestjs/common'; import { Module, NestModule, MiddlewareConsumer, RequestMethod } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { ConfigModule, ConfigService } from '@nestjs/config'; @@ -16,6 +17,14 @@ import { PuzzlesModule } from './puzzles/puzzles.module'; import { QuestsModule } from './quests/quests.module'; import { StreakModule } from './streak/strerak.module'; import { CategoriesModule } from './categories/categories.module'; +import { TransactionMiddleware } from './middleware/transaction/transaction.middleware'; +import { TransactionLogger } from './middleware/transaction/transaction.logger'; +import { CompressionMiddleware } from './middleware/compression/compression.middleware'; +import { IdempotencyMiddleware } from './middleware/idempotency/idempotency.middleware'; +import { SecurityHeadersMiddleware } from './middleware/security/security-headers.middleware'; + + + import { JwtAuthModule, JwtAuthMiddleware } from './auth/middleware/jwt-auth.module'; import { REDIS_CLIENT } from './redis/redis.constants'; import jwtConfig from './auth/authConfig/jwt.config'; @@ -23,9 +32,6 @@ import { UsersService } from './users/providers/users.service'; import { GeolocationMiddleware } from './common/middleware/geolocation.middleware'; import { HealthModule } from './health/health.module'; -// const ENV = process.env.NODE_ENV; -// console.log('NODE_ENV:', process.env.NODE_ENV); -// console.log('ENV:', ENV); @Module({ imports: [ @@ -87,7 +93,6 @@ import { HealthModule } from './health/health.module'; CommonModule, RedisModule, BlockchainModule, - ProgressModule, CategoriesModule, // Register the custom JWT Auth Middleware module JwtAuthModule.registerAsync({ @@ -104,9 +109,19 @@ import { HealthModule } from './health/health.module'; HealthModule, ], controllers: [AppController], - providers: [AppService], + providers: [AppService, TransactionLogger], }) export class AppModule implements NestModule { + configure(consumer: MiddlewareConsumer) { + // Apply transaction middleware globally + consumer.apply(TransactionMiddleware).forRoutes('*'); + + + consumer.apply(CompressionMiddleware).forRoutes('*'); + consumer.apply(IdempotencyMiddleware).forRoutes('*'); + consumer.apply(SecurityHeadersMiddleware).forRoutes('*'); + + /** * Apply the JWT Authentication Middleware to all routes except public ones. */ diff --git a/backend/src/common/controllers/security.controller.ts b/backend/src/common/controllers/security.controller.ts new file mode 100644 index 0000000..e527283 --- /dev/null +++ b/backend/src/common/controllers/security.controller.ts @@ -0,0 +1,14 @@ +import { Controller, Get, Res } from '@nestjs/common'; +import { Response } from 'express'; + +@Controller('.well-known') +export class SecurityController { + @Get('security.txt') + getSecurityTxt(@Res() res: Response) { + res.type('text/plain').send( + `Contact: security@yourdomain.com +Policy: https://yourdomain.com/security-policy +Acknowledgments: https://yourdomain.com/security-acknowledgments` + ); + } +} diff --git a/package-lock.json b/package-lock.json index 4257410..79a6928 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ "@nestjs/common": "^11.1.14", "@nestjs/core": "^11.1.14", "@tanstack/react-query": "^5.90.21", + "compression": "^1.8.1", "framer-motion": "^12.34.3", "minimatch": "^10.1.1", "reflect-metadata": "^0.2.2", @@ -8021,6 +8022,60 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/compressible": { + "version": "2.0.18", + "resolved": "https://registry.npmjs.org/compressible/-/compressible-2.0.18.tgz", + "integrity": "sha512-AF3r7P5dWxL8MxyITRMlORQNaOA2IkAFaTr4k7BUumjPtRpGDTZpl0Pb1XCO6JeDCBdp126Cgs9sMxqSjgYyRg==", + "license": "MIT", + "dependencies": { + "mime-db": ">= 1.43.0 < 2" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/compression": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/compression/-/compression-1.8.1.tgz", + "integrity": "sha512-9mAqGPHLakhCLeNyxPkK4xVo746zQ/czLH1Ky+vkitMnWfWZps8r0qXuwhwizagCRttsL4lfG4pIOvaWLpAP0w==", + "license": "MIT", + "dependencies": { + "bytes": "3.1.2", + "compressible": "~2.0.18", + "debug": "2.6.9", + "negotiator": "~0.6.4", + "on-headers": "~1.1.0", + "safe-buffer": "5.2.1", + "vary": "~1.1.2" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/compression/node_modules/debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "license": "MIT", + "dependencies": { + "ms": "2.0.0" + } + }, + "node_modules/compression/node_modules/ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==", + "license": "MIT" + }, + "node_modules/compression/node_modules/negotiator": { + "version": "0.6.4", + "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.4.tgz", + "integrity": "sha512-myRT3DiWPHqho5PrJaIRyaMv2kgYf0mUVgBNOYMuCH5Ki1yEiQaf/ZJuQ62nvpc44wL5WDbTX7yGJi1Neevw8w==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -14300,6 +14355,15 @@ "node": ">= 0.8" } }, + "node_modules/on-headers": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.1.0.tgz", + "integrity": "sha512-737ZY3yNnXy37FHkQxPzt4UZ2UWPWiCZWLvFZ4fu5cueciegX0zGPnrlY6bwRg4FdQOe9YU8MkmJwGhoMybl8A==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", diff --git a/package.json b/package.json index e8233f0..94643ec 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "@nestjs/common": "^11.1.14", "@nestjs/core": "^11.1.14", "@tanstack/react-query": "^5.90.21", + "compression": "^1.8.1", "framer-motion": "^12.34.3", "minimatch": "^10.1.1", "reflect-metadata": "^0.2.2",