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 high-severity CodeQL alerts (#4005)

Tejas Chopradea9092e1938
diff --git a/headroom/providers/cortex_code/runtime.py b/headroom/providers/cortex_code/runtime.py
index a88c814b6..110ec5af7 100644
--- a/headroom/providers/cortex_code/runtime.py
+++ b/headroom/providers/cortex_code/runtime.py
@@ -24,7 +24,7 @@ def default_api_url(environ: Mapping[str, str] | None = None) -> str:
if host:
if host.startswith("https://"):
return host
- if ".snowflakecomputing.com" in host:
+ if host.endswith(".snowflakecomputing.com"):
return f"https://{host}"
return f"https://{host}.snowflakecomputing.com"
return _FALLBACK_API_URL
diff --git a/headroom/providers/opencode/_dist/entry.opencode.js b/headroom/providers/opencode/_dist/entry.opencode.js
index 6d3dae693..90d4160e8 100644
--- a/headroom/providers/opencode/_dist/entry.opencode.js
+++ b/headroom/providers/opencode/_dist/entry.opencode.js
@@ -12431,8 +12431,13 @@ var _proxyUrlCache = null;
function getDefaultProxyUrl() {
return _proxyUrlCache ?? process.env.HEADROOM_BASE_URL ?? "http://localhost:8787";
}
+function trimTrailingSlashes(value) {
+ let end = value.length;
+ while (end > 0 && value.charCodeAt(end - 1) === 47) end--;
+ return value.slice(0, end);
+}
function createHeadroomRetrieveTool(config2) {
- const origin = config2.proxyBaseUrl.replace(/\/+$/, "");
+ const origin = trimTrailingSlashes(config2.proxyBaseUrl);
return {
name: "headroom_retrieve",
description: "Retrieve original uncompressed content from Headroom's compression store. Use when compressed context mentions a hash and you need the full details. Pass the hash from the compression marker (24 hex characters). Retrieval is by hash and always returns the full original content.",
@@ -12896,7 +12901,7 @@ function uninstallHeadroomTransport() {
// src/plugin.ts
function normalizeProxyUrl2(url2) {
- return url2.replace(/\/+$/, "");
+ return trimTrailingSlashes(url2);
}
function resolveProxyUrl(options) {
return normalizeProxyUrl2(
diff --git a/headroom/proxy/helpers.py b/headroom/proxy/helpers.py
index 79003c42f..f87d01a5e 100644
--- a/headroom/proxy/helpers.py
+++ b/headroom/proxy/helpers.py
@@ -24,6 +24,7 @@ from dataclasses import dataclass
from logging.handlers import RotatingFileHandler
from pathlib import Path
from typing import TYPE_CHECKING, Any, Literal, cast
+from urllib.parse import urlsplit, urlunsplit
from headroom import fileperms as _fileperms
from headroom import paths as _paths
@@ -368,6 +369,27 @@ def sanitize_forwarded_response_headers(
return {key: value for key, value in dict(headers).items() if key.lower() not in drop}
+def _path_for_log(path: str) -> str:
+ """Drop the query string, fragment and userinfo from an outbound URL.
+
+ Callers pass the full upstream URL, and Google routes put the API key in
+ the query (``?key=...``), so logging it verbatim writes the key to disk.
+ Scheme, host and path are enough to tell forwarder calls apart.
+ """
+ try:
+ parts = urlsplit(path)
+ port = parts.port
+ except ValueError:
+ return "<unparseable>"
+ host = parts.hostname or ""
+ # ``hostname`` drops the brackets around an IPv6 literal; put them back
+ # so the logged URL still names the upstream that was contacted.
+ netloc = f"[{host}]" if ":" in host else host
+ if port:
+ netloc = f"{netloc}:{port}"
+ return urlunsplit((parts.scheme, netloc, parts.path, "", ""))
+
+
def log_outbound_request(
*,
forwarder: str,
@@ -383,8 +405,8 @@ def log_outbound_request(
"""Structured log line for every outbound forwarder call.
Per realignment build constraints: every cache-affecting decision is
- logged. Never includes ``Authorization``/``x-api-key`` content or full
- body bytes.
+ logged. Never includes ``Authorization``/``x-api-key`` content, the URL
+ query string (where Google puts ``key=``) or full body bytes.
``dropped_mutation_reasons`` records edits that byte-faithful passthrough
discarded before the wire. That is a WARNING, not a detail: the line above
@@ -396,7 +418,7 @@ def log_outbound_request(
"body_mutated=%s mutation_reasons=%s source=%s request_id=%s",
forwarder,
method,
- path,
+ _path_for_log(path),
body_bytes_count,
"true" if body_mutated else "false",
",".join(mutation_reasons) if mutation_reasons else "",
diff --git a/headroom/reporting/generator.py b/headroom/reporting/generator.py
index 2a99eec05..d22484004 100644
--- a/headroom/reporting/generator.py
+++ b/headroom/reporting/generator.py
@@ -18,7 +18,9 @@ def _get_jinja2_template(template_str: str):
try:
from jinja2 import Template
- return Template(template_str)
+ # Request fields such as ``model`` come from clients; escape them so a
+ # crafted value cannot inject script into the report.
+ return Template(template_str, autoescape=True)
except ImportError as e:
raise ImportError(
"jinja2 is required for report generation. Install with: pip install headroom[reports]"
diff --git a/plugins/headroom-oauth2/src/headroom_oauth2/__init__.py b/plugins/headroom-oauth2/src/headroom_oauth2/__init__.py
index 074c76333..f43df4905 100644
--- a/plugins/headroom-oauth2/src/headroom_oauth2/__init__.py
+++ b/plugins/headroom-oauth2/src/headroom_oauth2/__init__.py
@@ -39,7 +39,12 @@ def parse_headers(s: str | None) -> dict[str, str]:
if not k:
continue
if _ctrl(k) or _ctrl(v) or " " in k or ":" in k:
- log.warning("headroom-oauth2: dropping malformed static header: %r", k)
+ # Don't echo the key: a pair like "Authorization: Bearer x=y" lands
+ # here whole, so the "key" can carry the credential itself.
+ log.warning(
+ "headroom-oauth2: dropping malformed static header "
+ "(name has a space, colon or control character)"
+ )
continue
out[k] = v
return out
diff --git a/plugins/opencode/src/plugin.ts b/plugins/opencode/src/plugin.ts
index a9fd39bf4..ba4ef98f0 100644
--- a/plugins/opencode/src/plugin.ts
+++ b/plugins/opencode/src/plugin.ts
@@ -2,7 +2,11 @@ import type { Plugin } from "@opencode-ai/plugin";
import { tool } from "@opencode-ai/plugin";
import { z } from "zod";
-import { createHeadroomRetrieveTool, getDefaultProxyUrl } from "./retrieve.js";
+import {
+ createHeadroomRetrieveTool,
+ getDefaultProxyUrl,
+ trimTrailingSlashes,
+} from "./retrieve.js";
import { installHeadroomTransport } from "./transport.js";
export interface HeadroomOpenCodePluginOptions {
@@ -14,7 +18,7 @@ export interface HeadroomOpenCodePluginOptions {
}
function normalizeProxyUrl(url: string): string {
- return url.replace(/\/+$/, "");
+ return trimTrailingSlashes(url);
}
function resolveProxyUrl(options?: HeadroomOpenCodePluginOptions): string {
diff --git a/plugins/opencode/src/provider.ts b/plugins/opencode/src/provider.ts
index dd6fa87d4..df57dfb31 100644
--- a/plugins/opencode/src/provider.ts
+++ b/plugins/opencode/src/provider.ts
@@ -1,3 +1,5 @@
+import { trimTrailingSlashes } from "./retrieve.js";
+
export interface HeadroomModelMapping {
name: string;
limit: {
@@ -39,7 +41,7 @@ export const DEFAULT_MODELS: Record<string, HeadroomModelMapping> = {
export const DEFAULT_MODEL = "claude-sonnet-4-6";
function resolveBaseUrl(options: HeadroomProviderOptions): string {
- if (options.proxyBaseUrl) return options.proxyBaseUrl.replace(/\/+$/, "");
+ if (options.proxyBaseUrl) return trimTrailingSlashes(options.proxyBaseUrl);
const port = options.proxyPort ?? 8787;
return `http://127.0.0.1:${port}`;
}
diff --git a/plugins/opencode/src/retrieve.test.ts b/plugins/opencode/src/retrieve.test.ts
new file mode 100644
index 000000000..0a335b4df
--- /dev/null
+++ b/plugins/opencode/src/retrieve.test.ts
@@ -0,0 +1,28 @@
+import { describe, expect, it } from "vitest";
+
+import { trimTrailingSlashes } from "./retrieve.js";
+
+describe("trimTrailingSlashes", () => {
+ it.each([
+ ["http://localhost:8787", "http://localhost:8787"],
+ ["http://localhost:8787/", "http://localhost:8787"],
+ ["http://localhost:8787///", "http://localhost:8787"],
+ ["http://localhost:8787/v1/", "http://localhost:8787/v1"],
+ ["///", ""],
+ ["", ""],
+ ])("trims %j to %j", (input, expected) => {
+ expect(trimTrailingSlashes(input)).toBe(expected);
+ });
+
+ it("runs in linear time on long runs of slashes", () => {
+ // The previous /\/+$/ regex backtracked quadratically on a long run of
+ // "/" that is not at the end of the string (CodeQL js/polynomial-redos).
+ const slashes = "/".repeat(100_000);
+ const input = `http://localhost:8787${slashes}x${slashes}`;
+
+ const start = performance.now();
+ const result = trimTrailingSlashes(input);
+ expect(performance.now() - start).toBeLessThan(1000);
+ expect(result).toBe(`http://localhost:8787${slashes}x`);
+ });
+});
diff --git a/plugins/opencode/src/retrieve.ts b/plugins/opencode/src/retrieve.ts
index 0ea5d46b7..5a6ce0148 100644
--- a/plugins/opencode/src/retrieve.ts
+++ b/plugins/opencode/src/retrieve.ts
@@ -15,8 +15,18 @@ export interface RetrieveToolConfig {
proxyBaseUrl: string;
}
+/**
+ * Strip trailing "/" characters. A loop rather than a `+$` regex, which
+ * backtracks quadratically on inputs with long runs of slashes.
+ */
+export function trimTrailingSlashes(value: string): string {
+ let end = value.length;
+ while (end > 0 && value.charCodeAt(end - 1) === 47) end--;
+ return value.slice(0, end);
+}
+
export function createHeadroomRetrieveTool(config: RetrieveToolConfig) {
- const origin = config.proxyBaseUrl.replace(/\/+$/, "");
+ const origin = trimTrailingSlashes(config.proxyBaseUrl);
return {
name: "headroom_retrieve",
diff --git a/sdk/typescript/src/client.ts b/sdk/typescript/src/client.ts
index 79355eeba..32e471c10 100644
--- a/sdk/typescript/src/client.ts
+++ b/sdk/typescript/src/client.ts
@@ -75,6 +75,16 @@ export interface HeadroomParams {
headroomToolProfiles?: Record<string, Record<string, any>>;
}
+/**
+ * Strip trailing "/" characters. A loop rather than a `+$` regex, which
+ * backtracks quadratically on inputs with long runs of slashes.
+ */
+function trimTrailingSlashes(value: string): string {
+ let end = value.length;
+ while (end > 0 && value.charCodeAt(end - 1) === 47) end--;
+ return value.slice(0, end);
+}
+
// --- Sub-clients ---
class ChatCompletions {
@@ -211,11 +221,9 @@ export class HeadroomClient implements HeadroomClientInterface {
readonly messages: Messages;
constructor(options: ExtendedClientOptions = {}) {
- this.baseUrl = (
- options.baseUrl ??
- getEnv("HEADROOM_BASE_URL") ??
- DEFAULT_BASE_URL
- ).replace(/\/+$/, "");
+ this.baseUrl = trimTrailingSlashes(
+ options.baseUrl ?? getEnv("HEADROOM_BASE_URL") ?? DEFAULT_BASE_URL,
+ );
this.apiKey = options.apiKey ?? getEnv("HEADROOM_API_KEY");
this.timeout = options.timeout ?? DEFAULT_TIMEOUT;
this.fallback = options.fallback ?? true;
diff --git a/sdk/typescript/test/client.test.ts b/sdk/typescript/test/client.test.ts
index b5997ca8e..1d41c2a43 100644
--- a/sdk/typescript/test/client.test.ts
+++ b/sdk/typescript/test/client.test.ts
@@ -119,6 +119,23 @@ describe("HeadroomClient", () => {
expect(url).toBe("http://localhost:8787/v1/compress");
});
+ it("handles long runs of slashes in baseUrl in linear time", async () => {
+ // The previous /\/+$/ regex backtracked quadratically on a long run of
+ // "/" that is not at the end of the string (CodeQL js/polynomial-redos).
+ const slashes = "/".repeat(100_000);
+ mockFetch.mockResolvedValueOnce(okResponse(sampleProxyResponse));
+
+ const start = performance.now();
+ const client = new HeadroomClient({
+ baseUrl: `http://localhost:8787${slashes}x${slashes}`,
+ });
+ expect(performance.now() - start).toBeLessThan(1000);
+
+ await client.compress(sampleMessages, { model: "gpt-4o" });
+ const [url] = mockFetch.mock.calls[0];
+ expect(url).toBe(`http://localhost:8787${slashes}x/v1/compress`);
+ });
+
it("throws HeadroomAuthError on 401 (always, even with fallback)", async () => {
mockFetch.mockResolvedValueOnce(
errorResponse(401, {
diff --git a/tests/test_provider_cortex_code.py b/tests/test_provider_cortex_code.py
index dde4ed964..b5be94f41 100644
--- a/tests/test_provider_cortex_code.py
+++ b/tests/test_provider_cortex_code.py
@@ -97,3 +97,10 @@ def test_cortex_code_install_registry_unknown_target_skipped() -> None:
)
assert "unknown-tool" not in result
assert "cortex-code" in result
+
+
+def test_cortex_code_default_api_url_only_trusts_snowflake_suffix() -> None:
+ # The domain must end the host, not merely appear somewhere in it.
+ url = default_api_url({"SNOWFLAKE_HOST": "acct.snowflakecomputing.com.evil.io"})
+ assert url != "https://acct.snowflakecomputing.com.evil.io"
+ assert url.endswith(".snowflakecomputing.com")
diff --git a/tests/test_proxy_byte_faithful_forwarding.py b/tests/test_proxy_byte_faithful_forwarding.py
index 4cdb4b0d1..12906439d 100644
--- a/tests/test_proxy_byte_faithful_forwarding.py
+++ b/tests/test_proxy_byte_faithful_forwarding.py
@@ -41,6 +41,7 @@ from headroom.proxy.body_forwarding import (
thinking_blocks_survived_mutation,
)
from headroom.proxy.helpers import (
+ _path_for_log,
_reset_session_beta_tracker_for_test,
append_text_to_latest_user_chat_message,
get_session_beta_tracker,
@@ -509,6 +510,67 @@ def test_log_outbound_request_emits_structured_fields() -> None:
assert "x-api-key" not in msg.lower()
+def test_log_outbound_request_strips_query_string_api_key() -> None:
+ """Google forwarders put the API key in ``?key=``; it must not reach the log."""
+ import logging
+
+ proxy_logger = logging.getLogger("headroom.proxy")
+ records: list[logging.LogRecord] = []
+
+ class _ListHandler(logging.Handler):
+ def emit(self, record: logging.LogRecord) -> None:
+ records.append(record)
+
+ handler = _ListHandler(level=logging.INFO)
+ prev_level = proxy_logger.level
+ proxy_logger.addHandler(handler)
+ proxy_logger.setLevel(logging.INFO)
+ try:
+ log_outbound_request(
+ forwarder="google_batch_passthrough",
+ method="POST",
+ path=(
+ "https://user:[email protected]/v1beta/"
+ "models/gemini-2.5-pro:batchGenerateContent?key=AIzaSECRET#frag"
+ ),
+ body_bytes_count=1,
+ body_mutated=False,
+ mutation_reasons=[],
+ request_id=None,
+ source="passthrough",
+ )
+ finally:
+ proxy_logger.removeHandler(handler)
+ proxy_logger.setLevel(prev_level)
+
+ msg = next(r.getMessage() for r in records if "outbound_request" in r.getMessage())
+ assert (
+ "path=https://generativelanguage.googleapis.com/v1beta/"
+ "models/gemini-2.5-pro:batchGenerateContent " in msg
+ )
+ assert "AIzaSECRET" not in msg
+ assert "key=" not in msg
+ assert "user:pw" not in msg
+
+
+ ("path", "expected"),
+ [
+ # IPv6 upstreams keep their brackets so the log stays a valid URL.
+ ("https://[::1]:8787/v1/messages?key=SECRET", "https://[::1]:8787/v1/messages"),
+ ("http://[2001:db8::1]/v1/chat", "http://[2001:db8::1]/v1/chat"),
+ ("https://api.example.com:8443/v1?x=1#f", "https://api.example.com:8443/v1"),
+ # Relative paths pass through without a query.
+ ("/v1/messages?key=SECRET", "/v1/messages"),
+ # An invalid port makes urlsplit raise; never fall back to the raw URL.
+ ("https://host:notaport/v1?key=SECRET", "<unparseable>"),
+ ("https://[::1/v1?key=SECRET", "<unparseable>"),
+ ],
+)
+def test_path_for_log(path: str, expected: str) -> None:
+ assert _path_for_log(path) == expected
+
+
# ---------------------------------------------------------------------------
# httpx-mock end-to-end byte-faithful checks
# ---------------------------------------------------------------------------
diff --git a/tests/test_reporting.py b/tests/test_reporting.py
index afac1a5c7..b6f06e47b 100644
--- a/tests/test_reporting.py
+++ b/tests/test_reporting.py
@@ -49,8 +49,9 @@ def test_reporting_public_export() -> None:
def test_get_jinja2_template_success_with_stub(monkeypatch) -> None:
class FakeTemplate:
- def __init__(self, template_str: str) -> None:
+ def __init__(self, template_str: str, **options) -> None:
self.template_str = template_str
+ self.options = options
def render(self, **kwargs) -> str:
return f"{self.template_str}:{kwargs['name']}"
@@ -58,6 +59,15 @@ def test_get_jinja2_template_success_with_stub(monkeypatch) -> None:
monkeypatch.setitem(sys.modules, "jinja2", SimpleNamespace(Template=FakeTemplate))
template = generator._get_jinja2_template("hello")
assert template.render(name="world") == "hello:world"
+ assert template.options == {"autoescape": True}
+
+
+def test_report_template_escapes_client_supplied_fields() -> None:
+ pytest.importorskip("jinja2")
+ template = generator._get_jinja2_template("<td>{{ model }}</td>")
+ html = template.render(model="<script>alert(1)</script>")
+ assert "<script>" not in html
+ assert "&lt;script&gt;" in html
def test_get_jinja2_template_raises_helpful_error(monkeypatch) -> None: