|
| 1 | +"""Regression tests for issue #2427. |
| 2 | +
|
| 3 | +``validate_key_format`` exists in two copies — ``src/memos/api/utils/api_keys.py`` |
| 4 | +(the canonical home) and ``src/memos/api/middleware/auth.py`` (the |
| 5 | +middleware gating every authenticated request). Both check the hex part |
| 6 | +with ``int(hex_part, 16)``, which accepts non-canonical forms a generated |
| 7 | +key can never have: |
| 8 | +
|
| 9 | +- leading ``+`` / ``-`` (sign) |
| 10 | +- ``0x`` / ``0X`` prefix |
| 11 | +- ``_`` separators (PEP 515) |
| 12 | +- surrounding whitespace |
| 13 | +
|
| 14 | +So the documented format ``krlk_<64-hex-chars>`` is not actually enforced: |
| 15 | +keys with embedded underscores, signs, or ``0x`` prefixes pass the format |
| 16 | +gate and are hashed/looked up as-is, bypassing the canonical form that |
| 17 | +``generate_api_key()`` produces. |
| 18 | +
|
| 19 | +These tests pin the new contract on both validator copies: |
| 20 | +
|
| 21 | +- the hex part must match ``[0-9a-f]{64}`` exactly |
| 22 | +- the only valid form is the canonical lowercase hex string |
| 23 | +""" |
| 24 | + |
| 25 | +from __future__ import annotations |
| 26 | + |
| 27 | +import pytest |
| 28 | + |
| 29 | +from memos.api.middleware.auth import validate_key_format as validate_in_auth |
| 30 | +from memos.api.utils.api_keys import generate_api_key, validate_key_format |
| 31 | + |
| 32 | + |
| 33 | +CANONICAL = "krlk_" + "0123456789abcdef" * 4 # 64 hex chars, lowercase |
| 34 | + |
| 35 | + |
| 36 | +class TestValidateKeyFormatUtils: |
| 37 | + def test_canonical_key_passes(self): |
| 38 | + assert validate_key_format(CANONICAL) is True |
| 39 | + |
| 40 | + def test_generated_key_always_passes(self): |
| 41 | + # The validator must accept every key that generate_api_key() |
| 42 | + # produces, since that is the documented format. |
| 43 | + for _ in range(20): |
| 44 | + assert validate_key_format(generate_api_key().key) is True |
| 45 | + |
| 46 | + @pytest.mark.parametrize( |
| 47 | + "bad_key", |
| 48 | + [ |
| 49 | + # Leading sign |
| 50 | + "krlk_" + "+" + "a" * 63, |
| 51 | + "krlk_" + "-" + "a" * 63, |
| 52 | + # 0x prefix |
| 53 | + "krlk_" + "0x" + "a" * 62, |
| 54 | + "krlk_" + "0X" + "a" * 62, |
| 55 | + # Underscore separator (PEP 515) |
| 56 | + "krlk_" + "a" * 32 + "_" + "b" * 31, |
| 57 | + # Uppercase hex (the generator emits lowercase only) |
| 58 | + "krlk_" + "A" * 64, |
| 59 | + # Surrounding whitespace |
| 60 | + " krlk_" + "a" * 64, |
| 61 | + "krlk_" + "a" * 64 + " ", |
| 62 | + # Wrong total length |
| 63 | + "krlk_" + "a" * 63, |
| 64 | + "krlk_" + "a" * 65, |
| 65 | + # Empty hex part |
| 66 | + "krlk_", |
| 67 | + # Missing prefix |
| 68 | + "0123456789abcdef" * 4, |
| 69 | + "Bearer " + CANONICAL, |
| 70 | + # Non-string |
| 71 | + "", |
| 72 | + ], |
| 73 | + ) |
| 74 | + def test_non_canonical_keys_rejected(self, bad_key): |
| 75 | + assert validate_key_format(bad_key) is False, ( |
| 76 | + f"validate_key_format({bad_key!r}) should return False" |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +class TestAuthMiddlewareValidator: |
| 81 | + """The middleware copy must enforce the same contract as the canonical |
| 82 | + one — issue #2427 documents both copies accepting non-canonical hex.""" |
| 83 | + |
| 84 | + def test_canonical_key_passes(self): |
| 85 | + assert validate_in_auth(CANONICAL) is True |
| 86 | + |
| 87 | + def test_signed_hex_rejected(self): |
| 88 | + # The most surprising bit: ``int("+a..", 16)`` succeeds, so a signed |
| 89 | + # hex passes the middleware's validator today. |
| 90 | + assert validate_in_auth("krlk_" + "+" + "a" * 63) is False |
| 91 | + |
| 92 | + def test_underscore_rejected(self): |
| 93 | + assert validate_in_auth("krlk_" + "a" * 32 + "_" + "b" * 31) is False |
| 94 | + |
| 95 | + def test_uppercase_rejected(self): |
| 96 | + assert validate_in_auth("krlk_" + "A" * 64) is False |
| 97 | + |
| 98 | + def test_0x_prefix_rejected(self): |
| 99 | + assert validate_in_auth("krlk_" + "0x" + "a" * 62) is False |
0 commit comments