diff --git a/scripts/lib-inline-script.sh b/scripts/lib-inline-script.sh new file mode 100644 index 0000000..68f84f5 --- /dev/null +++ b/scripts/lib-inline-script.sh @@ -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 , 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 ']*>[[:space:]]*[^[:space:]<]' \ + | grep -viE ']*[[:space:]]src[[:space:]]*=' \ + | grep -viE ']*type[[:space:]]*=[[:space:]]*["'\'']?(application/(ld\+)?json|importmap|speculationrules)' \ + || true +} diff --git a/scripts/smoke-site.sh b/scripts/smoke-site.sh index 4904a78..1f04026 100644 --- a/scripts/smoke-site.sh +++ b/scripts/smoke-site.sh @@ -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 }" | 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 ']*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 diff --git a/scripts/test-inline-script-check.sh b/scripts/test-inline-script-check.sh new file mode 100644 index 0000000..989df19 --- /dev/null +++ b/scripts/test-inline-script-check.sh @@ -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 ' + + + + + +
' +expect "src in the middle of the tag, single quotes, defer" clean "" +expect "a tag over several lines" clean '' +expect "an empty script" clean '' +expect "a JSON data block" clean '' +expect "a JSON-LD block and an import map" clean '' +expect "the old inline theme script" inline '' +expect "an inline module script" inline '' +expect "an inline script after good ones" inline '
' +expect "an inline script that starts with a comparison" inline '' +expect "text that only mentions the word script" clean '

Use a script tag with a src attribute.

' + +if [ "$failures" -ne 0 ]; then echo "$failures check(s) failed"; exit 1; fi +echo "all checks passed"