File tree Expand file tree Collapse file tree 3 files changed +25
-0
lines changed Expand file tree Collapse file tree 3 files changed +25
-0
lines changed Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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
9495ROOT_URLCONF = "vulnerablecode.urls"
You can’t perform that action at this time.
0 commit comments