Skip to content

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

fix edit block to avoid breaking change

Thiago Araujo da Silva88859660a47e
diff --git a/backend/src/lib/validator/log-safe-text.ts b/backend/src/lib/validator/log-safe-text.ts
index 5ec22e1b75..b31fddb460 100644
--- a/backend/src/lib/validator/log-safe-text.ts
+++ b/backend/src/lib/validator/log-safe-text.ts
@@ -76,6 +76,8 @@ const isUnsafeCharacter = (character: string) => {
return isControlCharacter(code) || isUncategorizedFormat(code) || FORMAT_CATEGORY.test(character);
};
+export const LOG_UNSAFE_TEXT_MESSAGE = "Value cannot contain control, escape, bidirectional or zero-width characters";
+
// `allowMultiline` is for fields a user types into a textarea, where a line break or tab is ordinary
// input. CR is included so CRLF-separated content is not rejected; the sink normalizes it to LF.
export const containsLogUnsafeCharacters = (value: string, { allowMultiline = false } = {}): boolean => {
diff --git a/backend/src/server/lib/audit-safe-text-schema.test.ts b/backend/src/server/lib/audit-safe-text-schema.test.ts
index fcf6444cc5..138a9b1b7b 100644
--- a/backend/src/server/lib/audit-safe-text-schema.test.ts
+++ b/backend/src/server/lib/audit-safe-text-schema.test.ts
@@ -61,8 +61,9 @@ describe("auditSafeText composition", () => {
describe("routes that feed audit metadata keep the wrapper", () => {
const read = (relative: string) => readFileSync(path.join(__dirname, "../..", relative), "utf8");
- // Every route that creates or renames an identity: the org and project routers take the same name
- // the legacy router does, and the web app creates project identities through the project router.
+ // Every route that creates an identity: the org and project routers take the same name the legacy
+ // router does, and the web app creates project identities through the project router. Renames are
+ // checked in the identity services instead, and only when the name changes.
test.each([
"server/routes/v1/identity-router.ts",
"server/routes/v1/org-identity-router.ts",
@@ -72,7 +73,7 @@ describe("routes that feed audit metadata keep the wrapper", () => {
.split("\n")
.filter((line) => line.trimStart().startsWith("name: auditSafeText("));
- expect(nameLines.length).toBeGreaterThanOrEqual(2); // create and update, and any route added later
+ expect(nameLines.length).toBeGreaterThanOrEqual(1);
nameLines.forEach((line) => {
expect(line).toContain(".max(255)");
expect(line).not.toContain("allowMultiline");
diff --git a/backend/src/server/lib/schemas.ts b/backend/src/server/lib/schemas.ts
index e8c20ff9be..d95aeb2291 100644
--- a/backend/src/server/lib/schemas.ts
+++ b/backend/src/server/lib/schemas.ts
@@ -3,7 +3,7 @@ import { z } from "zod";
import { TemporaryPermissionMode } from "@app/db/schemas";
import { ms } from "@app/lib/ms";
-import { containsLogUnsafeCharacters } from "@app/lib/validator/log-safe-text";
+import { containsLogUnsafeCharacters, LOG_UNSAFE_TEXT_MESSAGE } from "@app/lib/validator/log-safe-text";
import { CharacterType, characterValidator } from "@app/lib/validator/validate-string";
interface SlugSchemaInputs {
@@ -48,7 +48,7 @@ export const GenericResourceNameSchema = z
// ingest path normalizes the same characters.
export const auditSafeText = (schema: z.ZodString, { allowMultiline = false } = {}) =>
schema.refine((val) => !containsLogUnsafeCharacters(val, { allowMultiline }), {
- message: "Value cannot contain control, escape, bidirectional or zero-width characters"
+ message: LOG_UNSAFE_TEXT_MESSAGE
});
export const BaseSecretNameSchema = z.string().trim().min(1);
diff --git a/backend/src/server/routes/v1/identity-router.ts b/backend/src/server/routes/v1/identity-router.ts
index 0649b884f8..f57d228f91 100644
--- a/backend/src/server/routes/v1/identity-router.ts
+++ b/backend/src/server/routes/v1/identity-router.ts
@@ -126,7 +126,7 @@ export const registerIdentityRouter = async (server: FastifyZodProvider) => {
identityId: z.string().describe(IDENTITIES.UPDATE.identityId)
}),
body: z.object({
- name: auditSafeText(z.string().trim().min(1).max(255)).optional().describe(IDENTITIES.UPDATE.name),
+ name: z.string().trim().min(1).max(255).optional().describe(IDENTITIES.UPDATE.name),
role: z.string().trim().min(1).optional().describe(IDENTITIES.UPDATE.role),
hasDeleteProtection: z.boolean().optional().describe(IDENTITIES.UPDATE.hasDeleteProtection),
metadata: z
diff --git a/backend/src/server/routes/v1/org-identity-router.ts b/backend/src/server/routes/v1/org-identity-router.ts
index 75ae290d30..6a88cbfe23 100644
--- a/backend/src/server/routes/v1/org-identity-router.ts
+++ b/backend/src/server/routes/v1/org-identity-router.ts
@@ -107,7 +107,7 @@ export const registerOrgIdentityRouter = async (server: FastifyZodProvider) => {
identityId: z.string().trim().describe(IDENTITIES.UPDATE.identityId)
}),
body: z.object({
- name: auditSafeText(z.string().trim().min(1).max(255)).optional().describe(IDENTITIES.UPDATE.name),
+ name: z.string().trim().min(1).max(255).optional().describe(IDENTITIES.UPDATE.name),
hasDeleteProtection: z.boolean().optional().describe(IDENTITIES.UPDATE.hasDeleteProtection),
metadata: z.array(metadataSchema).optional().describe(IDENTITIES.UPDATE.metadata)
}),
diff --git a/backend/src/server/routes/v1/project-identity-router.ts b/backend/src/server/routes/v1/project-identity-router.ts
index 14d4eea578..2a2132db47 100644
--- a/backend/src/server/routes/v1/project-identity-router.ts
+++ b/backend/src/server/routes/v1/project-identity-router.ts
@@ -152,7 +152,7 @@ export const registerProjectIdentityRouter = async (server: FastifyZodProvider)
identityId: z.string().trim().describe(IDENTITIES.UPDATE.identityId)
}),
body: z.object({
- name: auditSafeText(z.string().trim().min(1).max(255)).optional().describe(IDENTITIES.UPDATE.name),
+ name: z.string().trim().min(1).max(255).optional().describe(IDENTITIES.UPDATE.name),
hasDeleteProtection: z.boolean().optional().describe(IDENTITIES.UPDATE.hasDeleteProtection),
metadata: z.array(metadataSchema).optional().describe(IDENTITIES.UPDATE.metadata)
}),
diff --git a/backend/src/services/identity-v2/identity-service.test.ts b/backend/src/services/identity-v2/identity-service.test.ts
index eab8ea41ab..af0da646fd 100644
--- a/backend/src/services/identity-v2/identity-service.test.ts
+++ b/backend/src/services/identity-v2/identity-service.test.ts
@@ -236,3 +236,48 @@ describe("instance admin identities are protected on the v2 surface", () => {
expect(identityDAL.updateById).not.toHaveBeenCalled();
});
});
+
+describe("updateIdentity name check", () => {
+ const SOFT_HYPHEN = String.fromCharCode(0xad);
+ const LEGACY_NAME = `ci${SOFT_HYPHEN}bot`;
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ getServerCfgMock.mockResolvedValue({ adminIdentityIds: [] });
+ });
+
+ const update = (service: ReturnType<typeof createService>["service"], data: Record<string, unknown>) =>
+ service.updateIdentity({ ...buildDto(), data } as never);
+
+ test("an unchanged name is let through even if it predates the rule", async () => {
+ const { service, identityDAL } = createService({
+ existingIdentity: { id: IDENTITY_ID, name: LEGACY_NAME, hasDeleteProtection: false }
+ });
+
+ await update(service, { name: LEGACY_NAME, hasDeleteProtection: true });
+
+ expect(identityDAL.updateById).toHaveBeenCalledWith(
+ IDENTITY_ID,
+ { name: LEGACY_NAME, hasDeleteProtection: true },
+ TX
+ );
+ });
+
+ test("a rename to an unsafe name is refused", async () => {
+ const { service, identityDAL } = createService();
+
+ await expect(update(service, { name: LEGACY_NAME })).rejects.toThrow(
+ "Value cannot contain control, escape, bidirectional or zero-width characters"
+ );
+
+ expect(identityDAL.updateById).not.toHaveBeenCalled();
+ });
+
+ test("a rename to a safe name goes through", async () => {
+ const { service, identityDAL } = createService();
+
+ await update(service, { name: "ci-bot" });
+
+ expect(identityDAL.updateById).toHaveBeenCalledWith(IDENTITY_ID, { name: "ci-bot" }, TX);
+ });
+});
diff --git a/backend/src/services/identity-v2/identity-service.ts b/backend/src/services/identity-v2/identity-service.ts
index 05fc0025b2..8e7be0d54d 100644
--- a/backend/src/services/identity-v2/identity-service.ts
+++ b/backend/src/services/identity-v2/identity-service.ts
@@ -32,6 +32,7 @@ import { groupBy } from "@app/lib/fn";
import { ms } from "@app/lib/ms";
import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { requestMemoize } from "@app/lib/request-context/request-memoizer";
+import { containsLogUnsafeCharacters, LOG_UNSAFE_TEXT_MESSAGE } from "@app/lib/validator/log-safe-text";
import { TAlertServiceFactory } from "@app/services/alert/alert-service";
import { IDENTITY_AUTHENTICATION_RESOURCE_TYPE } from "@app/services/alert/providers/identity-credential-alert-provider";
import { AgentVaultIdentities, IdentitiesMeter, PamIdentities, SecretIdentities } from "@app/services/license-client";
@@ -346,6 +347,10 @@ export const identityV2ServiceFactory = ({
await validateIdentityUpdateForSuperAdminPrivileges(dto.selector.identityId, dto.isActorSuperAdmin);
+ if (data.name !== undefined && data.name !== existingIdentity.name && containsLogUnsafeCharacters(data.name)) {
+ throw new BadRequestError({ message: LOG_UNSAFE_TEXT_MESSAGE });
+ }
+
const identity = await identityDAL.transaction(async (tx) => {
// Compared against undefined: `hasDeleteProtection: false` is a real update, and a truthiness check
// could never turn the flag off.
diff --git a/backend/src/services/identity/identity-service.test.ts b/backend/src/services/identity/identity-service.test.ts
index 98be6faab2..ddc5d77b9d 100644
--- a/backend/src/services/identity/identity-service.test.ts
+++ b/backend/src/services/identity/identity-service.test.ts
@@ -41,8 +41,9 @@ const createService = ({
// Equal to actorOrgId means the identity lives here and the row itself is deleted. Different
// means it was only invited in, so the delete just takes the membership away.
identityOrgId = ORG_ID,
- hasDeleteProtection = false
-}: { identityOrgId?: string; hasDeleteProtection?: boolean } = {}) => {
+ hasDeleteProtection = false,
+ identityName = "ident"
+}: { identityOrgId?: string; hasDeleteProtection?: boolean; identityName?: string } = {}) => {
const deleteAlertsForResource = vi.fn().mockResolvedValue(0);
const deleteAlertsForDeletedResource = vi.fn().mockResolvedValue(0);
const emit = vi.fn().mockResolvedValue(undefined);
@@ -50,7 +51,8 @@ const createService = ({
const identityDAL = {
transaction: vi.fn(async (cb: (tx: Knex) => Promise<unknown>) => cb(TX)),
deleteById: vi.fn().mockResolvedValue({ id: IDENTITY_ID, name: "ident" }),
- findById: vi.fn().mockResolvedValue({ id: IDENTITY_ID, name: "ident" })
+ findById: vi.fn().mockResolvedValue({ id: IDENTITY_ID, name: identityName, orgId: identityOrgId, projectId: null }),
+ updateById: vi.fn().mockResolvedValue({ id: IDENTITY_ID, name: "renamed", orgId: identityOrgId })
};
const membershipIdentityDAL = {
@@ -82,7 +84,7 @@ const createService = ({
keyStore: { sortedSetRangeByScore: vi.fn().mockResolvedValue([]) } as never,
orgDAL: {
findById: vi.fn().mockResolvedValue({ id: ORG_ID, shouldUseNewPrivilegeSystem: false }),
- findEffectiveOrgMembership: vi.fn()
+ findEffectiveOrgMembership: vi.fn().mockResolvedValue({ id: "membership-1", scopeOrgId: ORG_ID })
} as never,
additionalPrivilegeDAL: { delete: vi.fn().mockResolvedValue(undefined) } as never,
usageMeteringService: { emit: vi.fn() } as never,
@@ -181,3 +183,34 @@ describe("deleteIdentity org access reconcile", () => {
expect(emit).not.toHaveBeenCalled();
});
});
+
+describe("updateIdentity name check", () => {
+ const SOFT_HYPHEN = String.fromCharCode(0xad);
+ const LEGACY_NAME = `ci${SOFT_HYPHEN}bot`;
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ test("an unchanged name is let through even if it predates the rule", async () => {
+ const { service, identityDAL } = createService({ identityName: LEGACY_NAME });
+
+ await service.updateIdentity({ ...buildDto(), name: LEGACY_NAME, hasDeleteProtection: true } as never);
+
+ expect(identityDAL.updateById).toHaveBeenCalledWith(
+ IDENTITY_ID,
+ { name: LEGACY_NAME, hasDeleteProtection: true },
+ TX
+ );
+ });
+
+ test("a rename to an unsafe name is refused", async () => {
+ const { service, identityDAL } = createService();
+
+ await expect(service.updateIdentity({ ...buildDto(), name: LEGACY_NAME } as never)).rejects.toThrow(
+ "Value cannot contain control, escape, bidirectional or zero-width characters"
+ );
+
+ expect(identityDAL.updateById).not.toHaveBeenCalled();
+ });
+});
diff --git a/backend/src/services/identity/identity-service.ts b/backend/src/services/identity/identity-service.ts
index dd8ce5d047..3ea076e676 100644
--- a/backend/src/services/identity/identity-service.ts
+++ b/backend/src/services/identity/identity-service.ts
@@ -14,6 +14,7 @@ import { PgSqlLock, TKeyStoreFactory } from "@app/keystore/keystore";
import { BadRequestError, NotFoundError, PermissionBoundaryError } from "@app/lib/errors";
import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { requestMemoize } from "@app/lib/request-context/request-memoizer";
+import { containsLogUnsafeCharacters, LOG_UNSAFE_TEXT_MESSAGE } from "@app/lib/validator/log-safe-text";
import { TAlertServiceFactory } from "@app/services/alert/alert-service";
import { IDENTITY_AUTHENTICATION_RESOURCE_TYPE } from "@app/services/alert/providers/identity-credential-alert-provider";
import { TIdentityProjectDALFactory } from "@app/services/identity-project/identity-project-dal";
@@ -294,6 +295,10 @@ export const identityServiceFactory = ({
throw new BadRequestError({ message: `Identity is managed by project` });
}
+ if (name !== undefined && name !== identityDetails.name && containsLogUnsafeCharacters(name)) {
+ throw new BadRequestError({ message: LOG_UNSAFE_TEXT_MESSAGE });
+ }
+
const identity = await identityDAL.transaction(async (tx) => {
const newIdentity =
identityDetails.orgId === actorOrgId && (name || hasDeleteProtection)