Skip to content

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

fix(gateway): separate a wrong GCP audience from a rejected token

bernie-g7576737e41e3
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 f57bd7c9d2..0c0b49217c 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
@@ -210,8 +210,27 @@ describe("verifyGcpTokenAndExtractCaller", () => {
expect(validateIdTokenIdentity).not.toHaveBeenCalled();
});
- test("reports a signature or upstream failure separately from a rejected claim", async () => {
- vi.mocked(validateIdTokenIdentity).mockRejectedValue(new Error("bad signature"));
+ test("names a token minted for a different audience before calling a validator", async () => {
+ await expect(
+ verifyGcpTokenAndExtractCaller({
+ type: "gce",
+ jwt: jwtWith({ aud: "another-gateway", email: SERVICE_ACCOUNT }),
+ audience: "gw-1",
+ errorContext
+ })
+ ).rejects.toMatchObject({ detail: { reasonCode: "gcp_token_audience_rejected", resourceId: "gw-1" } });
+ expect(validateIdTokenIdentity).not.toHaveBeenCalled();
+ });
+
+ test("accepts an audience the token lists among several", async () => {
+ vi.mocked(validateIdTokenIdentity).mockResolvedValue({ email: SERVICE_ACCOUNT, computeEngineDetails: undefined });
+ const jwt = jwtWith({ aud: ["other", "gw-1"], email: SERVICE_ACCOUNT });
+ await verifyGcpTokenAndExtractCaller({ type: "gce", jwt, audience: "gw-1", errorContext });
+ expect(validateIdTokenIdentity).toHaveBeenCalledWith({ audience: "gw-1", jwt });
+ });
+
+ test("reports Google being unreachable separately from a rejected token", async () => {
+ vi.mocked(validateIdTokenIdentity).mockRejectedValue(new Error("ETIMEDOUT"));
await expect(
verifyGcpTokenAndExtractCaller({
type: "gce",
@@ -224,18 +243,27 @@ describe("verifyGcpTokenAndExtractCaller", () => {
});
});
- test("reports a claim the validator rejected as such", async () => {
- vi.mocked(validateIamIdentity).mockRejectedValue(new UnauthorizedError({ message: "Invalid audience" }));
+ // The audience is settled above, so this bucket can no longer be a wrong-audience token.
+ test("reports a token the validator rejected as such", async () => {
+ vi.mocked(validateIdTokenIdentity).mockRejectedValue(new UnauthorizedError({ message: "Invalid GCP ID token" }));
await expect(
verifyGcpTokenAndExtractCaller({
- type: "iam",
- jwt: jwtWith({ sub: SERVICE_ACCOUNT, aud: "gw-1", exp: futureExp }),
+ type: "gce",
+ jwt: jwtWith({ aud: "gw-1", email: SERVICE_ACCOUNT }),
audience: "gw-1",
errorContext
})
).rejects.toMatchObject({
detail: { reasonCode: "gcp_token_rejected", resourceId: "gw-1" }
});
+ await expect(
+ verifyGcpTokenAndExtractCaller({
+ type: "gce",
+ jwt: jwtWith({ aud: "gw-1", email: SERVICE_ACCOUNT }),
+ audience: "gw-1",
+ errorContext
+ })
+ ).rejects.toMatchObject({ message: expect.not.stringContaining("audience") as string });
});
test("refuses a verified iam token whose payload has no expiry", async () => {
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 27275c3e1d..4ffc58dac2 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
@@ -47,7 +47,7 @@ export const verifyGcpTokenAndExtractCaller = async ({
errorContext
}: TVerifyGcpTokenInput): Promise<TGcpIdentityDetails> => {
// A token that does not even parse is the caller's mistake, not a verification outcome.
- const unverifiedPayload = crypto.jwt().decode(jwt) as { email?: string } | null;
+ const unverifiedPayload = crypto.jwt().decode(jwt) as { email?: string; aud?: string | string[] } | null;
if (!unverifiedPayload) {
throw new UnauthorizedError({
message: "Access denied: the GCP token could not be parsed as a JWT.",
@@ -66,6 +66,20 @@ export const verifyGcpTokenAndExtractCaller = async ({
});
}
+ // Checked here rather than left to the validators: google-auth-library reports a wrong audience,
+ // a bad signature and an expired token as one indistinguishable failure. The signature is still
+ // verified below, so an attacker gains nothing by putting the right audience on a forged token.
+ const claimedAudience = unverifiedPayload.aud;
+ const audienceMatches = Array.isArray(claimedAudience)
+ ? claimedAudience.includes(audience)
+ : claimedAudience === audience;
+ if (!audienceMatches) {
+ throw new UnauthorizedError({
+ message: `Access denied: the GCP token was issued for a different audience. Request it with this gateway's ID (${audience}) as the audience.`,
+ detail: { reasonCode: ResourceAuthLoginFailureReason.GcpTokenAudienceRejected, ...errorContext }
+ });
+ }
+
let identityDetails: TGcpIdentityDetails;
try {
identityDetails =
@@ -78,15 +92,15 @@ export const verifyGcpTokenAndExtractCaller = async ({
`Resource GCP Auth Login: token verification failed [resourceId=${String(errorContext.resourceId)}]`
);
- // The validators raise UnauthorizedError when a claim is wrong and anything else when the
- // signature check or the call to Google fails, so the two are worth telling apart.
- const claimRejected = err instanceof UnauthorizedError;
+ // The validators raise UnauthorizedError when they reject the token itself and anything else
+ // when the call to Google fails, so the two are worth telling apart.
+ const tokenRejected = err instanceof UnauthorizedError;
throw new UnauthorizedError({
- message: claimRejected
- ? "Access denied: the GCP token's claims were rejected. Check that it was issued for this gateway's ID as the audience, and that it carries a service account identity."
- : "Access denied: the GCP token could not be verified against Google's signing keys. This is a bad signature, an expired token, or Google being unreachable.",
+ message: tokenRejected
+ ? "Access denied: the GCP token was rejected. Its signature did not verify against Google's signing keys, it has expired, or its service account identifier is not one Google issues."
+ : "Access denied: the GCP token could not be checked because Google's signing key endpoint could not be reached. Retry shortly.",
detail: {
- reasonCode: claimRejected
+ reasonCode: tokenRejected
? ResourceAuthLoginFailureReason.GcpTokenRejected
: ResourceAuthLoginFailureReason.GcpTokenVerificationFailed,
...errorContext
diff --git a/backend/src/ee/services/resource-auth-method/resource-auth-method-fns.ts b/backend/src/ee/services/resource-auth-method/resource-auth-method-fns.ts
index 36d8b309a6..406eb27cf1 100644
--- a/backend/src/ee/services/resource-auth-method/resource-auth-method-fns.ts
+++ b/backend/src/ee/services/resource-auth-method/resource-auth-method-fns.ts
@@ -174,6 +174,7 @@ export const ResourceAuthLoginFailureReason = {
GatewayProxyUnavailable: "gateway_proxy_unavailable",
GcpMalformedToken: "gcp_malformed_token",
GcpMissingEmailClaim: "gcp_missing_email_claim",
+ GcpTokenAudienceRejected: "gcp_token_audience_rejected",
GcpTokenRejected: "gcp_token_rejected",
GcpTokenVerificationFailed: "gcp_token_verification_failed",
GcpTokenLifetimeRejected: "gcp_token_lifetime_rejected",