feat: add nonce support for inline style and script tags in CSP#3709
Merged
Conversation
- Auto-inject nonce attribute onto inline <script> and <style> tags during server rendering (preact_hooks.ts vnode hook) - Expose render nonce via X-Fresh-Nonce response header (context.ts) - Add useNonce option to CSP middleware that replaces 'unsafe-inline' with nonce-based directives per request - Existing explicit nonce attributes on tags are preserved - Non-rendered responses (API routes) fall back to unsafe-inline Supersedes #1787. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This was referenced Mar 28, 2026
- Replace X-Fresh-Nonce header with a Symbol property on the Response object so the nonce never leaks when CSP middleware is absent - Use replaceAll instead of replace for 'unsafe-inline' substitution - Handle unsafe-inline in default-src, script-src-elem, style-src-elem, and style-src-attr directives (not just script-src and style-src) - Document that explicit nonce attributes on tags will be blocked by the CSP header (which only contains the Fresh-generated nonce) - Add tests for nonce non-leakage and default-src replacement Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Supersedes #1787.
Summary
nonceattribute onto inline<script>and<style>tags during server renderinguseNonceoption to the CSP middleware that replaces'unsafe-inline'with per-request'nonce-{value}'directivesX-Fresh-Nonceresponse header (internal, stripped by CSP middleware)Usage
This locks down the CSP policy so only Fresh-rendered inline scripts/styles are allowed. Each request gets a unique nonce.
How it works
nonce={RENDER_STATE.nonce}onto all<script>and<style>elements (unless they already have an explicit nonce)ctx.render()setsX-Fresh-Nonceon the responseuseNonce: true) reads the nonce, replaces'unsafe-inline'with'nonce-{value}'in script-src and style-src, and strips the internal headerTest plan
unsafe-inlinein CSP header (script-src and style-src)<script>tags<style>tagsunsafe-inlinenonceattribute preserveddeno fmtanddeno lintclean🤖 Generated with Claude Code