Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hashes for content with comment space #4118

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion privaterelay/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,25 @@
# not set by us, so we use an explicit allowlist with the hashes of the
# styles generated by Next.js.
_next_css_path = Path(STATIC_ROOT) / "_next" / "static" / "css"
hashes = []
for path in _next_css_path.glob("*.css"):
content = open(path, "rb").read()

# Use sha256 hashes, to keep in sync with Chrome.
# When CSP rules fail in Chrome, it provides the sha256 hash that would
# have matched.
the_hash = base64.b64encode(sha256(content).digest()).decode()
csp_style_values.append("'sha256-%s'" % the_hash)
hashes.append("'sha256-%s'" % the_hash)

# The sourceMappingURL is slightly different when loaded dynamically
# in next 14.0.0. Capture hash for alternate.
if content.endswith(b"map*/"):
space_content = content[:-2] + b" " + content[-2:]
assert space_content.endswith(b"map */")
space_hash = base64.b64encode(sha256(space_content).digest()).decode()
hashes.append("'sha256-%s'" % space_hash)
hashes.sort()
csp_style_values.extend(hashes)

CSP_STYLE_SRC = tuple(csp_style_values)

Expand Down