Skip to content

Commit 4d73c99

Browse files
committed
feat: enhance middleware and security configurations
- Added proxyCheckMiddleware to main.ts
1 parent cfa2181 commit 4d73c99

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

src/app.module.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ import { QueueModule } from '@queue/queue.module';
6060
configService.getOrThrow<string>('SWAGGER_PATH'),
6161
configService.getOrThrow<string>('SCALAR_PATH'),
6262
],
63+
serveStaticOptions: {
64+
dotfiles: 'deny',
65+
},
6366
},
6467
],
6568
}),

src/common/middlewares/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './basic-auth.middleware';
22
export * from './get-real-ip';
3+
export * from './proxy-check.middleware';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { NextFunction, Request, Response } from 'express';
2+
3+
import { Logger } from '@nestjs/common';
4+
5+
import { isDevelopment } from '@common/utils/startup-app';
6+
7+
const logger = new Logger('ProxyCheckMiddleware');
8+
9+
export function proxyCheckMiddleware(req: Request, res: Response, next: NextFunction) {
10+
if (isDevelopment()) {
11+
return next();
12+
}
13+
14+
const isProxy = Boolean(req.headers['x-forwarded-for']);
15+
const isHttps = Boolean(req.headers['x-forwarded-proto'] === 'https');
16+
17+
logger.debug(
18+
`X-Forwarded-For: ${req.headers['x-forwarded-for']}, X-Forwarded-Proto: ${req.headers['x-forwarded-proto']}`,
19+
);
20+
21+
if (!isHttps || !isProxy) {
22+
res.socket?.destroy();
23+
logger.error('Reverse proxy and HTTPS are required.');
24+
return false;
25+
}
26+
27+
return next();
28+
}

src/main.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import { ConfigService } from '@nestjs/config';
1313
import { NestFactory } from '@nestjs/core';
1414

1515
import { getDocs, isDevelopment, isProduction } from '@common/utils/startup-app';
16-
import { ProxyCheckGuard } from '@common/guards/proxy-check/proxy-check.guard';
16+
// import { ProxyCheckGuard } from '@common/guards/proxy-check/proxy-check.guard';
1717
import { getStartMessage } from '@common/utils/startup-app/get-start-message';
1818
import { getRealIp } from '@common/middlewares/get-real-ip';
19+
import { proxyCheckMiddleware } from '@common/middlewares';
1920
import { AxiosService } from '@common/axios';
2021

2122
import { AppModule } from './app.module';
@@ -106,6 +107,8 @@ async function bootstrap(): Promise<void> {
106107

107108
app.setGlobalPrefix(ROOT);
108109

110+
app.use(proxyCheckMiddleware);
111+
109112
await getDocs(app, config);
110113

111114
app.enableCors({
@@ -116,7 +119,7 @@ async function bootstrap(): Promise<void> {
116119

117120
app.useGlobalPipes(new ZodValidationPipe());
118121

119-
app.useGlobalGuards(new ProxyCheckGuard({ exclude: [] }));
122+
// app.useGlobalGuards(new ProxyCheckGuard({ exclude: [] }));
120123

121124
app.enableShutdownHooks();
122125

0 commit comments

Comments
 (0)