Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

fix: correctly resolve client IP behind proxies (#5323)

Léopold Jacquotf430caa1c0b9
diff --git a/.env.example b/.env.example
index c1e537b42..4156c5e76 100644
--- a/.env.example
+++ b/.env.example
@@ -14,6 +14,8 @@ DATA_ENCRYPTION_KEY="data encryption key with 32 char"
# bundle names like `app://{bundle-name}/`
WHITELISTED_ORIGINS="http://localhost:3170,http://localhost:3000,http://localhost:3100,app://localhost_3200,app://hoppscotch"
+# If true, the client’s IP address is understood as the left-most entry in the X-Forwarded-For header
+TRUST_PROXY=false
#-----------------------Frontend Config------------------------------#
diff --git a/packages/hoppscotch-backend/src/main.ts b/packages/hoppscotch-backend/src/main.ts
index b300d08f8..09ec06550 100644
--- a/packages/hoppscotch-backend/src/main.ts
+++ b/packages/hoppscotch-backend/src/main.ts
@@ -10,6 +10,7 @@ import * as morgan from 'morgan';
import { ConfigService } from '@nestjs/config';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { InfraTokenModule } from './infra-token/infra-token.module';
+import { NestExpressApplication } from '@nestjs/platform-express';
function setupSwagger(app, isProduction: boolean) {
const swaggerDocPath = '/api-docs';
@@ -38,7 +39,7 @@ function setupSwagger(app, isProduction: boolean) {
}
async function bootstrap() {
- const app = await NestFactory.create(AppModule);
+ const app = await NestFactory.create<NestExpressApplication>(AppModule);
const configService = app.get(ConfigService);
const isProduction = configService.get('PRODUCTION') === 'true';
@@ -84,7 +85,14 @@ async function bootstrap() {
transform: true,
}),
);
- app.use(morgan(':method :url :status - :response-time ms'));
+
+ if (configService.get('TRUST_PROXY') === 'true') {
+ console.log('Enabling trust proxy');
+
+ app.set('trust proxy', true);
+ }
+
+ app.use(morgan(':remote-addr :method :url :status - :response-time ms'));
await setupSwagger(app, isProduction);