Skip to content
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

Catch UnicodeDecodeError in extract_ajax_token #63

Merged
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
1 change: 1 addition & 0 deletions request_token/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
These exceptions all inherit from the PyJWT base InvalidTokenError.

"""

from __future__ import annotations

from jwt.exceptions import InvalidTokenError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
period of time.

"""

from argparse import ArgumentParser
from datetime import datetime, timedelta
from typing import Any
Expand Down
3 changes: 3 additions & 0 deletions request_token/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def extract_ajax_token(self, request: HttpRequest) -> str | None:
payload = json.loads(request.body)
except json.decoder.JSONDecodeError:
return None
except UnicodeDecodeError:
return None

try:
return payload.get(JWT_QUERYSTRING_ARG)
except AttributeError:
Expand Down
1 change: 1 addition & 0 deletions request_token/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Basic encode/decode utils, taken from PyJWT."""

from __future__ import annotations

import calendar
Expand Down
11 changes: 11 additions & 0 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,14 @@ def test_extract_json_token(self):
request = self.post_request_with_JSON(self.default_payload)
middleware = RequestTokenMiddleware(lambda r: HttpResponse())
self.assertEqual(middleware.extract_ajax_token(request), self.token.jwt())

def test_extract_ajax_token_catches_unicode_error(self):
request = self.factory.post(
"/", data=b"\xa0", content_type="application/json" # Invalid UTF-8 data
)
request.user = self.user
request.session = MockSession()

middleware = RequestTokenMiddleware(get_response=lambda r: HttpResponse())
result = middleware.extract_ajax_token(request)
self.assertIsNone(result)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ deps =
ruff

commands =
ruff request_token
ruff check request_token
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, what is this for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ruff changed the command we need to do at some point in the past in few months.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See here. It needs the check command.


[testenv:mypy]
description = Python source code type hints (mypy)
Expand Down
Loading