diff --git a/swh/web/api/throttling.py b/swh/web/api/throttling.py index c8337595..e27bb2a8 100644 --- a/swh/web/api/throttling.py +++ b/swh/web/api/throttling.py @@ -1,159 +1,162 @@ # Copyright (C) 2017-2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information from ipaddress import ( ip_address, ip_network, IPv4Network, IPv6Network ) from typing import Callable, List, TypeVar, Union from django.core.exceptions import ImproperlyConfigured import rest_framework from rest_framework.throttling import ScopedRateThrottle import sentry_sdk from swh.web.config import get_config APIView = TypeVar('APIView', bound='rest_framework.views.APIView') Request = rest_framework.request.Request class SwhWebRateThrottle(ScopedRateThrottle): """Custom request rate limiter for DRF enabling to exempt specific networks specified in swh-web configuration. Requests are grouped into scopes. It enables to apply different requests rate limiting based on the scope name but also the input HTTP request types. To associate a scope to requests, one must add a 'throttle_scope' attribute when using a class based view, or call the 'throttle_scope' decorator when using a function based view. By default, requests do not have an associated scope and are not rate limited. Rate limiting can also be configured according to the type of the input HTTP requests for fine grained tuning. For instance, the following YAML configuration section sets a rate of: - 1 per minute for POST requests - 60 per minute for other request types for the 'swh_api' scope while exempting those coming from the 127.0.0.0/8 ip network. .. code-block:: yaml throttling: scopes: swh_api: limiter_rate: default: 60/m POST: 1/m exempted_networks: - 127.0.0.0/8 """ scope = None def __init__(self): super().__init__() self.exempted_networks = None def get_exempted_networks(self, scope_name: str ) -> List[Union[IPv4Network, IPv6Network]]: if not self.exempted_networks: scopes = get_config()['throttling']['scopes'] scope = scopes.get(scope_name) if scope: networks = scope.get('exempted_networks') if networks: self.exempted_networks = [ip_network(network) for network in networks] return self.exempted_networks def allow_request(self, request: Request, view: APIView) -> bool: + # no throttling for staff users + if request.user.is_authenticated and request.user.is_staff: + return True # class based view case if not self.scope: default_scope = getattr(view, self.scope_attr, None) request_allowed = None if default_scope is not None: # check if there is a specific rate limiting associated # to the request type assert request.method is not None request_scope = f'{default_scope}_{request.method.lower()}' setattr(view, self.scope_attr, request_scope) try: request_allowed = super().allow_request(request, view) # use default rate limiting otherwise except ImproperlyConfigured as exc: sentry_sdk.capture_exception(exc) setattr(view, self.scope_attr, default_scope) if request_allowed is None: request_allowed = super().allow_request(request, view) # function based view case else: default_scope = self.scope # check if there is a specific rate limiting associated # to the request type self.scope = default_scope + '_' + request.method.lower() try: self.rate = self.get_rate() # use default rate limiting otherwise except ImproperlyConfigured as exc: sentry_sdk.capture_exception(exc) self.scope = default_scope self.rate = self.get_rate() self.num_requests, self.duration = self.parse_rate(self.rate) request_allowed = \ super(ScopedRateThrottle, self).allow_request(request, view) self.scope = default_scope exempted_networks = self.get_exempted_networks(default_scope) exempted_ip = False if exempted_networks: remote_address = ip_address(self.get_ident(request)) exempted_ip = any(remote_address in network for network in exempted_networks) request_allowed = exempted_ip or request_allowed # set throttling related data in the request metadata # in order for the ThrottlingHeadersMiddleware to # add X-RateLimit-* headers in the HTTP response if not exempted_ip and hasattr(self, 'history'): hit_count = len(self.history) request.META['RateLimit-Limit'] = self.num_requests request.META['RateLimit-Remaining'] = self.num_requests - hit_count wait = self.wait() if wait is not None: request.META['RateLimit-Reset'] = int(self.now + wait) return request_allowed def throttle_scope(scope: str) -> Callable[..., APIView]: """Decorator that allows the throttle scope of a DRF function based view to be set:: @api_view(['GET', ]) @throttle_scope('scope') def view(request): ... """ def decorator(func: APIView) -> APIView: SwhScopeRateThrottle = type( 'CustomScopeRateThrottle', (SwhWebRateThrottle,), {'scope': scope} ) func.throttle_classes = (SwhScopeRateThrottle, ) return func return decorator diff --git a/swh/web/tests/api/test_throttling.py b/swh/web/tests/api/test_throttling.py index f363289d..2d42ef14 100644 --- a/swh/web/tests/api/test_throttling.py +++ b/swh/web/tests/api/test_throttling.py @@ -1,148 +1,191 @@ -# Copyright (C) 2017-2019 The Software Heritage developers +# Copyright (C) 2017-2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information -from swh.web.settings.tests import ( - scope1_limiter_rate, scope1_limiter_rate_post, - scope2_limiter_rate, scope2_limiter_rate_post, - scope3_limiter_rate, scope3_limiter_rate_post -) +import pytest from django.conf.urls import url +from django.contrib.auth.models import User from django.test.utils import override_settings from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.decorators import api_view - from swh.web.api.throttling import SwhWebRateThrottle, throttle_scope +from swh.web.settings.tests import ( + scope1_limiter_rate, scope1_limiter_rate_post, + scope2_limiter_rate, scope2_limiter_rate_post, + scope3_limiter_rate, scope3_limiter_rate_post +) class MockViewScope1(APIView): throttle_classes = (SwhWebRateThrottle,) throttle_scope = 'scope1' def get(self, request): return Response('foo_get') def post(self, request): return Response('foo_post') @api_view(['GET', 'POST']) @throttle_scope('scope2') def mock_view_scope2(request): if request.method == 'GET': return Response('bar_get') elif request.method == 'POST': return Response('bar_post') class MockViewScope3(APIView): throttle_classes = (SwhWebRateThrottle,) throttle_scope = 'scope3' def get(self, request): return Response('foo_get') def post(self, request): return Response('foo_post') @api_view(['GET', 'POST']) @throttle_scope('scope3') def mock_view_scope3(request): if request.method == 'GET': return Response('bar_get') elif request.method == 'POST': return Response('bar_post') urlpatterns = [ url(r'^scope1_class$', MockViewScope1.as_view()), url(r'^scope2_func$', mock_view_scope2), url(r'^scope3_class$', MockViewScope3.as_view()), url(r'^scope3_func$', mock_view_scope3) ] def check_response(response, status_code, limit=None, remaining=None): assert response.status_code == status_code if limit is not None: assert response['X-RateLimit-Limit'] == str(limit) else: assert 'X-RateLimit-Limit' not in response if remaining is not None: assert response['X-RateLimit-Remaining'] == str(remaining) else: assert 'X-RateLimit-Remaining' not in response @override_settings(ROOT_URLCONF=__name__) def test_scope1_requests_are_throttled(api_client): """ Ensure request rate is limited in scope1 """ for i in range(scope1_limiter_rate): response = api_client.get('/scope1_class') check_response(response, 200, scope1_limiter_rate, scope1_limiter_rate - i - 1) response = api_client.get('/scope1_class') check_response(response, 429, scope1_limiter_rate, 0) for i in range(scope1_limiter_rate_post): response = api_client.post('/scope1_class') check_response(response, 200, scope1_limiter_rate_post, scope1_limiter_rate_post - i - 1) response = api_client.post('/scope1_class') check_response(response, 429, scope1_limiter_rate_post, 0) @override_settings(ROOT_URLCONF=__name__) def test_scope2_requests_are_throttled(api_client): """ Ensure request rate is limited in scope2 """ for i in range(scope2_limiter_rate): response = api_client.get('/scope2_func') check_response(response, 200, scope2_limiter_rate, scope2_limiter_rate - i - 1) response = api_client.get('/scope2_func') check_response(response, 429, scope2_limiter_rate, 0) for i in range(scope2_limiter_rate_post): response = api_client.post('/scope2_func') check_response(response, 200, scope2_limiter_rate_post, scope2_limiter_rate_post - i - 1) response = api_client.post('/scope2_func') check_response(response, 429, scope2_limiter_rate_post, 0) @override_settings(ROOT_URLCONF=__name__) def test_scope3_requests_are_throttled_exempted(api_client): """ Ensure request rate is not limited in scope3 as requests coming from localhost are exempted from rate limit. """ for _ in range(scope3_limiter_rate+1): response = api_client.get('/scope3_class') check_response(response, 200) for _ in range(scope3_limiter_rate_post+1): response = api_client.post('/scope3_class') check_response(response, 200) for _ in range(scope3_limiter_rate+1): response = api_client.get('/scope3_func') check_response(response, 200) for _ in range(scope3_limiter_rate_post+1): response = api_client.post('/scope3_func') check_response(response, 200) + + +@override_settings(ROOT_URLCONF=__name__) +@pytest.mark.django_db +def test_staff_users_are_not_rate_limited(api_client): + staff_user = User.objects.create_user( + username='johndoe', password='', is_staff=True) + + api_client.force_login(staff_user) + + for _ in range(scope2_limiter_rate+1): + response = api_client.get('/scope2_func') + check_response(response, 200) + + for _ in range(scope2_limiter_rate_post+1): + response = api_client.post('/scope2_func') + check_response(response, 200) + + +@override_settings(ROOT_URLCONF=__name__) +@pytest.mark.django_db +def test_non_staff_users_are_rate_limited(api_client): + user = User.objects.create_user( + username='johndoe', password='', is_staff=False) + + api_client.force_login(user) + + for i in range(scope2_limiter_rate): + response = api_client.get('/scope2_func') + check_response(response, 200, scope2_limiter_rate, + scope2_limiter_rate - i - 1) + + response = api_client.get('/scope2_func') + check_response(response, 429, scope2_limiter_rate, 0) + + for i in range(scope2_limiter_rate_post): + response = api_client.post('/scope2_func') + check_response(response, 200, scope2_limiter_rate_post, + scope2_limiter_rate_post - i - 1) + + response = api_client.post('/scope2_func') + check_response(response, 429, scope2_limiter_rate_post, 0)