Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix: use trusted_proxy_cidrs env var for fastify realIp

Tre Dubrava5f1c11bc6da3
diff --git a/backend/src/lib/config/env.ts b/backend/src/lib/config/env.ts
index 3adc11969e..18e071cf4d 100644
--- a/backend/src/lib/config/env.ts
+++ b/backend/src/lib/config/env.ts
@@ -454,6 +454,14 @@ const envSchema = z
})
),
+ /* Reverse Proxy ----------------------------------------------------------------------------- */
+ // Comma-separated list of trusted proxy CIDRs (e.g. "10.0.0.0/8,172.16.0.0/12") or
+ // proxy-addr aliases ("loopback", "linklocal", "uniquelocal"). When set, requests whose
+ // socket remote address is NOT in this set will have forwarded-IP headers ignored; the
+ // socket address is used as the real IP. When unset, legacy first-header-wins behavior
+ // is preserved for backwards compatibility.
+ TRUSTED_PROXY_CIDRS: zpStr(z.string().optional()),
+
/* OracleDB ----------------------------------------------------------------------------- */
TNS_ADMIN: zpStr(z.string().optional()),
diff --git a/backend/src/server/app.ts b/backend/src/server/app.ts
index 53095cefcf..6ff780af65 100644
--- a/backend/src/server/app.ts
+++ b/backend/src/server/app.ts
@@ -75,7 +75,9 @@ export const main = async ({
const server = fastify({
logger: appCfg.NODE_ENV === "test" ? false : logger,
genReqId: () => `req-${alphaNumericNanoId(14)}`,
- trustProxy: true,
+ // When TRUSTED_PROXY_CIDRS is configured, only requests from those sources have their
+ // forwarded-IP headers honored. Unset preserves legacy behavior (trust all) for backcompat.
+ trustProxy: appCfg.TRUSTED_PROXY_CIDRS ?? true,
connectionTimeout: 100_000,
ignoreTrailingSlash: true,
diff --git a/backend/src/server/plugins/ip.ts b/backend/src/server/plugins/ip.ts
index 7b5838d57b..4096bb8eb9 100644
--- a/backend/src/server/plugins/ip.ts
+++ b/backend/src/server/plugins/ip.ts
@@ -1,5 +1,7 @@
import fp from "fastify-plugin";
+import { getConfig } from "@app/lib/config/env";
+
/*! https://github.com/pbojinov/request-ip/blob/9501cdf6e73059cc70fc6890adb086348d7cca46/src/index.js.
MIT License. 2022 Petar Bojinov - [email protected] */
const headersOrder = [
@@ -20,7 +22,18 @@ const headersOrder = [
export const fastifyIp = fp(async (fastify) => {
fastify.decorateRequest("realIp", null);
+ const { TRUSTED_PROXY_CIDRS } = getConfig();
fastify.addHook("onRequest", async (req) => {
+ // Strict mode: TRUSTED_PROXY_CIDRS configured → delegate to Fastify's proxy-addr-backed
+ // req.ip, which validates the socket source against the trusted CIDR list and parses
+ // X-Forwarded-For right-to-left, discarding attacker-supplied values.
+ if (TRUSTED_PROXY_CIDRS) {
+ req.realIp = req.ip;
+ return;
+ }
+
+ // Legacy mode: first-matching-header wins. Preserved for backwards compatibility
+ // with self-hosted deployments that haven't configured a trusted proxy list.
const forwardedIpHeader = headersOrder.find((header) => Boolean(req.headers[header]));
const forwardedIp = forwardedIpHeader ? req.headers[forwardedIpHeader] : undefined;
if (forwardedIp) {