Skip to content

Commit cf6820a

Browse files
committed
Fixed security
1 parent 1a85f60 commit cf6820a

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

server/src/uds/core/util/middleware/security.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from django.http import HttpResponse
3535

3636
from uds.core.util.config import GlobalConfig
37+
from uds.core.auths.auth import isTrustedSource
3738

3839
if typing.TYPE_CHECKING:
3940
from django.http import HttpRequest
@@ -56,8 +57,18 @@ def __init__(
5657

5758
def __call__(self, request: 'HttpRequest') -> 'HttpResponse':
5859
# If bot, break now
59-
ua = request.META.get('HTTP_USER_AGENT', 'Connection Maybe a bot. No user agent detected.')
60-
if bot.search(ua):
60+
ua = request.META.get(
61+
'HTTP_USER_AGENT', 'Connection Maybe a bot. No user agent detected.'
62+
)
63+
# Simple ip check, to allow "trusted" ips to access UDS
64+
ip = (
65+
request.META.get(
66+
'REMOTE_ADDR',
67+
request.META.get('HTTP_X_FORWARDED_FOR', '').split(",")[-1],
68+
)
69+
or '0.0.0.0'
70+
)
71+
if not isTrustedSource(ip) and bot.search(ua):
6172
# Return emty response if bot is detected
6273
logger.info(
6374
'Denied Bot %s from %s to %s',
@@ -71,10 +82,13 @@ def __call__(self, request: 'HttpRequest') -> 'HttpResponse':
7182
return HttpResponse(content='Forbbiden', status=403)
7283

7384
response = self.get_response(request)
74-
85+
7586
if GlobalConfig.ENHANCED_SECURITY.getBool():
7687
# Legacy browser support for X-XSS-Protection
7788
response.headers.setdefault('X-XSS-Protection', '1; mode=block')
7889
# Add Content-Security-Policy, see https://www.owasp.org/index.php/Content_Security_Policy
79-
response.headers.setdefault('Content-Security-Policy', "default-src 'self' 'unsafe-inline' 'unsafe-eval' uds: udss:; img-src 'self' https: data:;")
90+
response.headers.setdefault(
91+
'Content-Security-Policy',
92+
"default-src 'self' 'unsafe-inline' 'unsafe-eval' uds: udss:; img-src 'self' https: data:;",
93+
)
8094
return response

0 commit comments

Comments
 (0)