Repository navigation
feat: add nonce to style tag - #4356
ericsen-tsai wants to merge 4 commits into
Conversation
|
|
Any update on this PR. @quantizor would you be able to take a look at this |
quantizor
left a comment
There was a problem hiding this comment.
This directly addresses #4258 (nonce configuration) which has strong community demand. The approach is solid:
<meta name="sc-nonce">tag for automatic detectionnonceoption onServerStyleSheetconstructor for SSR- Passes through to style tag creation
Needs a rebase onto current main — there have been significant changes to constants.ts, Sheet.ts, and ServerStyleSheet.tsx. Also needs a changeset file with the correct package name. Would welcome a rebase!
Add multiple ways to configure a CSP nonce for injected <style> tags: 1. StyleSheetManager nonce prop (recommended for Next.js, Remix) 2. ServerStyleSheet constructor nonce option (SSR) 3. <meta property="csp-nonce"> auto-detection (Vite convention) 4. <meta name="sc-nonce"> auto-detection (SC convention) 5. __webpack_nonce__ global (legacy, preserved for backward compat) The nonce flows through SheetOptions to makeStyleTag, ensuring all dynamically created style elements receive the nonce attribute. Based on PR #4356 by @ericsen-tsai. Closes #4258
|
Superseded by #5693 which builds on your work with additional framework support (Vite auto-detection, StyleSheetManager nonce prop, browser .nonce property reading). Your seed commit is attributed as the author. Thank you for the contribution! |
Add multiple ways to configure a CSP nonce for injected <style> tags: 1. StyleSheetManager nonce prop (recommended for Next.js, Remix) 2. ServerStyleSheet constructor nonce option (SSR) 3. <meta property="csp-nonce"> auto-detection (Vite convention) 4. <meta name="sc-nonce"> auto-detection (SC convention) 5. __webpack_nonce__ global (legacy, preserved for backward compat) The nonce flows through SheetOptions to makeStyleTag, ensuring all dynamically created style elements receive the nonce attribute. Based on PR #4356 by @ericsen-tsai. Closes #4258
Add multiple ways to configure a CSP nonce for injected <style> tags: 1. StyleSheetManager nonce prop (recommended for Next.js, Remix) 2. ServerStyleSheet constructor nonce option (SSR) 3. <meta property="csp-nonce"> auto-detection (Vite convention) 4. <meta name="sc-nonce"> auto-detection (SC convention) 5. __webpack_nonce__ global (legacy, preserved for backward compat) The nonce flows through SheetOptions to makeStyleTag, ensuring all dynamically created style elements receive the nonce attribute. Based on PR #4356 by @ericsen-tsai. Closes #4258
Add multiple ways to configure a CSP nonce for injected <style> tags: 1. StyleSheetManager nonce prop (recommended for Next.js, Remix) 2. ServerStyleSheet constructor nonce option (SSR) 3. <meta property="csp-nonce"> auto-detection (Vite convention) 4. <meta name="sc-nonce"> auto-detection (SC convention) 5. __webpack_nonce__ global (legacy, preserved for backward compat) The nonce flows through SheetOptions to makeStyleTag, ensuring all dynamically created style elements receive the nonce attribute. Based on PR #4356 by @ericsen-tsai. Closes #4258
Add multiple ways to configure a CSP nonce for injected <style> tags: 1. StyleSheetManager nonce prop (recommended for Next.js, Remix) 2. ServerStyleSheet constructor nonce option (SSR) 3. <meta property="csp-nonce"> auto-detection (Vite convention) 4. <meta name="sc-nonce"> auto-detection (SC convention) 5. __webpack_nonce__ global (legacy, preserved for backward compat) The nonce flows through SheetOptions to makeStyleTag, ensuring all dynamically created style elements receive the nonce attribute. Based on PR #4356 by @ericsen-tsai. Closes #4258
Add multiple ways to configure a CSP nonce for injected <style> tags: 1. StyleSheetManager nonce prop (recommended for Next.js, Remix) 2. ServerStyleSheet constructor nonce option (SSR) 3. <meta property="csp-nonce"> auto-detection (Vite convention) 4. <meta name="sc-nonce"> auto-detection (SC convention) 5. __webpack_nonce__ global (legacy, preserved for backward compat) The nonce flows through SheetOptions to makeStyleTag, ensuring all dynamically created style elements receive the nonce attribute. Based on PR #4356 by @ericsen-tsai. Closes #4258
Add multiple ways to configure a CSP nonce for injected <style> tags: 1. StyleSheetManager nonce prop (recommended for Next.js, Remix) 2. ServerStyleSheet constructor nonce option (SSR) 3. <meta property="csp-nonce"> auto-detection (Vite convention) 4. <meta name="sc-nonce"> auto-detection (SC convention) 5. __webpack_nonce__ global (legacy, preserved for backward compat) The nonce flows through SheetOptions to makeStyleTag, ensuring all dynamically created style elements receive the nonce attribute. Based on PR #4356 by @ericsen-tsai. Closes #4258 Co-authored-by: Ericsen Tsai <[email protected]>
Add NONCE support and improve nonce handling
Changes
Added a new
NONCEconstant:<meta name="sc-nonce">tag in the document head when available.undefinedif not in a browser environment or if the meta tag is not present.Enhanced
ServerStyleSheetclass:nonceproperty to the class.nonceparameter._emitSheetCSS,getStyleElement, and other methods to use the instance'snoncevalue.Improved nonce handling in
getStyleElement:<meta name="sc-nonce">tag in the returned array when a nonce is present.Modified
getNoncefunction:NONCEconstant as a fallback.Purpose
These changes improve the handling of Content Security Policy (CSP) nonces in styled-components, particularly in server-side rendering scenarios. By allowing the nonce to be set explicitly and adding a meta tag for client-side retrieval, we ensure better consistency between server and client nonce usage.
Impact
ServerStyleSheet.Testing