Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(gfsc): make log sanitizer a CodeQL-recognized barrier + scrub error (CWE-117)
Alfredo Lopez2edf638d0cf6
diff --git a/gfs-controller/src/api/serve.sanitizeForLog.test.ts b/gfs-controller/src/api/serve.sanitizeForLog.test.tsindex 15456f7fe..8226859ec 100644--- a/gfs-controller/src/api/serve.sanitizeForLog.test.ts+++ b/gfs-controller/src/api/serve.sanitizeForLog.test.ts@@ -2,15 +2,25 @@ import { describe, expect, it } from "vitest";import { sanitizeForLog } from "./serve";// CWE-117 log injection / log forging guard for the 5xx fail-loud log line in-// serve.ts, which interpolates user-controlled request fields (method, req.url).-// Sources built via String.fromCharCode so this file carries no raw control bytes.+// serve.ts, which interpolates user-controlled request fields (method, req.url)+// and the error message/stack. CR/LF (the forging vector) are removed via an+// empty-string replace that enumerates \n — the barrier CodeQL's js/log-injection+// query recognizes; other C0 controls + DEL become '?'. Sources are built via+// String.fromCharCode so this file carries no raw control bytes.const LF = String.fromCharCode(10);const CR = String.fromCharCode(13);-const CONTROL_CODES = [0, 8, 9, 10, 13, 27, 31, 127]; // NUL, BS, TAB, LF, CR, ESC, US, DEL+const CRLF_CODES = [10, 13]; // removed entirely (recognized barrier)+const OTHER_CONTROL_CODES = [0, 8, 9, 27, 31, 127]; // NUL, BS, TAB, ESC, US, DELdescribe("sanitizeForLog (CWE-117 log-injection guard)", () => {- it("replaces every C0 control char and DEL with '?'", () => {- for (const code of CONTROL_CODES) {+ it("removes CR and LF entirely (the log-forging vector)", () => {+ for (const code of CRLF_CODES) {+ expect(sanitizeForLog(`a${String.fromCharCode(code)}b`)).toBe("ab");+ }+ });++ it("replaces other C0 control chars and DEL with '?'", () => {+ for (const code of OTHER_CONTROL_CODES) {expect(sanitizeForLog(`a${String.fromCharCode(code)}b`)).toBe("a?b");}});@@ -20,7 +30,8 @@ describe("sanitizeForLog (CWE-117 log-injection guard)", () => {const out = sanitizeForLog(forge);expect(out).not.toContain(LF);expect(out).not.toContain(CR);- expect(out).toBe("GET /x??[gfsc] FAKE admin login -> 200");+ // CR/LF removed (not substituted) so no forged newline survives.+ expect(out).toBe("GET /x[gfsc] FAKE admin login -> 200");});it("passes through printable ASCII and unicode unchanged", () => {diff --git a/gfs-controller/src/api/serve.ts b/gfs-controller/src/api/serve.tsindex 0955fa189..c440f3e65 100644--- a/gfs-controller/src/api/serve.ts+++ b/gfs-controller/src/api/serve.ts@@ -27,14 +27,19 @@ import { planWrite, WriteError } from "./write";/*** Strip control characters (CR/LF/tab, C0 controls, DEL) and bound the length of* a user-controlled value before it is interpolated into a log line. Prevents- * log injection / log forging (CWE-117) when request fields (method, target)- * reach a log sink. Internal values (status, error code, stack) are NOT passed- * through this — the stack is intentionally multi-line and is not user-tainted.+ * log injection / log forging (CWE-117) when values that may carry user input+ * (request method/target, or an error message/stack that echoes a decoded path)+ * reach a log sink. CR/LF are removed; other control chars become '?'.*/export function sanitizeForLog(value: string): string {- // Replace CR/LF, other C0 controls, and DEL with '?' (CodeQL-recognized- // log-injection barrier), then bound length to guard against log flooding.- return value.replace(/[\u0000-\u001f\u007f]/g, "?").slice(0, 512);+ // Remove CR/LF first (the log-forging vector; empty-string replace that+ // enumerates \n is the barrier CodeQL's js/log-injection recognizes), then+ // scrub any remaining C0 controls and DEL to '?', and bound the length to+ // guard against log flooding.+ return value+ .replace(/[\n\r]/g, "")+ .replace(/[\u0000-\u001f\u007f]/g, "?")+ .slice(0, 512);}/**@@ -267,10 +272,13 @@ export class GfsServingHandler {// A 5xx must never be invisible: an unknown throw (e.g. the base64// RangeError that produced a bogus `internal` 500) previously left no// trace anywhere in the system. Log code + stack (never the body/content).+ // The stack/message is sanitized too: an underlying error (e.g. a driver+ // error echoing a decoded path) can carry user input with real newlines,+ // so it is a log-injection source just like method/url.console.error(- `[gfsc] ${sanitizeForLog(method)} ${sanitizeForLog(req.url ?? "?")} -> ${status} ${body.error.code}: ${- err instanceof Error ? (err.stack ?? err.message) : String(err)- }`+ `[gfsc] ${sanitizeForLog(method)} ${sanitizeForLog(req.url ?? "?")} -> ${status} ${body.error.code}: ${sanitizeForLog(+ err instanceof Error ? (err.stack ?? err.message ?? "") : String(err)+ )}`);}if (!res.headersSent) sendJson(res, status, body, abortConnectionHeader(req));