validate_key_format accepts non-canonical hex (signs, 0x, underscores, whitespace)
Bug Description
validate_key_format exists in two copies — src/memos/api/utils/api_keys.py and src/memos/api/middleware/auth.py (the latter gates every authenticated request at the middleware). Both validate the hex part with int(hex_part, 16), which accepts non-canonical forms a generated key can never have: leading +/-, a 0x prefix, _ separators, and surrounding whitespace:
>>> validate_key_format("krlk_" + "+" + "a"*63) # 64 chars
True
>>> validate_key_format("krlk_" + "0x" + "a"*62)
True
>>> validate_key_format("krlk_" + "a"*30 + "_" + "b"*33)
TrueSo the documented format krlk_<64-hex-chars> is not actually enforced: keys with embedded underscores, signs, or 0x prefixes pass the format gate and are hashed/looked up as-is.
Expected Behavior
The hex part should match exactly 64 lowercase hex characters ([0-9a-f]{64}), the only form generate_api_key() produces.
Environment
MemOS main, python 3.14.
Additional Context
Fix + red/green-verified tests ready; PR to follow referencing this issue.
Source: MemTensor/MemOS