Skip to content

feat(ci): add CSP, HSTS headers and PR preview deployments - #3

Merged
ace139 merged 1 commit into
mainfrom
feat/cloudflare-best-practices
Jan 24, 2026
Merged

feat(ci): add CSP, HSTS headers and PR preview deployments#3
ace139 merged 1 commit into
mainfrom
feat/cloudflare-best-practices

Conversation

@ace139

@ace139 ace139 commented Jan 24, 2026

Copy link
Copy Markdown
Owner

Summary

This PR improves the Cloudflare Pages deployment setup to follow security best practices:

  • Content-Security-Policy: Added CSP header that allows PostHog analytics, inline scripts (needed for Astro), and inline styles (Tailwind)
  • HSTS: Added Strict-Transport-Security header (1 year, includeSubDomains) for browser-level HTTPS enforcement
  • Headers file order: Fixed the _headers file to have specific paths first, global rules last (per Cloudflare best practices)
  • PR preview deployments: New workflow that deploys PRs to branch-specific URLs and comments the preview link
  • Deployment URL output: Production deployments now show URLs in GitHub Actions summary

Changes

File Change
scripts/generate-headers.js Restructured headers, added CSP and HSTS
.github/workflows/deploy.yml Added deployment URL to job summary
.github/workflows/preview.yml New workflow for PR previews

CSP Policy Details

default-src 'self'; 
script-src 'self' 'unsafe-inline' https://us.i.posthog.com; 
style-src 'self' 'unsafe-inline'; 
img-src 'self' data: https:; 
font-src 'self' data:; 
connect-src 'self' https://us.i.posthog.com; 
frame-ancestors 'none'; 
base-uri 'self'; 
form-action 'self'

Testing

  • Build passes locally (bun run build)
  • _headers file generated correctly
  • CSP doesn't break site in browser (verify after deploy)
  • PR preview workflow posts comment (verify with this PR)

What's NOT included

  • preload directive for HSTS (requires hstspreload.org submission, irreversible)
  • _redirects file (www redirect handled by Cloudflare Bulk Redirects, Astro handles trailing slashes)

Generated with Claude Code

- Add Content-Security-Policy header allowing PostHog and inline scripts
- Add HSTS header (1 year, includeSubDomains)
- Fix _headers file order (specific paths first, global rules last)
- Add PR preview deployment workflow with comment on PRs
- Add deployment URL output to GitHub Actions summary
- Add /_astro/* cache rules for Astro hashed assets

Co-Authored-By: Claude <noreply@anthropic.com>
@netlify

netlify Bot commented Jan 24, 2026

Copy link
Copy Markdown

Deploy Preview for soumyo-dey ready!

Name Link
🔨 Latest commit 4693bf3
🔍 Latest deploy log https://app.netlify.com/projects/soumyo-dey/deploys/6974583e1c6c9d000835ac21
😎 Deploy Preview https://deploy-preview-3--soumyo-dey.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 94
Accessibility: 97
Best Practices: 83
SEO: 92
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions

Copy link
Copy Markdown

Preview Deployment Ready!

Preview URL: https://24811bed.soumyo-com.pages.dev

This preview will be automatically updated when you push new commits.

@ace139
ace139 merged commit 02ba762 into main Jan 24, 2026
5 checks passed
@ace139
ace139 deleted the feat/cloudflare-best-practices branch January 24, 2026 05:28

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4693bf365d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +83 to +84
Permissions-Policy: camera=(), microphone=(), geolocation=()
Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' https://us.i.posthog.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; connect-src 'self' https://us.i.posthog.com; frame-ancestors 'none'; base-uri 'self'; form-action 'self'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Make CSP honor configurable PostHog host

The CSP hardcodes https://us.i.posthog.com for both script-src and connect-src, but the site’s PostHog host is configurable via PUBLIC_POSTHOG_HOST (see src/components/posthog.astro). If that env var is set to a non‑US or self‑hosted domain (e.g. https://eu.i.posthog.com), the browser will block loading /static/array.js and API calls, so analytics silently fails. Consider generating the CSP from the same configured host or allowing the alternative hosts you support.

Useful? React with 👍 / 👎.

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.

1 participant