From c2f5b1513d5b6fa590280e3a965f2d29963694b2 Mon Sep 17 00:00:00 2001 From: MemOS AutoDev Date: Tue, 29 Sep 2026 00:44:06 +0800 Subject: [PATCH 1/2] fix: enforce strict hex validation in validate_key_format Both validate_key_format implementations (middleware/auth.py and utils/api_keys.py) now use character-by-character validation to reject non-canonical hex forms that generated keys can never have: - Leading +/- signs - 0x prefix - _ digit separators - Whitespace (leading/trailing/embedded) - Uppercase hex characters The fix replaces int(hex_part, 16) with all(c in '0123456789abcdef' for c in hex_part) to enforce exactly 64 lowercase hex characters [0-9a-f] matching generate_api_key() output. Fixes #2427 --- src/memos/api/middleware/auth.py | 14 +-- src/memos/api/utils/api_keys.py | 9 +- tests/api/middleware/test_auth.py | 125 +++++++++++++++++++++++++ tests/api/utils/test_api_keys.py | 148 ++++++++++++++++++++++++++++++ 4 files changed, 285 insertions(+), 11 deletions(-) create mode 100644 tests/api/middleware/test_auth.py create mode 100644 tests/api/utils/test_api_keys.py diff --git a/src/memos/api/middleware/auth.py b/src/memos/api/middleware/auth.py index 15b217651..9e9039b48 100644 --- a/src/memos/api/middleware/auth.py +++ b/src/memos/api/middleware/auth.py @@ -63,17 +63,19 @@ def hash_api_key(key: str) -> str: def validate_key_format(key: str) -> bool: - """Validate API key format: krlk_<64-hex>.""" + """ + Validate API key format: krlk_<64-hex>. + + Only lowercase hex characters [0-9a-f] are accepted, matching generate_api_key() output. + This rejects non-canonical forms like +/- signs, 0x prefix, underscores, and whitespace. + """ if not key or not key.startswith("krlk_"): return False hex_part = key[5:] # Remove 'krlk_' prefix if len(hex_part) != 64: return False - try: - int(hex_part, 16) - return True - except ValueError: - return False + # Strict validation: only lowercase hex chars [0-9a-f] + return all(c in "0123456789abcdef" for c in hex_part) def get_key_prefix(key: str) -> str: diff --git a/src/memos/api/utils/api_keys.py b/src/memos/api/utils/api_keys.py index 29b493fd0..6980f9876 100644 --- a/src/memos/api/utils/api_keys.py +++ b/src/memos/api/utils/api_keys.py @@ -50,6 +50,7 @@ def validate_key_format(key: str) -> bool: Validate API key format. Valid format: krlk_<64-hex-chars> + Only lowercase hex characters [0-9a-f] are accepted, matching generate_api_key() output. """ if not key or not isinstance(key, str): return False @@ -61,11 +62,9 @@ def validate_key_format(key: str) -> bool: if len(hex_part) != 64: return False - try: - int(hex_part, 16) - return True - except ValueError: - return False + # Strict validation: only lowercase hex chars [0-9a-f] + # This rejects +/- signs, 0x prefix, underscores, whitespace, and uppercase + return all(c in "0123456789abcdef" for c in hex_part) def generate_master_key() -> tuple[str, str]: diff --git a/tests/api/middleware/test_auth.py b/tests/api/middleware/test_auth.py new file mode 100644 index 000000000..9b5cd112e --- /dev/null +++ b/tests/api/middleware/test_auth.py @@ -0,0 +1,125 @@ +"""Tests for authentication middleware.""" + + +from memos.api.middleware.auth import ( + get_key_prefix, + hash_api_key, + validate_key_format, +) + + +class TestValidateKeyFormat: + """Test API key format validation in middleware.""" + + def test_valid_key(self): + """Valid keys should pass validation.""" + # Test with all lowercase hex chars + test_key = "krlk_" + "a" * 64 + assert validate_key_format(test_key) + + test_key = "krlk_" + "0123456789abcdef" * 4 + assert validate_key_format(test_key) + + def test_invalid_prefix(self): + """Keys without krlk_ prefix should fail.""" + assert not validate_key_format("mk_" + "a" * 64) + assert not validate_key_format("api_" + "a" * 64) + assert not validate_key_format("a" * 69) + + def test_invalid_length(self): + """Keys with wrong hex length should fail.""" + assert not validate_key_format("krlk_" + "a" * 63) # Too short + assert not validate_key_format("krlk_" + "a" * 65) # Too long + assert not validate_key_format("krlk_") # No hex part + + def test_reject_leading_plus_sign(self): + """Keys with leading + should be rejected.""" + # This currently passes but should fail - int('+' + hex, 16) works + invalid_key = "krlk_" + "+" + "a" * 63 + assert not validate_key_format(invalid_key), "Should reject leading +" + + def test_reject_leading_minus_sign(self): + """Keys with leading - should be rejected.""" + invalid_key = "krlk_" + "-" + "a" * 63 + assert not validate_key_format(invalid_key), "Should reject leading -" + + def test_reject_0x_prefix(self): + """Keys with 0x prefix should be rejected.""" + # This currently passes but should fail - int('0x' + hex, 16) works + invalid_key = "krlk_" + "0x" + "a" * 62 + assert not validate_key_format(invalid_key), "Should reject 0x prefix" + + def test_reject_underscores(self): + """Keys with underscores in hex should be rejected.""" + # This currently passes but should fail - int() accepts _ separators + invalid_key = "krlk_" + "a" * 30 + "_" + "b" * 33 + assert not validate_key_format(invalid_key), "Should reject underscores" + + invalid_key = "krlk_" + "1234_5678_9abc_def0" + "a" * 46 + assert not validate_key_format(invalid_key), "Should reject underscores" + + def test_reject_whitespace(self): + """Keys with whitespace in hex should be rejected.""" + invalid_key = "krlk_" + " " + "a" * 63 + assert not validate_key_format(invalid_key), "Should reject leading space" + + invalid_key = "krlk_" + "a" * 63 + " " + assert not validate_key_format(invalid_key), "Should reject trailing space" + + invalid_key = "krlk_" + "a" * 32 + " " + "b" * 31 + assert not validate_key_format(invalid_key), "Should reject embedded space" + + invalid_key = "krlk_" + "\t" + "a" * 63 + assert not validate_key_format(invalid_key), "Should reject tabs" + + def test_reject_uppercase_hex(self): + """Keys with uppercase hex should be rejected.""" + # Generated keys use lowercase only + invalid_key = "krlk_" + "A" * 64 + assert not validate_key_format(invalid_key), "Should reject uppercase hex" + + invalid_key = "krlk_" + "a" * 32 + "B" + "c" * 31 + assert not validate_key_format(invalid_key), "Should reject mixed case" + + def test_reject_non_hex_chars(self): + """Keys with non-hex characters should be rejected.""" + assert not validate_key_format("krlk_" + "g" * 64) + assert not validate_key_format("krlk_" + "z" * 64) + assert not validate_key_format("krlk_" + "!" * 64) + + def test_none_and_empty(self): + """None and empty strings should fail.""" + assert not validate_key_format(None) + assert not validate_key_format("") + + +class TestHashApiKey: + """Test API key hashing.""" + + def test_hash_deterministic(self): + """Same key should produce same hash.""" + key = "krlk_" + "a" * 64 + hash1 = hash_api_key(key) + hash2 = hash_api_key(key) + assert hash1 == hash2 + + def test_hash_unique(self): + """Different keys should produce different hashes.""" + key1 = "krlk_" + "a" * 64 + key2 = "krlk_" + "b" * 64 + assert hash_api_key(key1) != hash_api_key(key2) + + +class TestGetKeyPrefix: + """Test key prefix extraction.""" + + def test_get_prefix_normal_key(self): + """Should extract first 12 chars.""" + key = "krlk_abcdef123456789" + assert get_key_prefix(key) == "krlk_abcdef1" + assert len(get_key_prefix(key)) == 12 + + def test_get_prefix_short_key(self): + """Should return full key if shorter than 12 chars.""" + key = "krlk_abc" + assert get_key_prefix(key) == key diff --git a/tests/api/utils/test_api_keys.py b/tests/api/utils/test_api_keys.py new file mode 100644 index 000000000..54b5a15c2 --- /dev/null +++ b/tests/api/utils/test_api_keys.py @@ -0,0 +1,148 @@ +"""Tests for API key utilities.""" + + +from memos.api.utils.api_keys import ( + generate_api_key, + hash_key, + validate_key_format, +) + + +class TestGenerateAPIKey: + """Test API key generation.""" + + def test_generate_api_key_format(self): + """Generated key should have correct format.""" + api_key = generate_api_key() + assert api_key.key.startswith("krlk_") + assert len(api_key.key) == 69 # 'krlk_' (5) + 64 hex chars + assert validate_key_format(api_key.key) + + def test_generate_api_key_randomness(self): + """Each generated key should be unique.""" + keys = [generate_api_key().key for _ in range(10)] + assert len(set(keys)) == 10 + + +class TestValidateKeyFormat: + """Test API key format validation.""" + + def test_valid_key(self): + """Valid keys should pass validation.""" + # Generate a real key + valid_key = generate_api_key().key + assert validate_key_format(valid_key) + + # Test with all lowercase hex chars + test_key = "krlk_" + "a" * 64 + assert validate_key_format(test_key) + + test_key = "krlk_" + "0123456789abcdef" * 4 + assert validate_key_format(test_key) + + def test_invalid_prefix(self): + """Keys without krlk_ prefix should fail.""" + assert not validate_key_format("mk_" + "a" * 64) + assert not validate_key_format("api_" + "a" * 64) + assert not validate_key_format("a" * 69) + + def test_invalid_length(self): + """Keys with wrong hex length should fail.""" + assert not validate_key_format("krlk_" + "a" * 63) # Too short + assert not validate_key_format("krlk_" + "a" * 65) # Too long + assert not validate_key_format("krlk_") # No hex part + + def test_reject_leading_plus_sign(self): + """Keys with leading + should be rejected.""" + # This currently passes but should fail + invalid_key = "krlk_" + "+" + "a" * 63 + assert not validate_key_format(invalid_key), "Should reject leading +" + + def test_reject_leading_minus_sign(self): + """Keys with leading - should be rejected.""" + invalid_key = "krlk_" + "-" + "a" * 63 + assert not validate_key_format(invalid_key), "Should reject leading -" + + def test_reject_0x_prefix(self): + """Keys with 0x prefix should be rejected.""" + # This currently passes but should fail + invalid_key = "krlk_" + "0x" + "a" * 62 + assert not validate_key_format(invalid_key), "Should reject 0x prefix" + + def test_reject_underscores(self): + """Keys with underscores in hex should be rejected.""" + # This currently passes but should fail + invalid_key = "krlk_" + "a" * 30 + "_" + "b" * 33 + assert not validate_key_format(invalid_key), "Should reject underscores" + + invalid_key = "krlk_" + "1234_5678_9abc_def0" + "a" * 46 + assert not validate_key_format(invalid_key), "Should reject underscores" + + def test_reject_whitespace(self): + """Keys with whitespace in hex should be rejected.""" + invalid_key = "krlk_" + " " + "a" * 63 + assert not validate_key_format(invalid_key), "Should reject leading space" + + invalid_key = "krlk_" + "a" * 63 + " " + assert not validate_key_format(invalid_key), "Should reject trailing space" + + invalid_key = "krlk_" + "a" * 32 + " " + "b" * 31 + assert not validate_key_format(invalid_key), "Should reject embedded space" + + invalid_key = "krlk_" + "\t" + "a" * 63 + assert not validate_key_format(invalid_key), "Should reject tabs" + + invalid_key = "krlk_" + "\n" + "a" * 63 + assert not validate_key_format(invalid_key), "Should reject newlines" + + def test_reject_uppercase_hex(self): + """Keys with uppercase hex should be rejected.""" + # Generated keys use lowercase only + invalid_key = "krlk_" + "A" * 64 + assert not validate_key_format(invalid_key), "Should reject uppercase hex" + + invalid_key = "krlk_" + "a" * 32 + "B" + "c" * 31 + assert not validate_key_format(invalid_key), "Should reject mixed case" + + def test_reject_non_hex_chars(self): + """Keys with non-hex characters should be rejected.""" + assert not validate_key_format("krlk_" + "g" * 64) + assert not validate_key_format("krlk_" + "z" * 64) + assert not validate_key_format("krlk_" + "!" * 64) + assert not validate_key_format("krlk_" + "a" * 63 + "g") + + def test_none_and_empty(self): + """None and empty strings should fail.""" + assert not validate_key_format(None) + assert not validate_key_format("") + assert not validate_key_format(" ") + + def test_non_string_input(self): + """Non-string inputs should fail.""" + assert not validate_key_format(123) + assert not validate_key_format(["krlk_", "a" * 64]) + assert not validate_key_format({"key": "krlk_" + "a" * 64}) + + +class TestHashKey: + """Test key hashing.""" + + def test_hash_key_deterministic(self): + """Same key should produce same hash.""" + key = "krlk_" + "a" * 64 + hash1 = hash_key(key) + hash2 = hash_key(key) + assert hash1 == hash2 + + def test_hash_key_unique(self): + """Different keys should produce different hashes.""" + key1 = "krlk_" + "a" * 64 + key2 = "krlk_" + "b" * 64 + assert hash_key(key1) != hash_key(key2) + + def test_hash_key_length(self): + """SHA-256 hash should be 64 hex chars.""" + key = generate_api_key().key + key_hash = hash_key(key) + assert len(key_hash) == 64 + assert all(c in "0123456789abcdef" for c in key_hash) From ad733f6dd195b52ec9c708cedaa9270cde39c60d Mon Sep 17 00:00:00 2001 From: MemOS AutoDev Date: Tue, 29 Sep 2026 00:54:50 +0800 Subject: [PATCH 2/2] fix(auth): use frozenset for hex validation and clean up stale test comments - Use module-level `_LOWERCASE_HEX = frozenset("0123456789abcdef")` in `validate_key_format` so hex membership tests are O(1) instead of scanning a string literal. The intent (fixed set of allowed characters) is also clearer than the string literal. - Drop the "currently passes but should fail" comments in `test_reject_leading_plus_sign`, `test_reject_0x_prefix`, and `test_reject_underscores`. Those comments described the pre-fix behavior; with strict character validation in place the tests now pass and the comments were misleading. Addresses PR #2431 open-code-review findings. --- src/memos/api/middleware/auth.py | 5 ++++- tests/api/middleware/test_auth.py | 3 --- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/memos/api/middleware/auth.py b/src/memos/api/middleware/auth.py index 9e9039b48..347f6be9e 100644 --- a/src/memos/api/middleware/auth.py +++ b/src/memos/api/middleware/auth.py @@ -27,6 +27,9 @@ 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"} +# Allowed characters for the hex portion of an API key (lowercase only) +_LOWERCASE_HEX = frozenset("0123456789abcdef") + # Connection pool for auth queries (lazy init) _auth_pool = None @@ -75,7 +78,7 @@ def validate_key_format(key: str) -> bool: if len(hex_part) != 64: return False # Strict validation: only lowercase hex chars [0-9a-f] - return all(c in "0123456789abcdef" for c in hex_part) + return all(c in _LOWERCASE_HEX for c in hex_part) def get_key_prefix(key: str) -> str: diff --git a/tests/api/middleware/test_auth.py b/tests/api/middleware/test_auth.py index 9b5cd112e..8044b91d5 100644 --- a/tests/api/middleware/test_auth.py +++ b/tests/api/middleware/test_auth.py @@ -34,7 +34,6 @@ def test_invalid_length(self): def test_reject_leading_plus_sign(self): """Keys with leading + should be rejected.""" - # This currently passes but should fail - int('+' + hex, 16) works invalid_key = "krlk_" + "+" + "a" * 63 assert not validate_key_format(invalid_key), "Should reject leading +" @@ -45,13 +44,11 @@ def test_reject_leading_minus_sign(self): def test_reject_0x_prefix(self): """Keys with 0x prefix should be rejected.""" - # This currently passes but should fail - int('0x' + hex, 16) works invalid_key = "krlk_" + "0x" + "a" * 62 assert not validate_key_format(invalid_key), "Should reject 0x prefix" def test_reject_underscores(self): """Keys with underscores in hex should be rejected.""" - # This currently passes but should fail - int() accepts _ separators invalid_key = "krlk_" + "a" * 30 + "_" + "b" * 33 assert not validate_key_format(invalid_key), "Should reject underscores"