Let the tests that forbid inline and outside scripts allow the Google tag, and nothing else
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
0d6197bedd
commit
99588ce306
4 files changed
+23
-6
No files matched your search
@@ -398,10 +398,13 @@ class HeadersTest(unittest.TestCase):
|
||||
csp = self.headers("/trails/john-muir/").get("content-security-policy", "")
|
||||
self.assertEqual(csp, self.headers("/").get("content-security-policy", ""))
|
||||
connect = re.search(r"connect-src ([^;]*)", csp).group(1).split()
|
||||
# The Google tag (layout.GOOGLE_TAG) reports to Google Analytics; nothing else is added.
|
||||
self.assertEqual(connect, ["'self'", "https://tiles.openfreemap.org",
|
||||
"https://*.google-analytics.com", "https://*.analytics.google.com",
|
||||
"https://*.googletagmanager.com",
|
||||
"https://s3.amazonaws.com/elevation-tiles-prod/terrarium/"])
|
||||
self.assertIn("worker-src 'self' blob:", csp)
|
||||
self.assertIn("img-src 'self' data: blob:;", csp)
|
||||
self.assertIn("img-src 'self' data: blob: https://*.google-analytics.com https://*.googletagmanager.com;", csp)
|
||||
with open(os.path.join(WEB, "assets", "map", "outdoor.json"), encoding="utf-8") as f:
|
||||
dem = json.load(f)["sources"]["olt-dem"]["tiles"][0]
|
||||
self.assertTrue(dem.startswith(connect[2]), dem)
|
||||
|
||||
@@ -112,8 +112,11 @@ class BuiltSiteTest(unittest.TestCase):
|
||||
target = resolve(OUT, url)
|
||||
if target and not os.path.isfile(target):
|
||||
missing.add((os.path.relpath(path, OUT), url))
|
||||
# The one external asset is the Google tag's loader (layout.GOOGLE_TAG).
|
||||
self.assertFalse(url.startswith(("http://", "https://")) and tag in ("script", "link")
|
||||
and a.get("rel") != "canonical", f"external asset {url} in {path}")
|
||||
and a.get("rel") != "canonical"
|
||||
and not url.startswith("https://www.googletagmanager.com/gtm.js?id="),
|
||||
f"external asset {url} in {path}")
|
||||
self.assertEqual(sorted(missing)[:20], [])
|
||||
|
||||
def test_no_inline_scripts(self):
|
||||
@@ -121,7 +124,10 @@ class BuiltSiteTest(unittest.TestCase):
|
||||
for a, body in parse(path).scripts:
|
||||
if a.get("type") in ("application/json", "application/ld+json"):
|
||||
continue
|
||||
self.assertTrue(a.get("src"), f"inline script in {path}")
|
||||
if not a.get("src"):
|
||||
# The one inline script is the Google tag's data-layer setup, allowed by its hash.
|
||||
self.assertEqual(body.strip(), layout.GOOGLE_TAG_INLINE.strip(), f"inline script in {path}")
|
||||
continue
|
||||
self.assertEqual(body.strip(), "", f"script with src and a body in {path}")
|
||||
|
||||
def test_style_attributes_only_carry_custom_properties(self):
|
||||
|
||||
@@ -128,10 +128,15 @@ class HeaderOnEveryPageTest(unittest.TestCase):
|
||||
for rel in self.files:
|
||||
doc = read_out(rel)
|
||||
self.assertNotIn("<style", doc, rel)
|
||||
inline = 0
|
||||
for tag, a, _ in tree(doc).tags:
|
||||
self.assertFalse([k for k in a if k.startswith("on")], f"{tag} in {rel}")
|
||||
if tag == "script":
|
||||
self.assertTrue(a.get("src") or a.get("type") in ("application/json", "application/ld+json"), rel)
|
||||
if tag == "script" and not (a.get("src") or a.get("type") in ("application/json", "application/ld+json")):
|
||||
inline += 1
|
||||
# The one inline script is the Google tag's data-layer setup, allowed by its hash.
|
||||
self.assertLessEqual(inline, 1, rel)
|
||||
if inline:
|
||||
self.assertIn("<script>" + layout.GOOGLE_TAG_INLINE + "</script>", doc, rel)
|
||||
|
||||
def test_the_switches_name_what_they_do(self):
|
||||
prefs = layout.prefs()
|
||||
|
||||
@@ -170,7 +170,10 @@ class JsonLdTest(unittest.TestCase):
|
||||
rules = serve.parse_headers(os.path.join(OUT, "_headers"))
|
||||
csp = next(v for pat, hs in rules if pat.match("/") for k, v in hs if k.lower() == "content-security-policy")
|
||||
script_src = re.search(r"script-src ([^;]*)", csp).group(1).split()
|
||||
self.assertEqual(script_src, ["'self'"])
|
||||
# Besides 'self': the Google tag's loader and the hash of its one inline script.
|
||||
self.assertEqual(script_src[:2], ["'self'", "https://www.googletagmanager.com"])
|
||||
self.assertEqual(len(script_src), 3)
|
||||
self.assertTrue(script_src[2].startswith("'sha256-"), script_src)
|
||||
self.assertNotIn("unsafe-inline", csp.replace("style-src-attr 'unsafe-inline'", ""))
|
||||
for path, doc in self.pages.items():
|
||||
self.assertNotRegex(doc, r'<script type="application/ld\+json"[^>]*\ssrc=', path)
|
||||
|
||||
Reference in new issue
Block a user