|
| 1 | +import logging |
| 2 | + |
| 3 | +import requests |
1 | 4 | from canonicalwebteam.flask_base.env import get_flask_env |
2 | 5 |
|
| 6 | +logger = logging.getLogger(__name__) |
| 7 | + |
| 8 | +# Used if the live fetch from Google fails at startup. Covers the regions |
| 9 | +# we've historically seen in CSP reports. |
| 10 | +_GOOGLE_DOMAINS_FALLBACK = [ |
| 11 | + "www.google.com", |
| 12 | + # Europe |
| 13 | + "www.google.at", "www.google.be", "www.google.ch", "www.google.co.uk", |
| 14 | + "www.google.cz", "www.google.de", "www.google.dk", "www.google.es", |
| 15 | + "www.google.fi", "www.google.fr", "www.google.gr", "www.google.hu", |
| 16 | + "www.google.ie", "www.google.it", "www.google.nl", "www.google.no", |
| 17 | + "www.google.pl", "www.google.pt", "www.google.ro", "www.google.se", |
| 18 | + # Americas |
| 19 | + "www.google.ca", "www.google.cl", "www.google.co", "www.google.com.ar", |
| 20 | + "www.google.com.br", "www.google.com.mx", "www.google.com.pe", |
| 21 | + # Asia & Pacific |
| 22 | + "www.google.co.id", "www.google.co.in", "www.google.co.jp", |
| 23 | + "www.google.co.kr", "www.google.co.nz", "www.google.co.th", |
| 24 | + "www.google.com.au", "www.google.com.hk", "www.google.com.my", |
| 25 | + "www.google.com.ph", "www.google.com.sg", "www.google.com.tw", |
| 26 | + "www.google.com.vn", |
| 27 | +] |
| 28 | + |
| 29 | + |
| 30 | +def _fetch_google_supported_domains(): |
| 31 | + """ |
| 32 | + Fetch Google's published list of regional search domains so GTM can |
| 33 | + reach the user's local google.<tld>. Falls back to a hardcoded list |
| 34 | + if the request fails so CSP remains valid. |
| 35 | + """ |
| 36 | + try: |
| 37 | + response = requests.get( |
| 38 | + "https://www.google.com/supported_domains", timeout=5 |
| 39 | + ) |
| 40 | + response.raise_for_status() |
| 41 | + domains = [ |
| 42 | + "www" + line.strip() |
| 43 | + for line in response.text.splitlines() |
| 44 | + if line.strip().startswith(".google.") |
| 45 | + ] |
| 46 | + if domains: |
| 47 | + return domains |
| 48 | + logger.warning("Google supported_domains response was empty") |
| 49 | + except requests.RequestException as exc: |
| 50 | + logger.warning("Failed to fetch Google supported_domains: %s", exc) |
| 51 | + return _GOOGLE_DOMAINS_FALLBACK |
| 52 | + |
| 53 | + |
| 54 | +GOOGLE_DOMAINS = _fetch_google_supported_domains() |
| 55 | + |
3 | 56 | CSP = { |
4 | 57 | "default-src": ["'self'"], |
5 | 58 | "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", |
76 | | - # Europe |
77 | | - "www.google.at", |
78 | | - "www.google.be", |
79 | | - "www.google.ch", |
80 | | - "www.google.co.uk", |
81 | | - "www.google.cz", |
82 | | - "www.google.de", |
83 | | - "www.google.dk", |
84 | | - "www.google.es", |
85 | | - "www.google.fi", |
86 | | - "www.google.fr", |
87 | | - "www.google.gr", |
88 | | - "www.google.hu", |
89 | | - "www.google.ie", |
90 | | - "www.google.it", |
91 | | - "www.google.nl", |
92 | | - "www.google.no", |
93 | | - "www.google.pl", |
94 | | - "www.google.pt", |
95 | | - "www.google.ro", |
96 | | - "www.google.se", |
97 | | - # Americas |
98 | | - "www.google.ca", |
99 | | - "www.google.cl", |
100 | | - "www.google.co", |
101 | | - "www.google.com.ar", |
102 | | - "www.google.com.br", |
103 | | - "www.google.com.mx", |
104 | | - "www.google.com.pe", |
105 | | - # Asia & Pacific |
106 | | - "www.google.co.id", |
107 | | - "www.google.co.in", |
108 | | - "www.google.co.jp", |
109 | | - "www.google.co.kr", |
110 | | - "www.google.co.nz", |
111 | | - "www.google.co.th", |
112 | | - "www.google.com.au", |
113 | | - "www.google.com.hk", |
114 | | - "www.google.com.my", |
115 | | - "www.google.com.ph", |
116 | | - "www.google.com.sg", |
117 | | - "www.google.com.tw", |
118 | | - "www.google.com.vn", |
119 | | - # Africa & Middle East |
120 | | - "www.google.co.id", |
121 | | - "www.google.co.in", |
122 | | - "www.google.co.jp", |
123 | | - "www.google.co.kr", |
124 | | - "www.google.co.nz", |
125 | | - "www.google.co.th", |
126 | | - "www.google.com.au", |
127 | | - "www.google.com.hk", |
128 | | - "www.google.com.my", |
129 | | - "www.google.com.ph", |
130 | | - "www.google.com.sg", |
131 | | - "www.google.com.tw", |
132 | | - "www.google.com.vn", |
| 128 | + # Regional google.<tld> domains used by GTM, sourced live from |
| 129 | + # https://www.google.com/supported_domains at app startup. |
| 130 | + *GOOGLE_DOMAINS, |
133 | 131 | "fonts.google.com", |
134 | 132 | "maps.googleapis.com", |
135 | 133 | "pixel-config.reddit.com", |
|
0 commit comments