Add trusted types to react on server side#16555
Closed
Siegrift wants to merge 2 commits intofacebook:masterfrom
Siegrift:tt-react-server
Closed
Add trusted types to react on server side#16555Siegrift wants to merge 2 commits intofacebook:masterfrom Siegrift:tt-react-server
Siegrift wants to merge 2 commits intofacebook:masterfrom
Siegrift:tt-react-server
Conversation
|
ReactDOM: size: 🔺+2.1%, gzip: 🔺+2.1% Details of bundled changes.Comparing: 507f0fb...df6577b react-dom
Generated by 🚫 dangerJS |
13 tasks
Contributor
|
We've just published a |
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution. |
Collaborator
|
Apologies for closing. We've accidentally pushed a master branch to the repo (due to an old fork), then deleted it, and this lead to a bunch of PRs getting auto-closed. GitHub seems confused and unfortunately does not offer a way to reopen this against |
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.
Trusted Types
Trusted Types (spec, introductory article) is a new experimental DOM API implemented within the WICG , with a working Chrome implementation.
The API creates a few new objects available on the global object in the browser, like most other web APIs (impl in TS and in Closure compiler).
Under certain conditions, controlled by a HTTP header (analogous to Content-Security-Policy behavior), the API can enable the enforcement - then it changes the signature of several DOM API functions and property setters, such that they accept specific object types, and reject strings. Colloquially, DOM API becomes strongly typed.
For example, with Trusted Types Element.innerHTML property setter accepts a TrustedHTML object.
Trusted Type objects stringify to their inner value. This API shape is a deliberate choice that enables existing web applications and libraries to gradually migrate from strings to Trusted Types without breaking functionality. In our example, it makes it possible to write the following:
The above code works regardless if the Trusted Types enforcement is enabled or not.
Reading from the DOM is unaffected, so Element.innerHTML getter returns a string. That's for practical reasons -- web applications read from DOM more often than they write to it, and only writing exposes the application to DOM XSS risks. Typing only the setters allows us to secure web applications with minimal code changes.
Adding Trusted Types to React for server side
Unfortunately, there are no Trusted Types (TT) on server side. However, it’s really easy to introduce a reflected xss attack via server side rendering. Markup rendered on server side is rendered to string (without any DOM emulation) and returned as a response from the server and there is no way TT can prevent this attack. This creates inconsistency when rendering on client and server side (client side would fail with TT violation), which shouldn’t happen. Also, if application uses hot reloading, you will get a TT error after each reload (because React templates are re-rendered to DOM, this time on client).
This PR enables applications to use Trusted Types on server side. Functions inside ReactDOMNodeStreamRenderer.js and ReactDOMStringRenderer.js in react-dom/server package now accept optional third parameter with trusted types polyfill implementation. If TT are provided, they are enforced, otherwise no behavioral change is made. If TT are enforced, we check whether the values are trusted before creating the markup from them and throw an error otherwise.
Reference