|
| 1 | +import logging |
| 2 | + |
| 3 | +import requests |
1 | 4 | import secrets |
2 | 5 |
|
3 | 6 | import flask |
4 | 7 | from canonicalwebteam.flask_base.env import get_flask_env |
5 | 8 |
|
| 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 | + |
6 | 59 | CSP = { |
7 | 60 | "default-src": ["'self'"], |
8 | 61 | "img-src": [ |
|
58 | 111 | ], |
59 | 112 | "connect-src": [ |
60 | 113 | "'self'", |
61 | | - "www.google.com", |
62 | 114 | "ubuntu.com", |
63 | 115 | "analytics.google.com", |
64 | 116 | "www.googletagmanager.com", |
|
73 | 125 | "ws.zoominfo.com", |
74 | 126 | "youtube.com", |
75 | 127 | "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, |
76 | 131 | "fonts.google.com", |
77 | 132 | "maps.googleapis.com", |
78 | 133 | "pixel-config.reddit.com", |
|
0 commit comments