Skip to content

security audit v4: remove unsafe-inline from style-src CSP#422

Merged
mohanadft merged 7 commits intomainfrom
security/audit-v4
Apr 13, 2026
Merged

security audit v4: remove unsafe-inline from style-src CSP#422
mohanadft merged 7 commits intomainfrom
security/audit-v4

Conversation

@mohanadft
Copy link
Copy Markdown
Collaborator

@mohanadft mohanadft commented Apr 13, 2026

Summary

  • Removes 'unsafe-inline' from style-src CSP directive, resolving the OWASP A05:2021 Security Misconfiguration finding from the v4 security audit
  • Replaces it with a per-request nonce ('nonce-${nonce}') consistent with how script-src already works
  • Covers all style injection paths: server-rendered <style> elements (HTMLRewriter), and client-side emotion/MUI dynamic injection (document.createElement patch)

Changes

src/middleware.ts

  • style-src uses 'nonce-${nonce}' instead of 'unsafe-inline'
  • HTMLRewriter now injects nonces into <style> elements in addition to <script> elements

src/layouts/Layout.astro

  • Adds <meta name="csp-nonce"> with the per-request nonce for client-side access
  • Adds an is:inline script that patches document.createElement to auto-set .nonce on every <style> element created by JavaScript — covers emotion/MUI across all React component trees without modifying any component files

All .astro files — every style="" HTML attribute removed and replaced with Tailwind:

  • index.astro, membership.astro, donate.astro, faq.astro, about.astro, project-details-temp.astro, team.astro, TFPLogo.astro, Layout.astro

Test plan

  • Verify donate page A/B test still toggles forms correctly
  • Verify floating donate button renders with correct orientation and rounded corners
  • Verify membership hero image crops to center 30%
  • Verify gradient blob decoration visible on About and Project Details pages
  • Confirm no CSP style violations in browser console on production

- Replace style-src 'unsafe-inline' with per-request nonce in middleware
- Extend HTMLRewriter to inject nonces into <style> elements
- Add <meta name="csp-nonce"> to Layout for client-side access
- Patch document.createElement in Layout to auto-nonce emotion/MUI
  injected <style> tags without modifying any component files
- Convert all inline style= attributes in .astro files to Tailwind classes

Resolves OWASP A05:2021 style-src unsafe-inline finding (security audit v4)
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages bot commented Apr 13, 2026

Deploying website with  Cloudflare Pages  Cloudflare Pages

Latest commit: 77a8e77
Status: ✅  Deploy successful!
Preview URL: https://03a79aee.website-aun.pages.dev
Branch Preview URL: https://security-audit-v4.website-aun.pages.dev

View logs

Prevents authenticated requests being forwarded to arbitrary upstream
endpoints (e.g. admin APIs). All legitimate callers (volunteerForm,
inputs-mapping) only call /api/method/* paths.
…llowlists

- Add Element.prototype.setAttribute patch in Layout.astro to convert
  setAttribute('style', ...) calls into individual setProperty() CSSOM
  mutations, preventing CSP violations from Email Octopus form script
- Add https://gallery.eo.page to font-src for Email Octopus custom font
- Add https://www.google.com to frame-src for Qgiv reCAPTCHA iframe
…parse

Email Octopus script injects styled HTML via innerHTML; the browser blocks
style attributes at HTML parse time before any setAttribute intercept fires.
Patch 3 renames style="..." to data-csp-style before handing the string to
the original innerHTML setter, then re-applies each rule via setProperty.
- Add https://secure.qgiv.com to style-src for Qgiv donate form stylesheet
- Add https://validaid.org to frame-src for donate page embed
@mohanadft mohanadft merged commit 9965be3 into main Apr 13, 2026
5 checks passed
@mohanadft mohanadft deleted the security/audit-v4 branch April 13, 2026 08:27
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