-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fixed #1682 -- alert user when using file field without proper encoding #1933
Merged
tim-schilling
merged 8 commits into
django-commons:main
from
bkdekoning:issue_1682_alerts_panel
Jul 4, 2024
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
97fa2e5
Fixed issue #1682 -- alert user when using file field without proper …
bkdekoning 00dc765
Changed issues to alerts, added docstring to check_invalid_... functi…
bkdekoning 0bbb66d
Update AlertsPanel documentation to list pre-defined alert cases
bkdekoning c332a6f
added check for file inputs that directly reference a form, including…
bkdekoning d89ee73
Update the alert messages to be on the panel as a map.
tim-schilling 11872eb
Expose a page in the example app that triggers the alerts panel.
tim-schilling e8dd720
Move the alerts panel below the more used panels.
tim-schilling 28e15a7
Missed a period.
tim-schilling 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 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,95 @@ | ||
from html.parser import HTMLParser | ||
|
||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from debug_toolbar.panels import Panel | ||
|
||
|
||
class FormParser(HTMLParser): | ||
""" | ||
HTML form parser, used to check for invalid configurations of forms that | ||
take file inputs. | ||
""" | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self.in_form = False | ||
self.current_form = {} | ||
self.forms = [] | ||
|
||
def handle_starttag(self, tag, attrs): | ||
attrs = dict(attrs) | ||
if tag == "form": | ||
self.in_form = True | ||
self.current_form = { | ||
"file_form": False, | ||
"form_attrs": attrs, | ||
"submit_element_attrs": [], | ||
} | ||
elif self.in_form and tag == "input" and attrs.get("type") == "file": | ||
self.current_form["file_form"] = True | ||
elif self.in_form and ( | ||
(tag == "input" and attrs.get("type") in {"submit", "image"}) | ||
or tag == "button" | ||
): | ||
self.current_form["submit_element_attrs"].append(attrs) | ||
|
||
def handle_endtag(self, tag): | ||
if tag == "form" and self.in_form: | ||
self.forms.append(self.current_form) | ||
self.in_form = False | ||
|
||
|
||
class AlertsPanel(Panel): | ||
""" | ||
A panel to alert users to issues. | ||
""" | ||
|
||
title = _("Alerts") | ||
|
||
template = "debug_toolbar/panels/alerts.html" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.issues = [] | ||
|
||
@property | ||
def nav_subtitle(self): | ||
if self.issues: | ||
bkdekoning marked this conversation as resolved.
Show resolved
Hide resolved
|
||
issue_text = "issue" if len(self.issues) == 1 else "issues" | ||
return f"{len(self.issues)} {issue_text} found" | ||
else: | ||
return "" | ||
|
||
def add_issue(self, issue): | ||
self.issues.append(issue) | ||
bkdekoning marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def check_invalid_file_form_configuration(self, html_content): | ||
bkdekoning marked this conversation as resolved.
Show resolved
Hide resolved
|
||
parser = FormParser() | ||
parser.feed(html_content) | ||
|
||
for form in parser.forms: | ||
if ( | ||
form["file_form"] | ||
and form["form_attrs"].get("enctype") != "multipart/form-data" | ||
and not any( | ||
elem.get("formenctype") == "multipart/form-data" | ||
for elem in form["submit_element_attrs"] | ||
) | ||
tim-schilling marked this conversation as resolved.
Show resolved
Hide resolved
|
||
): | ||
form_id = form["form_attrs"].get("id", "no form id") | ||
issue = ( | ||
f'Form with id "{form_id}" contains file input but ' | ||
"does not have multipart/form-data encoding." | ||
) | ||
self.add_issue({"issue": issue}) | ||
return self.issues | ||
|
||
def generate_stats(self, request, response): | ||
html_content = response.content.decode(response.charset) | ||
self.check_invalid_file_form_configuration(html_content) | ||
|
||
# Further issue checks can go here | ||
|
||
# Write all issues to record_stats | ||
self.record_stats({"issues": self.issues}) |
This file contains 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 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,12 @@ | ||
{% load i18n %} | ||
|
||
{% if issues %} | ||
<h4>{% trans "Issues found" %}</h4> | ||
{% for issue in issues %} | ||
<ul> | ||
<li>{{ issue.issue }}</li> | ||
</ul> | ||
{% endfor %} | ||
{% else %} | ||
<p>No issues found.</p> | ||
{% endif %} |
This file contains 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 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 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 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,57 @@ | ||
from django.http import HttpResponse | ||
from django.template import Context, Template | ||
|
||
from ..base import BaseTestCase | ||
|
||
|
||
class AlertsPanelTestCase(BaseTestCase): | ||
panel_id = "AlertsPanel" | ||
|
||
def test_issue_warning_display(self): | ||
""" | ||
Test that the panel (does not) display[s] a warning when there are | ||
(no) issues. | ||
""" | ||
self.panel.issues = 0 | ||
nav_subtitle = self.panel.nav_subtitle | ||
self.assertNotIn("issues found", nav_subtitle) | ||
|
||
self.panel.issues = ["Issue 1", "Issue 2"] | ||
nav_subtitle = self.panel.nav_subtitle | ||
self.assertIn("2 issues found", nav_subtitle) | ||
|
||
def test_file_form_without_enctype_multipart_form_data(self): | ||
""" | ||
Test that the panel displays a form invalid message when there is | ||
a file input but encoding not set to multipart/form-data. | ||
""" | ||
test_form = '<form id="test-form"><input type="file"></form>' | ||
result = self.panel.check_invalid_file_form_configuration(test_form) | ||
expected_error = ( | ||
'Form with id "test-form" contains file input ' | ||
"but does not have multipart/form-data encoding." | ||
) | ||
self.assertEqual(result[0]["issue"], expected_error) | ||
self.assertEqual(len(result), 1) | ||
|
||
def test_file_form_with_enctype_multipart_form_data(self): | ||
test_form = """<form id="test-form" enctype="multipart/form-data"> | ||
<input type="file"> | ||
</form>""" | ||
result = self.panel.check_invalid_file_form_configuration(test_form) | ||
|
||
self.assertEqual(len(result), 0) | ||
|
||
def test_integration_file_form_without_enctype_multipart_form_data(self): | ||
t = Template('<form id="test-form"><input type="file"></form>') | ||
c = Context({}) | ||
rendered_template = t.render(c) | ||
response = HttpResponse(content=rendered_template) | ||
|
||
self.panel.generate_stats(self.request, response) | ||
|
||
self.assertIn( | ||
"Form with id "test-form" contains file input " | ||
"but does not have multipart/form-data encoding.", | ||
self.panel.content, | ||
) |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it'd be better to set this in
enable_instrumentation
insteadThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I trust your call on this, but have to admit that I do not fully understand the
enable_instrumentation
method. What would the advantage be of setting it there? Looking at the other panels, I commonly see lists being initialized in__init__
.