Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(security): resolve 23 open CodeQL alerts (#4045)

Tejas Chopraa88e2d51452f
diff --git a/.github/workflows/devcontainers.yml b/.github/workflows/devcontainers.yml
index 214ae75f9..fff595f83 100644
--- a/.github/workflows/devcontainers.yml
+++ b/.github/workflows/devcontainers.yml
@@ -17,6 +17,9 @@ on:
- "uv.lock"
workflow_dispatch:
+permissions:
+ contents: read
+
jobs:
validate:
runs-on: ubuntu-latest
diff --git a/.github/workflows/eval.yml b/.github/workflows/eval.yml
index c6c879f30..d32489f87 100644
--- a/.github/workflows/eval.yml
+++ b/.github/workflows/eval.yml
@@ -10,6 +10,9 @@ on:
- 'headroom/evals/**'
- 'headroom/compress.py'
+permissions:
+ contents: read
+
jobs:
# Fast smoke test on PRs touching compression code (~$0.05, ~2 min)
smoke-test:
diff --git a/.github/workflows/init-e2e.yml b/.github/workflows/init-e2e.yml
index 8ec26ee1b..e6b4531a3 100644
--- a/.github/workflows/init-e2e.yml
+++ b/.github/workflows/init-e2e.yml
@@ -31,6 +31,9 @@ concurrency:
group: init-e2e-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
+permissions:
+ contents: read
+
jobs:
docker-init-e2e:
runs-on: ubuntu-latest
diff --git a/.github/workflows/init-native-e2e.yml b/.github/workflows/init-native-e2e.yml
index 952db263b..3e125771b 100644
--- a/.github/workflows/init-native-e2e.yml
+++ b/.github/workflows/init-native-e2e.yml
@@ -32,6 +32,9 @@ on:
branches: [main]
workflow_dispatch:
+permissions:
+ contents: read
+
jobs:
init-native:
runs-on: ${{ matrix.os }}
diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml
index 7010bd203..f57c8670a 100644
--- a/.github/workflows/security.yml
+++ b/.github/workflows/security.yml
@@ -120,7 +120,7 @@ jobs:
strategy:
fail-fast: false
matrix:
- language: [python, javascript-typescript]
+ language: [python, javascript-typescript, actions]
steps:
- uses: actions/checkout@v7
diff --git a/.github/workflows/wrap-e2e.yml b/.github/workflows/wrap-e2e.yml
index dae4a147a..0044c5df7 100644
--- a/.github/workflows/wrap-e2e.yml
+++ b/.github/workflows/wrap-e2e.yml
@@ -30,6 +30,9 @@ concurrency:
group: wrap-e2e-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
+permissions:
+ contents: read
+
jobs:
docker-wrap-e2e:
runs-on: ubuntu-latest
diff --git a/headroom/evals/comprehensive_benchmark.py b/headroom/evals/comprehensive_benchmark.py
index 86912851e..513ea2f36 100644
--- a/headroom/evals/comprehensive_benchmark.py
+++ b/headroom/evals/comprehensive_benchmark.py
@@ -248,8 +248,7 @@ def run_lm_eval(
if base_url:
args_parts.append(f"base_url={base_url}")
- if args_parts:
- cmd.extend(["--model_args", ",".join(args_parts)])
+ model_args_value = ",".join(args_parts) if args_parts else None
if num_fewshot is not None:
cmd.extend(["--num_fewshot", str(num_fewshot)])
@@ -264,7 +263,13 @@ def run_lm_eval(
cmd.extend(["--output_path", output_path])
- logger.info(f"Running: {' '.join(cmd)}")
+ # --model_args can carry an API key, so it is appended after the command is
+ # logged and the log shows it redacted.
+ logger.info(
+ f"Running: {' '.join(cmd)}" + (" --model_args <redacted>" if model_args_value else "")
+ )
+ if model_args_value:
+ cmd.extend(["--model_args", model_args_value])
# Run lm-eval
start_time = time.time()
diff --git a/headroom/proxy/handlers/anthropic.py b/headroom/proxy/handlers/anthropic.py
index 8ec9c7b8d..7eb7d9594 100644
--- a/headroom/proxy/handlers/anthropic.py
+++ b/headroom/proxy/handlers/anthropic.py
@@ -56,6 +56,7 @@ from headroom.proxy.compression_decision import CompressionDecision
from headroom.proxy.handlers._debug_dump import _debug_dump_mode, _redact_debug_value
from headroom.proxy.helpers import (
extract_tags,
+ invalid_request_body_message,
relocate_system_messages_to_top_level,
sanitize_forwarded_response_headers,
)
@@ -1166,7 +1167,7 @@ class AnthropicHandlerMixin:
"type": "error",
"error": {
"type": "invalid_request_error",
- "message": f"Invalid request body: {e!s}",
+ "message": invalid_request_body_message(e),
},
},
)
@@ -5653,7 +5654,7 @@ class AnthropicHandlerMixin:
"type": "error",
"error": {
"type": "invalid_request_error",
- "message": f"Invalid request body: {e!s}",
+ "message": invalid_request_body_message(e),
},
},
)
diff --git a/headroom/proxy/handlers/batch.py b/headroom/proxy/handlers/batch.py
index dd98cb7bf..0e88313c2 100644
--- a/headroom/proxy/handlers/batch.py
+++ b/headroom/proxy/handlers/batch.py
@@ -19,6 +19,7 @@ from headroom.proxy.helpers import (
COMPRESSION_TIMEOUT_SECONDS,
_headroom_bypass_enabled,
extract_tags,
+ invalid_request_body_message,
)
from headroom.proxy.outcome import RequestOutcome
@@ -86,7 +87,7 @@ class BatchHandlerMixin:
content={
"error": {
"code": 400,
- "message": f"Invalid request body: {e!s}",
+ "message": invalid_request_body_message(e),
"status": "INVALID_ARGUMENT",
}
},
@@ -397,7 +398,7 @@ class BatchHandlerMixin:
content={
"error": {
"code": 500,
- "message": f"Failed to forward batch request: {e!s}",
+ "message": "Failed to forward batch request",
"status": "INTERNAL",
}
},
@@ -853,7 +854,7 @@ class BatchHandlerMixin:
status_code=400,
content={
"error": {
- "message": f"Invalid request body: {e!s}",
+ "message": invalid_request_body_message(e),
"type": "invalid_request_error",
"code": "invalid_json",
}
diff --git a/headroom/proxy/handlers/gemini.py b/headroom/proxy/handlers/gemini.py
index b8778fffa..9297671f9 100644
--- a/headroom/proxy/handlers/gemini.py
+++ b/headroom/proxy/handlers/gemini.py
@@ -22,7 +22,11 @@ from headroom.agent_savings import proxy_pipeline_kwargs
from headroom.copilot_auth import build_copilot_upstream_url
from headroom.proxy.auth_mode import classify_client
from headroom.proxy.compression_decision import CompressionDecision
-from headroom.proxy.helpers import COMPRESSION_TIMEOUT_SECONDS, extract_tags
+from headroom.proxy.helpers import (
+ COMPRESSION_TIMEOUT_SECONDS,
+ extract_tags,
+ invalid_request_body_message,
+)
from headroom.proxy.identity import resolve_memory_identity
from headroom.proxy.outcome import RequestOutcome
from headroom.proxy.rate_limit_identity import rate_limit_identity
@@ -367,7 +371,7 @@ class GeminiHandlerMixin:
status_code=400,
content={
"error": {
- "message": f"Invalid request body: {e!s}",
+ "message": invalid_request_body_message(e),
"code": 400,
}
},
@@ -1133,7 +1137,7 @@ class GeminiHandlerMixin:
status_code=400,
content={
"error": {
- "message": f"Invalid request body: {e!s}",
+ "message": invalid_request_body_message(e),
"code": 400,
}
},
@@ -1361,7 +1365,7 @@ class GeminiHandlerMixin:
status_code=400,
content={
"error": {
- "message": f"Invalid request body: {e!s}",
+ "message": invalid_request_body_message(e),
"code": 400,
}
},
diff --git a/headroom/proxy/handlers/openai.py b/headroom/proxy/handlers/openai.py
index 51f63ef89..54cd81d80 100644
--- a/headroom/proxy/handlers/openai.py
+++ b/headroom/proxy/handlers/openai.py
@@ -30,6 +30,7 @@ from headroom.proxy.helpers import (
COMPRESSION_TIMEOUT_SECONDS,
_headroom_bypass_enabled,
extract_tags,
+ invalid_request_body_message,
jitter_delay_ms,
sanitize_forwarded_response_headers,
)
@@ -3621,7 +3622,7 @@ class OpenAIHandlerMixin:
status_code=400,
content={
"error": {
- "message": f"Invalid request body: {e!s}",
+ "message": invalid_request_body_message(e),
"type": "invalid_request_error",
"code": "invalid_json",
}
@@ -6031,7 +6032,7 @@ class OpenAIHandlerMixin:
status_code=400,
content={
"error": {
- "message": f"Invalid request body: {e!s}",
+ "message": invalid_request_body_message(e),
"type": "invalid_request_error",
"code": "invalid_json",
}
@@ -10652,7 +10653,7 @@ class OpenAIHandlerMixin:
except (json.JSONDecodeError, ValueError) as e:
return JSONResponse(
status_code=400,
- content={"error": f"Invalid request body: {e!s}"},
+ content={"error": invalid_request_body_message(e)},
)
messages = body.get("messages", [])
_bypass_payload = {
diff --git a/headroom/proxy/helpers.py b/headroom/proxy/helpers.py
index b983ea245..26a6e7b0b 100644
--- a/headroom/proxy/helpers.py
+++ b/headroom/proxy/helpers.py
@@ -3015,6 +3015,30 @@ class RequestBodyTooLarge(ValueError):
"""
+class RequestBodyNotObject(ValueError):
+ """The request body parsed as JSON but is not an object (e.g. a list)."""
+
+
+def invalid_request_body_message(exc: Exception) -> str:
+ """Client-facing text for a request body that failed to read or parse.
+
+ Fixed strings only, chosen by exception type. The exception's own text can
+ carry decoder internals and request metadata, and echoing it back is what
+ CodeQL's ``py/stack-trace-exposure`` flags. The type is logged so a 400 can
+ still be diagnosed from the proxy side.
+ """
+ logger.info("rejected request body (%s)", type(exc).__name__)
+ if isinstance(exc, RequestBodyTooLarge):
+ return "Invalid request body: too large"
+ if isinstance(exc, RequestBodyNotObject):
+ return "Invalid request body: must be a JSON object"
+ if isinstance(exc, json.JSONDecodeError):
+ return "Invalid request body: malformed JSON"
+ if isinstance(exc.__cause__, UnicodeDecodeError):
+ return "Invalid request body: not valid UTF-8 (possibly compressed?)"
+ return "Invalid request body"
+
+
def _inflate_bounded(raw: bytes, *, wbits: int, label: str, multi_member: bool = False) -> bytes:
"""Incrementally inflate ``raw``, stopping the instant output passes the cap.
@@ -3347,7 +3371,9 @@ async def _read_request_json(request: Request) -> dict[str, Any]:
result = json.loads(text)
if not isinstance(result, dict):
- raise ValueError("Request body must be a JSON object, not " + type(result).__name__)
+ raise RequestBodyNotObject(
+ "Request body must be a JSON object, not " + type(result).__name__
+ )
# Drop output-only blocks the request schema rejects (see
# ``strip_output_only_request_blocks``). Callers of this bytes-less reader
@@ -3387,7 +3413,9 @@ async def read_request_json_with_bytes(request: Request) -> tuple[dict[str, Any]
result = json.loads(text)
if not isinstance(result, dict):
- raise ValueError("Request body must be a JSON object, not " + type(result).__name__)
+ raise RequestBodyNotObject(
+ "Request body must be a JSON object, not " + type(result).__name__
+ )
# Drop output-only blocks (see ``strip_output_only_request_blocks``) before
# any downstream deepcopy / compression / 400-retry path. This is the shared
diff --git a/scripts/build_npm_release_assets.mjs b/scripts/build_npm_release_assets.mjs
index 1962315ec..a52187029 100644
--- a/scripts/build_npm_release_assets.mjs
+++ b/scripts/build_npm_release_assets.mjs
@@ -86,12 +86,43 @@ function run(command, args, cwd) {
}
}
-function runNpm(args, cwd) {
- if (process.platform === "win32") {
- run("cmd.exe", ["/d", "/s", "/c", "npm.cmd", ...args], cwd);
- return;
+// npm on Windows is a .cmd shim, which only runs through cmd.exe, and cmd.exe
+// reinterprets characters such as & and | inside arguments (paths included).
+// Run npm's own CLI script under this Node binary instead, so no shell is
+// involved on any platform.
+function npmCommand(args) {
+ if (process.platform !== "win32") {
+ return ["npm", args];
+ }
+ return [process.execPath, [findNpmCli(), ...args]];
+}
+
+// Node and npm can live in different directories (a node shim, or npm
+// upgraded into the global prefix), so look where npm itself says it is, then
+// next to node, then next to every npm.cmd on PATH. Every npm.cmd install
+// keeps its CLI at node_modules/npm/bin/npm-cli.js beside the shim.
+function findNpmCli() {
+ const cliPath = path.join("node_modules", "npm", "bin", "npm-cli.js");
+ const candidates = [];
+ if (process.env.npm_execpath?.endsWith("npm-cli.js")) {
+ candidates.push(process.env.npm_execpath);
+ }
+ candidates.push(path.join(path.dirname(process.execPath), cliPath));
+ for (const dir of (process.env.PATH || "").split(path.delimiter)) {
+ if (dir && existsSync(path.join(dir, "npm.cmd"))) {
+ candidates.push(path.join(dir, cliPath));
+ }
}
- run("npm", args, cwd);
+ const found = candidates.find((candidate) => existsSync(candidate));
+ if (!found) {
+ throw new Error(`npm CLI (npm-cli.js) not found. Looked in: ${candidates.join(", ")}`);
+ }
+ return found;
+}
+
+function runNpm(args, cwd) {
+ const [command, commandArgs] = npmCommand(args);
+ run(command, commandArgs, cwd);
}
function runNode(args, cwd) {
diff --git a/scripts/verify_npm_release_assets.mjs b/scripts/verify_npm_release_assets.mjs
index 4c155d824..77fc21c7e 100644
--- a/scripts/verify_npm_release_assets.mjs
+++ b/scripts/verify_npm_release_assets.mjs
@@ -1,6 +1,6 @@
#!/usr/bin/env node
-import { copyFileSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
+import { copyFileSync, existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { spawnSync } from "node:child_process";
@@ -65,15 +65,43 @@ function assertNoFileDependencies(pkg) {
}
}
-function runNpm(args, cwd) {
- if (process.platform === "win32") {
- return spawnSync("cmd.exe", ["/d", "/s", "/c", "npm.cmd", ...args], {
- cwd,
- encoding: "utf8",
- });
+// npm on Windows is a .cmd shim, which only runs through cmd.exe, and cmd.exe
+// reinterprets characters such as & and | inside arguments (paths included).
+// Run npm's own CLI script under this Node binary instead, so no shell is
+// involved on any platform.
+function npmCommand(args) {
+ if (process.platform !== "win32") {
+ return ["npm", args];
}
+ return [process.execPath, [findNpmCli(), ...args]];
+}
- return spawnSync("npm", args, {
+// Node and npm can live in different directories (a node shim, or npm
+// upgraded into the global prefix), so look where npm itself says it is, then
+// next to node, then next to every npm.cmd on PATH. Every npm.cmd install
+// keeps its CLI at node_modules/npm/bin/npm-cli.js beside the shim.
+function findNpmCli() {
+ const cliPath = path.join("node_modules", "npm", "bin", "npm-cli.js");
+ const candidates = [];
+ if (process.env.npm_execpath?.endsWith("npm-cli.js")) {
+ candidates.push(process.env.npm_execpath);
+ }
+ candidates.push(path.join(path.dirname(process.execPath), cliPath));
+ for (const dir of (process.env.PATH || "").split(path.delimiter)) {
+ if (dir && existsSync(path.join(dir, "npm.cmd"))) {
+ candidates.push(path.join(dir, cliPath));
+ }
+ }
+ const found = candidates.find((candidate) => existsSync(candidate));
+ if (!found) {
+ throw new Error(`npm CLI (npm-cli.js) not found. Looked in: ${candidates.join(", ")}`);
+ }
+ return found;
+}
+
+function runNpm(args, cwd) {
+ const [command, commandArgs] = npmCommand(args);
+ return spawnSync(command, commandArgs, {
cwd,
encoding: "utf8",
});
diff --git a/tests/test_evals/test_comprehensive_benchmark_log_redaction.py b/tests/test_evals/test_comprehensive_benchmark_log_redaction.py
new file mode 100644
index 000000000..73eaf9811
--- /dev/null
+++ b/tests/test_evals/test_comprehensive_benchmark_log_redaction.py
@@ -0,0 +1,63 @@
+"""run_lm_eval must not log --model_args, which can carry an API key (CodeQL #177).
+
+The redaction is log-only: lm_eval itself still has to receive the real value,
+or the benchmark silently runs against the wrong endpoint/credentials.
+"""
+
+import logging
+from types import SimpleNamespace
+
+import pytest
+
+from headroom.evals import comprehensive_benchmark
+
+FAKE_SECRET = "sk-test-FAKE-0123456789abcdef"
+
+
+def test_model_args_redacted_in_log_but_passed_to_lm_eval(monkeypatch, caplog, tmp_path):
+ calls: list[list[str]] = []
+
+ def fake_run(cmd, **_kwargs):
+ calls.append(list(cmd))
+ # Non-zero exit stops run_lm_eval right after the call, before it
+ # looks for result files we did not create.
+ return SimpleNamespace(returncode=1, stdout="", stderr="stopped by test")
+
+ monkeypatch.setattr(comprehensive_benchmark, "run", fake_run)
+
+ with caplog.at_level(logging.INFO, logger=comprehensive_benchmark.__name__):
+ with pytest.raises(RuntimeError, match="stopped by test"):
+ comprehensive_benchmark.run_lm_eval(
+ model_args=f"model=gpt-4o-mini,api_key={FAKE_SECRET}",
+ base_url="http://localhost:8787/v1",
+ tasks=["arc_easy"],
+ output_path=str(tmp_path),
+ )
+
+ running = [r.getMessage() for r in caplog.records if r.getMessage().startswith("Running:")]
+ assert len(running) == 1
+ assert "--model_args <redacted>" in running[0]
+ assert FAKE_SECRET not in caplog.text
+
+ assert len(calls) == 1
+ cmd = calls[0]
+ assert cmd[cmd.index("--model_args") + 1] == (
+ f"model=gpt-4o-mini,api_key={FAKE_SECRET},base_url=http://localhost:8787/v1"
+ )
+
+
+def test_no_model_args_logs_no_placeholder(monkeypatch, caplog, tmp_path):
+ calls: list[list[str]] = []
+
+ def fake_run(cmd, **_kwargs):
+ calls.append(list(cmd))
+ return SimpleNamespace(returncode=1, stdout="", stderr="stopped by test")
+
+ monkeypatch.setattr(comprehensive_benchmark, "run", fake_run)
+
+ with caplog.at_level(logging.INFO, logger=comprehensive_benchmark.__name__):
+ with pytest.raises(RuntimeError):
+ comprehensive_benchmark.run_lm_eval(tasks=["arc_easy"], output_path=str(tmp_path))
+
+ assert "--model_args" not in caplog.text
+ assert "--model_args" not in calls[0]
diff --git a/tests/test_invalid_request_body_message.py b/tests/test_invalid_request_body_message.py
new file mode 100644
index 000000000..bf653ff64
--- /dev/null
+++ b/tests/test_invalid_request_body_message.py
@@ -0,0 +1,55 @@
+"""Client-facing 400 text for unreadable request bodies (CodeQL py/stack-trace-exposure).
+
+The handlers used to echo ``str(exc)`` back to the client. These pin that the
+replacement still tells the client *what kind* of problem it was, and never
+carries the exception's own text.
+"""
+
+import json
+
+import pytest
+
+from headroom.proxy.helpers import (
+ RequestBodyNotObject,
+ RequestBodyTooLarge,
+ invalid_request_body_message,
+)
+
+
+def _unicode_error() -> ValueError:
+ try:
+ b"\xff".decode("utf-8")
+ except UnicodeDecodeError as cause:
+ try:
+ raise ValueError(f"Request body is not valid UTF-8: {cause}") from cause
+ except ValueError as exc:
+ return exc
+ raise AssertionError("unreachable")
+
+
+ ("exc", "expected"),
+ [
+ (RequestBodyTooLarge("Request body exceeds 100MB (Content-Length: 1)"), "too large"),
+ (RequestBodyNotObject("Request body must be a JSON object, not list"), "JSON object"),
+ (json.JSONDecodeError("Expecting value", "secret-doc", 0), "malformed JSON"),
+ (_unicode_error(), "not valid UTF-8"),
+ (ValueError("zstd frame header at offset 3"), None),
+ ],
+)
+def test_message_names_the_category_without_exception_text(exc, expected):
+ message = invalid_request_body_message(exc)
+
+ assert message.startswith("Invalid request body")
+ if expected is None:
+ assert message == "Invalid request body"
+ else:
+ assert expected in message
+ assert str(exc) not in message
+ assert "secret-doc" not in message
+
+
+def test_not_object_is_still_a_value_error():
+ # Existing ``except (json.JSONDecodeError, ValueError)`` call sites must keep
+ # catching it.
+ assert issubclass(RequestBodyNotObject, ValueError)