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(audit-log): keep line breaks and tabs in audit text
Vlad Ligaif6ff497945ba
diff --git a/backend/src/ee/services/audit-log/audit-log-queue.test.ts b/backend/src/ee/services/audit-log/audit-log-queue.test.tsindex b7b12647d4..96045a7075 100644--- a/backend/src/ee/services/audit-log/audit-log-queue.test.ts+++ b/backend/src/ee/services/audit-log/audit-log-queue.test.ts@@ -431,7 +431,7 @@ describe("audit-log-queue unified consumer", () => {expect((row.actorMetadata as Record<string, unknown>).name).toBe("svcacct");const eventMetadata = row.eventMetadata as Record<string, unknown>;- expect(eventMetadata.reason).toBe("approved by ops");+ expect(eventMetadata.reason).toBe(`approved by${String.fromCharCode(10)}ops`);expect((eventMetadata.nested as Record<string, unknown>).note).toBe("ab");};diff --git a/backend/src/lib/validator/log-safe-text.test.ts b/backend/src/lib/validator/log-safe-text.test.tsindex 46670a09ef..8fa353e74a 100644--- a/backend/src/lib/validator/log-safe-text.test.ts+++ b/backend/src/lib/validator/log-safe-text.test.ts@@ -9,8 +9,6 @@ const ZWSP = String.fromCharCode(0x200b);describe("sanitizeLogText", () => {test.each([- ["line feed", `deploy${String.fromCharCode(10)}worker`, "deploy worker"],- ["carriage return", `a${String.fromCharCode(13)}b`, "a b"],["null byte", `svc${NUL}account`, "svcaccount"],["ansi colour", `${ESC}[31mroot${ESC}[0m`, "root"],["ansi osc", `${ESC}]0;title${String.fromCharCode(7)}x`, "x"],@@ -121,10 +119,30 @@ describe("sanitizeLogPayload", () => {expect(value("__proto__~2")).toBe("second");});- test("whitespace controls collapse to a single space, preserving word boundaries", () => {- expect(sanitizeLogText(`approved by${String.fromCharCode(10)}ops`)).toBe("approved by ops");- expect(sanitizeLogText(`one${String.fromCharCode(13)}${String.fromCharCode(10)}two`)).toBe("one two");- expect(sanitizeLogText(`a${String.fromCharCode(9)}b`)).toBe("a b");+ test("keeps line feeds and tabs, so a multiline reason reads as the user typed it", () => {+ const LF = String.fromCharCode(10);+ const TAB = String.fromCharCode(9);++ expect(sanitizeLogText(`approved by${LF}ops`)).toBe(`approved by${LF}ops`);+ expect(sanitizeLogText(`step one${LF}${LF}step${TAB}two`)).toBe(`step one${LF}${LF}step${TAB}two`);+ });++ test("normalizes CRLF and a lone CR to LF, so nothing overprints a terminal line", () => {+ const CR = String.fromCharCode(13);+ const LF = String.fromCharCode(10);++ expect(sanitizeLogText(`one${CR}${LF}two`)).toBe(`one${LF}two`);+ expect(sanitizeLogText(`real value${CR}forged value`)).toBe(`real value${LF}forged value`);+ });++ test("turns VT and FF into a space rather than running words together", () => {+ expect(sanitizeLogText(`a${String.fromCharCode(11)}b${String.fromCharCode(12)}c`)).toBe("a b c");+ });++ test("still strips escape sequences and invisible characters on a multiline value", () => {+ const LF = String.fromCharCode(10);++ expect(sanitizeLogText(`${ESC}[31mline one${LF}line${ZWSP} two${NUL}`)).toBe(`line one${LF}line two`);});test("an unterminated OSC introducer does not swallow the rest of the value", () => {@@ -160,9 +178,11 @@ describe("sanitizeLogPayload", () => {expect(sanitizeLogText(value)).toBe("ab");});- test("line and paragraph separators collapse like other line breaks", () => {- expect(sanitizeLogText(`one${String.fromCodePoint(0x2028)}two`)).toBe("one two");- expect(sanitizeLogText(`one${String.fromCodePoint(0x2029)}two`)).toBe("one two");+ test("line and paragraph separators become LF, which JSON then escapes", () => {+ const LF = String.fromCharCode(10);++ expect(sanitizeLogText(`one${String.fromCodePoint(0x2028)}two`)).toBe(`one${LF}two`);+ expect(sanitizeLogText(`one${String.fromCodePoint(0x2029)}two`)).toBe(`one${LF}two`);});test.each([0x2028, 0x2029])("a single-line field rejects U+%s", (code) => {diff --git a/backend/src/lib/validator/log-safe-text.ts b/backend/src/lib/validator/log-safe-text.tsindex 839b1abf24..5ec22e1b75 100644--- a/backend/src/lib/validator/log-safe-text.ts+++ b/backend/src/lib/validator/log-safe-text.ts@@ -5,6 +5,10 @@ import RE2 from "re2";* overrides and invisible formatting characters. All are legal in JSON, so well-formed output says* nothing about how a value renders in a terminal, a CSV export or a line-based SIEM.*+ * Line breaks and tabs are kept. Every audit consumer JSON-encodes the value, which escapes them, and+ * some fields are multiline by design (PAM reasons); the credential-view reason is recorded nowhere+ * but the audit log, so collapsing it would lose what the user typed.+ ** Reject at the API boundary, where the caller can correct its input; strip on the way into the* log, where dropping the event would lose the record.*@@ -25,9 +29,15 @@ const ANSI_ESCAPE_PATTERN = new RE2("g");-// Whitespace controls carry word boundaries, so a run collapses to a single space rather than being-// deleted, which would run the surrounding words together.-const WHITESPACE_CONTROL_RUN = new RE2("[\\t\\n\\v\\f\\r\\u2028\\u2029]+", "g");+// CRLF and a lone CR become LF: a bare CR returns a terminal's cursor to the start of the line, so the+// rest of the value overprints what was already shown. U+2028/U+2029 become LF too, because+// JSON.stringify leaves them unescaped.+const LINE_BREAK_VARIANTS = new RE2("\\r\\n?|[\\u2028\\u2029]", "g");++// VT and FF still mark a word boundary, so they become a space rather than running words together.+const VERTICAL_WHITESPACE = new RE2("[\\v\\f]", "g");++const KEPT_WHITESPACE = new Set([0x09, 0x0a]);// The line-break set a multiline field is allowed to contain. U+2028/U+2029 are line and paragraph// separators: not C0 controls, but line breaks to anything that splits on them, so a single-line@@ -53,8 +63,8 @@ const isUncategorizedFormat = (code: number) =>// A whole-string screen, run before the per-character walk below. The walk crosses into RE2 once per// character, which measures ~30x the cost of the walk itself, and audit text is almost always clean,-// so one call per string answers the common case. The screen over-matches on ZWNJ and ZWJ, which-// only costs those strings a walk that then keeps them.+// so one call per string answers the common case. The screen over-matches on ZWNJ, ZWJ, LF and tab,+// which only costs those strings a walk that then keeps them.const UNSAFE_SCREEN = new RE2("[\\x00-\\x1f\\x7f-\\x9f\\x{2028}\\x{2029}]|[\\x{E0000}-\\x{E007F}]|\\p{Cf}", "u");// codePointAt, not charCodeAt: the astral ranges sit beyond U+FFFF, where charCodeAt would return a@@ -67,8 +77,7 @@ const isUnsafeCharacter = (character: string) => {};// `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-side strip still-// collapses them, so the stored record stays single-line.+// 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 => {if (!UNSAFE_SCREEN.test(value)) return false;@@ -87,10 +96,13 @@ export const sanitizeLogText = <T extends string | null | undefined>(value: T):if (!UNSAFE_SCREEN.test(value)) return value;// Sequences first, so a body leaves with its introducer rather than surviving as visible text.- const collapsed = value.replace(ANSI_ESCAPE_PATTERN, "").replace(WHITESPACE_CONTROL_RUN, " ");+ const normalized = value+ .replace(ANSI_ESCAPE_PATTERN, "")+ .replace(LINE_BREAK_VARIANTS, "\n")+ .replace(VERTICAL_WHITESPACE, " ");- return Array.from(collapsed)- .filter((character) => !isUnsafeCharacter(character))+ return Array.from(normalized)+ .filter((character) => KEPT_WHITESPACE.has(character.codePointAt(0) ?? 0) || !isUnsafeCharacter(character)).join("") as T;};