Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(gfsc): sanitize user-controlled fields in 5xx fail-loud log (CWE-117)

Alfredo Lopez9947c3127450
diff --git a/gfs-controller/src/api/serve.sanitizeForLog.test.ts b/gfs-controller/src/api/serve.sanitizeForLog.test.ts
new file mode 100644
index 000000000..15456f7fe
--- /dev/null
+++ b/gfs-controller/src/api/serve.sanitizeForLog.test.ts
@@ -0,0 +1,34 @@
+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.
+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
+
+describe("sanitizeForLog (CWE-117 log-injection guard)", () => {
+ it("replaces every C0 control char and DEL with '?'", () => {
+ for (const code of CONTROL_CODES) {
+ expect(sanitizeForLog(`a${String.fromCharCode(code)}b`)).toBe("a?b");
+ }
+ });
+
+ it("neutralizes a CRLF forged-log-line payload", () => {
+ const forge = `GET /x${CR}${LF}[gfsc] FAKE admin login -> 200`;
+ const out = sanitizeForLog(forge);
+ expect(out).not.toContain(LF);
+ expect(out).not.toContain(CR);
+ expect(out).toBe("GET /x??[gfsc] FAKE admin login -> 200");
+ });
+
+ it("passes through printable ASCII and unicode unchanged", () => {
+ const ok = "/v1/resources/AbC-123_+/=?x=y&z=1 café";
+ expect(sanitizeForLog(ok)).toBe(ok);
+ });
+
+ it("bounds length to 512 chars (log-flood guard)", () => {
+ expect(sanitizeForLog("z".repeat(1000))).toHaveLength(512);
+ });
+});
diff --git a/gfs-controller/src/api/serve.ts b/gfs-controller/src/api/serve.ts
index ee1be41f5..bdd39e591 100644
--- a/gfs-controller/src/api/serve.ts
+++ b/gfs-controller/src/api/serve.ts
@@ -24,6 +24,22 @@ import { GfsError } from "./errors";
import { normalizeResourceName, ResourceNameError } from "./resourceName";
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.
+ */
+export function sanitizeForLog(value: string): string {
+ let out = "";
+ for (const ch of value.slice(0, 512)) {
+ const code = ch.codePointAt(0) ?? 0;
+ out += code < 0x20 || code === 0x7f ? "?" : ch;
+ }
+ return out;
+}
+
/**
* gfsc read serving handler (spec community.md §Operator surfaces / §Auth,
* plan P1-S07/S08 composed). This is the broker's READ data plane — the single
@@ -255,7 +271,7 @@ export class GfsServingHandler {
// RangeError that produced a bogus `internal` 500) previously left no
// trace anywhere in the system. Log code + stack (never the body/content).
console.error(
- `[gfsc] ${method} ${req.url ?? "?"} -> ${status} ${body.error.code}: ${
+ `[gfsc] ${sanitizeForLog(method)} ${sanitizeForLog(req.url ?? "?")} -> ${status} ${body.error.code}: ${
err instanceof Error ? (err.stack ?? err.message) : String(err)
}`
);