Skip to content

Commit

Permalink
Merge pull request #69 from snok/mitigate-log4shell-alike-attacks
Browse files Browse the repository at this point in the history
Remove logging of invalid headers if they are not safe
  • Loading branch information
sondrelg committed Dec 13, 2021
2 parents 8f464a9 + 86e972d commit 90d541a
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 169 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
fail-fast: false
matrix:
python-version: [ "3.7", "3.8", "3.9", "3.10" ]
django-version: [ "3.1.4", "3.2" ]
django-version: [ "3.1.4", "3.2", "4.0" ]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
Expand Down
2 changes: 1 addition & 1 deletion django_guid/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django_guid.api import clear_guid, get_guid, set_guid # noqa F401

__version__ = '3.2.0'
__version__ = '3.2.1'
default_app_config = 'django_guid.apps.DjangoGuidConfig'

__all__ = ['clear_guid', 'get_guid', 'set_guid']
1 change: 0 additions & 1 deletion django_guid/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ def clear_guid() -> None:
if old_guid:
logger.info('Clearing %s from the guid ContextVar', old_guid)
guid.set(None)
return
2 changes: 0 additions & 2 deletions django_guid/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def process_incoming_request(request: HttpRequest) -> None:
for integration in settings.integrations:
logger.debug('Running integration: `%s`', integration.identifier)
integration.run(guid=guid.get())
return


def process_outgoing_request(response: HttpResponse, request: HttpRequest) -> None:
Expand All @@ -49,7 +48,6 @@ def process_outgoing_request(response: HttpResponse, request: HttpRequest) -> No
for integration in settings.integrations:
logger.debug('Running tear down for integration: `%s`', integration.identifier)
integration.cleanup()
return


@sync_and_async_middleware
Expand Down
1 change: 0 additions & 1 deletion django_guid/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ def clear_guid(sender: Optional[dict], **kwargs: Any) -> None:
"""
logger.debug('Received signal `request_finished`, clearing guid')
guid.set(None)
return
10 changes: 6 additions & 4 deletions django_guid/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def get_correlation_id_from_header(request: HttpRequest) -> str:
:param request: HttpRequest object
:return: GUID
"""
given_guid = str(request.headers.get(settings.guid_header_name))
given_guid: str = str(request.headers.get(settings.guid_header_name))
if not settings.validate_guid:
logger.debug('Returning ID from header without validating it as a GUID')
return given_guid
Expand All @@ -24,7 +24,10 @@ def get_correlation_id_from_header(request: HttpRequest) -> str:
return given_guid
else:
new_guid = generate_guid()
logger.warning('%s is not a valid GUID. New GUID is %s', given_guid, new_guid)
if all(letter.isalnum() or letter == '-' for letter in given_guid):
logger.warning('%s is not a valid GUID. New GUID is %s', given_guid, new_guid)
else:
logger.warning('Non-alnum %s provided. New GUID is %s', settings.guid_header_name, new_guid)
return new_guid


Expand All @@ -38,7 +41,7 @@ def get_id_from_header(request: HttpRequest) -> str:
"""
header: str = request.headers.get(settings.guid_header_name) # Case insensitive headers.get added in Django2.2
if header:
logger.info('%s found in the header: %s', settings.guid_header_name, header)
logger.info('%s found in the header', settings.guid_header_name)
request.correlation_id = get_correlation_id_from_header(request)
else:
request.correlation_id = generate_guid()
Expand Down Expand Up @@ -80,5 +83,4 @@ def validate_guid(original_guid: str) -> bool:
try:
return bool(uuid.UUID(original_guid, version=4).hex)
except ValueError:
logger.warning('Failed to validate GUID %s', original_guid)
return False

0 comments on commit 90d541a

Please sign in to comment.