Skip to content

Rate limiter: strip the Bearer scheme so per-key limiting actually engages - #2430

Closed
simpleqt wants to merge 1 commit into
MemTensor:mainfrom
simpleqt:sq/rate-limit-bearer-key
Closed

simpleqt wants to merge 1 commit into
MemTensor:mainfrom
simpleqt:sq/rate-limit-bearer-key

Conversation

@simpleqt

Copy link
Copy Markdown

Fixes #2428

Summary

_get_client_key in src/memos/api/middleware/rate_limit.py keys the rate-limit bucket by API key when the Authorization header starts with krlk_:

auth_header = request.headers.get("Authorization", "")
if auth_header.startswith("krlk_"):
    return f"ratelimit:key:{auth_header[:20]}"

Real requests send Authorization: Bearer krlk_..., so the header never starts with krlk_ and this branch is unreachable — every request, including ones carrying a valid API key, falls into the shared IP bucket (a single egress IP behind a proxy therefore shares one bucket across all keyed clients).

Fix

Strip the Bearer scheme before the prefix check; a raw krlk_... header keeps working unchanged:

scheme, _, credentials = auth_header.partition(" ")
key_part = credentials.strip() if scheme.lower() == "bearer" else auth_header
if key_part.startswith("krlk_"):
    return f"ratelimit:key:{key_part[:20]}"

Regression test

Added tests/api/test_rate_limit_key.py:

  • red/green verified: the Bearer krlk_... case fails against the old implementation (falls back to the IP key) and passes with the fix
  • covers lowercase bearer, the legacy raw-key header, non-key tokens, and the missing-header fallback

ruff check (repo-pinned 0.11.8) passes on both touched files.

AI Disclosure

  • Tool(s): Claude (ZCode CLI)
  • Used for: debugging assistance, code suggestions, and drafting this PR description. All changes human-reviewed.

_get_client_key checked that the Authorization header starts with
krlk_, but real requests send 'Bearer krlk_...', so the per-key
branch was unreachable and every request fell into the shared
IP bucket. Strip the scheme before the prefix check while keeping
raw-key headers working.

Fixes MemTensor#2428
Copilot AI lite review requested due to automatic review settings September 28, 2026 16:43
@Memtensor-AI Memtensor-AI added area:api 云服务 / FastAPI / OpenAPI / MCP status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Sep 28, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Memtensor-AI

Copy link
Copy Markdown
Collaborator

🤖 Open Code Review

Target: PR #2430
Task: 6bd078db5da45e3d
Base: main
Head: sq/rate-limit-bearer-key

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


1. tests/api/test_rate_limit_key.py (L33-L40)

The stubs injected into sys.modules are never removed. Because the fixture scope is module, these entries survive for the entire test-session lifetime. If any other test module later imports (or has already imported) the real memos, memos.log, memos.api, or memos.api.middleware packages, it may get the stub instead, causing hard-to-diagnose failures that depend on test execution order.

Consider yielding from the fixture and cleaning up afterward:

@pytest.fixture(scope="module")
def rate_limit_module():
    _install_memos_package_stub()
    ...
    mod = importlib.import_module("memos.api.middleware.rate_limit")
    yield mod
    # Teardown: remove the stubs we installed
    for key in list(sys.modules):
        if key.startswith("memos"):
            sys.modules.pop(key, None)

If the broader test suite never imports the real memos package, this risk is low; if it does, the cleanup is necessary to prevent cross-module interference.

💡 Suggested Change

Before:

@pytest.fixture(scope="module")
def rate_limit_module():
    _install_memos_package_stub()
    middleware_pkg = types.ModuleType("memos.api.middleware")
    middleware_pkg.__path__ = [str(SRC_DIR / "api" / "middleware")]
    sys.modules["memos.api.middleware"] = middleware_pkg
    sys.modules["memos"].api.middleware = middleware_pkg
    return importlib.import_module("memos.api.middleware.rate_limit")

After:

@pytest.fixture(scope="module")
def rate_limit_module():
    _install_memos_package_stub()
    middleware_pkg = types.ModuleType("memos.api.middleware")
    middleware_pkg.__path__ = [str(SRC_DIR / "api" / "middleware")]
    sys.modules["memos.api.middleware"] = middleware_pkg
    sys.modules["memos"].api.middleware = middleware_pkg
    mod = importlib.import_module("memos.api.middleware.rate_limit")
    yield mod
    # Remove all stub entries so they don't pollute other test modules
    for key in list(sys.modules):
        if key.startswith("memos"):
            sys.modules.pop(key, None)

Generated by cloud-assistant via Open Code Review.

@Memtensor-AI

Copy link
Copy Markdown
Collaborator

✅ Automated Test Results: PASSED

All tests passed (5/5 executed). memos_python_core/changed-repo-python: 5/5. Duration: 1s [advisory, non-gating] AI-generated tests on branch test/auto-gen-6bd078db5da45e3d-20260929005034: 20/20 passed — these do NOT affect the PR verdict; review the branch manually.

Branch: sq/rate-limit-bearer-key

@Memtensor-AI Memtensor-AI added status:ready Ready for implementation; waiting for assignee or AI dispatch | 可进入实现,等待认领或派发 and removed status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Sep 28, 2026
@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

area:api 云服务 / FastAPI / OpenAPI / MCP status:ready Ready for implementation; waiting for assignee or AI dispatch | 可进入实现,等待认领或派发 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.

Rate limiter never recognizes Bearer API keys, so per-key limiting is dead code

4 participants