Skip to content

Commit 35c12cf

Browse files
authored
Add Reporting API (#3414)
* Add Report-To header * Linting * More linting * More CSP updates
1 parent 322f3e2 commit 35c12cf

7 files changed

Lines changed: 32 additions & 8 deletions

File tree

src/server/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
get_versioned_filename,
1414
)
1515
from .config import TEMPLATES_DIR, STATIC_DIR
16-
from . import csp, feature_policy
16+
from . import csp, feature_policy, report_api
1717
import logging
1818

1919

@@ -53,6 +53,8 @@ def add_header(response):
5353
if response.status_code == 200 or response.status_code == 304:
5454
response.cache_control.public = True
5555
response.cache_control.max_age = 600
56+
# Add Report API header
57+
response.headers["Report-To"] = report_api.report_to
5658
return response
5759

5860

src/server/csp.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"script-src": [
55
"'self'",
66
"'strict-dynamic'",
7-
"www.google-analytics.com",
7+
"*.google-analytics.com",
88
"www.googletagmanager.com",
99
"'unsafe-inline'",
1010
],
@@ -14,7 +14,6 @@
1414
"webmention.io",
1515
"discuss.httparchive.org",
1616
"*.google-analytics.com",
17-
"www.google-analytics.com",
1817
"www.googletagmanager.com",
1918
],
2019
"img-src": ["'self'", "https:", "data:"],

src/server/embeds_csp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"script-src": [
55
"'self'",
66
"'strict-dynamic'",
7-
"www.google-analytics.com",
7+
"*.google-analytics.com",
88
"www.googletagmanager.com",
99
"'unsafe-inline'",
1010
],
@@ -13,7 +13,7 @@
1313
"'self'",
1414
"webmention.io",
1515
"discuss.httparchive.org",
16-
"www.google-analytics.com",
16+
"*.google-analytics.com",
1717
"www.googletagmanager.com",
1818
],
1919
"img-src": ["'self'", "https:", "data:"],

src/server/report_api.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Because of the "true" value we can't use a Python object
2+
# and have to use actual JSON
3+
report_to = """
4+
{
5+
"group": "default",
6+
"max_age": 31536000,
7+
"endpoints": [
8+
{
9+
"url": "https://httparchive.report-uri.com/a/d/g"
10+
}
11+
],
12+
"include_subdomains": true
13+
}
14+
""".replace(
15+
"\n", ""
16+
).replace(
17+
" ", ""
18+
)

src/server/search_csp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
"script-src": [
55
"'self'",
66
"'strict-dynamic'",
7-
"www.google-analytics.com",
7+
"*.google-analytics.com",
88
"www.googletagmanager.com",
99
"cse.google.com",
1010
"'unsafe-inline'",
1111
"'unsafe-eval'",
1212
],
1313
"font-src": ["'self'"],
14-
"connect-src": ["'self'", "www.google-analytics.com", "www.googletagmanager.com"],
14+
"connect-src": ["'self'", "*.google-analytics.com", "www.googletagmanager.com"],
1515
"img-src": [
1616
"'self'",
1717
"*.google-analytics.com",

src/server/stories_csp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"script-src": [
55
"'self'",
66
"cdn.ampproject.org",
7-
"www.google-analytics.com",
7+
"*.google-analytics.com",
88
"www.googletagmanager.com",
99
],
1010
"font-src": ["'self'", "fonts.gstatic.com"],

src/server/tests/routes_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,8 @@ def test_test_webvitals_js_versioned(client):
254254
def test_embed(client):
255255
response = client.get("/en/2022/embeds/structured-data-sankey")
256256
assert response.status_code == 200
257+
258+
259+
def test_report_to_header(client):
260+
response = client.get("/en/2022/")
261+
assert response.headers["Report-To"].startswith("{")

0 commit comments

Comments
 (0)