Skip to content

Enforce a Blog Content-Security-Policy Behind a Script Hash Gate - #514

Merged
ptr727 merged 10 commits into
developfrom
feature/blog-csp
Oct 25, 2026
Merged

ptr727 merged 10 commits into
developfrom
feature/blog-csp

Conversation

@ptr727

@ptr727 ptr727 commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner

Summary

The blog site block sends an enforcing Content-Security-Policy, which #501 tracks:

default-src 'self'; script-src 'self' <four sha256 hashes>; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src https://fonts.gstatic.com; img-src 'self'; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'
  • The survey behind it is on Survey What Blog Posts Load Before a Blog Content-Security-Policy #501. No post loads a script, frame, or object from another host. The theme writes four inline scripts, each byte-identical on every page, so script-src lists them by hash rather than allowing 'unsafe-inline'.
  • A new build gate, checks/check-csp-scripts.py, runs in make-release.sh after the URL contract, so it runs in validation, on every deploy, and on every local release. It hashes every inline script in public/ and fails when a hash is unlisted or a listed hash is unused. It also fails on an inline handler, a javascript: URL, or a script from an origin script-src does not name. It requires deploy/Caddyfile to send the exact value security-headers.txt holds. A Hugo or theme update that changes a script's bytes therefore fails the build rather than a reader's page.
  • img-src 'self' leaves out the two hosts that eight archived posts hotlink. Every one of those 95 images already answers 404, and Settle the Dead Hotlinked Images on Eight Archived Posts #513 tracks them.
  • layouts/shortcodes/googlemaps.html is deleted. It rendered an <iframe> from another host, which the policy blocks, and no post called it.
  • Docs: deploy/README.md "Security headers" and "What the script asserts", the checks/README.md table, the themes/README.md update procedure, the README's local build steps, the CONTENT.md gate table, and a Check: CSP Scripts editor task. deploy/README.md also states that the policy blocks any script the proxy injects, so Rocket Loader, Email Address Obfuscation, and Web Analytics stay off on the host side.

Verification

  • Report-Only trial on local staging (2e865b1). A headless Chromium pass loaded every URL in golden-urls.txt, plus /search/ with a query typed. It also clicked the code-copy buttons and scrolled each page. It recorded every securitypolicyviolation event. All 328 pages carried the header. The only violations were the 95 dead images.
  • Enforcing on local staging (83a8e8c). The same pass recorded the same 95 img-src blocks and nothing else. check-live-urls.sh passed 1296 URLs, asserting the new header on every response.
  • The gate catches drift. It passes on the minified release build and fails on an unminified build, whose script bytes differ. A mutation that makes the Caddyfile comparison vacuous fails its unit test.
  • python3 -m unittest discover -s checks/tests passes 154 tests, including the Docker-backed case that holds the Caddyfile and security-headers.txt equal. ruff, mypy, pyright on the new files, prose lint with sentence length, markdownlint, cspell, editorconfig, shellcheck, and shfmt are clean.
  • A local strict review ran before each push. Every introduced finding it raised is fixed.

Deferred findings, filed

Declined findings

  • Consolidating the gate's header-file parser with expected_headers() in checks/tests/test_check_live_urls_sample.py, consolidation. Each script under checks/ is standalone, and there is no shared module to put a parser in.
  • The claim that the www.elcolighting.com figure stops loading under img-src 'self'. That image already answers 404.
  • Moving the test's importlib loader onto a shared helper, consolidation. Each test under checks/tests loads its own script, which matches the standalone-script convention above.
  • Treating the case-insensitive, whitespace-stripping javascript: test as over-hardening. URL schemes are case-insensitive and browsers strip leading spaces, so a hand-typed JavaScript: in a post is a realistic shape.
  • Naming the CSP gate in the .github/actions/validate/action.yml step comment. The comment is not false, naming the gate would lengthen it, and deploy/README.md "What the script asserts" is the list, which names the gate.

🤖 Generated with Claude Code

Copilot AI lite review requested due to automatic review settings October 25, 2026 04:09
@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 818293bc-1d18-49f3-9791-652c59f9be15
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ptr727

ptr727 commented Oct 25, 2026

Copy link
Copy Markdown
Owner Author

A recorded local strict-review pass covers head 2c51a36f218d9710dcf1358665505676e1982a5c, the content this pull request carries at that commit against develop, and it recorded 1 finding.

@codecov

codecov Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 82.72727% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 50.85%. Comparing base (b13ce30) to head (0df172f).

Files with missing lines Patch % Lines
checks/check-csp-scripts.py 82.72% 19 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #514      +/-   ##
===========================================
+ Coverage    49.70%   50.85%   +1.14%     
===========================================
  Files           10       11       +1     
  Lines         3058     3168     +110     
===========================================
+ Hits          1520     1611      +91     
- Misses        1538     1557      +19     
Flag Coverage Δ
python-3.13 50.85% <82.72%> (+1.14%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical validation and test compatibility issues remain, and the CSP currently blocks the existing Google Maps shortcode.

3 open issues
What changed in this PR

This pull request adds an enforcing Content-Security-Policy for the blog and a release-time gate that synchronizes inline script hashes, external script origins, and deployed headers.

Changes:

  • Adds the blog CSP to Caddy and checks/security-headers.txt.
  • Adds checks/check-csp-scripts.py, its tests, and release-build integration.
  • Documents CSP behavior and hash maintenance for deployments and theme updates.
File Summary
themes/​README.md Documents CSP hash updates when replacing the vendored theme.
deploy/​README.md Documents the blog CSP, hash maintenance, and release verification.
deploy/​make-release.sh Runs the CSP gate during release builds.
deploy/​Caddyfile Adds the enforcing blog CSP header; the current policy blocks the existing Google Maps shortcode.
checks/​tests/​test_check_csp_scripts.py Adds CSP gate tests; contains f-strings that are invalid on supported Python 3.10/3.11 versions.
checks/​security-headers.txt Records the canonical blog CSP value.
checks/​README.md Adds the CSP validation check to the checks catalog.
checks/​check-csp-scripts.py Validates inline hashes and script sources; its MIME-type allowlist misses executable JavaScript MIME variants.

Review effort: Lite

Comment thread checks/check-csp-scripts.py Resolved
Comment thread checks/tests/test_check_csp_scripts.py Resolved
Comment thread deploy/Caddyfile Resolved
@ptr727

ptr727 commented Oct 25, 2026

Copy link
Copy Markdown
Owner Author

A recorded local strict-review pass covers head b6371209299c30d2b19e937f6c01240e611f4c88, the content this pull request carries at that commit against develop, and it recorded 2 findings.

@ptr727

ptr727 commented Oct 25, 2026

Copy link
Copy Markdown
Owner Author

A recorded local strict-review pass covers head 0df172fc6b9c6574c20ead11e89a629fbc75831e, the content this pull request carries at that commit against develop, and it recorded 0 findings.

@ptr727
ptr727 merged commit 46628fb into develop Oct 25, 2026
15 of 17 checks passed
@ptr727
ptr727 deleted the feature/blog-csp branch October 25, 2026 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants