MemOS icon indicating copy to clipboard operation
MemOS copied to clipboard

validate_key_format accepts non-canonical hex (signs, 0x, underscores, whitespace)

Open simpleqt opened this issue 2 months ago • 4 comments

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)
True

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.

simpleqt avatar Sep 28 '26 16:09 simpleqt

🤖 AutoDev has picked up this issue and started working on it.

Task ID: b2bf2e63b2be4a47 Working branch: bugfix/autodev-2427-20260928163747868 Target branch: latest dev* branch Workflow: 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.

Memtensor-AI avatar Sep 28 '26 16:09 Memtensor-AI

✅ AutoDev task b2bf2e63b2be4a47 completed.

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.

Root Cause

Python's int(hex_part, 16) accepts flexible integer literal syntax including:

  • Leading + or - signs
  • 0x prefix
  • _ digit separators (PEP 515)
  • Surrounding whitespace

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:

  • Leading +/- signs
  • 0x prefix
  • Underscore separators
  • Whitespace (leading/trailing/embedded)
  • Uppercase hex characters
  • Non-hex characters

Verification

$ python3 -m pytest tests/api/utils/test_api_keys.py tests/api/middleware/test_auth.py -v
32 passed in 3.68s

$ python3 -m pytest tests/api/ -v  
149 passed, 3 warnings in 5.65s

No regressions detected. The fix is backward compatible - all valid keys continue to work.

Base branch: main Branch: bugfix/autodev-2427-20260928163747868 Commit: c2f5b15 PR: https://github.com/MemTensor/MemOS/pull/2431 Assigned to: @syzsunshine219 Reviewers: @bittergreen

Memtensor-AI avatar Sep 28 '26 16:09 Memtensor-AI

This issue has been automatically marked as stale due to inactivity.

github-actions[bot] avatar Oct 30 '26 02:10 github-actions[bot]

This issue has been automatically closed due to inactivity.

github-actions[bot] avatar Nov 07 '26 02:11 github-actions[bot]