Skip to content

Commit 6f72ecf

Browse files
authored
Merge pull request #988 from TG1999/throttling
Enable throttling
2 parents 0475246 + 11d6bd0 commit 6f72ecf

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ Release notes
33

44

55

6+
Version v30.2.2
7+
----------------
8+
9+
- We enabled API throttling for a basic user and for a staff user
10+
they can have unlimited access on API.
11+
12+
613
Version v30.2.1
714
----------------
815

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# VulnerableCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/nexB/vulnerablecode for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
from django.contrib.auth import get_user_model
11+
from rest_framework.test import APIClient
12+
from rest_framework.test import APITestCase
13+
14+
User = get_user_model()
15+
16+
17+
class ThrottleApiTests(APITestCase):
18+
def setUp(self):
19+
# create a basic user
20+
self.user = User.objects.create_user("username", "[email protected]", "secret")
21+
self.auth = f"Token {self.user.auth_token.key}"
22+
self.csrf_client = APIClient(enforce_csrf_checks=True)
23+
self.csrf_client.credentials(HTTP_AUTHORIZATION=self.auth)
24+
25+
# create a staff user
26+
self.staff_user = User.objects.create_user(
27+
"staff", "[email protected]", "secret", is_staff=True
28+
)
29+
self.staff_auth = f"Token {self.staff_user.auth_token.key}"
30+
self.staff_csrf_client = APIClient(enforce_csrf_checks=True)
31+
self.staff_csrf_client.credentials(HTTP_AUTHORIZATION=self.staff_auth)
32+
33+
def test_api_throttling(self):
34+
35+
# A basic user can only access API 5 times a day
36+
for i in range(0, 5):
37+
response = self.csrf_client.get("/api/packages")
38+
self.assertEqual(response.status_code, 200)
39+
response = self.staff_csrf_client.get("/api/packages")
40+
self.assertEqual(response.status_code, 200)
41+
42+
response = self.csrf_client.get("/api/packages")
43+
# 429 - too many requests for basic user
44+
self.assertEqual(response.status_code, 429)
45+
46+
response = self.staff_csrf_client.get("/api/packages", format="json")
47+
# 200 - staff user can access API unlimited times
48+
self.assertEqual(response.status_code, 200)

vulnerabilities/throttling.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# VulnerableCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/nexB/vulnerablecode for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
from django.contrib.auth import get_user_model
11+
from rest_framework.throttling import UserRateThrottle
12+
13+
User = get_user_model()
14+
15+
16+
class StaffUserRateThrottle(UserRateThrottle):
17+
def allow_request(self, request, view):
18+
"""
19+
Do not apply throttling for superusers and admins.
20+
"""
21+
if request.user.is_superuser or request.user.is_staff:
22+
return True
23+
24+
return super().allow_request(request, view)

vulnerablecode/settings.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,12 @@
150150

151151
LOGIN_REDIRECT_URL = "/"
152152
LOGOUT_REDIRECT_URL = "/"
153+
THROTTLING_RATE = env.str("THROTTLING_RATE", default="1000/day")
153154

154155
if IS_TESTS:
155156
VULNERABLECODEIO_REQUIRE_AUTHENTICATION = True
157+
THROTTLING_RATE = "5/day"
158+
156159

157160
USE_L10N = True
158161

@@ -184,6 +187,10 @@
184187
"django_filters.rest_framework.DjangoFilterBackend",
185188
"rest_framework.filters.SearchFilter",
186189
),
190+
"DEFAULT_THROTTLE_CLASSES": [
191+
"vulnerabilities.throttling.StaffUserRateThrottle",
192+
],
193+
"DEFAULT_THROTTLE_RATES": {"user": THROTTLING_RATE},
187194
"DEFAULT_PAGINATION_CLASS": "vulnerabilities.pagination.SmallResultSetPagination",
188195
# Limit the load on the Database returning a small number of records by default. https://github.com/nexB/vulnerablecode/issues/819
189196
"PAGE_SIZE": 10,

0 commit comments

Comments
 (0)