Skip to content

Fix flask bug with duplicate values in form body #425

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
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 19 additions & 6 deletions aikido_zen/sources/flask/extract_form_data.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
from typing import Dict, List

from aikido_zen.context import get_current_context
from aikido_zen.helpers.logging import logger


def extract_form_data_from_flask_request_and_save_data(req):
"""Extract form data from flask request"""
context = get_current_context()
if not context:
return

Check warning on line 11 in aikido_zen/sources/flask/extract_form_data.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sources/flask/extract_form_data.py#L11

Added line #L11 was not covered by tests
try:
if context:
if req.form:
context.set_body(req.form)
else:
context.set_body(req.data.decode("utf-8"))
context.set_as_current_context()
# https://flask.palletsprojects.com/en/stable/api/#flask.Request
# req.form is an ImmutableMultiDict, we will try and extract all values for a certain key
if req.form:
form_data: Dict[str, List] = {}

Check warning on line 16 in aikido_zen/sources/flask/extract_form_data.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sources/flask/extract_form_data.py#L16

Added line #L16 was not covered by tests

# Extract to a dict of lists
for key, value in req.form.items(multi=True):
if not key in form_data:
form_data[key] = list()
form_data[key].append(value)

Check warning on line 22 in aikido_zen/sources/flask/extract_form_data.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sources/flask/extract_form_data.py#L19-L22

Added lines #L19 - L22 were not covered by tests

context.set_body(form_data)

Check warning on line 24 in aikido_zen/sources/flask/extract_form_data.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sources/flask/extract_form_data.py#L24

Added line #L24 was not covered by tests
else:
context.set_body(req.data.decode("utf-8"))
context.set_as_current_context()

Check warning on line 27 in aikido_zen/sources/flask/extract_form_data.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sources/flask/extract_form_data.py#L26-L27

Added lines #L26 - L27 were not covered by tests
except Exception as e:
logger.debug("Exception occurred whilst extracting flask body data: %s", e)
Loading