Skip to content
Merged
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions src/secfoo/web/static/js/interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@
// -- a normal form-submit button that just asks for confirmation
// first; cancelling prevents the submit (replaces the old
// onclick="return confirm(...)" pattern, also CSP-blocked).
// <select name="status" data-autosubmit>...</select>
// -- submits its enclosing form as soon as the selection changes
// (replaces the old onchange="this.form.submit()" filter dropdowns,
// which the CSP blocked the same way).
document.addEventListener("change", function (event) {
const field = event.target.closest("[data-autosubmit]");
if (field && field.form) {
field.form.submit();
}
});

document.addEventListener("click", function (event) {
const deleteButton = event.target.closest("[data-delete-url]");
if (deleteButton) {
Expand Down
2 changes: 1 addition & 1 deletion src/secfoo/web/templates/activities/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ <h2 style="margin-top:var(--space-12)">Architecture diagram</h2>
</p>
{% if diagram_projects | length > 1 %}
<form method="get" action="/activities/{{ skill.id }}" style="margin-top:var(--space-4);max-width:20rem">
<select class="select" name="diagram_project" onchange="this.form.submit()">
<select class="select" name="diagram_project" data-autosubmit>
{% for p in diagram_projects %}
<option value="{{ p.project_id }}" {% if selected_diagram_project and p.project_id == selected_diagram_project.project_id %}selected{% endif %}>{{ p.project_display_name }}</option>
{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ <h1>All findings</h1>
</div>
<form method="get" action="/activities/sast/findings" style="display:flex;gap:var(--space-3);align-items:center;flex-wrap:wrap">
<input type="hidden" name="status" value="{{ selected_status }}">
<select class="select" name="project_id" onchange="this.form.submit()">
<select class="select" name="project_id" data-autosubmit>
<option value="">All projects</option>
{% for p in projects %}
<option value="{{ p.project_id }}" {% if selected_project_id == p.project_id %}selected{% endif %}>{{ p.project_display_name }}</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h1>All vulnerabilities</h1>

<div style="display:flex;justify-content:flex-end;margin-top:var(--space-6)">
<form method="get" action="/activities/{{ skill_id }}/vulnerabilities">
<select class="select" name="project_id" onchange="this.form.submit()">
<select class="select" name="project_id" data-autosubmit>
<option value="">All projects</option>
{% for p in projects %}
<option value="{{ p.project_id }}" {% if selected_project_id == p.project_id %}selected{% endif %}>{{ p.project_display_name }}</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h1>All findings</h1>

<div style="display:flex;justify-content:flex-end;margin-top:var(--space-6)">
<form method="get" action="/activities/secret-scanning/findings">
<select class="select" name="project_id" onchange="this.form.submit()">
<select class="select" name="project_id" data-autosubmit>
<option value="">All projects</option>
{% for p in projects %}
<option value="{{ p.project_id }}" {% if selected_project_id == p.project_id %}selected{% endif %}>{{ p.project_display_name }}</option>
Expand Down
2 changes: 1 addition & 1 deletion src/secfoo/web/templates/assessments/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ <h1>Assessment register</h1>
</div>
<form method="get" action="/assessments" style="display:flex;gap:var(--space-3);align-items:center">
<input type="hidden" name="type" value="{{ '' if active_type == 'all' else active_type }}">
<select class="select" name="status" onchange="this.form.submit()">
<select class="select" name="status" data-autosubmit>
<option value="">All statuses</option>
<option value="ready" {% if active_status == 'ready' %}selected{% endif %}>Ready</option>
<option value="in_progress" {% if active_status == 'in_progress' %}selected{% endif %}>In Progress</option>
Expand Down
15 changes: 9 additions & 6 deletions tests/test_web/test_no_inline_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,28 @@
Path(__file__).parents[2] / "portal" / "templates",
]

_ONCLICK_RE = re.compile(r"\bonclick\s*=")
# Any inline event-handler attribute inside a tag (onclick, onchange,
# onsubmit, oninput, ...) -- the CSP blocks all of them, not just onclick.
_INLINE_HANDLER_RE = re.compile(r"<[^>]*\son[a-z]+\s*=", re.IGNORECASE)
_INLINE_SCRIPT_RE = re.compile(r"<script(?![^>]*\bsrc=)[^>]*>")


def _all_templates() -> list[Path]:
return [f for d in TEMPLATE_DIRS for f in d.rglob("*.html")]


def test_no_template_uses_an_inline_onclick_attribute():
def test_no_template_uses_an_inline_event_handler_attribute():
offenders = {
str(f): m.group(0)
for f in _all_templates()
for m in [_ONCLICK_RE.search(f.read_text())]
for m in [_INLINE_HANDLER_RE.search(f.read_text())]
if m
}
assert not offenders, (
f"Found onclick=... attributes, CSP-blocked on the portal: {offenders}. "
"Use data-href (row navigation) or data-confirm/data-delete-url "
"(confirm dialogs, delete buttons) with static/js/interactive.js instead."
f"Found inline on...= event handlers, CSP-blocked on the portal: {offenders}. "
"Use data-href (row navigation), data-confirm/data-delete-url "
"(confirm dialogs, delete buttons) or data-autosubmit (filter dropdowns) "
"with static/js/interactive.js instead."
)


Expand Down
Loading