Skip to content

Commit a0255fb

Browse files
committed
feat: add TRUST_PROXY configuration for client IP detection
1 parent ce1273f commit a0255fb

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/app.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
GIT_PASSWORD,
3131
GIT_PUSH_RETRIES,
3232
GIT_USER,
33+
TRUST_PROXY,
3334
} from 'src/validators'
3435
import swaggerUi from 'swagger-ui-express'
3536
import giteaCheckLatest from './gitea/connect'
@@ -41,6 +42,7 @@ const env = cleanEnv({
4142
GIT_PASSWORD,
4243
EXPRESS_PAYLOAD_LIMIT,
4344
GIT_PUSH_RETRIES,
45+
TRUST_PROXY,
4446
})
4547

4648
const debug = Debug('otomi:app')
@@ -153,6 +155,14 @@ export async function initApp(inOtomiStack?: OtomiStack) {
153155
// Only create lightship in production (not in tests)
154156
const lightship = env.isTest ? null : createLightship()
155157
const app = express()
158+
159+
// Configure trust proxy for rate limiting behind Kubernetes Ingress
160+
// See: https://github.com/express-rate-limit/express-rate-limit/wiki/Troubleshooting-Proxy-Issues
161+
// Set to number of proxies between user and server (typically 1 for Kubernetes Ingress)
162+
if (!env.isTest && env.TRUST_PROXY > 0) {
163+
app.set('trust proxy', env.TRUST_PROXY)
164+
}
165+
156166
const apiRoutesPath = path.resolve(__dirname, 'api')
157167
await loadSpec()
158168
const authz = new Authz(otomiSpec.spec)

src/validators.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ export const RATE_LIMIT_AUTH_MAX_ATTEMPTS = num({
166166
desc: 'Maximum number of failed authentication attempts per IP per time window',
167167
default: 500,
168168
})
169+
export const TRUST_PROXY = num({
170+
desc: 'Number of reverse proxies to trust for client IP detection (0 to disable, 1 for Kubernetes Ingress, 2 for LB + Ingress)',
171+
default: 1,
172+
devDefault: 0,
173+
})
169174
const { env } = process
170175
export function cleanEnv<T>(validators: { [K in keyof T]: ValidatorSpec<T[K]> }, options: CleanOptions<T> = {}) {
171176
if (env.NODE_ENV === 'test') {

0 commit comments

Comments
 (0)