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): label gcp audit identifiers and cover the token verifier
bernie-geacb213bbe81
diff --git a/backend/src/ee/routes/v3/gateway-router.ts b/backend/src/ee/routes/v3/gateway-router.tsindex 4b0fbb03f6..f218b96218 100644--- a/backend/src/ee/routes/v3/gateway-router.ts+++ b/backend/src/ee/routes/v3/gateway-router.ts@@ -623,6 +623,7 @@ export const registerGatewayV3Router = async (server: FastifyZodProvider) => {metadata: {resourceType: "gateway",resourceId: result.resourceId,+ resourceName: result.resourceName,method: ResourceAuthMethodType.Gcp,methodConfigId: result.configId,gcpServiceAccountEmail: result.serviceAccountEmail,@@ -668,6 +669,7 @@ export const registerGatewayV3Router = async (server: FastifyZodProvider) => {metadata: {resourceType: "gateway",resourceId: error.detail.resourceId as string,+ resourceName: error.detail.resourceName as string | undefined,method: ResourceAuthMethodType.Gcp,reasonCode: error.detail.reasonCode as string,message: error.message,diff --git a/backend/src/ee/services/audit-log/audit-log-types.ts b/backend/src/ee/services/audit-log/audit-log-types.tsindex 8db785d8ef..84e3daa03f 100644--- a/backend/src/ee/services/audit-log/audit-log-types.ts+++ b/backend/src/ee/services/audit-log/audit-log-types.ts@@ -7285,6 +7285,7 @@ interface ResourceAuthMethodLoginEvent {metadata: {resourceType: ResourceAuthMethodResourceType;resourceId: string;+ resourceName?: string;method: ResourceAuthMethodKind;methodConfigId: string;principalArn?: string;@@ -7303,6 +7304,7 @@ interface ResourceAuthMethodLoginFailedEvent {metadata: {resourceType: ResourceAuthMethodResourceType;resourceId: string;+ resourceName?: string;method: ResourceAuthMethodKind;reasonCode: string;message: string;@@ -7321,6 +7323,7 @@ interface ResourceAuthMethodUpdateEvent {metadata: {resourceType: ResourceAuthMethodResourceType;resourceId: string;+ resourceName?: string;method: ResourceAuthMethodKind;methodConfigId: string;stsEndpoint?: string;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.tsindex f66c4efad3..aeff33887b 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,7 @@import { UnauthorizedError } from "@app/lib/errors";+import { validateIamIdentity, validateIdTokenIdentity } from "@app/services/identity-gcp-auth/identity-gcp-auth-fns";-import { assertIamTokenLifetime, validateGcpAllowlists } from "./gcp-auth-fns";+import { assertIamTokenLifetime, validateGcpAllowlists, verifyGcpTokenAndExtractCaller } from "./gcp-auth-fns";import { GcpAuthType } from "./resource-auth-method-fns";const SERVICE_ACCOUNT = "[email protected]";@@ -149,3 +150,68 @@ describe("validateGcpAllowlists", () => {).not.toThrow();});});++vi.mock("@app/services/identity-gcp-auth/identity-gcp-auth-fns", () => ({+ validateIdTokenIdentity: vi.fn(),+ validateIamIdentity: vi.fn()+}));++// Neither the logger nor the crypto layer is initialised in the unit environment.+vi.mock("@app/lib/logger", () => ({ logger: { error: vi.fn(), info: vi.fn(), warn: vi.fn() } }));+vi.mock("@app/lib/crypto", () => ({+ crypto: {+ jwt: () => ({+ decode: (token: string) =>+ JSON.parse(Buffer.from(token.split(".")[1], "base64url").toString()) as Record<string, unknown>+ })+ }+}));++describe("verifyGcpTokenAndExtractCaller", () => {+ const errorContext = { resourceId: "gw-1", orgId: "org-1" };+ const futureExp = Math.floor(Date.now() / 1000) + 600;+ // Header and signature are irrelevant: the mocked validators stand in for the real verification,+ // and only the payload is decoded here for the lifetime check.+ const jwtWith = (payload: object) => `x.${Buffer.from(JSON.stringify(payload)).toString("base64url")}.y`;++ beforeEach(() => {+ vi.mocked(validateIdTokenIdentity).mockReset();+ vi.mocked(validateIamIdentity).mockReset();+ });++ test("dispatches the gce type to the ID token validator", async () => {+ vi.mocked(validateIdTokenIdentity).mockResolvedValue({ email: SERVICE_ACCOUNT, computeEngineDetails: undefined });+ await verifyGcpTokenAndExtractCaller({ type: "gce", jwt: "t", audience: "gw-1", errorContext });+ expect(validateIdTokenIdentity).toHaveBeenCalledWith({ audience: "gw-1", jwt: "t" });+ expect(validateIamIdentity).not.toHaveBeenCalled();+ });++ test("dispatches the iam type to the signed JWT validator", async () => {+ vi.mocked(validateIamIdentity).mockResolvedValue({ email: SERVICE_ACCOUNT });+ const jwt = jwtWith({ sub: SERVICE_ACCOUNT, aud: "gw-1", exp: futureExp });+ await verifyGcpTokenAndExtractCaller({ type: "iam", jwt, audience: "gw-1", errorContext });+ expect(validateIamIdentity).toHaveBeenCalledWith({ audience: "gw-1", jwt });+ expect(validateIdTokenIdentity).not.toHaveBeenCalled();+ });++ test("wraps a verification failure as an unauthorized error carrying the resource id", async () => {+ vi.mocked(validateIdTokenIdentity).mockRejectedValue(new Error("bad signature"));+ await expect(+ verifyGcpTokenAndExtractCaller({ type: "gce", jwt: "t", audience: "gw-1", errorContext })+ ).rejects.toMatchObject({+ detail: { reasonCode: "gcp_token_verification_failed", resourceId: "gw-1" }+ });+ });++ test("refuses a verified iam token whose payload has no expiry", async () => {+ vi.mocked(validateIamIdentity).mockResolvedValue({ email: SERVICE_ACCOUNT });+ await expect(+ verifyGcpTokenAndExtractCaller({+ type: "iam",+ jwt: jwtWith({ sub: SERVICE_ACCOUNT, aud: "gw-1" }),+ audience: "gw-1",+ errorContext+ })+ ).rejects.toMatchObject({ detail: { reasonCode: "gcp_token_verification_failed" } });+ });+});