Skip to content

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

fix(gateways): recheck the direct host immediately before dialing

bernie-gb9e1f12200b8
diff --git a/backend/src/ee/services/dynamic-secret/dynamic-secret-fns.ts b/backend/src/ee/services/dynamic-secret/dynamic-secret-fns.ts
index ec814d7d20..5ef3cd61cf 100644
--- a/backend/src/ee/services/dynamic-secret/dynamic-secret-fns.ts
+++ b/backend/src/ee/services/dynamic-secret/dynamic-secret-fns.ts
@@ -6,7 +6,11 @@ import { BadRequestError } from "@app/lib/errors";
import { isPrivateIp } from "@app/lib/ip/ipRange";
import { getDbConnectionHost } from "@app/lib/knex";
+let reservedIpsCache: { ips: string[]; expiresAt: number } | null = null;
+const RESERVED_IPS_TTL_MS = 5 * 60 * 1000;
+
const getReservedIps = async () => {
+ if (reservedIpsCache && reservedIpsCache.expiresAt > Date.now()) return reservedIpsCache.ips;
const appCfg = getConfig();
const reservedHosts = [appCfg.DB_HOST || getDbConnectionHost(appCfg.DB_CONNECTION_URI)].concat(
(appCfg.DB_READ_REPLICAS || []).map((el) => getDbConnectionHost(el.DB_CONNECTION_URI)),
@@ -26,6 +30,7 @@ const getReservedIps = async () => {
}
}
}
+ reservedIpsCache = { ips: exclusiveIps, expiresAt: Date.now() + RESERVED_IPS_TTL_MS };
return exclusiveIps;
};
@@ -34,9 +39,6 @@ export const assertHostNotInfisicalInfrastructure = async ({ host }: { host: str
const appCfg = getConfig();
if (appCfg.isDevelopmentMode || appCfg.isTestMode) return;
- const exclusiveIps = await getReservedIps();
- if (!exclusiveIps.length) return;
-
let hostIps: string[];
if (net.isIP(host)) {
hostIps = [host];
@@ -49,6 +51,7 @@ export const assertHostNotInfisicalInfrastructure = async ({ host }: { host: str
}
}
+ const exclusiveIps = await getReservedIps();
if (hostIps.some((el) => exclusiveIps.includes(el))) {
throw new BadRequestError({
message:
diff --git a/backend/src/lib/gateway-v2/gateway-v2.ts b/backend/src/lib/gateway-v2/gateway-v2.ts
index 7c0c824fd7..d001ce755b 100644
--- a/backend/src/lib/gateway-v2/gateway-v2.ts
+++ b/backend/src/lib/gateway-v2/gateway-v2.ts
@@ -5,7 +5,10 @@ import tls from "node:tls";
import { isAxiosError } from "axios";
import https from "https";
-import { verifyHostInputValidity } from "@app/ee/services/dynamic-secret/dynamic-secret-fns";
+import {
+ assertHostNotInfisicalInfrastructure,
+ verifyHostInputValidity
+} from "@app/ee/services/dynamic-secret/dynamic-secret-fns";
import { TGatewayV2ConnectionDetails } from "@app/ee/services/gateway-v2/gateway-v2-types";
import { splitPemChain } from "@app/services/certificate/certificate-fns";
@@ -250,15 +253,14 @@ export const setupRelayServer = async ({
const tunnelId = crypto.randomBytes(4).toString("hex");
const hasRelayFallback = Boolean(directAddress && relayHost && relay);
- // Validated at registration; re-checking here would resolve DNS on every dial.
- const dialDirect = () => {
+ const dialDirect = async () => {
const parsed = new URL(`tcp://${directAddress}`);
const serverName = parsed.hostname.startsWith("[") ? parsed.hostname.slice(1, -1) : parsed.hostname;
- return Promise.resolve({
- conn: net.connect({ host: serverName, port: Number(parsed.port) }),
- serverName,
- direct: true
- });
+ // Rechecked immediately before connecting, not just at registration: a name that passed then
+ // can be rebound since. Connecting by name rather than by the resolved address keeps Node's
+ // own multi-address fallback, which a dual-stack gateway relies on.
+ await assertHostNotInfisicalInfrastructure({ host: serverName });
+ return { conn: net.connect({ host: serverName, port: Number(parsed.port) }), serverName, direct: true };
};
const dialRelay = async () => {