Skip to content

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

Closed as not planned

Description

@simpleqt

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.

Activity

  1. added
    ai:taskDispatched to AI coding agent | 已派发给 AI 编码任务
    area:api云服务 / FastAPI / OpenAPI / MCP
    status:in-progressSomeone or AI is working on it | 人工或 AI 正在处理
    types:bugSomething isn't working | 功能异常
    on Sep 28, 2026
  2. self-assigned this
    on Sep 28, 2026
  3. Memtensor-AI commented on Sep 28, 2026

    @Memtensor-AI
    Collaborator

    🤖 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.

  4. added a commit that references this issue on Sep 28, 2026
    c2f5b15
  5. Memtensor-AI commented on Sep 28, 2026

    @Memtensor-AI
    Collaborator

    ✅ 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: #2431
    Assigned to: @syzsunshine219
    Reviewers: @bittergreen

  6. added
    ai:testingAI agent is running tests | AI 正在运行测试
    ai:pr-readyAI-created PR is ready for review | AI 生成的 PR 已等待评审
    and removed
    ai:codingAI agent is coding | AI 正在编码
    ai:testingAI agent is running tests | AI 正在运行测试
    ai:taskDispatched to AI coding agent | 已派发给 AI 编码任务
    on Sep 28, 2026
  7. github-actions commented on Oct 30, 2026

    @github-actions
    Contributor

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

  8. added
    status:staleAuto-marked stale after 30d inactivity; closes 7d later unless exempt.
    on Oct 30, 2026
  9. github-actions commented on Nov 7, 2026

    @github-actions
    Contributor

    This issue has been automatically closed due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

ai:pr-readyAI-created PR is ready for review | AI 生成的 PR 已等待评审area:api云服务 / FastAPI / OpenAPI / MCPstatus:in-progressSomeone or AI is working on it | 人工或 AI 正在处理status:staleAuto-marked stale after 30d inactivity; closes 7d later unless exempt.types:bugSomething isn't working | 功能异常

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions