diff --git a/web/tests/test_extra.py b/web/tests/test_extra.py index 2c09c12..d7d6553 100644 --- a/web/tests/test_extra.py +++ b/web/tests/test_extra.py @@ -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) diff --git a/web/tests/test_pages.py b/web/tests/test_pages.py index d8fcecf..8e0b681 100644 --- a/web/tests/test_pages.py +++ b/web/tests/test_pages.py @@ -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): diff --git a/web/tests/test_saved.py b/web/tests/test_saved.py index e2009e7..2082ef2 100644 --- a/web/tests/test_saved.py +++ b/web/tests/test_saved.py @@ -128,10 +128,15 @@ class HeaderOnEveryPageTest(unittest.TestCase): for rel in self.files: doc = read_out(rel) self.assertNotIn("" + layout.GOOGLE_TAG_INLINE + "", doc, rel) def test_the_switches_name_what_they_do(self): prefs = layout.prefs() diff --git a/web/tests/test_structured_data.py b/web/tests/test_structured_data.py index 32448a5..2e6ab71 100644 --- a/web/tests/test_structured_data.py +++ b/web/tests/test_structured_data.py @@ -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'