A GPU task manager for Windows, Linux, and WSL: per-process VRAM, GPU activity, and LLM model/KV-cache insights (Windows AMD and NVIDIA).
Python
Harden the llama-server API key probe
Scott Hanselman908f6acab5be
diff --git a/README.md b/README.mdindex 74a63aa..06aae79 100644--- a/README.md+++ b/README.md@@ -705,11 +705,12 @@ The collector probes existing local listening ports with GET requests to `/propsabsent, disabled, or authenticated; other evidence is still useful. It does not launch `ollama`,`lms`, or an inference request. Throughput may update only after a request completes.-If llama-server was started with `--api-key` or `--api-key-file`, the probe sends that key as a-bearer token to the same server's own port. The key is never stored, printed, or written to JSON,-and the inspector shows it as `***`. A key set only through `LLAMA_API_KEY` can't be seen from-outside the process. In that case the probe stops at the first 401, so the server logs one-rejected request every 30 seconds, and the inspector says why server details are missing.+If llama-server was started with `--api-key`, the probe sends that key as a bearer token to the same+server's own port, and never follows a redirect with it. The key is never stored, printed, or written+to JSON, and the inspector shows it as `***`. A key in `--api-key-file` or `LLAMA_API_KEY` is not used:+gputaskman does not open a path taken from another process's command line, and an environment variable+can't be seen from outside the process. In that case the probe stops at the first 401, so the server logs+one rejected request every 30 seconds, and the inspector says why server details are missing.Ollama's local `server.log` is considered when one Ollama runner is detected. Log formats,rotation, and stale load blocks can affect attribution, so compare the reported model anddiff --git a/src/gputaskman/collector.py b/src/gputaskman/collector.pyindex 5fdf5ea..92e68b9 100644--- a/src/gputaskman/collector.py+++ b/src/gputaskman/collector.py@@ -346,8 +346,8 @@ class Collector:locked = None if own else self.prober.locked_port(ports)if locked:why = ("rejected the key from the launch arguments" if key else- "wants an API key, and none was found in the launch arguments (a key set through "- "LLAMA_API_KEY can't be read from outside the process)")+ "wants an API key, and none was found in the launch arguments (keys from LLAMA_API_KEY "+ "or --api-key-file are not read)")row.llm.notes.append(f"Local API on port {locked} {why}. Server details are skipped.")self.inspector.prune(set(procs))diff --git a/src/gputaskman/llm.py b/src/gputaskman/llm.pyindex 4fb4526..448c4d7 100644--- a/src/gputaskman/llm.py+++ b/src/gputaskman/llm.py@@ -117,7 +117,14 @@ def ollama_log_path() -> str:# ---------------------------------------------------------------- local servers+class _NoRedirect(urllib.request.HTTPRedirectHandler):+ def redirect_request(self, *args, **kwargs):+ return None++_opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))+# urllib forwards the Authorization header across redirects, so a request that carries a key never follows one.+_keyed_opener = urllib.request.build_opener(urllib.request.ProxyHandler({}), _NoRedirect)_SECRET_FLAGS = ("--api-key", "--api-key-file")@@ -127,30 +134,21 @@ UNAUTHORIZED = object() # _get result for HTTP 401: the server wants a keydef api_key(cmd: list[str]) -> str | None:"""llama-server's API key from its own launch arguments, if it was given one there.- `--api-key` may list several comma-separated keys; any of them works. `--api-key-file` holds one key per- line. A key set only through the LLAMA_API_KEY environment variable can't be seen from outside the process.+ `--api-key` may list several comma-separated keys; any of them works. `--api-key-file` is not read: its path+ comes from another process's command line, and opening it would read a file on that process's behalf. A key+ set only through the LLAMA_API_KEY environment variable can't be seen from outside the process.The key is only sent back to that process's own localhost port and is never stored or shown."""for i, arg in enumerate(cmd):key, val = arg, Noneif "=" in arg and arg.startswith("--"):key, val = arg.split("=", 1)- if key not in _SECRET_FLAGS:+ if key != "--api-key":continueif val is None and i + 1 < len(cmd):val = cmd[i + 1]- if not val:- continue- if key == "--api-key":+ if val:return next((k.strip() for k in val.split(",") if k.strip()), None)- try:- with open(val, encoding="utf-8", errors="replace") as f:- lines = f.read(64 << 10).splitlines()- except OSError:- continue- found = next((ln.strip() for ln in lines if ln.strip()), None)- if found:- return foundreturn None@@ -176,7 +174,8 @@ def _get(url: str, timeout: float = 0.5, key: str | None = None):if key:headers["Authorization"] = f"Bearer {key}"try:- with _opener.open(urllib.request.Request(url, headers=headers), timeout=timeout) as r:+ with (_keyed_opener if key else _opener).open(urllib.request.Request(url, headers=headers),+ timeout=timeout) as r:if r.status != 200:return Nonebody = r.read(4 << 20)diff --git a/tests/test_core.py b/tests/test_core.pyindex 17cd405..95d7e46 100644--- a/tests/test_core.py+++ b/tests/test_core.py@@ -28,7 +28,7 @@ from gputaskman.collector import Collector, ProcRow, Snapshot, select_adapter, sfrom gputaskman.dxgi import Adapterfrom gputaskman.gguf import GgufInfo, read_gguffrom gputaskman.handy import HandyEvidence, inspect_handy, _log_cache, _settings_cache-from gputaskman.llm import (LlmView, ServerInfo, ServerProber, api_key, build_view, is_gpu_device,+from gputaskman.llm import (LlmView, ServerInfo, ServerProber, _get, api_key, build_view, is_gpu_device,llama_server_models, lmstudio_models, parse_flags, parse_load_log, probe_server,redact_cmdline)from gputaskman.nvml import NvDevice, Nvml@@ -774,11 +774,12 @@ class IdentityTests(unittest.TestCase):self.assertEqual(api_key(["llama-server", "--api-key=abc,def"]), "abc")self.assertIsNone(api_key(["llama-server", "-m", "x.gguf"]))self.assertIsNone(api_key(["llama-server", "--api-key"]))- with tempfile.TemporaryDirectory() as root:+ with tempfile.TemporaryDirectory() as root: # a path from another process's command line is never openedpath = os.path.join(root, "keys.txt")- Path(path).write_text("\n file-key \nsecond\n", encoding="utf-8")- self.assertEqual(api_key(["llama-server", "--api-key-file", path]), "file-key")- self.assertIsNone(api_key(["llama-server", "--api-key-file", os.path.join(root, "missing")]))+ Path(path).write_text("file-key\n", encoding="utf-8")+ self.assertIsNone(api_key(["llama-server", "--api-key-file", path]))+ self.assertIsNone(api_key(["llama-server", f"--api-key-file={path}"]))+ self.assertEqual(api_key(["llama-server", "--api-key-file", path, "--api-key", "abc"]), "abc")self.assertEqual(redact_cmdline(["llama-server", "--api-key", "abc", "--api-key-file=/k", "--port", "8080"]),["llama-server", "--api-key", "***", "--api-key-file=***", "--port", "8080"])self.assertNotIn("api_key", str(parse_flags(["--api-key", "abc"])))@@ -827,6 +828,43 @@ class IdentityTests(unittest.TestCase):server.shutdown()server.server_close()+ def test_api_key_is_not_forwarded_by_a_redirect(self):+ import http.server+ seen: dict[str, list[str | None]] = {"first": [], "other": []}++ def handler(name, redirect_to=None):+ class Handler(http.server.BaseHTTPRequestHandler):+ def do_GET(self):+ seen[name].append(self.headers.get("Authorization"))+ self.send_response(302 if redirect_to else 200)+ if redirect_to:+ self.send_header("Location", redirect_to)+ self.end_headers()+ if not redirect_to:+ self.wfile.write(b"{}")++ def log_message(self, *args):+ pass+ return Handler++ other = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler("other"))+ first = http.server.ThreadingHTTPServer(+ ("127.0.0.1", 0), handler("first", f"http://127.0.0.1:{other.server_address[1]}/props"))+ for server in (other, first):+ threading.Thread(target=server.serve_forever, daemon=True).start()+ try:+ url = f"http://127.0.0.1:{first.server_address[1]}/props"+ self.assertIsNone(_get(url, key="s3cret"))+ self.assertEqual(len(seen["first"]), 1)+ self.assertIsNotNone(seen["first"][0]) # the server the key belongs to did get it+ self.assertEqual(seen["other"], []) # the redirect target was never contacted+ self.assertEqual(_get(url), {}) # requests without a key behave as before+ self.assertEqual(seen["other"], [None])+ finally:+ for server in (other, first):+ server.shutdown()+ server.server_close()+def test_lmstudio_falls_back_to_v0_when_v1_is_an_error(self):replies = {"/api/v1/models": {"error": "Unexpected endpoint or method. (GET /api/v1/models)"},"/api/v0/models": {"data": [{"id": "qwen", "state": "loaded", "max_context_length": 8192}]}}