File tree 3 files changed +25
-0
lines changed
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 HttpResponseForbidden
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 HttpResponseForbidden ("You are banned!" , content_type = "text/html" )
Original file line number Diff line number Diff line change @@ -650,3 +650,9 @@ def test_with_invalid_cpes(self):
650
650
content_type = "application/json" ,
651
651
).json ()
652
652
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 "banned" in str (response .content )
Original file line number Diff line number Diff line change 89
89
"django.contrib.auth.middleware.AuthenticationMiddleware" ,
90
90
"django.contrib.messages.middleware.MessageMiddleware" ,
91
91
"django.middleware.clickjacking.XFrameOptionsMiddleware" ,
92
+ "vulnerabilities.middleware.ban_user_agent.BanUserAgent" ,
92
93
)
93
94
94
95
ROOT_URLCONF = "vulnerablecode.urls"
You can’t perform that action at this time.
0 commit comments