Smoke: controle op inline scripts zonder grep -P, JSON-blokken en importmaps tellen niet; test erbij

De controle staat in scripts/lib-inline-script.sh (alleen POSIX-tools), met een test (scripts/test-inline-script-check.sh) en een lokale proef van smoke-site.sh tegen een admin-pagina met en zonder inline script. De melding noemt nu wat er gevonden is.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 22:41:57 +01:00
co-authored by Claude Sonnet 5.5
parent 28b1c6d013
commit 9f07934f9e
3 changed files with 62 additions and 1 deletions
+15
View File
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
# Finds inline scripts in an HTML page (read from stdin): the ones a Content-Security-Policy with script-src 'self' blocks.
#
# An inline script is a <script> WITHOUT a src attribute (wherever the attribute stands in the tag) whose content is not
# empty. Not counted: <script src="..."> (also with type="module", crossorigin, defer, ... in any order), an empty
# <script></script>, and data blocks that are not executed (type="application/json", "application/ld+json",
# "importmap" and the like). Prints the opening tag of every inline script found; no output means none.
# Plain POSIX tools only (no grep -P), so it behaves the same on every runner.
inline_scripts() {
tr '\r\n' ' ' \
| grep -oiE '<script[^>]*>[[:space:]]*[^[:space:]<]' \
| grep -viE '<script[^>]*[[:space:]]src[[:space:]]*=' \
| grep -viE '<script[^>]*type[[:space:]]*=[[:space:]]*["'\'']?(application/(ld\+)?json|importmap|speculationrules)' \
|| true
}
+6 -1
View File
@@ -10,6 +10,8 @@
# master CMS only; this script is the check that the deployed composition of a site is the one that was intended.
# A failing check makes the CI run fail.
set -u
# shellcheck source=lib-inline-script.sh
. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib-inline-script.sh"
base="$(printf '%s' "${1:?usage: smoke-site.sh <url-on-the-host> <profile>}" | sed -E 's#^(https?://[^/]+).*#\1#')"
profile="${2:?profile (sls-test | asf-test) is required}"
@@ -58,9 +60,12 @@ expect_status /api/v1/hero/all 401 "the hero management read is protected"
expect_status /media/hero/0123456789abcdef/wide-960.webp 404 "media that is not stored is a plain 404"
# The admin page must have no inline script: the host only allows scripts from its own origin (script-src 'self').
# The check lives in lib-inline-script.sh (and is tested by test-inline-script-check.sh); it prints what it found.
admin_html="$(curl_get "$base/admin/" || true)"
if printf '%s' "$admin_html" | grep -q 'id="root"'; then
if printf '%s' "$admin_html" | grep -qiP '<script(?![^>]*src=)[^>]*>'; then fail "the admin page has an inline script (blocked by the Content-Security-Policy)"; else pass "the admin page has no inline script"; fi
inline_found="$(printf '%s' "$admin_html" | inline_scripts)"
if [ -n "$inline_found" ]; then fail "the admin page has an inline script (blocked by the Content-Security-Policy): $(printf '%s' "$inline_found" | head -3 | tr '
' ' ')"; else pass "the admin page has no inline script"; fi
expect_status /admin/theme-init.js 200 "the theme script is a file of its own"
fi
+41
View File
@@ -0,0 +1,41 @@
#!/usr/bin/env bash
# Tests inline_scripts (scripts/lib-inline-script.sh) against HTML as the admin page looks. Run: bash scripts/test-inline-script-check.sh
set -u
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=lib-inline-script.sh
. "$here/lib-inline-script.sh"
failures=0
expect() { # description expected(clean|inline) html
local found; found="$(printf '%s' "$3" | inline_scripts)"
if [ "$2" = "clean" ] && [ -z "$found" ]; then echo "ok $1"
elif [ "$2" = "inline" ] && [ -n "$found" ]; then echo "ok $1"
else echo "FAIL $1 (expected $2, found: ${found:-nothing})"; failures=$((failures + 1)); fi
}
expect "the built admin page (module script with crossorigin before src)" clean '<!doctype html><html><head>
<link rel="icon" href="/admin/favicon.svg" />
<script src="/admin/theme-init.js"></script>
<script type="module" crossorigin src="/admin/assets/index-Da0WHZBX.js"></script>
<link rel="modulepreload" crossorigin href="/admin/assets/button.js">
<link rel="stylesheet" crossorigin href="/admin/assets/index.css">
</head><body><div id="root"></div></body></html>'
expect "src in the middle of the tag, single quotes, defer" clean "<script defer src='/a.js' data-x=\"1\"></script><script async crossorigin src = \"/b.js\"></script>"
expect "a tag over several lines" clean '<script
type="module"
crossorigin
src="/admin/assets/index.js"
></script>'
expect "an empty script" clean '<script></script><script> </script>'
expect "a JSON data block" clean '<script type="application/json" id="cms-data">{"a":1}</script>'
expect "a JSON-LD block and an import map" clean '<script type="application/ld+json">{"@context":"https://schema.org"}</script><script type="importmap">{"imports":{}}</script>'
expect "the old inline theme script" inline '<head><script>
(function () { document.documentElement.classList.add("dark"); })();
</script></head>'
expect "an inline module script" inline '<script type="module">import "/x.js"</script>'
expect "an inline script after good ones" inline '<script src="/a.js"></script><div></div><script nonce="abc">window.x = 1</script>'
expect "an inline script that starts with a comparison" inline '<script>if (a < b) { go(); }</script>'
expect "text that only mentions the word script" clean '<p>Use a script tag with a src attribute.</p><script src="/ok.js"></script>'
if [ "$failures" -ne 0 ]; then echo "$failures check(s) failed"; exit 1; fi
echo "all checks passed"