Skip to content

Commit 8cd9c73

Browse files
authored
Merge pull request #2542 from canonical/feature/WD-36600-csp-gtm-domains
WD-36600: allow regional google.* domains in connect-src for GTM
2 parents 1297e03 + bf5aee0 commit 8cd9c73

1 file changed

Lines changed: 56 additions & 1 deletion

File tree

webapp/handlers.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,61 @@
1+
import logging
2+
3+
import requests
14
import secrets
25

36
import flask
47
from canonicalwebteam.flask_base.env import get_flask_env
58

9+
logger = logging.getLogger(__name__)
10+
11+
# Used if the live fetch from Google fails at startup. Covers the regions
12+
# we've historically seen in CSP reports.
13+
_GOOGLE_DOMAINS_FALLBACK = [
14+
"www.google.com",
15+
# Europe
16+
"www.google.at", "www.google.be", "www.google.ch", "www.google.co.uk",
17+
"www.google.cz", "www.google.de", "www.google.dk", "www.google.es",
18+
"www.google.fi", "www.google.fr", "www.google.gr", "www.google.hu",
19+
"www.google.ie", "www.google.it", "www.google.nl", "www.google.no",
20+
"www.google.pl", "www.google.pt", "www.google.ro", "www.google.se",
21+
# Americas
22+
"www.google.ca", "www.google.cl", "www.google.co", "www.google.com.ar",
23+
"www.google.com.br", "www.google.com.mx", "www.google.com.pe",
24+
# Asia & Pacific
25+
"www.google.co.id", "www.google.co.in", "www.google.co.jp",
26+
"www.google.co.kr", "www.google.co.nz", "www.google.co.th",
27+
"www.google.com.au", "www.google.com.hk", "www.google.com.my",
28+
"www.google.com.ph", "www.google.com.sg", "www.google.com.tw",
29+
"www.google.com.vn",
30+
]
31+
32+
33+
def _fetch_google_supported_domains():
34+
"""
35+
Fetch Google's published list of regional search domains so GTM can
36+
reach the user's local google.<tld>. Falls back to a hardcoded list
37+
if the request fails so CSP remains valid.
38+
"""
39+
try:
40+
response = requests.get(
41+
"https://www.google.com/supported_domains", timeout=5
42+
)
43+
response.raise_for_status()
44+
domains = [
45+
"www" + line.strip()
46+
for line in response.text.splitlines()
47+
if line.strip().startswith(".google.")
48+
]
49+
if domains:
50+
return domains
51+
logger.warning("Google supported_domains response was empty")
52+
except requests.RequestException as exc:
53+
logger.warning("Failed to fetch Google supported_domains: %s", exc)
54+
return _GOOGLE_DOMAINS_FALLBACK
55+
56+
57+
GOOGLE_DOMAINS = _fetch_google_supported_domains()
58+
659
CSP = {
760
"default-src": ["'self'"],
861
"img-src": [
@@ -58,7 +111,6 @@
58111
],
59112
"connect-src": [
60113
"'self'",
61-
"www.google.com",
62114
"ubuntu.com",
63115
"analytics.google.com",
64116
"www.googletagmanager.com",
@@ -73,6 +125,9 @@
73125
"ws.zoominfo.com",
74126
"youtube.com",
75127
"google.com",
128+
# Regional google.<tld> domains used by GTM, sourced live from
129+
# https://www.google.com/supported_domains at app startup.
130+
*GOOGLE_DOMAINS,
76131
"fonts.google.com",
77132
"maps.googleapis.com",
78133
"pixel-config.reddit.com",

0 commit comments

Comments
 (0)