8000 Allow `nonce` to be used on hoistable styles by Andarist · Pull Request #32461 · facebook/react · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
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

Open
wants to merge 9 commits into
base: main
Choose a base branch
8000
from

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: 0c28a09...9967076

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 +0.11% 1.83 kB 1.83 kB
oss-stable/react-dom/cjs/react-dom-client.production.js = 527.72 kB 527.72 kB = 93.07 kB 93.07 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 6.69 kB 6.69 kB +0.11% 1.83 kB 1.83 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js = 633.34 kB 633.34 kB = 111.25 kB 111.25 kB
facebook-www/ReactDOM-prod.classic.js = 671.13 kB 671.13 kB = 117.70 kB 117.70 kB
facebook-www/ReactDOM-prod.modern.js = 661.41 kB 661.41 kB = 116.14 kB 116.14 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.20% 65.70 kB 65.83 kB +0.16% 16.50 kB 16.53 kB
oss-experimental/react-markup/cjs/react-markup.react-server.development.js = 537.01 kB 535.46 kB = 96.21 kB 96.08 kB
oss-experimental/react-dom/cjs/react-dom-server.node.development.js = 413.42 kB 412.05 kB = 71.93 kB 71.82 kB
oss-experimental/react-dom/cjs/react-dom-server.edge.development.js = 418.51 kB 417.07 kB = 72.75 kB 72.65 kB
oss-experimental/react-dom/cjs/react-dom-server.browser.development.js = 417.50 kB 416.06 kB = 72.54 kB 72.43 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.browser.development.js = 392.11 kB 390.73 kB = 69.51 kB 69.43 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.node.development.js = 392.11 kB 390.73 kB = 69.51 kB 69.43 kB
facebook-www/ReactDOMServer-dev.classic.js = 381.64 kB 380.27 kB = 68.29 kB 68.20 kB
oss-stable/react-dom/cjs/react-dom-server.node.development.js = 378.07 kB 376.69 kB = 67.93 kB 67.82 kB
oss-stable-semver/react-dom/cjs/react-dom-server.node.development.js = 377.99 kB 376.62 kB = 67.87 kB 67.77 kB
facebook-www/ReactDOMServer-dev.modern.js = 378.19 kB 376.81 kB = 67.76 kB 67.66 kB
oss-experimental/react-dom/cjs/react-dom-server.bun.development.js = 350.01 kB 348.70 kB = 66.45 kB 66.34 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.browser.development.js = 365.44 kB 364.06 kB = 66.27 kB 66.18 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.node.development.js = 365.43 kB 364.06 kB = 66.27 kB 66.18 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.browser.development.js = 365.41 kB 364.03 kB = 66.24 kB 66.15 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.node.development.js = 365.41 kB 364.03 kB = 66.24 kB 66.15 kB
oss-stable/react-dom/cjs/react-dom-server.edge.development.js = 382.39 kB 380.95 kB = 68.72 kB 68.61 kB
oss-stable-semver/react-dom/cjs/react-dom-server.edge.development.js = 382.32 kB 380.88 kB = 68.66 kB 68.55 kB
oss-stable/react-dom/cjs/react-dom-server.browser.development.js = 381.61 kB 380.17 kB = 68.57 kB 68.46 kB
oss-stable-semver/react-dom/cjs/react-dom-server.browser.development.js = 381.54 kB 380.10 kB = 68.52 kB 68.40 kB
facebook-www/ReactDOMServerStreaming-dev.modern.js = 369.48 kB 368.02 kB = 66.42 kB 66.32 kB
oss-stable/react-dom/cjs/react-dom-server.bun.development.js = 325.33 kB 324.01 kB = 63.20 kB 63.09 kB
oss-stable-semver/react-dom/cjs/react-dom-server.bun.development.js = 325.25 kB 323.94 kB = 63.17 kB 63.06 kB
oss-experimental/react-markup/cjs/react-markup.development.js = 362.08 kB 360.53 kB = 64.98 kB 64.87 kB
oss-experimental/react-markup/cjs/react-markup.react-server.production.js = 320.33 kB 318.56 kB = 59.73 kB 59.52 kB
oss-experimental/react-dom/cjs/react-dom-server.node.production.js = 262.34 kB 260.82 kB = 46.20 kB 46.09 kB
oss-experimental/react-dom/cjs/react-dom-server.edge.production.js = 266.42 kB 264.82 kB = 47.21 kB 47.07 kB
oss-experimental/react-dom/cjs/react-dom-server.browser.production.js = 260.47 kB 258.87 kB = 45.07 kB 44.94 kB
oss-stable/react-dom/cjs/react-dom-server.node.production.js = 234.80 kB 233.28 kB = 42.66 kB 42.53 kB
oss-stable-semver/react-dom/cjs/react-dom-server.node.production.js = 234.72 kB 233.20 kB = 42.63 kB 42.50 kB
oss-stable/react-dom/cjs/react-dom-server.edge.production.js = 238.24 kB 236.63 kB = 43.65 kB 43.54 kB
oss-stable-semver/react-dom/cjs/react-dom-server.edge.production.js = 238.16 kB 236.56 kB = 43.62 kB 43.51 kB
oss-stable/react-dom/cjs/react-dom-server.browser.production.js = 233.03 kB 231.43 kB = 41.71 kB 41.58 kB
oss-stable-semver/react-dom/cjs/react-dom-server.browser.production.js = 232.96 kB 231.35 kB = 41.69 kB 41.55 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.node.production.js = 238.03 kB 236.31 kB = 43.42 kB 43.21 kB
oss-experimental/react-dom/cjs/react-dom-server.bun.production.js = 238.57 kB 236.81 kB = 42.82 kB 42.62 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.browser.production.js = 232.95 kB 231.23 kB = 41.51 kB 41.31 kB
facebook-www/ReactDOMServerStreaming-prod.modern.js = 226.44 kB 224.74 kB = 41.53 kB 41.34 kB
facebook-www/ReactDOMServer-prod.classic.js = 224.87 kB 223.15 kB = 40.28 kB 40.09 kB
facebook-www/ReactDOMServer-prod.modern.js = 222.18 kB 220.46 kB = 39.96 kB 39.76 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.node.production.js = 218.49 kB 216.78 kB = 40.65 kB 40.45 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.node.production.js = 218.47 kB 216.75 kB = 40.63 kB 40.42 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.browser.production.js = 213.98 kB 212.26 kB = 38.89 kB 38.70 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.browser.production.js = 213.95 kB 212.24 kB = 38.87 kB 38.68 kB
oss-stable/react-dom/cjs/react-dom-server.bun.production.js = 218.27 kB 216.51 kB = 40.08 kB 39.89 kB
oss-stable-semver/react-dom/cjs/react-dom-server.bun.production.js = 218.19 kB 216.44 kB = 40.05 kB 39.86 kB
oss-experimental/react-markup/cjs/react-markup.production.js = 219.88 kB 218.11 kB = 40.48 kB 40.27 kB

Generated by 🚫 dangerJS against 9967076

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 pro 10000 bably 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

Current status of the PR:

  • the change is implemented
  • as it is, pushLink and preinitStyle accept nonce and assign that to styleQueue. That's not technically needed but it simplifies the implementation. This was requested by @gnoff here.
  • it was proposed to keep original nonce as a dev-only property here to create potential dev-only warnings. But I do have some security concerns mentioned here. So, for the time-being (?), I decided to keep nonce as string on the styleQueue and convert that to a chunk when it gets emitted

@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)

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
0