From c62364a70bc5cb912ac523dd7cdda01128be7b03 Mon Sep 17 00:00:00 2001 From: RealmsNetwork Date: Tue, 20 Oct 2026 12:24:26 +0000 Subject: [PATCH 1/2] Replace unkeyed secret hashes with ephemeral HMAC fingerprints --- src/map_server/client.py | 21 +++++++++++++-- tests/test_safety.py | 56 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/map_server/client.py b/src/map_server/client.py index c9be0cf..1f4082f 100644 --- a/src/map_server/client.py +++ b/src/map_server/client.py @@ -3,10 +3,12 @@ import base64 import hashlib +import hmac import ipaddress import json import os import re +import secrets import threading import time from typing import Any @@ -102,6 +104,21 @@ def _safe_json(value: Any) -> Any: +# Cache fingerprints exist only to keep in-memory auth sessions separated when credentials +# rotate. Use a process-random HMAC key rather than an unkeyed fast hash of secrets/passwords. +# The key is intentionally ephemeral because both caches are memory-only. +_CACHE_FINGERPRINT_KEY = secrets.token_bytes(32) + + +def _credential_fingerprint(value: str) -> str: + """Return an opaque, process-local fingerprint for an in-memory cache key.""" + return hmac.new( + _CACHE_FINGERPRINT_KEY, + value.encode("utf-8"), + hashlib.sha256, + ).hexdigest() + + _OAUTH_CACHE: dict[str, tuple[str, float]] = {} _OAUTH_LOCK = threading.Lock() _AMP_SESSION_CACHE: dict[str, tuple[str, float]] = {} @@ -124,7 +141,7 @@ def _oauth_access_token(panel: dict[str, Any]) -> str: panel_id, str(panel.get("base_url", "")), str(panel.get("oauth_client_id", "")), - hashlib.sha256(str(panel.get("oauth_client_secret", "")).encode("utf-8")).hexdigest(), + _credential_fingerprint(str(panel.get("oauth_client_secret", ""))), str(panel.get("oauth_token_path", "/oauth2/token")), )) now = time.monotonic() @@ -197,7 +214,7 @@ def _amp_session(panel: dict[str, Any]) -> str: password = str(panel.get("auth_password", "")) if not username or not password: raise RuntimeError(f"AMP panel '{panel_id}' requires auth_username and auth_password.") - password_fingerprint = hashlib.sha256(password.encode("utf-8")).hexdigest() + password_fingerprint = _credential_fingerprint(password) cache_key = "|".join((panel_id, base_url, username, password_fingerprint)) now = time.monotonic() cached = _AMP_SESSION_CACHE.get(cache_key) diff --git a/tests/test_safety.py b/tests/test_safety.py index 0f916a9..c569332 100644 --- a/tests/test_safety.py +++ b/tests/test_safety.py @@ -906,3 +906,59 @@ def test_amp_login_path_rejects_trailing_slash_variant(monkeypatch): with pytest.raises(ValueError, match="login is managed automatically"): client._api_request("amp", "native", "POST", "Core/Login/", json_body={}) assert calls == [] + + + +def test_cache_secret_fingerprints_are_keyed_and_stable_within_process(): + first = client._credential_fingerprint("very-secret-password") + second = client._credential_fingerprint("very-secret-password") + different = client._credential_fingerprint("different-password") + + assert first == second + assert first != different + assert first != "very-secret-password" + assert len(first) == 64 + + +def test_oauth_and_amp_cache_keys_do_not_contain_raw_credentials(monkeypatch): + oauth_secret = "oauth-client-secret-value" + amp_password = "amp-password-secret-value" + monkeypatch.setenv( + "MAP_PANELS_JSON", + json.dumps([ + { + "id": "puffer", + "provider": "pufferpanel", + "base_url": "https://puffer.example.test", + "oauth_client_id": "oauth-id", + "oauth_client_secret": oauth_secret, + }, + { + "id": "amp", + "provider": "amp", + "base_url": "https://amp.example.test", + "auth_username": "service-user", + "auth_password": amp_password, + }, + ]), + ) + oauth_panel = server._panel("puffer") + amp_panel = server._panel("amp") + oauth_key = "|".join(( + "puffer", + str(oauth_panel.get("base_url", "")), + str(oauth_panel.get("oauth_client_id", "")), + client._credential_fingerprint(str(oauth_panel.get("oauth_client_secret", ""))), + str(oauth_panel.get("oauth_token_path", "/oauth2/token")), + )) + amp_key = "|".join(( + "amp", + str(amp_panel["base_url"]).rstrip("/"), + str(amp_panel.get("auth_username", "")), + client._credential_fingerprint(str(amp_panel.get("auth_password", ""))), + )) + + assert oauth_secret not in oauth_key + assert amp_password not in amp_key + assert client._credential_fingerprint(oauth_secret) in oauth_key + assert client._credential_fingerprint(amp_password) in amp_key From db2d401877959a1c6b3e9b6f23167e9fbadc1a0e Mon Sep 17 00:00:00 2001 From: RealmsNetwork Date: Tue, 20 Oct 2026 12:27:37 +0000 Subject: [PATCH 2/2] Remove secret-derived cache keys flagged by CodeQL --- docs/configuration.md | 2 ++ src/map_server/client.py | 26 +++-------------- tests/test_safety.py | 60 +++------------------------------------- 3 files changed, 10 insertions(+), 78 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index a1cf54f..52d02e2 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -161,3 +161,5 @@ Set MCSManager `panel_version` to `9.x` or `10.x` so MAP can choose the correct Prefer `deploy/configure_panels.py` for systemd. It reads keys with hidden input and writes the panel array in base64 form to `/etc/realmsnetwork-map/map.env`. The file should remain root-owned, group-readable by the MAP account, and mode `0640`. Base64 is encoding, not encryption. Filesystem permissions still matter. The helper preserves the server name and request/response/file-size limits and prompts for each provider's authentication type. Never put secrets in command-line arguments, committed JSON, screenshots, bug reports, or prompts. + +MAP's OAuth and AMP session caches are process-local and keyed only by non-secret panel identity fields. **Restart the MAP service after rotating an OAuth client secret, AMP password, or TOTP configuration** so cached tokens and sessions are discarded. diff --git a/src/map_server/client.py b/src/map_server/client.py index 1f4082f..070f3f3 100644 --- a/src/map_server/client.py +++ b/src/map_server/client.py @@ -2,13 +2,10 @@ from __future__ import annotations import base64 -import hashlib -import hmac import ipaddress import json import os import re -import secrets import threading import time from typing import Any @@ -104,21 +101,8 @@ def _safe_json(value: Any) -> Any: -# Cache fingerprints exist only to keep in-memory auth sessions separated when credentials -# rotate. Use a process-random HMAC key rather than an unkeyed fast hash of secrets/passwords. -# The key is intentionally ephemeral because both caches are memory-only. -_CACHE_FINGERPRINT_KEY = secrets.token_bytes(32) - - -def _credential_fingerprint(value: str) -> str: - """Return an opaque, process-local fingerprint for an in-memory cache key.""" - return hmac.new( - _CACHE_FINGERPRINT_KEY, - value.encode("utf-8"), - hashlib.sha256, - ).hexdigest() - - +# These caches are memory-only and are cleared when MAP restarts. Cache keys deliberately +# contain panel identity fields only, never passwords, client secrets, or derived fingerprints. _OAUTH_CACHE: dict[str, tuple[str, float]] = {} _OAUTH_LOCK = threading.Lock() _AMP_SESSION_CACHE: dict[str, tuple[str, float]] = {} @@ -140,8 +124,7 @@ def _oauth_access_token(panel: dict[str, Any]) -> str: cache_key = "|".join(( panel_id, str(panel.get("base_url", "")), - str(panel.get("oauth_client_id", "")), - _credential_fingerprint(str(panel.get("oauth_client_secret", ""))), + str(client_id), str(panel.get("oauth_token_path", "/oauth2/token")), )) now = time.monotonic() @@ -214,8 +197,7 @@ def _amp_session(panel: dict[str, Any]) -> str: password = str(panel.get("auth_password", "")) if not username or not password: raise RuntimeError(f"AMP panel '{panel_id}' requires auth_username and auth_password.") - password_fingerprint = _credential_fingerprint(password) - cache_key = "|".join((panel_id, base_url, username, password_fingerprint)) + cache_key = "|".join((panel_id, base_url, username)) now = time.monotonic() cached = _AMP_SESSION_CACHE.get(cache_key) if cached and cached[1] > now + 30: diff --git a/tests/test_safety.py b/tests/test_safety.py index c569332..a783e3f 100644 --- a/tests/test_safety.py +++ b/tests/test_safety.py @@ -444,6 +444,8 @@ def fake_request(method, url, **kwargs): assert api_calls[0][1] == "https://puffer.example.test/api/servers" assert api_calls[0][2]["headers"]["Authorization"] == "Bearer cached-access-token" assert "oauth-client-secret" not in json.dumps(server.list_panels()) + assert all("oauth-client-secret" not in key for key in client._OAUTH_CACHE) + assert all("oauth-client-secret" not in value[0] for value in client._OAUTH_CACHE.values()) def test_pterodactyl_style_tools_reject_unrelated_panel_types(monkeypatch): @@ -587,6 +589,8 @@ def fake_request(method, url, **kwargs): assert first["data"]["SESSIONID"] == "[REDACTED]" assert "amp-session-secret" not in json.dumps(first) assert "hidden-amp-password" not in json.dumps(server.list_panels()) + assert all("hidden-amp-password" not in key for key in client._AMP_SESSION_CACHE) + assert all("hidden-amp-password" not in value[0] for value in client._AMP_SESSION_CACHE.values()) def test_amp_native_api_requires_post_and_json_object(monkeypatch): @@ -906,59 +910,3 @@ def test_amp_login_path_rejects_trailing_slash_variant(monkeypatch): with pytest.raises(ValueError, match="login is managed automatically"): client._api_request("amp", "native", "POST", "Core/Login/", json_body={}) assert calls == [] - - - -def test_cache_secret_fingerprints_are_keyed_and_stable_within_process(): - first = client._credential_fingerprint("very-secret-password") - second = client._credential_fingerprint("very-secret-password") - different = client._credential_fingerprint("different-password") - - assert first == second - assert first != different - assert first != "very-secret-password" - assert len(first) == 64 - - -def test_oauth_and_amp_cache_keys_do_not_contain_raw_credentials(monkeypatch): - oauth_secret = "oauth-client-secret-value" - amp_password = "amp-password-secret-value" - monkeypatch.setenv( - "MAP_PANELS_JSON", - json.dumps([ - { - "id": "puffer", - "provider": "pufferpanel", - "base_url": "https://puffer.example.test", - "oauth_client_id": "oauth-id", - "oauth_client_secret": oauth_secret, - }, - { - "id": "amp", - "provider": "amp", - "base_url": "https://amp.example.test", - "auth_username": "service-user", - "auth_password": amp_password, - }, - ]), - ) - oauth_panel = server._panel("puffer") - amp_panel = server._panel("amp") - oauth_key = "|".join(( - "puffer", - str(oauth_panel.get("base_url", "")), - str(oauth_panel.get("oauth_client_id", "")), - client._credential_fingerprint(str(oauth_panel.get("oauth_client_secret", ""))), - str(oauth_panel.get("oauth_token_path", "/oauth2/token")), - )) - amp_key = "|".join(( - "amp", - str(amp_panel["base_url"]).rstrip("/"), - str(amp_panel.get("auth_username", "")), - client._credential_fingerprint(str(amp_panel.get("auth_password", ""))), - )) - - assert oauth_secret not in oauth_key - assert amp_password not in amp_key - assert client._credential_fingerprint(oauth_secret) in oauth_key - assert client._credential_fingerprint(amp_password) in amp_key