|
| 1 | +from html.parser import HTMLParser |
| 2 | + |
| 3 | +from django.utils.translation import gettext_lazy as _ |
| 4 | + |
| 5 | +from debug_toolbar.panels import Panel |
| 6 | + |
| 7 | + |
| 8 | +class FormParser(HTMLParser): |
| 9 | + """ |
| 10 | + HTML form parser, used to check for invalid configurations of forms that |
| 11 | + take file inputs. |
| 12 | + """ |
| 13 | + |
| 14 | + def __init__(self): |
| 15 | + super().__init__() |
| 16 | + self.in_form = False |
| 17 | + self.current_form = {} |
| 18 | + self.forms = [] |
| 19 | + |
| 20 | + def handle_starttag(self, tag, attrs): |
| 21 | + attrs = dict(attrs) |
| 22 | + if tag == "form": |
| 23 | + self.in_form = True |
| 24 | + self.current_form = { |
| 25 | + "file_form": False, |
| 26 | + "form_attrs": attrs, |
| 27 | + "submit_element_attrs": [], |
| 28 | + } |
| 29 | + elif self.in_form and tag == "input" and attrs.get("type") == "file": |
| 30 | + self.current_form["file_form"] = True |
| 31 | + elif self.in_form and ( |
| 32 | + (tag == "input" and attrs.get("type") in {"submit", "image"}) |
| 33 | + or tag == "button" |
| 34 | + ): |
| 35 | + self.current_form["submit_element_attrs"].append(attrs) |
| 36 | + |
| 37 | + def handle_endtag(self, tag): |
| 38 | + if tag == "form" and self.in_form: |
| 39 | + self.forms.append(self.current_form) |
| 40 | + self.in_form = False |
| 41 | + |
| 42 | + |
| 43 | +class AlertsPanel(Panel): |
| 44 | + """ |
| 45 | + A panel to alert users to issues. |
| 46 | + """ |
| 47 | + |
| 48 | + title = _("Alerts") |
| 49 | + |
| 50 | + template = "debug_toolbar/panels/alerts.html" |
| 51 | + |
| 52 | + def __init__(self, *args, **kwargs): |
| 53 | + super().__init__(*args, **kwargs) |
| 54 | + self.issues = [] |
| 55 | + |
| 56 | + @property |
| 57 | + def nav_subtitle(self): |
| 58 | + if self.issues: |
| 59 | + issue_text = "issue" if len(self.issues) == 1 else "issues" |
| 60 | + return f"{len(self.issues)} {issue_text} found" |
| 61 | + else: |
| 62 | + return "" |
| 63 | + |
| 64 | + def add_issue(self, issue): |
| 65 | + self.issues.append(issue) |
| 66 | + |
| 67 | + def check_invalid_file_form_configuration(self, html_content): |
| 68 | + parser = FormParser() |
| 69 | + parser.feed(html_content) |
| 70 | + |
| 71 | + for form in parser.forms: |
| 72 | + if ( |
| 73 | + form["file_form"] |
| 74 | + and form["form_attrs"].get("enctype") != "multipart/form-data" |
| 75 | + and not any( |
| 76 | + elem.get("formenctype") == "multipart/form-data" |
| 77 | + for elem in form["submit_element_attrs"] |
| 78 | + ) |
| 79 | + ): |
| 80 | + form_id = form["form_attrs"].get("id", "no form id") |
| 81 | + issue = ( |
| 82 | + f'Form with id "{form_id}" contains file input but ' |
| 83 | + "does not have multipart/form-data encoding." |
| 84 | + ) |
| 85 | + self.add_issue({"issue": issue}) |
| 86 | + return self.issues |
| 87 | + |
| 88 | + def generate_stats(self, request, response): |
| 89 | + html_content = response.content.decode(response.charset) |
| 90 | + self.check_invalid_file_form_configuration(html_content) |
| 91 | + |
| 92 | + # Further issue checks can go here |
| 93 | + |
| 94 | + # Write all issues to record_stats |
| 95 | + self.record_stats({"issues": self.issues}) |
0 commit comments