Skip to content

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

fix(pki): keep truncation from splitting a surrogate pair

bernie-g6006414a5a84
diff --git a/backend/src/services/pki-sync/pki-sync-post-sync-command-fns.test.ts b/backend/src/services/pki-sync/pki-sync-post-sync-command-fns.test.ts
index eb63276703..63680feca8 100644
--- a/backend/src/services/pki-sync/pki-sync-post-sync-command-fns.test.ts
+++ b/backend/src/services/pki-sync/pki-sync-post-sync-command-fns.test.ts
@@ -271,6 +271,18 @@ describe("runPostSyncCommand", () => {
expect(result.output?.endsWith("... (truncated)")).toBe(true);
});
+ test("does not split a surrogate pair when truncating", async () => {
+ // The cut would otherwise land inside the emoji, and a lone surrogate cannot be encoded as UTF-8
+ // so it reaches the audit log as a replacement character.
+ const result = await runPostSyncCommand({
+ syncId: "sync-id",
+ execute: async () => ({ stdout: `${"x".repeat(984)}🔒${"y".repeat(100)}`, stderr: "", exitCode: 0 })
+ });
+
+ expect(result.output).not.toContain("�");
+ expect(Buffer.from(result.output ?? "", "utf8").toString("utf8")).toBe(result.output);
+ });
+
test("returns no output when the command was silent", async () => {
const result = await runPostSyncCommand({
syncId: "sync-id",
diff --git a/backend/src/services/pki-sync/pki-sync-post-sync-command-fns.ts b/backend/src/services/pki-sync/pki-sync-post-sync-command-fns.ts
index 02bd5be33a..179002199b 100644
--- a/backend/src/services/pki-sync/pki-sync-post-sync-command-fns.ts
+++ b/backend/src/services/pki-sync/pki-sync-post-sync-command-fns.ts
@@ -175,10 +175,17 @@ const redactSecrets = (text: string, secrets: Array<string | undefined>): string
const TRUNCATION_SUFFIX = "... (truncated)";
+// A cut between the halves of a surrogate pair leaves a lone surrogate, which is not valid UTF-8.
+const sliceWholeCharacters = (text: string, end: number): string => {
+ const cut = text.slice(0, end);
+ const last = cut.charCodeAt(cut.length - 1);
+ return last >= 0xd800 && last <= 0xdbff ? cut.slice(0, -1) : cut;
+};
+
// The suffix counts against maxLength, so the cap bounds what actually gets stored.
const truncate = (text: string, maxLength: number): string =>
text.length > maxLength
- ? `${text.slice(0, Math.max(0, maxLength - TRUNCATION_SUFFIX.length))}${TRUNCATION_SUFFIX}`
+ ? `${sliceWholeCharacters(text, Math.max(0, maxLength - TRUNCATION_SUFFIX.length))}${TRUNCATION_SUFFIX}`
: text;
const firstNonEmptyLine = (text?: string): string | undefined =>