Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Check a route limit under require_scope once per request: the ASR Voice Recorder and search limits, and the token and account budgets on those routes, were counted twice (#484)
Murtaza Nasirc7f5e6852f29
diff --git a/src/utils/token_auth.py b/src/utils/token_auth.pyindex 6672762d..f9d9cb85 100644--- a/src/utils/token_auth.py+++ b/src/utils/token_auth.py@@ -231,6 +231,12 @@ def require_scope(*scopes):def decorator(f):state = {'limiter': None, 'limited': None}+ # A route limit from a lazy rate_limit() decorator under this one+ # (src/api/api_v1.py). Its own wrapper would run a second limiter check+ # per request, and each check counts every limit of the route, so the+ # route limit joins this stack instead and the view runs unwrapped.+ route_limit = getattr(f, '_rate_limit', None)+ route_view = getattr(f, '__wrapped__', None) if route_limit else None@wraps(f)def wrapper(*args, **kwargs):@@ -243,13 +249,16 @@ def require_scope(*scopes):# against the account. The token budget is counted per# route, as before; the account budget is one shared bucket# per kind across all routes.+ target = f+ if route_view is not None:+ target = limiter.limit(route_limit, **getattr(f, '_rate_limit_options', {}))(route_view)per_token = limiter.limit(lambda: _RATE_LIMITS[category], key_func=token_rate_key,exempt_when=lambda: not is_token_request(),scope=lambda endpoint: f"token-{category}",error_message=lambda: (f"Rate limit reached for this token: "f"{_RATE_LIMITS[category]} ({category} requests)."),- )(f)+ )(target)state['limited'] = limiter.shared_limit(lambda: _USER_RATE_LIMITS[category], scope=f"user-{category}",key_func=user_rate_key,diff --git a/tests/test_api_token_scopes.py b/tests/test_api_token_scopes.pyindex 2aced6bf..bc2b8827 100644--- a/tests/test_api_token_scopes.py+++ b/tests/test_api_token_scopes.py@@ -269,10 +269,12 @@ def live_limiter():from flask_limiter import Limiterfrom flask_limiter.util import get_remote_addressimport src.app as app_module+ from src.api import api_v1from src.utils import token_auth as talive = Limiter(get_remote_address, storage_uri="memory://", enabled=True)live.init_app(Flask("limiter-host"))- with patch.object(app_module, "limiter", live), \+ # The route limits in api_v1 use the same limiter, as in the app.+ with patch.object(app_module, "limiter", live), patch.object(api_v1, "limiter", live), \patch.dict(ta._RATE_LIMITS, {"read": "3 per minute", "write": "2 per minute"}), \patch.dict(ta._USER_RATE_LIMITS, {"read": "5 per minute", "write": "4 per minute"}):yield live@@ -329,6 +331,50 @@ def test_session_traffic_is_not_held_by_token_budgets(world, live_limiter):assert {c.get("/api/v1/recordings").status_code for _ in range(8)} == {200}+def _codes(send, n):+ return [send().status_code for _ in range(n)]+++def test_the_asr_upload_limit_counts_each_request_once(live_limiter):+ """520 units a minute at 52 per request: ten uploads, not five."""+ c = app.test_client()++ def send():+ # A wrong secret is refused after the limit has counted the request.+ return c.post("/api/v1/integrations/asr-voice-recorder/upload", data={"secret": "wrong"},+ content_type="multipart/form-data", environ_base={"REMOTE_ADDR": "203.0.113.48"})+ assert _codes(send, 11) == [401] * 10 + [429]+++def test_the_search_limit_counts_each_request_once(world, live_limiter):+ from src.utils import token_auth as ta+ plain = world["token"](None)+ with patch.dict(ta._RATE_LIMITS, {"read": "100 per minute"}), \+ patch.dict(ta._USER_RATE_LIMITS, {"read": "100 per minute"}):+ with app.app_context():+ c = _Client(app, app.response_class)+ codes = _codes(lambda: c.get("/api/v1/search?q=x", headers=_h(plain)), 31)+ assert codes == [400] * 30 + [429] # API_SEARCH_RATE_LIMIT: 30 per minute++[email protected]("token_limit,account_limit,code", [+ ("4 per minute", "100 per minute", "token_rate_limit_exceeded"),+ ("100 per minute", "4 per minute", "account_rate_limit_exceeded"),+])+def test_token_and_account_budgets_count_once_on_a_route_with_its_own_limit(world, live_limiter, token_limit,+ account_limit, code):+ from src.utils import token_auth as ta+ plain = world["token"](None)+ with patch.dict(ta._RATE_LIMITS, {"read": token_limit}), \+ patch.dict(ta._USER_RATE_LIMITS, {"read": account_limit}):+ with app.app_context():+ c = _Client(app, app.response_class)+ codes = _codes(lambda: c.get("/api/v1/search?q=x", headers=_h(plain)), 4)+ refused = c.get("/api/v1/search?q=x", headers=_h(plain))+ assert codes == [400] * 4+ assert refused.status_code == 429 and refused.get_json()["code"] == code++# ------------------------------------------------------------ token capdef test_the_token_cap_refuses_another_active_token_until_one_is_revoked(world):