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:
@@ -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"
|
||||
Reference in New Issue
Block a user