Skip to content

feat: add nonce to style tag - #4356

Closed
ericsen-tsai wants to merge 4 commits into
styled-components:mainfrom
ericsen-tsai:main
Closed

ericsen-tsai wants to merge 4 commits into
styled-components:mainfrom
ericsen-tsai:main

Conversation

@ericsen-tsai

Copy link
Copy Markdown
Contributor

Add NONCE support and improve nonce handling

Changes

  1. Added a new NONCE constant:

    • Retrieves the nonce value from a <meta name="sc-nonce"> tag in the document head when available.
    • Falls back to undefined if not in a browser environment or if the meta tag is not present.
  2. Enhanced ServerStyleSheet class:

    • Added a nonce property to the class.
    • Modified the constructor to accept an optional nonce parameter.
    • Updated _emitSheetCSS, getStyleElement, and other methods to use the instance's nonce value.
  3. Improved nonce handling in getStyleElement:

    • Now includes a <meta name="sc-nonce"> tag in the returned array when a nonce is present.
  4. Modified getNonce function:

    • Updated to use the new NONCE constant 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

  • Improves security by supporting CSP nonces more robustly.
  • Enhances server-side rendering capabilities with better nonce handling.
  • Provides a more flexible API for setting nonces in ServerStyleSheet.

Testing

  • Tested server-side rendering with and without nonces.
  • Verified client-side nonce retrieval from the new meta tag.
  • Ensured backward compatibility with existing usage patterns.

@changeset-bot

changeset-bot Bot commented Oct 5, 2024 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 5b5b8d5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@ameersami

Copy link
Copy Markdown

Any update on this PR. @quantizor would you be able to take a look at this

@quantizor quantizor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This directly addresses #4258 (nonce configuration) which has strong community demand. The approach is solid:

  • <meta name="sc-nonce"> tag for automatic detection
  • nonce option on ServerStyleSheet constructor 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!

quantizor pushed a commit that referenced this pull request Mar 19, 2026
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
@quantizor quantizor mentioned this pull request Mar 19, 2026
3 of 5 tasks
@quantizor

Copy link
Copy Markdown
Contributor

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!

@quantizor quantizor closed this Mar 19, 2026
quantizor pushed a commit that referenced this pull request Mar 19, 2026
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
quantizor pushed a commit that referenced this pull request Mar 19, 2026
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
quantizor pushed a commit that referenced this pull request Mar 19, 2026
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
quantizor pushed a commit that referenced this pull request Mar 19, 2026
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
quantizor pushed a commit that referenced this pull request Mar 19, 2026
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
quantizor added a commit that referenced this pull request Mar 19, 2026
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]>
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.

3 participants