Skip to content

Middleware: use regular HttpResponse and log the suspicious operation #9366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions readthedocs/core/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
MiddlewareNotUsed,
SuspiciousOperation,
)
from django.http import HttpResponse
from django.utils.cache import patch_vary_headers
from django.utils.http import http_date

Expand Down Expand Up @@ -211,7 +212,14 @@ def __init__(self, get_response):
def __call__(self, request):
for key, value in request.GET.items():
if "\x00" in value:
raise SuspiciousOperation(
f"There are NULL (0x00) characters in at least one of the parameters ({key}) passed to the request." # noqa
log.info(
"NULL (0x00) characters in GET attributes.",
attribute=key,
value=value,
url=request.build_absolute_uri(),
)
return HttpResponse(
f"There are NULL (0x00) characters in at least one of the parameters ({key}) passed to the request.", # noqa
status=400,
)
return self.get_response(request)
8 changes: 6 additions & 2 deletions readthedocs/rtd_tests/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from corsheaders.middleware import CorsMiddleware
from django.conf import settings
from django.core.exceptions import SuspiciousOperation
from django.http import HttpResponse
from django.test import TestCase, override_settings
from django.test.client import RequestFactory
Expand Down Expand Up @@ -272,4 +271,9 @@ def setUp(self):

def test_request_with_null_chars(self):
request = self.factory.get("/?language=en\x00es&project_slug=myproject")
self.assertRaises(SuspiciousOperation, lambda: self.middleware(request))
response = self.middleware(request)
self.assertContains(
response,
"There are NULL (0x00) characters in at least one of the parameters (language) passed to the request.",
status_code=400,
)