-
Notifications
You must be signed in to change notification settings - Fork 3
Django: cleanup & extract cookies from request #417
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a43da76
create try_extract_body and try_extract_cookies for django
bitterpanda63 9545195
Cleanup run_init_stage
bitterpanda63 45623ec
Linting
bitterpanda63 71554a1
Django use context.set_body
bitterpanda63 bdeb177
Add end2end test for new cookie code
bitterpanda63 8990298
Fix e2e test for cookie fix
bitterpanda63 15ed4f8
Allow clearing of mock events
bitterpanda63 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 |
---|---|---|
@@ -1,54 +1,26 @@ | ||
"""Exports run_init_stage function""" | ||
|
||
import json | ||
from aikido_zen.context import Context | ||
from aikido_zen.helpers.logging import logger | ||
from .try_extract_body import try_extract_body_from_django_request | ||
from .try_extract_cookies import try_extract_cookies_from_django_request | ||
from ..functions.request_handler import request_handler | ||
|
||
|
||
def run_init_stage(request): | ||
"""Parse request and body, run "init" stage with request_handler""" | ||
body = None | ||
try: | ||
# try-catch loading of form parameters, this is to fix issue with DATA_UPLOAD_MAX_NUMBER_FIELDS : | ||
try: | ||
body = request.POST.dict() | ||
if len(body) == 0: | ||
body = None # Reset | ||
except Exception: | ||
pass | ||
context = None | ||
if ( | ||
hasattr(request, "scope") and request.scope is not None | ||
): # This request is an ASGI request | ||
context = Context(req=request.scope, source="django_async") | ||
elif hasattr(request, "META") and request.META is not None: # WSGI request | ||
context = Context(req=request.META, source="django") | ||
else: | ||
return | ||
|
||
# Check for JSON or XML : | ||
if body is None and request.content_type == "application/json": | ||
try: | ||
body = json.loads(request.body) | ||
except Exception: | ||
pass | ||
if body is None or len(body) == 0: | ||
# E.g. XML Data | ||
body = request.body | ||
if body is None or len(body) == 0: | ||
# During a GET request, django leaves the body as an empty byte string (e.g. `b''`). | ||
# When an attack is detected, this body needs to be serialized which would fail. | ||
# So a byte string gets converted into a string to stop that from happening. | ||
body = "" # Set body to an empty string. | ||
except Exception as e: | ||
logger.debug("Exception occurred in run_init_stage function (Django) : %s", e) | ||
# Parse some attributes separately | ||
context.set_body(try_extract_body_from_django_request(request)) | ||
context.cookies = try_extract_cookies_from_django_request(request) | ||
|
||
# In a separate try-catch we set the context : | ||
try: | ||
context = None | ||
if ( | ||
hasattr(request, "scope") and request.scope is not None | ||
): # This request is an ASGI request | ||
context = Context(req=request.scope, body=body, source="django_async") | ||
elif hasattr(request, "META") and request.META is not None: # WSGI request | ||
context = Context(req=request.META, body=body, source="django") | ||
else: | ||
return | ||
context.set_as_current_context() | ||
|
||
# Init stage needs to be run with context already set : | ||
request_handler(stage="init") | ||
except Exception as e: | ||
logger.debug("Exception occurred in run_init_stage function (Django): %s", e) | ||
context.set_as_current_context() | ||
request_handler(stage="init") |
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,32 @@ | ||
import json | ||
from aikido_zen.helpers.logging import logger | ||
|
||
|
||
def try_extract_body_from_django_request(request): | ||
body = None | ||
try: | ||
# try-catch loading of form parameters, this is to fix issue with DATA_UPLOAD_MAX_NUMBER_FIELDS : | ||
try: | ||
body = request.POST.dict() | ||
if len(body) == 0: | ||
body = None # Reset | ||
except Exception: | ||
pass | ||
|
||
# Check for JSON or XML : | ||
if body is None and request.content_type == "application/json": | ||
try: | ||
body = json.loads(request.body) | ||
except Exception: | ||
pass | ||
if body is None or len(body) == 0: | ||
# E.g. XML Data | ||
body = request.body | ||
if body is None or len(body) == 0: | ||
# During a GET request, django leaves the body as an empty byte string (e.g. `b''`). | ||
# When an attack is detected, this body needs to be serialized which would fail. | ||
# So a byte string gets converted into a string to stop that from happening. | ||
return "" # Set body to an empty string. | ||
except Exception as e: | ||
logger.debug("Exception occurred trying to extract django body: %s", e) | ||
return body |
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,11 @@ | ||
from aikido_zen.helpers.logging import logger | ||
|
||
|
||
def try_extract_cookies_from_django_request(request): | ||
try: | ||
# https://github.com/django/django/blob/7091801e046dc85dba2238ed4eaf0b3f62bcfc7f/django/core/handlers/wsgi.py#L100 | ||
# https://github.com/django/django/blob/7091801e046dc85dba2238ed4eaf0b3f62bcfc7f/django/core/handlers/asgi.py#L131 | ||
cookies = request.COOKIES | ||
return cookies | ||
except Exception as e: | ||
logger.debug("Exception occurred trying to extract django cookies: %s", e) | ||
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
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
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
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.