You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
So 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.
Summary: Fixed strict hex validation bug in validate_key_format across both middleware and utils modules.
Changes Made
Fixed validate_key_format in both src/memos/api/middleware/auth.py and src/memos/api/utils/api_keys.py to enforce strict lowercase hex character validation [0-9a-f]. The previous implementation used int(hex_part, 16) which incorrectly accepted non-canonical forms that generated keys can never have.
These non-canonical forms create a validation gap: keys with embedded underscores, signs, or 0x prefixes pass format validation and are hashed/looked up, but can never be generated by generate_api_key().
Solution
Replaced int(hex_part, 16) validation with character-by-character check: all(c in "0123456789abcdef" for c in hex_part). This enforces exactly 64 lowercase hex characters matching the output format of generate_api_key().
Test Coverage
Added comprehensive test suites covering all rejection cases:
17 tests in tests/api/utils/test_api_keys.py
15 tests in tests/api/middleware/test_auth.py
All 149 API tests pass with zero failures. Tests verify rejection of:
Activity
Memtensor-AI commentedon Sep 28, 2026
🤖 AutoDev has picked up this issue and started working on it.
Task ID:
b2bf2e63b2be4a47Working branch:
bugfix/autodev-2427-20260928163747868Target branch:
latest dev* branchWorkflow: opsp (analysis → coding → testing → PR)
I will post the PR link here once done. If I need more information, I will ask in the comments.
fix: enforce strict hex validation in validate_key_format
Memtensor-AI commentedon Sep 28, 2026
✅ AutoDev task
b2bf2e63b2be4a47completed.Summary: Fixed strict hex validation bug in validate_key_format across both middleware and utils modules.
Changes Made
Fixed
validate_key_formatin bothsrc/memos/api/middleware/auth.pyandsrc/memos/api/utils/api_keys.pyto enforce strict lowercase hex character validation [0-9a-f]. The previous implementation usedint(hex_part, 16)which incorrectly accepted non-canonical forms that generated keys can never have.Root Cause
Python's
int(hex_part, 16)accepts flexible integer literal syntax including:These non-canonical forms create a validation gap: keys with embedded underscores, signs, or 0x prefixes pass format validation and are hashed/looked up, but can never be generated by
generate_api_key().Solution
Replaced
int(hex_part, 16)validation with character-by-character check:all(c in "0123456789abcdef" for c in hex_part). This enforces exactly 64 lowercase hex characters matching the output format ofgenerate_api_key().Test Coverage
Added comprehensive test suites covering all rejection cases:
tests/api/utils/test_api_keys.pytests/api/middleware/test_auth.pyAll 149 API tests pass with zero failures. Tests verify rejection of:
Verification
No regressions detected. The fix is backward compatible - all valid keys continue to work.
Base branch:
mainBranch:
bugfix/autodev-2427-20260928163747868Commit:
c2f5b15PR: #2431
Assigned to: @syzsunshine219
Reviewers: @bittergreen
fix(api): enforce canonical hex format on validate_key_format (MemTen…
fix(api): enforce canonical hex format on validate_key_format (#2427) (…
github-actions commentedon Oct 30, 2026
This issue has been automatically marked as stale due to inactivity.
github-actions commentedon Nov 7, 2026
This issue has been automatically closed due to inactivity.