Skip to content

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

fix: enhance GCP identity token validation with detailed error handling

Victor Hugo dos Santos192ff24923f3
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 4864158608..7423313221 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
@@ -23,14 +23,25 @@ export const validateIdTokenIdentity = async ({
}) => {
const oAuth2Client = new OAuth2Client();
const response = await oAuth2Client.getFederatedSignonCerts();
- const ticket = await oAuth2Client.verifySignedJwtWithCertsAsync(
- identityToken,
- response.certs,
- identityId, // audience
- ["https://accounts.google.com"]
- );
- const payload = ticket.getPayload() as TGcpIdTokenPayload;
- if (!payload || !payload.email) throw new UnauthorizedError();
+
+ let payload: TGcpIdTokenPayload | undefined;
+ try {
+ const ticket = await oAuth2Client.verifySignedJwtWithCertsAsync(
+ identityToken,
+ response.certs,
+ identityId, // audience
+ ["https://accounts.google.com"]
+ );
+ payload = ticket.getPayload() as TGcpIdTokenPayload;
+ } catch {
+ // google-auth-library puts the whole token in its error messages, so the cause is dropped
+ // rather than attached to an error that gets logged.
+ throw new UnauthorizedError({ message: "Invalid GCP ID token" });
+ }
+
+ if (!payload || !payload.email) {
+ throw new UnauthorizedError({ message: "GCP ID token is missing an email claim" });
+ }
return { email: payload.email, computeEngineDetails: payload.google?.compute_engine };
};
@@ -51,7 +62,15 @@ export const validateIamIdentity = async ({
identityId: string;
jwt: string;
}) => {
- const decodedJwt = crypto.jwt().decode(serviceAccountJwt, { complete: true }) as TDecodedGcpIamAuthJwt;
+ let decodedJwt: TDecodedGcpIamAuthJwt;
+ try {
+ decodedJwt = crypto.jwt().decode(serviceAccountJwt, { complete: true }) as TDecodedGcpIamAuthJwt;
+ } catch {
+ throw new UnauthorizedError({ message: "Invalid GCP IAM token" });
+ }
+ if (!decodedJwt?.payload || !decodedJwt?.header?.kid) {
+ throw new UnauthorizedError({ message: "Invalid GCP IAM token" });
+ }
const { sub, aud } = decodedJwt.payload;
if (!sub || !serviceAccountEmailRegex.test(sub)) {
@@ -67,10 +86,17 @@ export const validateIamIdentity = async ({
} = await request.get(`https://www.googleapis.com/service_accounts/v1/metadata/x509/${encodeURIComponent(sub)}`);
const publicKey = data[decodedJwt.header.kid];
+ if (!publicKey) {
+ throw new UnauthorizedError({ message: "No matching signing key found for the GCP IAM token" });
+ }
- crypto.jwt().verify(serviceAccountJwt, publicKey, {
- algorithms: ["RS256"]
- });
+ try {
+ crypto.jwt().verify(serviceAccountJwt, publicKey, {
+ algorithms: ["RS256"]
+ });
+ } catch {
+ throw new UnauthorizedError({ message: "Invalid GCP IAM token signature" });
+ }
if (aud !== identityId) throw new UnauthorizedError({ message: "Invalid audience in GCP IAM Token" });
return { email: sub };
diff --git a/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.ts b/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.ts
index 8f0c5a6892..0e40599d0a 100644
--- a/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.ts
+++ b/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.ts
@@ -123,6 +123,22 @@ export const identityGcpAuthServiceFactory = ({
}
}
+ if (
+ identityGcpAuth.type === "gce" &&
+ (identityGcpAuth.allowedProjects || identityGcpAuth.allowedZones) &&
+ !gcpIdentityDetails.computeEngineDetails
+ ) {
+ throw new UnauthorizedError({
+ message: "Access denied: GCP identity token is missing the Compute Engine details required to verify it.",
+ detail: {
+ reasonCode: "compute_engine_details_missing",
+ identityId: identity.id,
+ orgId: identity.orgId,
+ identityName: identity.name
+ }
+ });
+ }
+
if (identityGcpAuth.allowedServiceAccounts) {
// validate if the service account is in the list of allowed service accounts
@@ -143,11 +159,7 @@ export const identityGcpAuthServiceFactory = ({
});
}
- if (
- identityGcpAuth.type === "gce" &&
- identityGcpAuth.allowedProjects &&
- gcpIdentityDetails.computeEngineDetails
- ) {
+ if (identityGcpAuth.type === "gce" && identityGcpAuth.allowedProjects) {
// validate if the project that the service account belongs to is in the list of allowed projects
const isProjectAllowed = identityGcpAuth.allowedProjects
@@ -167,7 +179,7 @@ export const identityGcpAuthServiceFactory = ({
});
}
- if (identityGcpAuth.type === "gce" && identityGcpAuth.allowedZones && gcpIdentityDetails.computeEngineDetails) {
+ if (identityGcpAuth.type === "gce" && identityGcpAuth.allowedZones) {
const isZoneAllowed = identityGcpAuth.allowedZones
.split(",")
.map((zone) => zone.trim())