Skip to content

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

save suffix

Thiago Araujo da Silva90c43d5706eb
diff --git a/backend/src/lib/validator/log-safe-text.test.ts b/backend/src/lib/validator/log-safe-text.test.ts
index 0cb627a32e..c546fa2d25 100644
--- a/backend/src/lib/validator/log-safe-text.test.ts
+++ b/backend/src/lib/validator/log-safe-text.test.ts
@@ -168,6 +168,28 @@ describe("sanitizeLogPayload", () => {
expect(sanitized).toEqual({ email: "real", "email~2": "spoofed", ab: 2, "ab~2": 3, "ab~3": 1 });
});
+ test("suffixes thousands of look-alike keys without slowing down", () => {
+ const count = 20_000;
+ const WORD_JOINER = String.fromCharCode(0x2060);
+ // Each index written in binary with two invisible characters, so every key is distinct and short.
+ const invisible = (index: number) =>
+ [...index.toString(2)].map((bit) => (bit === "1" ? ZWSP : WORD_JOINER)).join("");
+ const lookAlikes = Object.fromEntries(Array.from({ length: count }, (_, index) => [`a${invisible(index)}`, index]));
+
+ const started = performance.now();
+ const sanitized = sanitizeLogPayload<Record<string, unknown>>({ a: "real", "a~3": "taken", ...lookAlikes });
+ const elapsedMs = performance.now() - started;
+
+ expect(Object.keys(sanitized)).toHaveLength(count + 2);
+ expect(sanitized.a).toBe("real");
+ expect(sanitized["a~2"]).toBe(0);
+ expect(sanitized["a~3"]).toBe("taken");
+ expect(sanitized["a~4"]).toBe(1);
+ expect(sanitized[`a~${count + 2}`]).toBe(count - 1);
+ // Restarting the search at 2 for every key took seconds here.
+ expect(elapsedMs).toBeLessThan(1000);
+ });
+
test("keeps a key of __proto__ as an own property", () => {
// Computed keys: the literal form would set the prototype as the object is built.
const sanitized = sanitizeLogPayload({ ["__proto__"]: { sub: `a${NUL}b` }, [`__pro${ZWSP}to__`]: "second" });
diff --git a/backend/src/lib/validator/log-safe-text.ts b/backend/src/lib/validator/log-safe-text.ts
index 10678a72db..84e17ce935 100644
--- a/backend/src/lib/validator/log-safe-text.ts
+++ b/backend/src/lib/validator/log-safe-text.ts
@@ -83,11 +83,14 @@ export const sanitizeLogText = <T extends string | null | undefined>(value: T):
};
// Two keys can clean up to the same string, so suffix the second one instead of losing a field.
-const disambiguate = (key: string, taken: Set<string>) => {
+// `nextSuffix` resumes the search where the last one stopped. Restarting at 2 is quadratic, and the
+// keys are user input (OIDC bound claims), so thousands of look-alikes would stall the audit queue.
+const disambiguate = (key: string, taken: Set<string>, nextSuffix: Map<string, number>) => {
if (!taken.has(key)) return key;
- let suffix = 2;
+ let suffix = nextSuffix.get(key) ?? 2;
while (taken.has(`${key}~${suffix}`)) suffix += 1;
+ nextSuffix.set(key, suffix + 1);
return `${key}~${suffix}`;
};
@@ -159,12 +162,13 @@ export const sanitizeLogPayload = <T>(payload: T): T => {
// first, would take the name and push the real field to `email~2`.
const cleanKeys = new Set(entries.filter(([key]) => isClean(key)).map(([key]) => key));
const taken = new Set(cleanKeys);
+ const nextSuffix = new Map<string, number>();
entries.forEach(([key, value]) => {
if (cleanKeys.has(key)) {
assign(key, value);
return;
}
- const sanitizedKey = disambiguate(sanitizeLogText(key), taken);
+ const sanitizedKey = disambiguate(sanitizeLogText(key), taken, nextSuffix);
taken.add(sanitizedKey);
assign(sanitizedKey, value);
});