Skip to content

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

fix(gateway): accept every google service account domain, bound iam token lifetime

bernie-g5c106b10283f
diff --git a/backend/src/ee/services/resource-auth-method/gcp-auth-fns.test.ts b/backend/src/ee/services/resource-auth-method/gcp-auth-fns.test.ts
index 7f36793bed..f66c4efad3 100644
--- a/backend/src/ee/services/resource-auth-method/gcp-auth-fns.test.ts
+++ b/backend/src/ee/services/resource-auth-method/gcp-auth-fns.test.ts
@@ -1,6 +1,6 @@
import { UnauthorizedError } from "@app/lib/errors";
-import { validateGcpAllowlists } from "./gcp-auth-fns";
+import { assertIamTokenLifetime, validateGcpAllowlists } from "./gcp-auth-fns";
import { GcpAuthType } from "./resource-auth-method-fns";
const SERVICE_ACCOUNT = "[email protected]";
@@ -35,11 +35,44 @@ const reasonCodeOf = (fn: () => void) => {
return undefined;
};
+describe("assertIamTokenLifetime", () => {
+ const now = 1_700_000_000;
+
+ test("refuses a token with no expiry", () => {
+ expect(assertIamTokenLifetime({}, now)).toBe("missing_expiry");
+ });
+
+ test("refuses a null payload", () => {
+ expect(assertIamTokenLifetime(null, now)).toBe("missing_expiry");
+ });
+
+ test("refuses an expired token", () => {
+ expect(assertIamTokenLifetime({ exp: now - 1 }, now)).toBe("expired");
+ });
+
+ test("refuses an expiry beyond the 12 hour cap", () => {
+ expect(assertIamTokenLifetime({ exp: now + 12 * 60 * 60 + 1 }, now)).toBe("lifetime_too_long");
+ });
+
+ test("accepts an expiry inside the cap", () => {
+ expect(assertIamTokenLifetime({ exp: now + 600 }, now)).toBeNull();
+ });
+});
+
describe("validateGcpAllowlists", () => {
test("refuses a config with no allowlist at all", () => {
expect(reasonCodeOf(() => validate({}))).toBe("no_allowlist_configured");
});
+ test("accepts a Compute Engine default service account", () => {
+ expect(() =>
+ validate({
+ identityDetails: { email: "[email protected]" },
+ allowedServiceAccounts: "[email protected]"
+ })
+ ).not.toThrow();
+ });
+
test("accepts a service account on the allowlist", () => {
expect(() =>
validate({ allowedServiceAccounts: `[email protected], ${SERVICE_ACCOUNT}` })
diff --git a/backend/src/ee/services/resource-auth-method/gcp-auth-fns.ts b/backend/src/ee/services/resource-auth-method/gcp-auth-fns.ts
index bf00e47a98..8e5b30fc5b 100644
--- a/backend/src/ee/services/resource-auth-method/gcp-auth-fns.ts
+++ b/backend/src/ee/services/resource-auth-method/gcp-auth-fns.ts
@@ -1,3 +1,4 @@
+import { crypto } from "@app/lib/crypto";
import { UnauthorizedError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
import { validateIamIdentity, validateIdTokenIdentity } from "@app/services/identity-gcp-auth/identity-gcp-auth-fns";
@@ -12,6 +13,19 @@ type TVerifyGcpTokenInput = {
errorContext: Record<string, unknown>;
};
+// Google caps a signJwt expiry at 12 hours ahead, but accepts a payload with no `exp` at all, and
+// jsonwebtoken only enforces an expiry that is present. Without this, anyone who briefly holds
+// signJwt on an allowed service account can mint a proof that outlives the revocation of that
+// permission.
+const MAX_IAM_TOKEN_LIFETIME_SECONDS = 12 * 60 * 60;
+
+export const assertIamTokenLifetime = (payload: { exp?: number } | null, nowSeconds: number) => {
+ if (!payload?.exp) return "missing_expiry" as const;
+ if (payload.exp <= nowSeconds) return "expired" as const;
+ if (payload.exp - nowSeconds > MAX_IAM_TOKEN_LIFETIME_SECONDS) return "lifetime_too_long" as const;
+ return null;
+};
+
export const verifyGcpTokenAndExtractCaller = async ({
type,
jwt,
@@ -22,7 +36,14 @@ export const verifyGcpTokenAndExtractCaller = async ({
if (type === GcpAuthType.Gce) {
return await validateIdTokenIdentity({ audience, jwt });
}
- return await validateIamIdentity({ audience, jwt });
+
+ const identityDetails = await validateIamIdentity({ audience, jwt });
+ const payload = crypto.jwt().decode(jwt) as { exp?: number } | null;
+ const lifetimeProblem = assertIamTokenLifetime(payload, Math.floor(Date.now() / 1000));
+ if (lifetimeProblem) {
+ throw new Error(`signed GCP service account token ${lifetimeProblem}`);
+ }
+ return identityDetails;
} catch (err) {
logger.error(
err,
diff --git a/backend/src/ee/services/resource-auth-method/gcp-auth-validators.ts b/backend/src/ee/services/resource-auth-method/gcp-auth-validators.ts
index 92483c87e2..e4970d3a17 100644
--- a/backend/src/ee/services/resource-auth-method/gcp-auth-validators.ts
+++ b/backend/src/ee/services/resource-auth-method/gcp-auth-validators.ts
@@ -2,8 +2,11 @@ import RE2 from "re2";
import { z } from "zod";
// Service accounts are matched against the token's `email` claim verbatim, so no wildcards here:
-// an entry that does not parse as a service account address can never match anything.
-const serviceAccountEntry = new RE2("^[a-z0-9-]{1,100}@[a-z0-9-]{1,100}\\.iam\\.gserviceaccount\\.com$");
+// an entry that does not parse as a service account address can never match anything. Every Google
+// service account domain is accepted, not just the user-managed `<project>.iam` one: a Compute
+// Engine default account is `<project-number>[email protected]`, and App
+// Engine's is `<project-id>@appspot.gserviceaccount.com`.
+const serviceAccountEntry = new RE2("^[a-z0-9][a-z0-9-]{0,99}@[a-z0-9][a-z0-9.-]{0,99}\\.gserviceaccount\\.com$");
const projectEntry = new RE2("^[a-z][a-z0-9-]{4,28}[a-z0-9]$");
const zoneEntry = new RE2("^[a-z]([a-z0-9-]{0,62}[a-z0-9])?$");
diff --git a/backend/src/services/identity-gcp-auth/identity-gcp-auth-fns.ts b/backend/src/services/identity-gcp-auth/identity-gcp-auth-fns.ts
index f104a22b72..a484ef178a 100644
--- a/backend/src/services/identity-gcp-auth/identity-gcp-auth-fns.ts
+++ b/backend/src/services/identity-gcp-auth/identity-gcp-auth-fns.ts
@@ -26,7 +26,11 @@ export const validateIdTokenIdentity = async ({ audience, jwt: identityToken }:
return { email: payload.email, computeEngineDetails: payload.google?.compute_engine };
};
-const serviceAccountEmailRegex = new RE2(/^[a-z0-9-]+@[a-z0-9-]+\.iam\.gserviceaccount\.com$/);
+// Covers every Google service account domain, not only the user-managed `<project>.iam` one: the
+// Compute Engine default account lives on `developer.gserviceaccount.com` and App Engine's on
+// `appspot.gserviceaccount.com`. This is a sanity check on `sub` before the certificate fetch, so
+// widening it grants nothing: the signature check and the allowlist are the actual controls.
+const serviceAccountEmailRegex = new RE2(/^[a-z0-9][a-z0-9-]*@[a-z0-9][a-z0-9.-]*\.gserviceaccount\.com$/);
/**
* Validates that the signed JWT token for a GCP service account is valid as part of GCP IAM authentication.
diff --git a/frontend/src/pages/organization/NetworkingPage/GatewayDetailsByIDPage/components/GatewayAuthMethod/GcpStartCommandDialog.tsx b/frontend/src/pages/organization/NetworkingPage/GatewayDetailsByIDPage/components/GatewayAuthMethod/GcpStartCommandDialog.tsx
index 0a282ee4ca..abe3005851 100644
--- a/frontend/src/pages/organization/NetworkingPage/GatewayDetailsByIDPage/components/GatewayAuthMethod/GcpStartCommandDialog.tsx
+++ b/frontend/src/pages/organization/NetworkingPage/GatewayDetailsByIDPage/components/GatewayAuthMethod/GcpStartCommandDialog.tsx
@@ -17,6 +17,7 @@ type Props = {
gatewayId: string;
gatewayName: string;
gcpAuthType: "gce" | "iam";
+ allowedServiceAccounts: string;
isDirect: boolean;
listenAddress: string;
};
@@ -29,6 +30,7 @@ export const GcpStartCommandContent = ({
gatewayId,
gatewayName,
gcpAuthType,
+ allowedServiceAccounts,
isDirect,
listenAddress
}: Props) => {
@@ -42,6 +44,11 @@ export const GcpStartCommandContent = ({
const resolvedRelayName = isDirect || relay.id === "_auto" ? "" : relay.name;
// gce is the CLI default, so only the iam type needs the flag spelled out.
const authTypePart = gcpAuthType === "iam" ? " --gcp-auth-type=iam" : "";
+ // Without this annotation the chart creates an unbound service account, and the pod authenticates
+ // as nothing the gateway allows. The first allowed account is the best guess available here.
+ const workloadIdentityAccount =
+ allowedServiceAccounts.split(",")[0]?.trim() ||
+ "<gsa-name>@<project-id>.iam.gserviceaccount.com";
const cliCommand = useMemo(() => {
const relayPart = resolvedRelayName ? ` --target-relay-name=${resolvedRelayName}` : "";
@@ -69,8 +76,17 @@ helm install infisical-gateway infisical/infisical-gateway \\
--set gateway.name=${gatewayName} \\
--set gateway.domain=${siteURL} \\
--set gateway.enrollment.method=gcp \\
- --set gateway.enrollment.gcp.gatewayId=${gatewayId}${authTypeSet}${relayPart}${directPart}`;
- }, [gatewayName, gatewayId, gcpAuthType, isDirect, listenAddress, resolvedRelayName, siteURL]);
+ --set gateway.enrollment.gcp.gatewayId=${gatewayId}${authTypeSet}${annotationSet}${relayPart}${directPart}`;
+ }, [
+ gatewayName,
+ gatewayId,
+ gcpAuthType,
+ workloadIdentityAccount,
+ isDirect,
+ listenAddress,
+ resolvedRelayName,
+ siteURL
+ ]);
const startServiceCommand = `sudo systemctl start ${gatewayName}`;
diff --git a/frontend/src/pages/organization/NetworkingPage/GatewayDetailsByIDPage/components/GatewayDeploySection/GatewayDeploySection.tsx b/frontend/src/pages/organization/NetworkingPage/GatewayDetailsByIDPage/components/GatewayDeploySection/GatewayDeploySection.tsx
index 8543128325..159af27517 100644
--- a/frontend/src/pages/organization/NetworkingPage/GatewayDetailsByIDPage/components/GatewayDeploySection/GatewayDeploySection.tsx
+++ b/frontend/src/pages/organization/NetworkingPage/GatewayDetailsByIDPage/components/GatewayDeploySection/GatewayDeploySection.tsx
@@ -253,6 +253,7 @@ export const GatewayDeploySection = ({
gatewayId={gatewayId}
gatewayName={gatewayName}
gcpAuthType={authMethod.config.type}
+ allowedServiceAccounts={authMethod.config.allowedServiceAccounts}
isDirect={connectionMode === "direct"}
listenAddress={commandListenAddress}
/>