diff --git a/src/memos/api/middleware/auth.py b/src/memos/api/middleware/auth.py index 6d812337b..6411c2c64 100644 --- a/src/memos/api/middleware/auth.py +++ b/src/memos/api/middleware/auth.py @@ -7,6 +7,7 @@ import hashlib import os +import secrets import time from typing import Any @@ -34,6 +35,18 @@ MASTER_KEY_HASH = os.getenv("MASTER_KEY_HASH") # SHA-256 hash of master key INTERNAL_SERVICE_IPS = {"127.0.0.1", "::1", "memos-mcp", "moltbot", "clawdbot"} +# Operator warning (issue #2553): enabling auth without configuring the +# internal-service secret used to silently fall through to ``True`` because +# both sides of the ``==`` were ``None``. The fix in ``is_internal_request`` +# makes the header path fail closed; this warning surfaces the +# misconfiguration so operators notice before they rely on it. +if AUTH_ENABLED and not os.getenv("INTERNAL_SERVICE_SECRET"): + logger.warning( + "AUTH_ENABLED=true but INTERNAL_SERVICE_SECRET is unset or empty; " + "the X-Internal-Service header path will fail closed. " + "Set INTERNAL_SERVICE_SECRET to enable container-to-container auth." + ) + # Connection pool for auth queries (lazy init) _auth_pool = None @@ -148,16 +161,36 @@ async def lookup_api_key(key_hash: str) -> dict[str, Any] | None: def is_internal_request(request: Request) -> bool: - """Check if request is from internal service.""" + """Check if request is from internal service. + + Policy (issue #2553): the ``X-Internal-Service`` header is only trusted + when ``INTERNAL_SERVICE_SECRET`` is configured to a non-empty value. If + the operator forgets to set the secret we must fail closed — otherwise + ``os.getenv(...)`` returns ``None``, ``request.headers.get(...)`` also + returns ``None`` for an unauthenticated external caller, and the plain + ``==`` comparison promotes every anonymous request to ``user_name= + "internal"`` with ``scopes=["all"]``. + + The configured header value is compared with ``secrets.compare_digest`` + to avoid a timing side channel on the shared secret. + + ``INTERNAL_SERVICE_IPS`` is intentionally still trusted at the network + layer; the scope of this fix is the header path. + """ client_host = request.client.host if request.client else None # Check internal IPs if client_host in INTERNAL_SERVICE_IPS: return True - # Check internal header (for container-to-container) - internal_header = request.headers.get("X-Internal-Service") - return internal_header == os.getenv("INTERNAL_SERVICE_SECRET") + # Check internal header (for container-to-container). + internal_secret = os.getenv("INTERNAL_SERVICE_SECRET") + if not internal_secret: + # Fail closed when the secret is unset or empty. + return False + + internal_header = request.headers.get("X-Internal-Service") or "" + return secrets.compare_digest(internal_header, internal_secret) async def verify_api_key( diff --git a/tests/api/test_is_internal_request.py b/tests/api/test_is_internal_request.py new file mode 100644 index 000000000..7efa98a31 --- /dev/null +++ b/tests/api/test_is_internal_request.py @@ -0,0 +1,135 @@ +"""Regression tests for issue #2553. + +``is_internal_request()`` previously used plain ``==`` to compare the +``X-Internal-Service`` header against ``os.getenv("INTERNAL_SERVICE_SECRET")``. +When the environment variable was unset, ``os.getenv`` returned ``None`` — and +if the client omitted the header, ``request.headers.get(...)`` also returned +``None``. The comparison therefore evaluated to ``None == None`` → ``True`` and +every unauthenticated request was promoted to ``user_name="internal"`` with +``scopes=["all"]``. + +These tests pin the new contract on both the raw helper and the +``verify_api_key`` dependency: + +- unset / empty secret: ALWAYS fail closed, regardless of what the header says; +- set secret: only the exact header value authenticates; otherwise fail closed; +- loopback IPs still short-circuit as "internal" (behaviour deliberately + preserved — see the issue note about ``INTERNAL_SERVICE_IPS``); +- the comparison uses ``secrets.compare_digest`` (timing-safe) rather than + ``==``. +""" + +from __future__ import annotations + +import asyncio + +from types import SimpleNamespace +from unittest import mock + +import pytest + +import memos.api.middleware.auth as auth_module + +from memos.api.middleware.auth import is_internal_request, verify_api_key + + +def _make_request( + *, + client_host: str | None = "203.0.113.9", + headers: dict[str, str] | None = None, +): + """Build a FastAPI-ish request stand-in with the pieces our code touches.""" + headers = headers or {} + client = SimpleNamespace(host=client_host) if client_host is not None else None + return SimpleNamespace(client=client, headers=headers) + + +class TestIsInternalRequestFailsClosed: + """Secret missing or empty must behave as 'no trusted header configured'.""" + + @pytest.mark.parametrize("secret_env", [None, ""]) + def test_unset_secret_rejects_missing_header(self, monkeypatch, secret_env): + if secret_env is None: + monkeypatch.delenv("INTERNAL_SERVICE_SECRET", raising=False) + else: + monkeypatch.setenv("INTERNAL_SERVICE_SECRET", secret_env) + req = _make_request(headers={}) + + assert is_internal_request(req) is False + + @pytest.mark.parametrize("secret_env", [None, ""]) + def test_unset_secret_rejects_any_header_value(self, monkeypatch, secret_env): + if secret_env is None: + monkeypatch.delenv("INTERNAL_SERVICE_SECRET", raising=False) + else: + monkeypatch.setenv("INTERNAL_SERVICE_SECRET", secret_env) + # Even sending the literal word "None" or an empty string must not pass. + for header_value in ("", "None", "guess", "null", "undefined"): + req = _make_request(headers={"X-Internal-Service": header_value}) + assert is_internal_request(req) is False, header_value + + +class TestIsInternalRequestSecretMatch: + """A configured secret authenticates only on exact header match.""" + + def test_exact_match_authenticates(self, monkeypatch): + monkeypatch.setenv("INTERNAL_SERVICE_SECRET", "s3cret-token") + req = _make_request(headers={"X-Internal-Service": "s3cret-token"}) + assert is_internal_request(req) is True + + def test_wrong_header_rejected(self, monkeypatch): + monkeypatch.setenv("INTERNAL_SERVICE_SECRET", "s3cret-token") + req = _make_request(headers={"X-Internal-Service": "nope"}) + assert is_internal_request(req) is False + + def test_missing_header_rejected(self, monkeypatch): + monkeypatch.setenv("INTERNAL_SERVICE_SECRET", "s3cret-token") + req = _make_request(headers={}) + assert is_internal_request(req) is False + + def test_comparison_is_timing_safe(self, monkeypatch): + """The fix must route through secrets.compare_digest.""" + monkeypatch.setenv("INTERNAL_SERVICE_SECRET", "s3cret-token") + req = _make_request(headers={"X-Internal-Service": "s3cret-token"}) + + with mock.patch( + "memos.api.middleware.auth.secrets.compare_digest", + wraps=auth_module.secrets.compare_digest, + ) as spy: + assert is_internal_request(req) is True + assert spy.call_count == 1 + + +class TestLoopbackPreserved: + """INTERNAL_SERVICE_IPS short-circuit stays. The issue flags this as a + separate discussion, not something to change here.""" + + def test_loopback_v4_still_internal(self, monkeypatch): + monkeypatch.delenv("INTERNAL_SERVICE_SECRET", raising=False) + req = _make_request(client_host="127.0.0.1", headers={}) + assert is_internal_request(req) is True + + def test_loopback_v6_still_internal(self, monkeypatch): + monkeypatch.delenv("INTERNAL_SERVICE_SECRET", raising=False) + req = _make_request(client_host="::1", headers={}) + assert is_internal_request(req) is True + + def test_known_container_hostname_still_internal(self, monkeypatch): + monkeypatch.delenv("INTERNAL_SERVICE_SECRET", raising=False) + req = _make_request(client_host="memos-mcp", headers={}) + assert is_internal_request(req) is True + + +class TestVerifyApiKeyUnsetSecret: + """End-to-end: an unauthenticated external request must get 401, not + silently become ``user_name='internal'``.""" + + def test_external_no_credentials_rejected(self, monkeypatch): + monkeypatch.setattr(auth_module, "AUTH_ENABLED", True) + monkeypatch.delenv("INTERNAL_SERVICE_SECRET", raising=False) + req = _make_request(client_host="203.0.113.9", headers={}) + + with pytest.raises(Exception) as exc_info: + asyncio.get_event_loop().run_until_complete(verify_api_key(req, api_key=None)) + # Must be an HTTPException(401), not an auth bypass. + assert getattr(exc_info.value, "status_code", None) == 401