From 6da2468b2e821e379524a5d726c4e5f1bc0e52bc Mon Sep 17 00:00:00 2001 From: skgndi12 Date: Mon, 18 Mar 2024 15:39:06 +0900 Subject: [PATCH] feat: add config value for proxy chain length --- api/config/default.yaml | 1 + api/config/test.yaml | 1 + api/src/config/loader.ts | 1 + api/src/config/types.ts | 2 ++ api/src/controller/http/server.ts | 2 +- api/src/controller/http/types.ts | 1 + api/test/config/loader.test.ts | 3 ++- 7 files changed, 9 insertions(+), 2 deletions(-) diff --git a/api/config/default.yaml b/api/config/default.yaml index e58b9e9a7..115110526 100644 --- a/api/config/default.yaml +++ b/api/config/default.yaml @@ -4,6 +4,7 @@ timeout: http: host: '127.0.0.1' port: 8080 + numTrustedProxies: 0 logger: level: 'http' # Log only if less than or equal to this level format: 'text' diff --git a/api/config/test.yaml b/api/config/test.yaml index 77f8d89b8..c3c1e3b8f 100644 --- a/api/config/test.yaml +++ b/api/config/test.yaml @@ -4,6 +4,7 @@ timeout: http: host: '127.0.0.1' port: 0 # PORT 0 randomly assigns a usable port. Set up for parallel testing. + numTrustedProxies: 0 logger: level: 'silly' # Log only if less than or equal to this level format: 'text' diff --git a/api/src/config/loader.ts b/api/src/config/loader.ts index c76ab2673..575cd71f2 100644 --- a/api/src/config/loader.ts +++ b/api/src/config/loader.ts @@ -52,6 +52,7 @@ export function buildHttpConfig(config: Config): HttpConfig { env: config.env, host: config.http.host, port: config.http.port, + numTrustedProxies: config.http.numTrustedProxies, cookieExpirationHours: config.jwt.expirationHour }; } diff --git a/api/src/config/types.ts b/api/src/config/types.ts index e6ac0a25b..f90dd825e 100644 --- a/api/src/config/types.ts +++ b/api/src/config/types.ts @@ -20,8 +20,10 @@ export interface ConfigTimeout { export interface ConfigHttp { host: string; port: number; + numTrustedProxies: number } + export interface ConfigLogger { level: LogLevel; format: LogFormat; diff --git a/api/src/controller/http/server.ts b/api/src/controller/http/server.ts index 7a561fc4d..7e2f7d1fe 100644 --- a/api/src/controller/http/server.ts +++ b/api/src/controller/http/server.ts @@ -47,7 +47,7 @@ export class HttpServer { public start = async (): Promise => { this.app = express(); this.app.disable('x-powered-by'); - this.app.set('trust proxy', 0); + this.app.set('trust proxy', this.config.numTrustedProxies); this.app.use(express.json()); await this.buildApiDocument(); this.app.use('/api', cookieParser()); diff --git a/api/src/controller/http/types.ts b/api/src/controller/http/types.ts index d76a3b172..359ea6500 100644 --- a/api/src/controller/http/types.ts +++ b/api/src/controller/http/types.ts @@ -4,5 +4,6 @@ export interface HttpConfig { env: string; host: string; port: number; + numTrustedProxies: number; cookieExpirationHours: number; } diff --git a/api/test/config/loader.test.ts b/api/test/config/loader.test.ts index bba3d8d48..96bbad494 100644 --- a/api/test/config/loader.test.ts +++ b/api/test/config/loader.test.ts @@ -16,7 +16,7 @@ describe('Test config loader', () => { expect(loadConfig()).toStrictEqual({ env: 'test', timeout: { shutdownSeconds: 30 }, - http: { host: '127.0.0.1', port: 0 }, + http: { host: '127.0.0.1', port: 0, numTrustedProxies: 0 }, logger: { level: 'silly', format: 'text' }, database: { host: '127.0.0.1', @@ -91,6 +91,7 @@ describe('Test build http config', () => { env: 'test', host: '127.0.0.1', port: 0, + numTrustedProxies: 0, cookieExpirationHours: 1 }); });