Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
fix(gateway): impersonation
Andrey12c0a8613060
diff --git a/backend/src/ee/routes/v1/pam-routers/pam-session-router.ts b/backend/src/ee/routes/v1/pam-routers/pam-session-router.tsindex 757d3aa44d..a0230599a3 100644--- a/backend/src/ee/routes/v1/pam-routers/pam-session-router.ts+++ b/backend/src/ee/routes/v1/pam-routers/pam-session-router.ts@@ -330,6 +330,7 @@ export const registerPamWebAccessRouter = async (server: FastifyZodProvider) =>sessionId: z.string().describe("The ID of the created session"),accountType: z.nativeEnum(PamAccountType).describe("The account type"),metadata: z.record(z.string()).optional().describe("Account-type-specific metadata (e.g., username)"),+ gatewayId: z.string().optional().describe("The ID of the gateway the connection details are for"),relayHost: z.string().optional().describe("The relay host to connect to"),directAddress: z.string().optional().describe("The gateway address for a direct connection"),relayClientCertificate: z.string().optional().describe("Client certificate for the relay connection"),@@ -414,6 +415,7 @@ export const registerPamWebAccessRouter = async (server: FastifyZodProvider) =>sessionId: result.sessionId,accountType: result.accountType,metadata: result.metadata,+ gatewayId: result.gatewayId,relayHost: result.relayHost,directAddress: result.directAddress,relayClientCertificate: result.relayClientCertificate,diff --git a/backend/src/ee/services/gateway-v2/gateway-v2-constants.ts b/backend/src/ee/services/gateway-v2/gateway-v2-constants.tsindex 7baba5e2a0..b8f032303f 100644--- a/backend/src/ee/services/gateway-v2/gateway-v2-constants.ts+++ b/backend/src/ee/services/gateway-v2/gateway-v2-constants.ts@@ -5,6 +5,8 @@ export const GATEWAY_ROUTING_INFO_OID = "1.3.6.1.4.1.12345.100.1";export const GATEWAY_ACTOR_OID = "1.3.6.1.4.1.12345.100.2";export const PAM_INFO_OID = "1.3.6.1.4.1.12345.100.3";+export const GATEWAY_IDENTITY_URI_PREFIX = "urn:infisical:gateway:";+export enum GatewayTransport {Direct = "direct",Relay = "relay"diff --git a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts b/backend/src/ee/services/gateway-v2/gateway-v2-service.tsindex 86ee99e44e..0e18cdc230 100644--- a/backend/src/ee/services/gateway-v2/gateway-v2-service.ts+++ b/backend/src/ee/services/gateway-v2/gateway-v2-service.ts@@ -50,6 +50,7 @@ import {import {DEFAULT_HEARTBEAT_TTL,GATEWAY_ACTOR_OID,+ GATEWAY_IDENTITY_URI_PREFIX,GATEWAY_ROUTING_INFO_OID,GatewayTransport,PAM_INFO_OID@@ -720,7 +721,8 @@ export const gatewayV2ServiceFactory = ({const subjectAlternativeNames: x509.JsonGeneralName[] = [{ type: "dns", value: "localhost" },{ type: "ip", value: "127.0.0.1" },- { type: "ip", value: "::1" }+ { type: "ip", value: "::1" },+ { type: "url", value: `${GATEWAY_IDENTITY_URI_PREFIX}${gateway.id}` }];if (gateway.directAddress) {const { host } = parseDirectAddress(gateway.directAddress);diff --git a/backend/src/ee/services/pam-session/pam-session-service.ts b/backend/src/ee/services/pam-session/pam-session-service.tsindex 2d5e3cb2d0..29e5a41f90 100644--- a/backend/src/ee/services/pam-session/pam-session-service.ts+++ b/backend/src/ee/services/pam-session/pam-session-service.ts@@ -782,6 +782,7 @@ export const pamSessionServiceFactory = ({metadata,sessionDurationMs,accessMethod: PamAccessMethod.Cli,+ gatewayId: certs.gatewayId,relayHost: certs.relayHost,directAddress: certs.directAddress,relayClientCertificate: certs.relay?.clientCertificate,diff --git a/backend/src/lib/gateway-v2/gateway-v2.test.ts b/backend/src/lib/gateway-v2/gateway-v2.test.tsindex b48341f5b2..c7d7ac4250 100644--- a/backend/src/lib/gateway-v2/gateway-v2.test.ts+++ b/backend/src/lib/gateway-v2/gateway-v2.test.ts@@ -3,6 +3,7 @@ import tls from "node:tls";import forge from "node-forge";+import { GATEWAY_IDENTITY_URI_PREFIX } from "@app/ee/services/gateway-v2/gateway-v2-constants";import { GatewayProxyProtocol } from "@app/lib/gateway-v2/types";import { setupGatewayProxy } from "./gateway-v2";@@ -96,16 +97,20 @@ type TFakeRelay = {* carries a second, inner TLS server. That nesting is the part under test, because a tunnel is only* "open" from the gateway's point of view once the inner handshake completes.*/-const startFakeRelay = async (): Promise<TFakeRelay> => {+const startFakeRelay = async ({ gatewayIdentity }: { gatewayIdentity?: string } = {}): Promise<TFakeRelay> => {const port = await freePort();const relayHost = `127.0.0.1:${port}`;- const serverCert = issue({- subject: "relay",- altNames: [- { type: 2, value: "127.0.0.1" },- { type: 7, ip: "127.0.0.1" }- ]- });+ const altNames: TAltName[] = [+ { type: 2, value: "127.0.0.1" },+ { type: 7, ip: "127.0.0.1" }+ ];+ const serverCert = issue({ subject: "relay", altNames });+ const gatewayCert = gatewayIdentity+ ? issue({+ subject: "gateway",+ altNames: [...altNames, { type: 6, value: `${GATEWAY_IDENTITY_URI_PREFIX}${gatewayIdentity}` }]+ })+ : serverCert;let opened = 0;let closed = 0;@@ -119,7 +124,7 @@ const startFakeRelay = async (): Promise<TFakeRelay> => {const inner = new tls.TLSSocket(outer, {isServer: true,key: keyPem,- cert: serverCert,+ cert: gatewayCert,ca: caPem,requestCert: true,rejectUnauthorized: false,@@ -309,4 +314,39 @@ describe("setupGatewayProxy", () => {await drainChannels(1);});});++ describe("verifying which gateway answered", () => {+ let relay: TFakeRelay | undefined;++ afterEach(async () => {+ await relay?.close();+ relay = undefined;+ });++ test("accepts the gateway its certificate names", async () => {+ relay = await startFakeRelay({ gatewayIdentity: GATEWAY_ID });+ const server = await setupGatewayProxy({ ...argsFor(relay.relayHost), eager: true, longLived: true });+ await expect(connectAndEcho(server.port)).resolves.toBe("ping");++ await server.cleanup();+ await drainChannels(1);+ });++ test("rejects a gateway whose certificate names a different gateway", async () => {+ relay = await startFakeRelay({ gatewayIdentity: "22222222-2222-2222-2222-222222222222" });+ await expect(setupGatewayProxy({ ...argsFor(relay.relayHost), eager: true })).rejects.toThrow(+ `The connection reached a gateway other than '${GATEWAY_ID}'`+ );+ expect(tracker.channelOpened).not.toHaveBeenCalled();+ });++ test("still accepts a certificate issued before gateway identities existed", async () => {+ relay = await startFakeRelay();+ const server = await setupGatewayProxy({ ...argsFor(relay.relayHost), eager: true, longLived: true });+ await expect(connectAndEcho(server.port)).resolves.toBe("ping");++ await server.cleanup();+ await drainChannels(1);+ });+ });});diff --git a/backend/src/lib/gateway-v2/gateway-v2.ts b/backend/src/lib/gateway-v2/gateway-v2.tsindex 2c11cf026b..8e94dc2878 100644--- a/backend/src/lib/gateway-v2/gateway-v2.ts+++ b/backend/src/lib/gateway-v2/gateway-v2.ts@@ -9,6 +9,7 @@ import {assertHostNotInfisicalInfrastructure,verifyHostInputValidity} from "@app/ee/services/dynamic-secret/dynamic-secret-fns";+import { GATEWAY_IDENTITY_URI_PREFIX } from "@app/ee/services/gateway-v2/gateway-v2-constants";import { TGatewayV2ConnectionDetails } from "@app/ee/services/gateway-v2/gateway-v2-types";import { splitPemChain } from "@app/services/certificate/certificate-fns";@@ -130,8 +131,17 @@ const GATEWAY_IDLE_TIMEOUT_MS = 120000;const DIRECT_HANDSHAKE_TIMEOUT_MS = 3000;+const checkGatewayIdentity = (gatewayId: string, cert: tls.PeerCertificate) => {+ const identities = (cert.subjectaltname ?? "")+ .split(", ")+ .filter((name) => name.startsWith(`URI:${GATEWAY_IDENTITY_URI_PREFIX}`));+ if (!identities.length || identities.includes(`URI:${GATEWAY_IDENTITY_URI_PREFIX}${gatewayId}`)) return undefined;+ return new Error(`The connection reached a gateway other than '${gatewayId}'`);+};+export const createGatewayConnection = async (transportConn: net.Socket,+ gatewayId: string,gateway: { clientCertificate: string; clientPrivateKey: string; serverCertificateChain: string },protocol: GatewayProxyProtocol,tunnelId?: string,@@ -170,7 +180,13 @@ export const createGatewayConnection = async (host: isIpLiteral ? serverName : undefined,servername: isIpLiteral ? undefined : serverName,ALPNProtocols: protocolToAlpn[protocol],- checkServerIdentity: appCfg.isDevelopmentMode ? () => undefined : tls.checkServerIdentity+ checkServerIdentity: (hostname, cert) => {+ if (!appCfg.isDevelopmentMode) {+ const hostnameError = tls.checkServerIdentity(hostname, cert);+ if (hostnameError) return hostnameError;+ }+ return checkGatewayIdentity(gatewayId, cert);+ }};return new Promise((resolve, reject) => {@@ -288,6 +304,7 @@ export const setupGatewayProxy = async ({try {const gwConn = await createGatewayConnection(conn,+ gatewayId,gateway,protocol,tunnelId,