Skip to content

Allow nonce to be used on hoistable styles #32461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
May 29, 2025

Conversation

Andarist
Copy link
Contributor

fixes #32449

This is my first time touching this code. There are multiple systems in place here and I wouldn't be surprised to learn that this has to be handled in some other areas too. I have found some other style-related code areas but I had no time yet to double-check them.

cc @gnoff

@react-sizebot
Copy link

react-sizebot commented Feb 23, 2025

Comparing: c0464ae...acc9f05

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 6.68 kB 6.68 kB = 1.83 kB 1.83 kB
oss-stable/react-dom/cjs/react-dom-client.production.js = 529.83 kB 529.83 kB = 93.51 kB 93.51 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 6.69 kB 6.69 kB = 1.83 kB 1.83 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js = 650.93 kB 650.93 kB = 114.65 kB 114.65 kB
facebook-www/ReactDOM-prod.classic.js = 675.87 kB 675.87 kB = 118.93 kB 118.93 kB
facebook-www/ReactDOM-prod.modern.js = 666.15 kB 666.15 kB = 117.31 kB 117.31 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
test_utils/ReactAllWarnings.js +0.72% 65.97 kB 66.44 kB +0.63% 16.57 kB 16.67 kB
oss-stable-semver/react-dom/cjs/react-dom-server.bun.development.js +0.50% 344.03 kB 345.75 kB +0.44% 66.72 kB 67.01 kB
oss-stable/react-dom/cjs/react-dom-server.bun.development.js +0.50% 344.11 kB 345.83 kB +0.44% 66.75 kB 67.04 kB
oss-stable-semver/react-dom/cjs/react-dom-server.node.development.js +0.48% 400.78 kB 402.73 kB +0.40% 72.16 kB 72.45 kB
oss-stable/react-dom/cjs/react-dom-server.node.development.js +0.48% 400.86 kB 402.80 kB +0.40% 72.20 kB 72.50 kB
oss-stable-semver/react-dom/cjs/react-dom-server.browser.development.js +0.48% 404.22 kB 406.15 kB +0.41% 72.80 kB 73.10 kB
oss-stable/react-dom/cjs/react-dom-server.browser.development.js +0.48% 404.30 kB 406.22 kB +0.41% 72.85 kB 73.15 kB
oss-stable-semver/react-dom/cjs/react-dom-server.edge.development.js +0.48% 405.00 kB 406.93 kB +0.42% 72.94 kB 73.25 kB
oss-stable/react-dom/cjs/react-dom-server.edge.development.js +0.48% 405.08 kB 407.01 kB +0.42% 72.99 kB 73.30 kB
oss-experimental/react-markup/cjs/react-markup.development.js +0.43% 388.45 kB 390.11 kB +0.51% 69.66 kB 70.02 kB
oss-experimental/react-dom/cjs/react-dom-server.node.development.js +0.40% 443.32 kB 445.10 kB +0.46% 77.40 kB 77.76 kB
oss-experimental/react-dom/cjs/react-dom-server.bun.development.js +0.40% 377.07 kB 378.59 kB +0.49% 71.93 kB 72.28 kB
oss-experimental/react-dom/cjs/react-dom-server.browser.development.js +0.40% 447.32 kB 449.09 kB +0.49% 78.04 kB 78.42 kB
oss-experimental/react-dom/cjs/react-dom-server.edge.development.js +0.39% 448.33 kB 450.10 kB +0.49% 78.25 kB 78.63 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.node.development.js +0.39% 384.21 kB 385.71 kB +0.40% 69.53 kB 69.81 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.browser.development.js +0.39% 384.21 kB 385.71 kB +0.40% 69.53 kB 69.81 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.node.development.js +0.39% 384.23 kB 385.74 kB +0.40% 69.56 kB 69.83 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.browser.development.js +0.39% 384.23 kB 385.74 kB +0.40% 69.56 kB 69.83 kB
oss-stable-semver/react-dom/cjs/react-dom-server.browser.production.js +0.37% 252.27 kB 253.21 kB +0.31% 45.57 kB 45.71 kB
oss-stable/react-dom/cjs/react-dom-server.browser.production.js +0.37% 252.34 kB 253.29 kB +0.31% 45.59 kB 45.73 kB
facebook-www/ReactDOMServer-dev.modern.js +0.37% 404.64 kB 406.15 kB +0.41% 72.55 kB 72.84 kB
facebook-www/ReactDOMServer-dev.classic.js +0.37% 408.10 kB 409.61 kB +0.41% 73.11 kB 73.41 kB
oss-stable-semver/react-dom/cjs/react-dom-server.edge.production.js +0.37% 257.58 kB 258.52 kB +0.30% 47.56 kB 47.70 kB
oss-stable/react-dom/cjs/react-dom-server.edge.production.js +0.37% 257.66 kB 258.60 kB +0.30% 47.58 kB 47.72 kB
oss-stable-semver/react-dom/cjs/react-dom-server.node.production.js +0.37% 254.17 kB 255.10 kB +0.32% 46.58 kB 46.73 kB
oss-stable/react-dom/cjs/react-dom-server.node.production.js +0.37% 254.25 kB 255.18 kB +0.32% 46.61 kB 46.76 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.node.development.js +0.36% 417.27 kB 418.78 kB +0.42% 74.07 kB 74.38 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.browser.development.js +0.36% 417.27 kB 418.78 kB +0.42% 74.07 kB 74.38 kB
facebook-www/ReactDOMServerStreaming-dev.modern.js +0.36% 402.05 kB 403.48 kB +0.40% 71.86 kB 72.15 kB
oss-stable-semver/react-dom/cjs/react-dom-server.bun.production.js +0.34% 236.60 kB 237.39 kB +0.30% 43.20 kB 43.32 kB
oss-stable/react-dom/cjs/react-dom-server.bun.production.js +0.34% 236.67 kB 237.47 kB +0.30% 43.22 kB 43.35 kB
oss-experimental/react-markup/cjs/react-markup.react-server.development.js +0.29% 570.25 kB 571.91 kB +0.37% 101.88 kB 102.26 kB
oss-experimental/react-markup/cjs/react-markup.production.js +0.26% 246.08 kB 246.72 kB +0.41% 44.87 kB 45.05 kB
oss-experimental/react-dom/cjs/react-dom-server.bun.production.js +0.24% 269.79 kB 270.45 kB +0.39% 48.08 kB 48.26 kB
oss-experimental/react-dom/cjs/react-dom-server.browser.production.js +0.24% 288.92 kB 289.60 kB +0.37% 50.30 kB 50.49 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.browser.production.js +0.23% 230.04 kB 230.58 kB +0.34% 41.67 kB 41.81 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.browser.production.js +0.23% 230.06 kB 230.60 kB +0.34% 41.70 kB 41.84 kB
oss-experimental/react-dom/cjs/react-dom-server.edge.production.js +0.23% 294.86 kB 295.54 kB +0.37% 52.48 kB 52.67 kB
oss-experimental/react-dom/cjs/react-dom-server.node.production.js +0.23% 290.82 kB 291.49 kB +0.37% 51.50 kB 51.70 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.node.production.js +0.23% 234.66 kB 235.20 kB +0.33% 43.47 kB 43.62 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.node.production.js +0.23% 234.69 kB 235.22 kB +0.33% 43.50 kB 43.64 kB
facebook-www/ReactDOMServer-prod.modern.js +0.22% 246.45 kB 246.99 kB +0.38% 44.17 kB 44.34 kB
facebook-www/ReactDOMServer-prod.classic.js +0.21% 251.89 kB 252.43 kB +0.36% 44.91 kB 45.08 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.browser.production.js +0.21% 259.45 kB 259.98 kB +0.37% 45.94 kB 46.11 kB
facebook-www/ReactDOMServerStreaming-prod.modern.js +0.20% 257.72 kB 258.24 kB +0.28% 46.92 kB 47.05 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.node.production.js +0.20% 264.51 kB 265.05 kB +0.30% 47.87 kB 48.01 kB

Generated by 🚫 dangerJS against acc9f05

Comment on lines 2767 to 2770
if (!('nonce' in styleQueue)) {
// `styleQueue` could have been created by `preinit` where `nonce` is not required
styleQueue.nonce = nonce && stringToChunk(escapeTextForBrowser(nonce));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

perhaps we should just consult the the one from the renderState? I'm not sure if it's a user requirement to pass nonce to renderToPipeableStream though and then maintain the consistency across the tree .

Copy link
Collaborator

Choose a reason for hiding this comment

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

we've thought about whether we should auto-nonce styles and scripts but have held off b/c you aren't necessarily in control of every tag you emit and so it's a little safer to still require the author pass these nonces around. However it's not really great protection b/c if you have a compromised package you are probably in trouble in many other ways too.

That said the nonce argument is implied to be for scripts and while rare it's technically possible for the nonce for styles to be different than the nonce for scripts so we'd have to expose another new option. Worth considering perhaps but I think making it just work with rendered props is the way to go

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So my question wasn't about auto-noncing anything but rather about checking the renderState.nonce's value. This way we could cache the chunk on it directly and reuse it here if the per-style nonce would match it. But I'm not sure if it is a requirement to pass nonce to renderToPipeableStream (and that's how renderState learns about the nonce in the first place).

Copy link
Collaborator

Choose a reason for hiding this comment

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

you need to pass nonce to renderToPipeableStream if you are going to use CSP to restrict script execution by nonce. This is because React emits scripts to implement streaming rendering. But we don't typically that this option as an invitation to apply nonces to all possible nonceable things that a user might render like style tags and their own scripts. It's still on you to provide the nonce for your own scripts

Copy link
Collaborator

@gnoff gnoff left a comment

Choose a reason for hiding this comment

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

Look very promising. left some notes for you. lmk if you have any questions

Comment on lines 2767 to 2770
if (!('nonce' in styleQueue)) {
// `styleQueue` could have been created by `preinit` where `nonce` is not required
styleQueue.nonce = nonce && stringToChunk(escapeTextForBrowser(nonce));
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

we've thought about whether we should auto-nonce styles and scripts but have held off b/c you aren't necessarily in control of every tag you emit and so it's a little safer to still require the author pass these nonces around. However it's not really great protection b/c if you have a compromised package you are probably in trouble in many other ways too.

That said the nonce argument is implied to be for scripts and while rare it's technically possible for the nonce for styles to be different than the nonce for scripts so we'd have to expose another new option. Worth considering perhaps but I think making it just work with rendered props is the way to go

};
renderState.styles.set(precedence, styleQueue);
} else {
if (!('nonce' in styleQueue)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The nonce should be known early even if using preinitStyle. we can assume whatever nonce was used at styleQueue creation time is sufficient for all other style rules and don't need this lazy nonce check.

We can separately add a dev only warning which indicates when you are passing incompatible nonces to style tags. You would do this by having a dev only extra property like

const styleQueue = {
  ...
}
if (__DEV__) {
  styleQueue.__nonceString = nonce
}

It wouldn't be part of the type and you'd have to type cast to any when you read it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The nonce should be known early even if using preinitStyle.

nonce isn't a part of the PreinitStyleOptions:

export type PreinitStyleOptions = {
crossOrigin?: ?CrossOriginEnum,
integrity?: ?string,
fetchPriority?: ?string,
};
export type PreinitScriptOptions = {
crossOrigin?: ?CrossOriginEnum,
integrity?: ?string,
fetchPriority?: ?string,
nonce?: ?string,
};

According to the research I've done before opening this, nonce has no effect on external stylesheets. They are controlled by style-src directive. That's the reason I concluded I have to make it work conditionally like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gnoff friendly 🏓

Copy link
Collaborator

Choose a reason for hiding this comment

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

in this context the nonce for style tag would be the nonce you are configuring for style-src CPS directive. It can be but doesn't have to be the same nonce value used for script-src. but from the perspective of preinitStyle this is irrelevant since this just maps the argument to the <style nonce=...> attribute. So you should just add it as an optional option property for PreinitStyleOptions. It's already part of the public typescript type because we merge all the option types for the public API

Copy link
Collaborator

Choose a reason for hiding this comment

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

basically in a correctly configured program you will always pass a nonce to preinitStyle or never. So we can infer that the option passed in on the first invocation actually applies to all invocations

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I then assume that it has to be passed to preinitStyle for inline <style/>s to work properly (as that will require it to be known upfront)?

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah

Copy link
Contributor Author

@Andarist Andarist Apr 25, 2025

Choose a reason for hiding this comment

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

we can assume whatever nonce was used at styleQueue creation time is sufficient for all other style rules and don't need this lazy nonce check.

Can we do that though? Styles texts are merged together. This is how it currently works:

  it('can render styles with nonce', async () => {
    CSPnonce = 'R4nd0m';
    await act(() => {
      const {pipe} = renderToPipeableStream(
        <>
          <style
            href="foo"
            precedence="default"
            nonce={CSPnonce}>{`.foo { color: hotpink; }`}</style>
          <style
            href="bar"
            precedence="default"
            nonce={CSPnonce}>{`.bar { background-color: blue; }`}</style>
        </>,
      );
      pipe(writable);
    });
    expect(document.querySelector('style').nonce).toBe(CSPnonce);
    expect(getVisibleChildren(document)).toEqual(
      <html>
        <head />
        <body>
          <div id="container">
            <style
              data-precedence="default"
              data-href="foo bar"
              nonce={
                CSPnonce
              }>{`.foo { color: hotpink; }.bar { background-color: blue; }`}</style>
          </div>
        </body>
      </html>,
    );
  });

But now if the second style element would have a different nonce we really shouldn't merge it with the first style (the one with the "correct" nonce).

styleQueue.nonce = nonce && stringToChunk(escapeTextForBrowser(nonce));
}
if (__DEV__) {
if (nonce !== styleQueue.nonce) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

this won't work b/c Chunks are sometimes objects (typed arrays). But if you do what I suggested above you can use a dev only string version of the nonce to determine if this warning is necessary

);
});

// @gate __DEV__
Copy link
Collaborator

Choose a reason for hiding this comment

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

the right way to assert warnings is to use assertConsoleErrorDev inside a test that isn't gated specifically on dev environment.

In this case you might want to assert the existed behavior that both rules are included in the final output style tag regardless of the bad nonce and that in dev you get a warning for it. In this case you're not actually asserting that the program output any styles at all so while I suspect it works it'd be good to encode the expected semantic even when the test is about the warning

@facebook facebook deleted a comment from Alexectramagneta Feb 25, 2025
@Andarist Andarist requested a review from gnoff March 6, 2025 09:02
@Andarist
Copy link
Contributor Author

Andarist commented May 7, 2025

@gnoff friendly 🏓

@sebmarkbage
Copy link
Collaborator

I think we'll want to make this option separate for style and script so that you don't have to include the nonce for every style tag if you're not enforcing it on styles. E.g. nonce: { script: ..., style: ... }

@hi-ogawa
Copy link

hi-ogawa commented May 10, 2025

Would this also require passing along nonce in ReactDOMFloat.js? Otherwise ReactDOM.preinit(href, { as: "style", nonce }) might still drop nonce, which was the case for ReactDOM.preloadModule #33120.

if (as === 'style') {
ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */
.S(
/* preinitStyle */
href,
typeof options.precedence === 'string'
? options.precedence
: undefined,
{
crossOrigin,
integrity,
fetchPriority,
},
);

(EDIT: oh, but this doesn't matter in practice since ReactDOM.preinit(href, ...) is not inline script/style?)

@Andarist
Copy link
Contributor Author

I think we'll want to make this option separate for style and script so that you don't have to include the nonce for every style tag if you're not enforcing it on styles. E.g. nonce: { script: ..., style: ... }

I thought this was explicitly a stated non-goal, see: #32461 (comment)

@gnoff
Copy link
Collaborator

gnoff commented May 23, 2025

I think we'll want to make this option separate for style and script so that you don't have to include the nonce for every style tag if you're not enforcing it on styles. E.g. nonce: { script: ..., style: ... }

I thought this was explicitly a stated non-goal, see: #32461 (comment)

Yeah the nonce argument is about providing a nonce to React to use for things it needs to emit that the user doesn't render directly. At the moment I don't think there are any styles that React emits so I don't think we actually need to add this option. But we could go with this if that were to change.

@gnoff
Copy link
Collaborator

gnoff commented May 23, 2025

@Andarist I think I know what we should do to resolves the consistency / security issues you brought up.

type Options = {
  ...
  nonce?: string | { script?: string, style?: string }
  ...
}

we define nonce as implicitly script nonce only if it is a string. Then whenever you opt into react-managed styles you need to provide the matching nonce you passed into the render call. If you pass the wrong nonce we log an error and ignore the style.

Conversely if you did not configure the render to convey that there is a nonce then if you pass a nonce to the style it will log an error and include the style but it will not have the nonce value.

So to get nonces on style tags you must pass it to the render method as an option.
To get style rules into those style tags you must provide the nonce on the rule when rendered

I'm not sure we need to be as prescriptive with external stylesheets since we can just emit each one with the provided nonce and then the browser will or will not load and apply the stylesheet.

@Andarist Andarist force-pushed the fix/style-precendence-with-nonce branch 2 times, most recently from adeb8b7 to 1c11d72 Compare May 26, 2025 22:28
@Andarist Andarist force-pushed the fix/style-precendence-with-nonce branch from 1c11d72 to 6936609 Compare May 26, 2025 22:29
@Andarist
Copy link
Contributor Author

@gnoff I implemented the requested changes. Could you take a look?

Copy link
Collaborator

@gnoff gnoff left a comment

Choose a reason for hiding this comment

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

looks good but think we should land a few adjustments first

@@ -5176,6 +5230,7 @@ function flushStyleTagsLateForBoundary(
}
let i = 0;
if (hrefs.length) {
writeChunk(this, styleQueue.start);
Copy link
Collaborator

Choose a reason for hiding this comment

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

any reason we don't just use renderState.startInlineStyle? doesn't seem necessary to have this on every style queue when it must by necessity be the same for each queue

Copy link
Contributor Author

@Andarist Andarist May 29, 2025

Choose a reason for hiding this comment

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

renderState isn't available here. So I'd have to introduce extra inline functions at those 2 call sites to pass the extra argument:

// ...
hoistableState.styles.forEach(flushStyleTagsLateForBoundary, destination);

// ...
renderState.styles.forEach(flushStylesInPreamble, destination);

I could use a module-level variable instead though. I'll push this change in a second.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done in 68d592a but I imagine you might want to have it done slightly differently - if that's the case I'd appreciate concrete tips as to the preferred solution that would match codebase's style and preferences

@Andarist Andarist requested a review from gnoff May 29, 2025 10:03
@gnoff gnoff merged commit 14094f8 into facebook:main May 29, 2025
240 checks passed
github-actions bot pushed a commit that referenced this pull request May 29, 2025
fixes #32449

This is my first time touching this code. There are multiple systems in
place here and I wouldn't be surprised to learn that this has to be
handled in some other areas too. I have found some other style-related
code areas but I had no time yet to double-check them.

cc @gnoff

DiffTrain build for [14094f8](14094f8)
@Andarist Andarist deleted the fix/style-precendence-with-nonce branch May 29, 2025 15:40
github-actions bot pushed a commit to code/lib-react that referenced this pull request May 29, 2025
fixes facebook#32449

This is my first time touching this code. There are multiple systems in
place here and I wouldn't be surprised to learn that this has to be
handled in some other areas too. I have found some other style-related
code areas but I had no time yet to double-check them.

cc @gnoff

DiffTrain build for [14094f8](facebook@14094f8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

nonce is dropped from style tags that use precedence
6 participants