Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Register the require_scope limits once per limiter under a lock, with callables built at decoration time, so concurrent first requests cannot double every count (#484)
Murtaza Nasir1b1d50ae57ac
diff --git a/src/utils/token_auth.py b/src/utils/token_auth.pyindex f9d9cb85..ab89f1d0 100644--- a/src/utils/token_auth.py+++ b/src/utils/token_auth.py@@ -7,6 +7,7 @@ allowing users to authenticate with Bearer tokens instead of session cookies.import hashlibimport os+import threadingfrom datetime import datetimefrom functools import wrapsfrom flask import request, g, has_request_context@@ -229,8 +230,32 @@ def require_scope(*scopes):required = frozenset(scopes)category = _rate_category(set(scopes))+ # Built once, so a second registration (another limiter in tests) yields+ # limits equal to the first and Flask-Limiter keeps one of each.+ def token_limit():+ return _RATE_LIMITS[category]++ def user_limit():+ return _USER_RATE_LIMITS[category]++ def is_exempt():+ return not is_token_request()++ def token_scope(endpoint):+ return f"token-{category}"++ def token_message():+ return f"Rate limit reached for this token: {_RATE_LIMITS[category]} ({category} requests)."++ def user_message():+ return (f"Rate limit reached for your account: {_USER_RATE_LIMITS[category]} "+ f"({category} requests), shared by all your API tokens.")+def decorator(f):state = {'limiter': None, 'limited': None}+ # gthread workers serve the first requests of a route at the same time;+ # registering the limits twice would count every request twice.+ lock = threading.Lock()# 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@@ -243,36 +268,33 @@ def require_scope(*scopes):from src.app import limiterif limiter is not None and getattr(limiter, 'enabled', True):if state['limiter'] is not limiter:- # Both limits register under the view's name, so one check- # evaluates the two. With equal windows the token limit is- # checked first, and a request it refuses is not counted- # 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)."),- )(target)- state['limited'] = limiter.shared_limit(- lambda: _USER_RATE_LIMITS[category], scope=f"user-{category}",- key_func=user_rate_key,- exempt_when=lambda: not is_token_request(),- override_defaults=False,- error_message=lambda: (f"Rate limit reached for your account: "- f"{_USER_RATE_LIMITS[category]} ({category} requests), "- f"shared by all your API tokens."),- )(per_token)- state['limiter'] = limiter+ with lock:+ if state['limiter'] is not limiter:+ state['limited'] = _register(limiter)+ state['limiter'] = limiterreturn state['limited'](*args, **kwargs)return f(*args, **kwargs)+ def _register(limiter):+ # Both limits register under the view's name, so one check+ # evaluates the two. With equal windows the token limit is checked+ # first, and a request it refuses is not counted 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(+ token_limit, key_func=token_rate_key, exempt_when=is_exempt,+ scope=token_scope, error_message=token_message,+ )(target)+ return limiter.shared_limit(+ user_limit, scope=f"user-{category}", key_func=user_rate_key,+ exempt_when=is_exempt, override_defaults=False, error_message=user_message,+ )(per_token)+wrapper._required_scopes = required+ wrapper._limit_state = statereturn wrapperreturn decoratordiff --git a/tests/test_api_token_scopes.py b/tests/test_api_token_scopes.pyindex bc2b8827..e5087c87 100644--- a/tests/test_api_token_scopes.py+++ b/tests/test_api_token_scopes.py@@ -375,6 +375,41 @@ def test_token_and_account_budgets_count_once_on_a_route_with_its_own_limit(worlassert refused.status_code == 429 and refused.get_json()["code"] == code+def test_registering_the_limits_again_still_counts_each_request_once(world, live_limiter):+ """A second registration on the same limiter (two threads that both saw the+ route unregistered) must not add a second copy of each limit."""+ plain = world["token"](None)+ view = app.view_functions["api_v1.list_recordings"]+ with app.app_context():+ c = _Client(app, app.response_class)+ first = _read(c, plain)+ view._limit_state["limiter"] = None # as the thread that lost the race sees it+ rest = [_read(c, plain).status_code for _ in range(3)]+ assert [first.status_code] + rest == [200, 200, 200, 429] # token budget 3+++def test_concurrent_first_requests_register_the_limits_once(world, live_limiter):+ import threading+ plain = world["token"](None)+ barrier = threading.Barrier(6)+ codes = []++ def first_request():+ with app.app_context():+ c = _Client(app, app.response_class)+ barrier.wait()+ codes.append(_read(c, plain, "/api/v1/tags").status_code)++ threads = [threading.Thread(target=first_request) for _ in range(6)]+ for t in threads:+ t.start()+ for t in threads:+ t.join()+ # Token budget 3 on this route: three pass, three are refused, whatever+ # the interleaving; a doubled registration would let only one through.+ assert sorted(codes) == [200, 200, 200, 429, 429, 429]++# ------------------------------------------------------------ token capdef test_the_token_cap_refuses_another_active_token_until_one_is_revoked(world):