Skip to content

Commit fca8815

Browse files
authored
Add middleware to ban bytedance user agent (#1347)
* Add middleware to ban bytedance user agent Signed-off-by: Tushar Goel <[email protected]> * Change response type Signed-off-by: Tushar Goel <[email protected]> --------- Signed-off-by: Tushar Goel <[email protected]>
1 parent c94e7e9 commit fca8815

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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.http import HttpResponseNotFound
11+
from django.utils.deprecation import MiddlewareMixin
12+
13+
14+
class BanUserAgent(MiddlewareMixin):
15+
def process_request(self, request):
16+
user_agent = request.META.get("HTTP_USER_AGENT", None)
17+
if user_agent and "bytedance" in user_agent:
18+
return HttpResponseNotFound(404)

vulnerabilities/tests/test_api.py

+6
Original file line numberDiff line numberDiff line change
@@ -650,3 +650,9 @@ def test_with_invalid_cpes(self):
650650
content_type="application/json",
651651
).json()
652652
assert response == {"Error": "Invalid CPE: CVE-2022-2022"}
653+
654+
655+
class TesBanUserAgent(TestCase):
656+
def test_ban_request_with_bytedance_user_agent(self):
657+
response = self.client.get(f"/api/packages", format="json", HTTP_USER_AGENT="bytedance")
658+
assert 404 == response.status_code

vulnerablecode/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"django.contrib.auth.middleware.AuthenticationMiddleware",
9090
"django.contrib.messages.middleware.MessageMiddleware",
9191
"django.middleware.clickjacking.XFrameOptionsMiddleware",
92+
"vulnerabilities.middleware.ban_user_agent.BanUserAgent",
9293
)
9394

9495
ROOT_URLCONF = "vulnerablecode.urls"

0 commit comments

Comments
 (0)