-
Notifications
You must be signed in to change notification settings - Fork 557
fix(API): reject NUL bytes in query parameters instead of crashing with 500 #8204
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
Open
bardock-2393
wants to merge
2
commits into
Flagsmith:main
Choose a base branch
from
bardock-2393:fix/nul-byte-query-params
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+95
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| from collections.abc import Callable | ||
|
|
||
| from django.http import HttpRequest, HttpResponse, HttpResponseBadRequest | ||
|
|
||
|
|
||
| class RejectNulByteQueryParamsMiddleware: | ||
| """ | ||
| Reject requests whose query parameters contain a NUL (0x00) character. | ||
|
|
||
| Passing one through to a query against the string field of a Postgres | ||
| row raises an unhandled `ValueError: A string literal cannot contain | ||
| NUL (0x00) characters`, so reject it here, before any view can pass it | ||
| to the ORM. | ||
| """ | ||
|
|
||
| def __init__(self, get_response: Callable[[HttpRequest], HttpResponse]) -> None: | ||
| self.get_response = get_response | ||
|
|
||
| def __call__(self, request: HttpRequest) -> HttpResponse: | ||
| # `.values()` only yields the last value per key: a repeated key | ||
| # (`?a=x&a=y`) would let a NUL byte in an earlier value slip through. | ||
| # `.lists()` yields every value for every key. | ||
| if any( | ||
| "\x00" in value for _, values in request.GET.lists() for value in values | ||
| ): | ||
| return HttpResponseBadRequest( | ||
| "Query parameters must not contain NUL characters." | ||
| ) | ||
| return self.get_response(request) | ||
63 changes: 63 additions & 0 deletions
63
api/tests/unit/core/middleware/test_unit_core_middleware_query_params.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| from django.http import HttpResponse | ||
| from django.test import RequestFactory | ||
|
|
||
| from core.middleware.query_params import RejectNulByteQueryParamsMiddleware | ||
|
|
||
|
|
||
| def test_reject_nul_byte_query_params_middleware__nul_byte_in_query_param__returns_bad_request( # type: ignore[no-untyped-def] # noqa: E501 | ||
| mocker, rf: RequestFactory | ||
| ): | ||
| # Given | ||
| mocked_get_response = mocker.MagicMock() | ||
| request = rf.get( | ||
| "/api/v1/environments/some-key/identities/", {"identifier": "foo\x00bar"} | ||
| ) | ||
|
|
||
| middleware = RejectNulByteQueryParamsMiddleware(mocked_get_response) | ||
|
|
||
| # When | ||
| response = middleware(request) | ||
|
|
||
| # Then | ||
| assert response.status_code == 400 | ||
| mocked_get_response.assert_not_called() | ||
|
|
||
|
|
||
| def test_reject_nul_byte_query_params_middleware__no_nul_byte__calls_get_response( # type: ignore[no-untyped-def] # noqa: E501 | ||
| mocker, rf: RequestFactory | ||
| ): | ||
| # Given | ||
| a_response = HttpResponse() | ||
| mocked_get_response = mocker.MagicMock(return_value=a_response) | ||
| request = rf.get( | ||
| "/api/v1/environments/some-key/identities/", {"identifier": "foobar"} | ||
| ) | ||
|
|
||
| middleware = RejectNulByteQueryParamsMiddleware(mocked_get_response) | ||
|
|
||
| # When | ||
| response = middleware(request) | ||
|
|
||
| # Then | ||
| assert response is a_response | ||
| mocked_get_response.assert_called_once_with(request) | ||
|
|
||
|
|
||
| def test_reject_nul_byte_query_params_middleware__nul_byte_in_repeated_key__returns_bad_request( # type: ignore[no-untyped-def] # noqa: E501 | ||
| mocker, rf: RequestFactory | ||
| ): | ||
| # Given - `identifier` is repeated; `QueryDict.values()` would only see | ||
| # the last ("foobar"), silently missing the NUL byte in the first. | ||
| mocked_get_response = mocker.MagicMock() | ||
| request = rf.get( | ||
| "/api/v1/environments/some-key/identities/?identifier=foo%00bar&identifier=foobar" | ||
| ) | ||
|
|
||
| middleware = RejectNulByteQueryParamsMiddleware(mocked_get_response) | ||
|
|
||
| # When | ||
| response = middleware(request) | ||
|
|
||
| # Then | ||
| assert response.status_code == 400 | ||
| mocked_get_response.assert_not_called() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.