Skip to content

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

fix(identity-auth): ignore separator-only GCP lists and cover the AWS service paths

Calvin2f38d07f8f89
diff --git a/backend/src/services/identity-aws-auth/identity-aws-auth-service.test.ts b/backend/src/services/identity-aws-auth/identity-aws-auth-service.test.ts
new file mode 100644
index 0000000000..02387f7221
--- /dev/null
+++ b/backend/src/services/identity-aws-auth/identity-aws-auth-service.test.ts
@@ -0,0 +1,225 @@
+import { beforeEach, describe, expect, test, vi } from "vitest";
+
+import { IdentityAuthMethod } from "@app/db/schemas";
+import { logger } from "@app/lib/logger";
+
+import { identityAwsAuthServiceFactory } from "./identity-aws-auth-service";
+
+const { request } = vi.hoisted(() => ({ request: vi.fn() }));
+
+vi.mock("@app/lib/logger", () => ({
+ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }
+}));
+
+vi.mock("@app/lib/config/env", () => ({
+ getConfig: () => ({ OTEL_TELEMETRY_COLLECTION_ENABLED: false })
+}));
+
+vi.mock("@fastify/request-context", () => ({
+ requestContext: { get: vi.fn(() => undefined) }
+}));
+
+vi.mock("@app/lib/config/request", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("@app/lib/config/request")>();
+ return { ...actual, request };
+});
+
+vi.mock("../identity/identity-auth-permission-fns", () => ({ assertIdentityAuthAccessAllowed: vi.fn() }));
+vi.mock("../super-admin/super-admin-fns", () => ({ validateIdentityUpdateForSuperAdminPrivileges: vi.fn() }));
+vi.mock("@app/services/identity/identity-auth-method-events", () => ({
+ emitIdentityAuthMethodChanged: vi.fn(),
+ IdentityAuthMethodChange: { Added: "added", Updated: "updated", Removed: "removed" }
+}));
+
+const IDENTITY_ID = "5d3a8b2e-7c61-4b0f-9f3e-2a1c4d6e8f90";
+const ORG_ID = "org-id";
+const ACCESS_TOKEN = "access-token";
+const ACCOUNT_ID = "123456789012";
+const ROLE_ARN = `arn:aws:iam::${ACCOUNT_ID}:role/app-role`;
+
+type TAwsAuthRow = {
+ allowedAccountIds: string;
+ allowedPrincipalArns: string;
+};
+
+const makeService = (awsAuth: Partial<TAwsAuthRow> = {}, authMethods: string[] = []) => {
+ const updateById = vi.fn().mockImplementation(async (_id: string, data: Record<string, unknown>) => ({
+ id: "auth-id",
+ ...data
+ }));
+ const create = vi.fn();
+ const issueIdentityAccessToken = vi.fn().mockResolvedValue({
+ accessToken: ACCESS_TOKEN,
+ identityAccessToken: { id: "access-token-id" }
+ });
+
+ const service = identityAwsAuthServiceFactory({
+ identityAwsAuthDAL: {
+ findOne: vi.fn().mockResolvedValue({
+ id: "auth-id",
+ identityId: IDENTITY_ID,
+ stsEndpoint: "https://sts.amazonaws.com/",
+ allowedAccountIds: "",
+ allowedPrincipalArns: "",
+ accessTokenTTL: 3600,
+ accessTokenMaxTTL: 7200,
+ accessTokenNumUsesLimit: 0,
+ accessTokenPeriod: 0,
+ accessTokenTrustedIps: [{ ipAddress: "0.0.0.0/0" }],
+ ...awsAuth
+ }),
+ transaction: vi.fn(async (fn: (tx: unknown) => unknown) => fn({})),
+ updateById,
+ create,
+ delete: vi.fn()
+ } as never,
+ identityDAL: {
+ findById: vi.fn().mockResolvedValue({ id: IDENTITY_ID, orgId: ORG_ID, name: "aws-identity" })
+ } as never,
+ orgDAL: {
+ findById: vi.fn().mockResolvedValue({ id: ORG_ID, name: "org", slug: "org", rootOrgId: null, parentOrgId: null })
+ } as never,
+ membershipIdentityDAL: {
+ update: vi.fn().mockResolvedValue(undefined),
+ getIdentityById: vi.fn().mockResolvedValue({
+ scopeOrgId: ORG_ID,
+ identity: { id: IDENTITY_ID, orgId: ORG_ID, projectId: null, authMethods }
+ })
+ } as never,
+ keyStore: { setItemWithExpiryNX: vi.fn().mockResolvedValue(null) } as never,
+ identityAccessTokenDAL: {} as never,
+ permissionService: {} as never,
+ licenseService: { getPlan: vi.fn().mockResolvedValue({ ipAllowlisting: true }) } as never,
+ identityAccessTokenService: {
+ issueIdentityAccessToken,
+ invalidateTrustedIpsCache: vi.fn().mockResolvedValue(undefined)
+ } as never,
+ eventEmitter: { emit: vi.fn() } as never
+ });
+
+ return { service, updateById, create, issueIdentityAccessToken };
+};
+
+beforeEach(() => {
+ vi.clearAllMocks();
+});
+
+describe("awsAuthService.login without a trust binding", () => {
+ const stsCaller = {
+ Account: ACCOUNT_ID,
+ Arn: `arn:aws:sts::${ACCOUNT_ID}:assumed-role/app-role/session`,
+ UserId: "AROA"
+ };
+
+ const login = (awsAuth: Partial<TAwsAuthRow>) => {
+ const { service, issueIdentityAccessToken } = makeService(awsAuth);
+ request.mockResolvedValue({
+ data: { GetCallerIdentityResponse: { GetCallerIdentityResult: stsCaller } }
+ });
+ const headers = {
+ Authorization: "AWS4-HMAC-SHA256 Credential=AKIA/20260101/us-east-1/sts/aws4_request, SignedHeaders=host"
+ };
+ return {
+ result: service.login({
+ identityId: IDENTITY_ID,
+ iamHttpRequestMethod: "POST",
+ iamRequestBody: Buffer.from("Action=GetCallerIdentity&Version=2011-06-15").toString("base64"),
+ iamRequestHeaders: Buffer.from(JSON.stringify(headers)).toString("base64")
+ } as never),
+ issueIdentityAccessToken
+ };
+ };
+
+ test("allows an identity with no account ID or principal ARN and logs a warning", async () => {
+ const { result } = login({});
+
+ await expect(result).resolves.toMatchObject({ accessToken: ACCESS_TOKEN });
+ expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining(IDENTITY_ID));
+ });
+
+ test("does not warn when an allow list is set", async () => {
+ const { result } = login({ allowedAccountIds: ACCOUNT_ID });
+
+ await expect(result).resolves.toMatchObject({ accessToken: ACCESS_TOKEN });
+ expect(logger.warn).not.toHaveBeenCalled();
+ });
+
+ test("still enforces an allow list that is set", async () => {
+ const { result } = login({ allowedAccountIds: "999999999999" });
+
+ await expect(result).rejects.toMatchObject({ detail: { reasonCode: "account_id_not_allowed" } });
+ });
+});
+
+// Attach and update are the only places a binding can be saved. The permission service is an empty object, so a
+// call that got past the binding check would throw a TypeError instead of the binding message.
+describe("awsAuthService trust binding on write", () => {
+ const BINDING_MESSAGE = "Configure at least one allowed AWS account ID or principal ARN";
+
+ const attach = (body: Record<string, unknown>) => {
+ const { service, create } = makeService({}, []);
+ return {
+ result: service.attachAwsAuth({
+ identityId: IDENTITY_ID,
+ actorOrgId: ORG_ID,
+ stsEndpoint: "https://sts.amazonaws.com/",
+ allowedAccountIds: "",
+ allowedPrincipalArns: "",
+ accessTokenTTL: 3600,
+ accessTokenMaxTTL: 7200,
+ accessTokenNumUsesLimit: 0,
+ accessTokenTrustedIps: [],
+ ...body
+ } as never),
+ create
+ };
+ };
+
+ const update = (existing: Partial<TAwsAuthRow>, body: Record<string, unknown>) => {
+ const { service, updateById } = makeService(existing, [IdentityAuthMethod.AWS_AUTH]);
+ return {
+ result: service.updateAwsAuth({ identityId: IDENTITY_ID, actorOrgId: ORG_ID, ...body } as never),
+ updateById
+ };
+ };
+
+ test("attach rejects an identity with no account ID or principal ARN", async () => {
+ const { result, create } = attach({});
+
+ await expect(result).rejects.toThrow(BINDING_MESSAGE);
+ expect(create).not.toHaveBeenCalled();
+ });
+
+ test("update rejects clearing the last allow list", async () => {
+ const { result, updateById } = update({ allowedAccountIds: ACCOUNT_ID }, { allowedAccountIds: "" });
+
+ await expect(result).rejects.toThrow(BINDING_MESSAGE);
+ expect(updateById).not.toHaveBeenCalled();
+ });
+
+ test("update rejects a change to an identity that has no binding and does not add one", async () => {
+ const { result } = update({}, { accessTokenTTL: 1800 });
+
+ await expect(result).rejects.toThrow(BINDING_MESSAGE);
+ });
+
+ test("update allows clearing one list while the other stays set", async () => {
+ const { result, updateById } = update(
+ { allowedAccountIds: ACCOUNT_ID, allowedPrincipalArns: ROLE_ARN },
+ { allowedAccountIds: "" }
+ );
+
+ await expect(result).resolves.toBeDefined();
+ expect(updateById.mock.calls[0][1]).toMatchObject({ allowedAccountIds: "" });
+ });
+
+ test("a TTL-only update leaves the stored lists alone", async () => {
+ const { result, updateById } = update({ allowedAccountIds: ACCOUNT_ID }, { accessTokenTTL: 1800 });
+
+ await expect(result).resolves.toBeDefined();
+ const written = updateById.mock.calls[0][1] as Record<string, unknown>;
+ expect(written.accessTokenTTL).toBe(1800);
+ expect(written.allowedAccountIds).toBeUndefined();
+ expect(written.allowedPrincipalArns).toBeUndefined();
+ });
+});
diff --git a/backend/src/services/identity-aws-auth/identity-aws-auth-validators.test.ts b/backend/src/services/identity-aws-auth/identity-aws-auth-validators.test.ts
new file mode 100644
index 0000000000..643281690d
--- /dev/null
+++ b/backend/src/services/identity-aws-auth/identity-aws-auth-validators.test.ts
@@ -0,0 +1,32 @@
+import { describe, expect, test } from "vitest";
+
+import {
+ validateAccountIds,
+ validateAccountIdsForUpdate,
+ validatePrincipalArns,
+ validatePrincipalArnsForUpdate
+} from "./identity-aws-auth-validators";
+
+describe("AWS allow list validators", () => {
+ test("an omitted list stays undefined on update, so the stored value is kept", () => {
+ expect(validateAccountIdsForUpdate.parse(undefined)).toBeUndefined();
+ expect(validatePrincipalArnsForUpdate.parse(undefined)).toBeUndefined();
+ });
+
+ test("an omitted list defaults to empty on create", () => {
+ expect(validateAccountIds.parse(undefined)).toBe("");
+ expect(validatePrincipalArns.parse(undefined)).toBe("");
+ });
+
+ test("an explicit empty string is kept on update, so a list can be cleared", () => {
+ expect(validateAccountIdsForUpdate.parse("")).toBe("");
+ expect(validatePrincipalArnsForUpdate.parse(" ")).toBe("");
+ });
+
+ test("a list that only holds separators is rejected", () => {
+ expect(validateAccountIds.safeParse(",").success).toBe(false);
+ expect(validateAccountIdsForUpdate.safeParse(", ,").success).toBe(false);
+ expect(validatePrincipalArns.safeParse(",").success).toBe(false);
+ expect(validatePrincipalArnsForUpdate.safeParse(", ,").success).toBe(false);
+ });
+});
diff --git a/backend/src/services/identity-gcp-auth/identity-gcp-auth-fns.test.ts b/backend/src/services/identity-gcp-auth/identity-gcp-auth-fns.test.ts
index aa0a684c44..de7e0f2456 100644
--- a/backend/src/services/identity-gcp-auth/identity-gcp-auth-fns.test.ts
+++ b/backend/src/services/identity-gcp-auth/identity-gcp-auth-fns.test.ts
@@ -4,7 +4,7 @@ import { afterAll, beforeAll, beforeEach, describe, expect, test, vi } from "vit
import { crypto as infisicalCrypto } from "@app/lib/crypto";
-import { validateIamIdentity, validateIdTokenIdentity } from "./identity-gcp-auth-fns";
+import { hasGcpTrustBinding, validateIamIdentity, validateIdTokenIdentity } from "./identity-gcp-auth-fns";
const { getFederatedSignonCerts, verifySignedJwtWithCertsAsync, requestGet } = vi.hoisted(() => ({
getFederatedSignonCerts: vi.fn(),
@@ -256,3 +256,28 @@ describe("validateIamIdentity", () => {
).rejects.toThrow("Invalid audience in GCP IAM Token");
});
});
+
+describe("hasGcpTrustBinding", () => {
+ test.each([
+ { type: "gce", allowedServiceAccounts: null, allowedProjects: null },
+ { type: "gce", allowedServiceAccounts: "", allowedProjects: " " },
+ { type: "gce", allowedServiceAccounts: ",", allowedProjects: ", ," },
+ { type: "iam", allowedServiceAccounts: ", ", allowedProjects: "my-project" }
+ ])("is false when no usable entry is set: %o", (binding) => {
+ expect(hasGcpTrustBinding(binding)).toBe(false);
+ });
+
+ test("is true for a service account on either type", () => {
+ expect(hasGcpTrustBinding({ type: "iam", allowedServiceAccounts: "[email protected]" })).toBe(true);
+ expect(hasGcpTrustBinding({ type: "gce", allowedServiceAccounts: "[email protected]" })).toBe(true);
+ });
+
+ test("is true for a project only on a GCE identity", () => {
+ expect(hasGcpTrustBinding({ type: "gce", allowedProjects: "my-project" })).toBe(true);
+ expect(hasGcpTrustBinding({ type: "iam", allowedProjects: "my-project" })).toBe(false);
+ });
+
+ test("ignores empty entries around a real one", () => {
+ expect(hasGcpTrustBinding({ type: "iam", allowedServiceAccounts: ", [email protected]," })).toBe(true);
+ });
+});
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 318adcdb63..34ebdb9b13 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,9 @@ import { UnauthorizedError } from "@app/lib/errors";
import { TDecodedGcpIamAuthJwt, TGcpIdTokenPayload } from "./identity-gcp-auth-types";
+// A list like "," is not empty as a string but names nothing, so count entries rather than characters.
+const hasListEntry = (list?: string | null) => Boolean(list?.split(",").some((entry) => entry.trim()));
+
// Zones are shared by every GCP customer, so a zone alone does not tie the identity to an owner. A project
// does, because a project ID belongs to one customer. Zones can only narrow a service account or project.
export const hasGcpTrustBinding = ({
@@ -18,9 +21,9 @@ export const hasGcpTrustBinding = ({
allowedServiceAccounts?: string | null;
allowedProjects?: string | null;
}) => {
- if (allowedServiceAccounts?.trim()) return true;
+ if (hasListEntry(allowedServiceAccounts)) return true;
if (type === "iam") return false;
- return Boolean(allowedProjects?.trim());
+ return hasListEntry(allowedProjects);
};
export const GCP_TRUST_BINDING_REQUIRED_MESSAGE =
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.ts
index 26ffc3cbc3..5513b21266 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
@@ -268,6 +268,10 @@ describe("gcpAuthService trust binding on write", () => {
await expect(attach({ type: "gce", allowedZones: "us-central1-a" })).rejects.toThrow(BINDING_MESSAGE);
});
+ test("attach rejects a list that only holds separators", async () => {
+ await expect(attach({ type: "iam", allowedServiceAccounts: ", " })).rejects.toThrow(BINDING_MESSAGE);
+ });
+
test("attach rejects an IAM identity with a project but no service account", async () => {
await expect(attach({ type: "iam", allowedProjects: "my-project" })).rejects.toThrow(BINDING_MESSAGE);
});
diff --git a/frontend/src/pages/organization/AccessManagementPage/components/OrgIdentityTab/components/IdentitySection/IdentityGcpAuthForm.tsx b/frontend/src/pages/organization/AccessManagementPage/components/OrgIdentityTab/components/IdentitySection/IdentityGcpAuthForm.tsx
index e7e9d66616..f2f7bcfdce 100644
--- a/frontend/src/pages/organization/AccessManagementPage/components/OrgIdentityTab/components/IdentitySection/IdentityGcpAuthForm.tsx
+++ b/frontend/src/pages/organization/AccessManagementPage/components/OrgIdentityTab/components/IdentitySection/IdentityGcpAuthForm.tsx
@@ -57,8 +57,9 @@ const buildSchema = (maxAccessTokenTTL: number) =>
.required()
.superRefine(superRefineAccessTokenTtl)
.superRefine((data, ctx) => {
- const hasServiceAccounts = Boolean(data.allowedServiceAccounts.trim());
- const hasProjects = data.type === "gce" && Boolean(data.allowedProjects.trim());
+ const hasEntry = (list: string) => list.split(",").some((entry) => entry.trim());
+ const hasServiceAccounts = hasEntry(data.allowedServiceAccounts);
+ const hasProjects = data.type === "gce" && hasEntry(data.allowedProjects);
if (!hasServiceAccounts && !hasProjects) {
const message =
"Configure at least one allowed GCP service account, or for GCE identities an allowed project.";