Skip to content

fix: harden security — meta CSP, security.txt, Firebase WS transport - #146

Merged
therealsama merged 1 commit into
mainfrom
feat/security-csp
Jun 23, 2026
Merged

therealsama merged 1 commit into
mainfrom
feat/security-csp

Conversation

@therealsama

@therealsama therealsama commented Jun 11, 2026 •

Copy link
Copy Markdown
Collaborator

GitHub Pages can't set response headers, so this is done at the static layer:

  • vite.config.js: build-time <meta http-equiv> Content-Security-Policy. script-src
    avoids 'unsafe-inline' by hashing every executable inline script post-transform
    (inert application/ld+json blocks are skipped — script-src doesn't govern them);
    connect-src allows Algolia + Firebase (incl. wss://, via the *.firebaseio.com
    wildcard required by RTDB's dynamic shard hosts). Throws if the <meta charset>
    anchor is missing. Intentionally no upgrade-insecure-requests (it breaks the
    WebKit preview e2e and buys nothing on HTTPS-only Pages).
  • vite.config.js: after injecting the CSP, re-parse the final HTML with jsdom (an
    independent parser, not the extraction regex) and fail the build if any
    executable inline script lacks a matching hash — closes the regex's " src="
    blind spot and guards against future plugins injecting unhashed scripts.
  • vite.config.js: emit /.well-known/security.txt (RFC 9116) with a build-time
    Expires (+6 months) so it can't silently go stale.
  • src/api/hnSdk.ts: pin Firebase RTDB to WebSockets. Its long-poll fallback is
    JSONP <script> injection to *.firebaseio.com/.lp, which the strict script-src
    blocks; forceWebSockets() drops that already-dead transport (lossless under CSP).
  • e2e/csp.spec.ts: assert the production build ships exactly one enforced meta CSP
    and raises zero violations on load + Firebase-backed navigation. A missing CSP or
    a dropped inline-script hash is otherwise functionally invisible to the suite.
  • SECURITY.md: document the policy and the accepted GitHub Pages constraints,
    including the no-frame-ancestors/no-X-Frame-Options clickjacking gap.

@therealsama
therealsama requested a review from kplachkov as a code owner June 11, 2026 14:40
@therealsama
therealsama force-pushed the feat/security-csp branch 2 times, most recently from 594d86c to 9ecf816 Compare June 14, 2026 08:35
@therealsama therealsama changed the title Harden security: meta CSP, security.txt, Firebase WS transport fix: harden security — meta CSP, security.txt, Firebase WS transport Jun 14, 2026
@therealsama
therealsama force-pushed the feat/security-csp branch 4 times, most recently from 100b9c3 to f6fd162 Compare June 21, 2026 06:46
@therealsama
therealsama enabled auto-merge June 21, 2026 19:19
auto-merge was automatically disabled June 21, 2026 19:29

Repository rule violations found

@therealsama
therealsama enabled auto-merge June 21, 2026 19:34
@therealsama
therealsama force-pushed the feat/security-csp branch 2 times, most recently from 71e0289 to 8a9fa59 Compare June 22, 2026 07:00
GitHub Pages can't set response headers, so this is done at the static layer:

- vite.config.js: build-time <meta http-equiv> Content-Security-Policy. script-src
  avoids 'unsafe-inline' by hashing every executable inline script post-transform
  (inert application/ld+json blocks are skipped — script-src doesn't govern them);
  connect-src allows Algolia + Firebase (incl. wss://, via the *.firebaseio.com
  wildcard required by RTDB's dynamic shard hosts). Throws if the <meta charset>
  anchor is missing. Intentionally no upgrade-insecure-requests (it breaks the
  WebKit preview e2e and buys nothing on HTTPS-only Pages).
- vite.config.js: after injecting the CSP, re-parse the final HTML with jsdom (an
  independent parser, not the extraction regex) and fail the build if any
  executable inline script lacks a matching hash — closes the regex's " src="
  blind spot and guards against future plugins injecting unhashed scripts.
- vite.config.js: emit /.well-known/security.txt (RFC 9116) with a build-time
  Expires (+6 months) so it can't silently go stale.
- src/api/hnSdk.ts: pin Firebase RTDB to WebSockets. Its long-poll fallback is
  JSONP <script> injection to *.firebaseio.com/.lp, which the strict script-src
  blocks; forceWebSockets() drops that already-dead transport (lossless under CSP).
- e2e/csp.spec.ts: assert the production build ships exactly one enforced meta CSP
  and raises zero violations on load + Firebase-backed navigation. A missing CSP or
  a dropped inline-script hash is otherwise functionally invisible to the suite.
- SECURITY.md: document the policy and the accepted GitHub Pages constraints,
  including the no-frame-ancestors/no-X-Frame-Options clickjacking gap.
@therealsama
therealsama merged commit 94f404f into main Jun 23, 2026
4 checks passed
@therealsama
therealsama deleted the feat/security-csp branch June 23, 2026 05:55
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