From f080c2df1fcf1563d6bb1575ddd642965699dac7 Mon Sep 17 00:00:00 2001 From: MemOS AutoDev Date: Tue, 29 Sep 2026 00:46:06 +0800 Subject: [PATCH 1/2] fix: strip Bearer prefix from Authorization header for rate limit key extraction - Rate limiter middleware now correctly extracts API keys from 'Authorization: Bearer krlk_...' headers - Added case-insensitive Bearer scheme stripping before checking for krlk_ prefix - Previously all Bearer token requests fell through to IP-based limiting - Added 7 comprehensive tests covering Bearer tokens, lowercase bearer, direct keys, and IP fallback - All 120 API tests passing, no regressions Fixes #2428 --- src/memos/api/middleware/rate_limit.py | 6 + tests/api/test_rate_limit_middleware.py | 185 ++++++++++++++++++++++++ 2 files changed, 191 insertions(+) create mode 100644 tests/api/test_rate_limit_middleware.py diff --git a/src/memos/api/middleware/rate_limit.py b/src/memos/api/middleware/rate_limit.py index c547378ca..dafc3155a 100644 --- a/src/memos/api/middleware/rate_limit.py +++ b/src/memos/api/middleware/rate_limit.py @@ -59,6 +59,12 @@ def _get_client_key(request: Request) -> str: """ # Try to get API key from header auth_header = request.headers.get("Authorization", "") + + # Strip Bearer scheme if present (case-insensitive) + # Standard format is "Bearer " but also support direct key + if auth_header.lower().startswith("bearer "): + auth_header = auth_header[7:].strip() + if auth_header.startswith("krlk_"): # Use first 20 chars of key as identifier return f"ratelimit:key:{auth_header[:20]}" diff --git a/tests/api/test_rate_limit_middleware.py b/tests/api/test_rate_limit_middleware.py new file mode 100644 index 000000000..b1fc18b9e --- /dev/null +++ b/tests/api/test_rate_limit_middleware.py @@ -0,0 +1,185 @@ +""" +Tests for rate limit middleware Bearer token handling. + +Verifies that the middleware correctly extracts API keys from +Authorization: Bearer headers for per-key rate limiting. +""" + +from starlette.applications import Starlette +from starlette.requests import Request +from starlette.responses import PlainTextResponse +from starlette.routing import Route +from starlette.testclient import TestClient + +from memos.api.middleware.rate_limit import ( + RateLimitMiddleware, + _get_client_key, +) + + +def test_get_client_key_with_bearer_token(): + """Test that Bearer token is correctly extracted for rate limiting.""" + # Create a mock request with Bearer token + app = Starlette() + + # Simulate a request with Bearer token + with TestClient(app): + # Create request manually with Bearer header + scope = { + "type": "http", + "method": "GET", + "path": "/test", + "headers": [ + (b"authorization", b"Bearer krlk_test_key_12345678"), + ], + "client": ("127.0.0.1", 8000), + } + request = Request(scope) + + key = _get_client_key(request) + + # Should extract the key and use it for rate limiting + assert key == "ratelimit:key:krlk_test_key_123456" + assert key.startswith("ratelimit:key:") + + +def test_get_client_key_with_lowercase_bearer(): + """Test that lowercase 'bearer' scheme is also recognized.""" + app = Starlette() + + with TestClient(app): + scope = { + "type": "http", + "method": "GET", + "path": "/test", + "headers": [ + (b"authorization", b"bearer krlk_another_key_abc"), + ], + "client": ("127.0.0.1", 8000), + } + request = Request(scope) + + key = _get_client_key(request) + + assert key == "ratelimit:key:krlk_another_key_abc" + assert key.startswith("ratelimit:key:") + + +def test_get_client_key_with_direct_key(): + """Test backward compatibility with direct key (no Bearer prefix).""" + app = Starlette() + + with TestClient(app): + scope = { + "type": "http", + "method": "GET", + "path": "/test", + "headers": [ + (b"authorization", b"krlk_direct_key_xyz"), + ], + "client": ("127.0.0.1", 8000), + } + request = Request(scope) + + key = _get_client_key(request) + + # Should still work for direct keys + assert key == "ratelimit:key:krlk_direct_key_xyz" + + +def test_get_client_key_fallback_to_ip(): + """Test that non-keyed requests fall back to IP-based limiting.""" + app = Starlette() + + with TestClient(app): + scope = { + "type": "http", + "method": "GET", + "path": "/test", + "headers": [], + "client": ("192.168.1.100", 8000), + } + request = Request(scope) + + key = _get_client_key(request) + + assert key == "ratelimit:ip:192.168.1.100" + + +def test_get_client_key_with_x_forwarded_for(): + """Test that X-Forwarded-For is used when present.""" + app = Starlette() + + with TestClient(app): + scope = { + "type": "http", + "method": "GET", + "path": "/test", + "headers": [ + (b"x-forwarded-for", b"203.0.113.42, 10.0.0.1"), + ], + "client": ("10.0.0.2", 8000), + } + request = Request(scope) + + key = _get_client_key(request) + + # Should use the first IP from X-Forwarded-For + assert key == "ratelimit:ip:203.0.113.42" + + +def test_rate_limit_different_keys_separate_buckets(): + """ + Integration test: verify that different API keys get separate rate limit buckets. + """ + + # Create a simple test app + async def hello(request): + return PlainTextResponse("ok") + + app = Starlette(routes=[Route("/test", hello)]) + app.add_middleware(RateLimitMiddleware) + + client = TestClient(app) + + # Make requests with two different keys + response1 = client.get("/test", headers={"Authorization": "Bearer krlk_key1_xxx"}) + response2 = client.get("/test", headers={"Authorization": "Bearer krlk_key2_yyy"}) + + # Both should succeed (different buckets) + assert response1.status_code == 200 + assert response2.status_code == 200 + + # Both should have rate limit headers + assert "X-RateLimit-Limit" in response1.headers + assert "X-RateLimit-Limit" in response2.headers + + +def test_rate_limit_bearer_token_not_treated_as_ip(): + """ + Regression test: verify Bearer tokens are not treated as IP addresses. + + Before the fix, 'Authorization: Bearer krlk_...' would fail the startswith check + and fall through to IP-based limiting, causing all keyed clients behind a shared + NAT to share one rate limit bucket. + """ + app = Starlette() + + with TestClient(app): + scope = { + "type": "http", + "method": "GET", + "path": "/test", + "headers": [ + (b"authorization", b"Bearer krlk_test_key_12345678"), + ], + "client": ("10.0.0.1", 8000), + } + request = Request(scope) + + key = _get_client_key(request) + + # Should NOT be an IP-based key + assert not key.startswith("ratelimit:ip:") + # Should be a key-based bucket + assert key.startswith("ratelimit:key:") From d89793203b5f4c8e7ab70d8e93f579cef5a95d4b Mon Sep 17 00:00:00 2001 From: AutoDev Date: Tue, 29 Sep 2026 00:58:16 +0800 Subject: [PATCH 2/2] test(rate_limit): add explicit truncation comment to Bearer key assertion The input token 'krlk_test_key_12345678' is 22 chars; [:20] produces 'krlk_test_key_123456'. The assertion was correct but silent about why the expected value differs from the input. Add a comment explaining the truncation intent so future readers don't mistake it for a typo. Addresses OCR finding: tests/api/test_rate_limit_middleware.py L41-43 --- tests/api/test_rate_limit_middleware.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/api/test_rate_limit_middleware.py b/tests/api/test_rate_limit_middleware.py index b1fc18b9e..c192ece85 100644 --- a/tests/api/test_rate_limit_middleware.py +++ b/tests/api/test_rate_limit_middleware.py @@ -38,7 +38,9 @@ def test_get_client_key_with_bearer_token(): key = _get_client_key(request) - # Should extract the key and use it for rate limiting + # The raw token is "krlk_test_key_12345678" (22 chars). + # _get_client_key takes [:20] → "krlk_test_key_123456" (20 chars), + # keeping Redis keys bounded while preserving per-key isolation. assert key == "ratelimit:key:krlk_test_key_123456" assert key.startswith("ratelimit:key:")