Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(local-installation): redact URL credentials in linear time

shoudi zhengb08cc58d2cbf
diff --git a/packages/local-installation/src/localmodels-failure.test.ts b/packages/local-installation/src/localmodels-failure.test.ts
index 8cae1872..a4e9c108 100644
--- a/packages/local-installation/src/localmodels-failure.test.ts
+++ b/packages/local-installation/src/localmodels-failure.test.ts
@@ -88,6 +88,7 @@ it("redacts credentials from the cause and the log", () => {
"https://objects.githubusercontent.com/a/b?X-Amz-Signature=deadbeef&X-Amz-Credential=AKIA",
"token: ghp_0123456789abcdefghijklmn",
"plain sk-proj-0123456789abcdefghij",
+ "9git+https://user:[email protected]/x",
];
const failure = localmodelsFailure(args, {
code: 1,
@@ -107,6 +108,7 @@ it("redacts credentials from the cause and the log", () => {
"AKIA",
"ghp_0123456789abcdefghijklmn",
"sk-proj-0123456789abcdefghij",
+ "hunter22",
])
expect(text).not.toContain(value);
expect(failure.cause).toContain(
@@ -115,6 +117,17 @@ it("redacts credentials from the cause and the log", () => {
expect(redactLocalmodelsOutput("a\u0007b\u001b[31mc")).toBe("abc");
});
+it("redacts URL credentials after long alphanumeric runs in linear time", () => {
+ // Hex digests and base64 alternate letters and digits; a pattern that may
+ // start at every letter of such a run takes about a second on this input.
+ const run = "a1".repeat(20_000);
+ const started = performance.now();
+ expect(redactLocalmodelsOutput(`${run} ${run}https://u:p@h/x`)).toBe(
+ `${run} ${run}https://[REDACTED]@h/x`,
+ );
+ expect(performance.now() - started).toBeLessThan(250);
+});
+
it("keeps the end of long output within bounded log events", () => {
const failure = localmodelsFailure(args, {
code: 1,
diff --git a/packages/local-installation/src/redaction.ts b/packages/local-installation/src/redaction.ts
index 09188dbe..fce5edbc 100644
--- a/packages/local-installation/src/redaction.ts
+++ b/packages/local-installation/src/redaction.ts
@@ -1,12 +1,20 @@
import { stripVTControlCharacters } from "node:util";
export function redact(text: string): string {
- return stripVTControlCharacters(text)
- .replace(
- /(\b(?:[A-Z_][\w.-]*[_-])?(?:api[_-]?key|password|passwd|token|secret(?:[_-]seed)?|credential|authorization|cookie|set-cookie)\b["']?\s*[:=]\s*)(?:Bearer\s+[^\s]+|"[^"\n]*"|'[^'\n]*'|[^\s,;}]+)/gi,
- "$1[REDACTED]",
- )
- .replace(/Bearer\s+[^\s"']+/gi, "Bearer [REDACTED]")
- .replace(/([a-z][a-z0-9+.-]*:\/\/)[^\s/@:]+:[^\s/@]+@/gi, "$1[REDACTED]@");
+ return (
+ stripVTControlCharacters(text)
+ .replace(
+ /(\b(?:[A-Z_][\w.-]*[_-])?(?:api[_-]?key|password|passwd|token|secret(?:[_-]seed)?|credential|authorization|cookie|set-cookie)\b["']?\s*[:=]\s*)(?:Bearer\s+[^\s]+|"[^"\n]*"|'[^'\n]*'|[^\s,;}]+)/gi,
+ "$1[REDACTED]",
+ )
+ .replace(/Bearer\s+[^\s"']+/gi, "Bearer [REDACTED]")
+ // Start once per run of scheme characters and skip its leading non-letters:
+ // starting at every letter yields the same result but is quadratic on long
+ // runs such as hex digests or base64.
+ .replace(
+ /(?<![a-z0-9+.-])([0-9+.-]*[a-z][a-z0-9+.-]*:\/\/)[^\s/@:]+:[^\s/@]+@/gi,
+ "$1[REDACTED]@",
+ )
+ );
}
export function lineRedactor(write: (value: string) => void) {
let pending = "",