Skip to content

Fix #2427: validate_key_format accepts non-canonical hex (signs, 0x, underscores, whitespac - #2431

Closed
Memtensor-AI wants to merge 2 commits into
MemTensor:dev-v2.0.36from
Memtensor-AI:bugfix/autodev-2427-20260928163747868
Closed

Memtensor-AI wants to merge 2 commits into
MemTensor:dev-v2.0.36from
Memtensor-AI:bugfix/autodev-2427-20260928163747868

Conversation

@Memtensor-AI

Copy link
Copy Markdown
Collaborator

Description

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.

Related Issue (Required): Fixes #2427

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (does not change functionality, e.g. code style improvements, linting)
  • Documentation update

How Has This Been Tested?

Not run; documentation-only change.

  • Unit Test
  • Test Script Or Test Steps (please provide)
  • Pipeline Automated API Test (please provide)

Checklist

  • I have performed a self-review of my own code
  • I have commented my code in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works
  • I have created related documentation issue/PR in MemOS-Docs (if applicable)
  • I have linked the issue to this PR (if applicable)
  • I have mentioned the person who will review this PR

@bittergreen please review this PR.

Reviewer Checklist

Both validate_key_format implementations (middleware/auth.py and
utils/api_keys.py) now use character-by-character validation to reject
non-canonical hex forms that generated keys can never have:

- Leading +/- signs
- 0x prefix
- _ digit separators
- Whitespace (leading/trailing/embedded)
- Uppercase hex characters

The fix replaces int(hex_part, 16) with all(c in '0123456789abcdef' for c in hex_part)
to enforce exactly 64 lowercase hex characters [0-9a-f] matching generate_api_key() output.

Fixes MemTensor#2427
@Memtensor-AI Memtensor-AI added ai:generated Generated or modified by AI | 由 AI 生成或修改 area:api 云服务 / FastAPI / OpenAPI / MCP status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Sep 28, 2026
@Memtensor-AI

Memtensor-AI commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator Author

🤖 Open Code Review

Target: PR #2431
Task: b2bf2e63b2be4a47
Base: dev-v2.0.36
Head: bugfix/autodev-2427-20260928163747868
Head SHA: ad733f6dd195b52ec9c708cedaa9270cde39c60d

🔍 OpenCodeReview found 3 issue(s) in this PR.


1. tests/api/utils/test_api_keys.py (L55-L59)

The comment 'This currently passes but should fail' is now outdated. The implementation in api_keys.py was updated to use all(c in "0123456789abcdef" for c in hex_part), which strictly rejects + characters. This test will pass as written, so the comment misrepresents the current state and should be removed.

💡 Suggested Change

Before:

    def test_reject_leading_plus_sign(self):
        """Keys with leading + should be rejected."""
        # This currently passes but should fail
        invalid_key = "krlk_" + "+" + "a" * 63
        assert not validate_key_format(invalid_key), "Should reject leading +"

After:

    def test_reject_leading_plus_sign(self):
        """Keys with leading + should be rejected."""
        invalid_key = "krlk_" + "+" + "a" * 63
        assert not validate_key_format(invalid_key), "Should reject leading +"

2. tests/api/utils/test_api_keys.py (L66-L70)

Same issue: the comment 'This currently passes but should fail' no longer applies. The strict character validation now correctly rejects x (not in [0-9a-f]), so 0x prefix is rejected. Remove the stale comment.

💡 Suggested Change

Before:

    def test_reject_0x_prefix(self):
        """Keys with 0x prefix should be rejected."""
        # This currently passes but should fail
        invalid_key = "krlk_" + "0x" + "a" * 62
        assert not validate_key_format(invalid_key), "Should reject 0x prefix"

After:

    def test_reject_0x_prefix(self):
        """Keys with 0x prefix should be rejected."""
        invalid_key = "krlk_" + "0x" + "a" * 62
        assert not validate_key_format(invalid_key), "Should reject 0x prefix"

3. tests/api/utils/test_api_keys.py (L72-L75)

Same issue: the comment 'This currently passes but should fail' is stale. The strict validation rejects _ since it is not in [0-9a-f]. Remove the comment.

💡 Suggested Change

Before:

    def test_reject_underscores(self):
        """Keys with underscores in hex should be rejected."""
        # This currently passes but should fail
        invalid_key = "krlk_" + "a" * 30 + "_" + "b" * 33

After:

    def test_reject_underscores(self):
        """Keys with underscores in hex should be rejected."""
        invalid_key = "krlk_" + "a" * 30 + "_" + "b" * 33

Generated by cloud-assistant via Open Code Review.

@Memtensor-AI

Copy link
Copy Markdown
Collaborator Author

🔧 Open Code Review requested Agent fix

Open Code Review found 4 issue(s). I have resumed the development Agent to fix them.

  • Task: b2bf2e63b2be4a47
  • Fix attempt: 1/2
  • Finding delta: 0 repeated / 4 new / 0 likely resolved

The Agent will push a new commit to this PR branch. OCR will recheck after the commit is pushed.

…omments

- Use module-level `_LOWERCASE_HEX = frozenset("0123456789abcdef")` in
  `validate_key_format` so hex membership tests are O(1) instead of
  scanning a string literal. The intent (fixed set of allowed characters)
  is also clearer than the string literal.
- Drop the "currently passes but should fail" comments in
  `test_reject_leading_plus_sign`, `test_reject_0x_prefix`, and
  `test_reject_underscores`. Those comments described the pre-fix
  behavior; with strict character validation in place the tests now
  pass and the comments were misleading.

Addresses PR MemTensor#2431 open-code-review findings.
@Memtensor-AI

Copy link
Copy Markdown
Collaborator Author

✅ Automated Test Results: PASSED

All tests passed (33/33 executed). memos_github_open_source/smoke: 1/1, memos_python_core/changed-repo-python: 32/32. Duration: 10s [advisory, non-gating] AI-generated tests on branch test/auto-gen-b2bf2e63b2be4a47-20260929010131: 119/149 passed, 30 failed — these do NOT affect the PR verdict; review the branch manually.

Branch: bugfix/autodev-2427-20260928163747868

@github-actions

Copy link
Copy Markdown
Contributor

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

@github-actions github-actions Bot added the status:stale Auto-marked stale after 30d inactivity; closes 7d later unless exempt. label Oct 29, 2026
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

This PR has been automatically closed due to inactivity.

@github-actions github-actions Bot closed this Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai:generated Generated or modified by AI | 由 AI 生成或修改 area:api 云服务 / FastAPI / OpenAPI / MCP status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 status:stale Auto-marked stale after 30d inactivity; closes 7d later unless exempt.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants