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(identity-auth): require a trust binding for AWS and GCP machine identities
Calvin92302d43c4a0
diff --git a/backend/src/server/routes/v1/identity-aws-iam-auth-router.ts b/backend/src/server/routes/v1/identity-aws-iam-auth-router.tsindex 4619cfa585..2981d5c25d 100644--- a/backend/src/server/routes/v1/identity-aws-iam-auth-router.ts+++ b/backend/src/server/routes/v1/identity-aws-iam-auth-router.ts@@ -259,8 +259,8 @@ export const registerIdentityAwsAuthRouter = async (server: FastifyZodProvider)body: z.object({stsEndpoint: z.string().trim().min(1).optional().describe(AWS_AUTH.UPDATE.stsEndpoint),- allowedPrincipalArns: validatePrincipalArns.describe(AWS_AUTH.UPDATE.allowedPrincipalArns),- allowedAccountIds: validateAccountIds.describe(AWS_AUTH.UPDATE.allowedAccountIds),+ allowedPrincipalArns: validatePrincipalArns.optional().describe(AWS_AUTH.UPDATE.allowedPrincipalArns),+ allowedAccountIds: validateAccountIds.optional().describe(AWS_AUTH.UPDATE.allowedAccountIds),accessTokenTrustedIps: z.object({ipAddress: z.string().trim()diff --git a/backend/src/services/identity-aws-auth/identity-aws-auth-fns.test.ts b/backend/src/services/identity-aws-auth/identity-aws-auth-fns.test.tsnew file mode 100644index 0000000000..3dc5ac61b9--- /dev/null+++ b/backend/src/services/identity-aws-auth/identity-aws-auth-fns.test.ts@@ -0,0 +1,24 @@+import { describe, expect, test } from "vitest";++import { hasAwsTrustBinding } from "./identity-aws-auth-fns";++describe("hasAwsTrustBinding", () => {+ test.each([+ { allowedAccountIds: null, allowedPrincipalArns: null },+ { allowedAccountIds: undefined, allowedPrincipalArns: undefined },+ { allowedAccountIds: "", allowedPrincipalArns: "" },+ { allowedAccountIds: " ", allowedPrincipalArns: " " }+ ])("is false when both lists are unset: %o", (binding) => {+ expect(hasAwsTrustBinding(binding)).toBe(false);+ });++ test("is true when only the account list is set", () => {+ expect(hasAwsTrustBinding({ allowedAccountIds: "123456789012", allowedPrincipalArns: "" })).toBe(true);+ });++ test("is true when only the principal ARN list is set", () => {+ expect(+ hasAwsTrustBinding({ allowedAccountIds: "", allowedPrincipalArns: "arn:aws:iam::123456789012:role/app" })+ ).toBe(true);+ });+});diff --git a/backend/src/services/identity-aws-auth/identity-aws-auth-fns.ts b/backend/src/services/identity-aws-auth/identity-aws-auth-fns.tsindex a028bc25f9..8a79edde0e 100644--- a/backend/src/services/identity-aws-auth/identity-aws-auth-fns.ts+++ b/backend/src/services/identity-aws-auth/identity-aws-auth-fns.ts@@ -91,3 +91,15 @@ export const extractPrincipalArn = (arn: string, formatAsIamRole: boolean = falsreturn `arn:${entity.Partition}:${formatAsIamRole ? "iam" : entity.Service}::${entity.AccountNumber}:${entity.Type}/${entity.FriendlyName}`;};++// Login only checks the lists that are set, so an empty pair would admit any valid AWS principal.+export const hasAwsTrustBinding = ({+ allowedAccountIds,+ allowedPrincipalArns+}: {+ allowedAccountIds?: string | null;+ allowedPrincipalArns?: string | null;+}) => Boolean(allowedAccountIds?.trim()) || Boolean(allowedPrincipalArns?.trim());++export const AWS_TRUST_BINDING_REQUIRED_MESSAGE =+ "Configure at least one allowed AWS account ID or principal ARN. Without one, any AWS principal that knows this identity's ID could log in as it.";diff --git a/backend/src/services/identity-aws-auth/identity-aws-auth-service.ts b/backend/src/services/identity-aws-auth/identity-aws-auth-service.tsindex f0977b4427..23476fe44c 100644--- a/backend/src/services/identity-aws-auth/identity-aws-auth-service.ts+++ b/backend/src/services/identity-aws-auth/identity-aws-auth-service.ts@@ -40,7 +40,12 @@ import { recordIdentityLastLoginDebounced } from "../membership-identity/membersimport { TOrgDALFactory } from "../org/org-dal";import { validateIdentityUpdateForSuperAdminPrivileges } from "../super-admin/super-admin-fns";import { TIdentityAwsAuthDALFactory } from "./identity-aws-auth-dal";-import { extractPrincipalArn, extractPrincipalArnEntity } from "./identity-aws-auth-fns";+import {+ AWS_TRUST_BINDING_REQUIRED_MESSAGE,+ extractPrincipalArn,+ extractPrincipalArnEntity,+ hasAwsTrustBinding+} from "./identity-aws-auth-fns";import {TAttachAwsAuthDTO,TAwsGetCallerIdentityHeaders,@@ -149,6 +154,19 @@ export const identityAwsAuthServiceFactory = ({let subOrganizationId = isSubOrgIdentity ? org.id : null;try {+ if (!hasAwsTrustBinding(identityAwsAuth)) {+ throw new UnauthorizedError({+ message:+ "Access denied: this identity has no AWS account ID or principal ARN configured. An admin must add one before it can authenticate.",+ detail: {+ reasonCode: "trust_binding_missing",+ identityId: identity.id,+ orgId: identity.orgId,+ identityName: identity.name+ }+ });+ }+const headers: TAwsGetCallerIdentityHeaders = JSON.parse(Buffer.from(iamRequestHeaders, "base64").toString());const body: string = Buffer.from(iamRequestBody, "base64").toString();@@ -382,6 +400,10 @@ export const identityAwsAuthServiceFactory = ({throw new BadRequestError({ message: "Access token TTL cannot be greater than max TTL" });}+ if (!hasAwsTrustBinding({ allowedAccountIds, allowedPrincipalArns })) {+ throw new BadRequestError({ message: AWS_TRUST_BINDING_REQUIRED_MESSAGE });+ }+await assertIdentityAuthAccessAllowed({ permissionService, orgDAL },{@@ -483,6 +505,15 @@ export const identityAwsAuthServiceFactory = ({const identityAwsAuth = await identityAwsAuthDAL.findOne({ identityId });+ if (+ !hasAwsTrustBinding({+ allowedAccountIds: allowedAccountIds ?? identityAwsAuth.allowedAccountIds,+ allowedPrincipalArns: allowedPrincipalArns ?? identityAwsAuth.allowedPrincipalArns+ })+ ) {+ throw new BadRequestError({ message: AWS_TRUST_BINDING_REQUIRED_MESSAGE });+ }+if ((accessTokenMaxTTL || identityAwsAuth.accessTokenMaxTTL) > 0 &&(accessTokenTTL || identityAwsAuth.accessTokenMaxTTL) > (accessTokenMaxTTL || identityAwsAuth.accessTokenMaxTTL)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.tsindex 22477cb69b..36dc596d41 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@@ -7,6 +7,27 @@ import { UnauthorizedError } from "@app/lib/errors";import { TDecodedGcpIamAuthJwt, TGcpIdTokenPayload } from "./identity-gcp-auth-types";+// IAM tokens only carry a service account, so that is the only binding that can restrict them. GCE tokens+// can also be restricted by project or zone, which is what the Compute Engine checks in login read.+export const hasGcpTrustBinding = ({+ type,+ allowedServiceAccounts,+ allowedProjects,+ allowedZones+}: {+ type: string;+ allowedServiceAccounts?: string | null;+ allowedProjects?: string | null;+ allowedZones?: string | null;+}) => {+ if (allowedServiceAccounts?.trim()) return true;+ if (type === "iam") return false;+ return Boolean(allowedProjects?.trim()) || Boolean(allowedZones?.trim());+};++export const GCP_TRUST_BINDING_REQUIRED_MESSAGE =+ "Configure at least one allowed GCP service account, or for GCE identities a project or zone. Without one, any GCP workload that knows this identity's ID could log in as it.";+/*** Validates that the identity token [jwt] sent in from a client GCE instance as part of GCP ID Token authentication* is valid.diff --git a/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.test.ts b/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.test.tsindex 94e1216870..6d6242acce 100644--- a/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.test.ts+++ b/backend/src/services/identity-gcp-auth/identity-gcp-auth-service.test.ts@@ -8,7 +8,11 @@ const { validateIamIdentity, validateIdTokenIdentity } = vi.hoisted(() => ({validateIdTokenIdentity: vi.fn()}));-vi.mock("./identity-gcp-auth-fns", () => ({ validateIamIdentity, validateIdTokenIdentity }));+vi.mock("./identity-gcp-auth-fns", async (importOriginal) => ({+ ...(await importOriginal<typeof import("./identity-gcp-auth-fns")>()),+ validateIamIdentity,+ validateIdTokenIdentity+}));vi.mock("@app/lib/config/env", () => ({getConfig: () => ({ OTEL_TELEMETRY_COLLECTION_ENABLED: false })@@ -121,9 +125,12 @@ describe("gcpAuthService.login GCE Compute Engine detail requirement", () => {});// Nothing about the identity depends on where the token came from, so requiring GCE details- // here would break every unrestricted GCE configuration in the field.- test("allows a token with no Compute Engine details when neither projects nor zones are restricted", async () => {- const { result } = login({ type: "gce" }, { email: SERVICE_ACCOUNT, computeEngineDetails: undefined });+ // here would break every service-account-restricted GCE configuration in the field.+ test("allows a token with no Compute Engine details when only the service account is restricted", async () => {+ const { result } = login(+ { type: "gce", allowedServiceAccounts: SERVICE_ACCOUNT },+ { email: SERVICE_ACCOUNT, computeEngineDetails: undefined }+ );await expect(result).resolves.toMatchObject({ accessToken: ACCESS_TOKEN });});@@ -132,7 +139,12 @@ describe("gcpAuthService.login GCE Compute Engine detail requirement", () => {// stay scoped to the gce type even when the stale project and zone columns are populated.test("allows an IAM login even while project and zone columns are populated", async () => {const { result } = login(- { type: "iam", allowedProjects: "my-project", allowedZones: "us-central1-a" },+ {+ type: "iam",+ allowedServiceAccounts: SERVICE_ACCOUNT,+ allowedProjects: "my-project",+ allowedZones: "us-central1-a"+ },{ email: SERVICE_ACCOUNT });@@ -182,3 +194,39 @@ describe("gcpAuthService.login GCE allow lists", () => {await expect(result).rejects.toMatchObject({ detail: { reasonCode: "zone_not_allowed" } });});});++// An identity with no binding would otherwise accept any token that carries its UUID as audience,+// so the login refuses it before the token is looked at.+describe("gcpAuthService.login trust binding", () => {+ test("rejects a GCE identity with no service account, project, or zone before verifying the token", async () => {+ const { result, issueIdentityAccessToken } = login(+ { type: "gce" },+ { email: SERVICE_ACCOUNT, computeEngineDetails }+ );++ await expect(result).rejects.toMatchObject({+ detail: { reasonCode: "trust_binding_missing", identityId: IDENTITY_ID, orgId: ORG_ID }+ });+ expect(validateIdTokenIdentity).not.toHaveBeenCalled();+ expect(issueIdentityAccessToken).not.toHaveBeenCalled();+ });++ test("rejects an IAM identity that has only project and zone set", async () => {+ const { result } = login(+ { type: "iam", allowedProjects: "my-project", allowedZones: "us-central1-a" },+ { email: SERVICE_ACCOUNT }+ );++ await expect(result).rejects.toMatchObject({ detail: { reasonCode: "trust_binding_missing" } });+ expect(validateIamIdentity).not.toHaveBeenCalled();+ });++ test("treats whitespace-only lists as unset", async () => {+ const { result } = login(+ { type: "gce", allowedServiceAccounts: " ", allowedProjects: " ", allowedZones: "" },+ { email: SERVICE_ACCOUNT, computeEngineDetails }+ );++ await expect(result).rejects.toMatchObject({ detail: { reasonCode: "trust_binding_missing" } });+ });+});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.tsindex 54979b2d17..837fedf3e0 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@@ -35,7 +35,12 @@ import { recordIdentityLastLoginDebounced } from "../membership-identity/membersimport { TOrgDALFactory } from "../org/org-dal";import { validateIdentityUpdateForSuperAdminPrivileges } from "../super-admin/super-admin-fns";import { TIdentityGcpAuthDALFactory } from "./identity-gcp-auth-dal";-import { validateIamIdentity, validateIdTokenIdentity } from "./identity-gcp-auth-fns";+import {+ GCP_TRUST_BINDING_REQUIRED_MESSAGE,+ hasGcpTrustBinding,+ validateIamIdentity,+ validateIdTokenIdentity+} from "./identity-gcp-auth-fns";import {TAttachGcpAuthDTO,TGcpIdentityDetails,@@ -103,6 +108,19 @@ export const identityGcpAuthServiceFactory = ({let subOrganizationId = isSubOrgIdentity ? org.id : null;try {+ if (!hasGcpTrustBinding(identityGcpAuth)) {+ throw new UnauthorizedError({+ message:+ "Access denied: this identity has no GCP service account, project, or zone configured. An admin must add one before it can authenticate.",+ detail: {+ reasonCode: "trust_binding_missing",+ identityId: identity.id,+ orgId: identity.orgId,+ identityName: identity.name+ }+ });+ }+let gcpIdentityDetails: TGcpIdentityDetails;switch (identityGcpAuth.type) {case "gce": {@@ -340,6 +358,10 @@ export const identityGcpAuthServiceFactory = ({throw new BadRequestError({ message: "Access token TTL cannot be greater than max TTL" });}+ if (!hasGcpTrustBinding({ type, allowedServiceAccounts, allowedProjects, allowedZones })) {+ throw new BadRequestError({ message: GCP_TRUST_BINDING_REQUIRED_MESSAGE });+ }+await assertIdentityAuthAccessAllowed({ permissionService, orgDAL },{@@ -442,6 +464,17 @@ export const identityGcpAuthServiceFactory = ({const identityGcpAuth = await identityGcpAuthDAL.findOne({ identityId });+ if (+ !hasGcpTrustBinding({+ type: type ?? identityGcpAuth.type,+ allowedServiceAccounts: allowedServiceAccounts ?? identityGcpAuth.allowedServiceAccounts,+ allowedProjects: allowedProjects ?? identityGcpAuth.allowedProjects,+ allowedZones: allowedZones ?? identityGcpAuth.allowedZones+ })+ ) {+ throw new BadRequestError({ message: GCP_TRUST_BINDING_REQUIRED_MESSAGE });+ }+if ((accessTokenMaxTTL || identityGcpAuth.accessTokenMaxTTL) > 0 &&(accessTokenTTL || identityGcpAuth.accessTokenMaxTTL) > (accessTokenMaxTTL || identityGcpAuth.accessTokenMaxTTL)