Skip to content

Commit e8d87d4

Browse files
committed
[flags] land enableTrustedTypesIntegration (react#35816)
## Summary This flag enables React's integration with the browser [Trusted Types API](https://developer.mozilla.org/en-US/docs/Web/API/Trusted_Types_API). The Trusted Types API is a browser security feature that helps prevent DOM-based XSS attacks. When a site enables Trusted Types enforcement via `Content-Security-Policy: require-trusted-types-for 'script'`, the browser requires that values passed to DOM injection sinks (like `innerHTML`) are typed objects (`TrustedHTML`, `TrustedScript`, `TrustedScriptURL`) created through developer-defined sanitization policies, rather than raw strings. ### What changed Previously, React always coerced values to strings (via `'' + value`) before passing them to DOM APIs like `setAttribute` and `innerHTML`. This broke Trusted Types because it converted typed objects into plain strings, which the browser would then reject under Trusted Types enforcement. React now passes values directly to DOM APIs without string coercion, preserving Trusted Types objects so the browser can validate them. This applies to `dangerouslySetInnerHTML`, all HTML and SVG attributes, and URL attributes (`href`, `action`, etc). ### Before (broken) Using Trusted Types with something like`dangerouslySetInnerHTML` would throw: ```js const sanitizer = trustedTypes.createPolicy('sanitizer', { createHTML: (input) => DOMPurify.sanitize(input), }); function Comment({text}) { const clean = sanitizer.createHTML(text); // clean is a TrustedHTML object, but React would call '' + clean, // converting it back to a plain string before setting innerHTML. // Under Trusted Types enforcement, the browser rejects the string: // // TypeError: Failed to set 'innerHTML' on 'Element': // This document requires 'TrustedHTML' assignment. return <div dangerouslySetInnerHTML={{__html: clean}} />; } ``` ### After (works) React now passes the TrustedHTML object directly to the DOM without stringifying it: ```js const policy = trustedTypes.createPolicy('sanitizer', { createHTML: (input) => DOMPurify.sanitize(input), }); function Comment({text}) { // TrustedHTML objects are passed directly to innerHTML return <div dangerouslySetInnerHTML={{__html: policy.createHTML(text)}} />; } function UserProfile({bio}) { // String attribute values also preserve Trusted Types objects return <div data-bio={policy.createHTML(bio)} />; } ``` ## Non-breaking change - Sites using Trusted Types: React no longer breaks Trusted Types enforcement. TrustedHTML and TrustedScriptURL objects passed through React props are forwarded to the DOM without being stringified. - Sites not using Trusted Types: No behavior change. DOM APIs accept both strings and Trusted Types objects, so removing the explicit string coercion is functionally identical. DiffTrain build for [074d96b](react@074d96b)
1 parent 8b1d595 commit e8d87d4

35 files changed

Lines changed: 236 additions & 349 deletions

compiled/eslint-plugin-react-hooks/index.js

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45054,17 +45054,76 @@ function validateNoRefAccessInRenderImpl(fn, env, errors) {
4505445054
}
4505545055
if (!didError) {
4505645056
const isRefLValue = isUseRefType(instr.lvalue.identifier);
45057-
for (const operand of eachInstructionValueOperand(instr.value)) {
45058-
if (isRefLValue ||
45059-
(hookKind != null &&
45060-
hookKind !== 'useState' &&
45061-
hookKind !== 'useReducer')) {
45057+
if (isRefLValue ||
45058+
(hookKind != null &&
45059+
hookKind !== 'useState' &&
45060+
hookKind !== 'useReducer')) {
45061+
for (const operand of eachInstructionValueOperand(instr.value)) {
4506245062
validateNoDirectRefValueAccess(errors, operand, env);
4506345063
}
45064-
else if (interpolatedAsJsx.has(instr.lvalue.identifier.id)) {
45064+
}
45065+
else if (interpolatedAsJsx.has(instr.lvalue.identifier.id)) {
45066+
for (const operand of eachInstructionValueOperand(instr.value)) {
4506545067
validateNoRefValueAccess(errors, env, operand);
4506645068
}
45067-
else {
45069+
}
45070+
else if (hookKind == null && instr.effects != null) {
45071+
const visitedEffects = new Set();
45072+
for (const effect of instr.effects) {
45073+
let place = null;
45074+
let validation = 'none';
45075+
switch (effect.kind) {
45076+
case 'Freeze': {
45077+
place = effect.value;
45078+
validation = 'direct-ref';
45079+
break;
45080+
}
45081+
case 'Mutate':
45082+
case 'MutateTransitive':
45083+
case 'MutateConditionally':
45084+
case 'MutateTransitiveConditionally': {
45085+
place = effect.value;
45086+
validation = 'ref-passed';
45087+
break;
45088+
}
45089+
case 'Render': {
45090+
place = effect.place;
45091+
validation = 'ref-passed';
45092+
break;
45093+
}
45094+
case 'Capture':
45095+
case 'Alias':
45096+
case 'MaybeAlias':
45097+
case 'Assign':
45098+
case 'CreateFrom': {
45099+
place = effect.from;
45100+
validation = 'ref-passed';
45101+
break;
45102+
}
45103+
case 'ImmutableCapture': {
45104+
place = effect.from;
45105+
const isFrozen = instr.effects.some(e => e.kind === 'Freeze' &&
45106+
e.value.identifier.id === effect.from.identifier.id);
45107+
validation = isFrozen ? 'direct-ref' : 'ref-passed';
45108+
break;
45109+
}
45110+
}
45111+
if (place !== null && validation !== 'none') {
45112+
const key = `${place.identifier.id}:${validation}`;
45113+
if (!visitedEffects.has(key)) {
45114+
visitedEffects.add(key);
45115+
if (validation === 'direct-ref') {
45116+
validateNoDirectRefValueAccess(errors, place, env);
45117+
}
45118+
else {
45119+
validateNoRefPassedToFunction(errors, env, place, place.loc);
45120+
}
45121+
}
45122+
}
45123+
}
45124+
}
45125+
else {
45126+
for (const operand of eachInstructionValueOperand(instr.value)) {
4506845127
validateNoRefPassedToFunction(errors, env, operand, operand.loc);
4506945128
}
4507045129
}

compiled/facebook-www/REVISION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
c0060cf2a695d719152c939cfc3cced8f7da3e52
1+
074d96b9dd57ea748f2e869959a436695bbc88bf
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
c0060cf2a695d719152c939cfc3cced8f7da3e52
1+
074d96b9dd57ea748f2e869959a436695bbc88bf

compiled/facebook-www/React-dev.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,7 @@ __DEV__ &&
14821482
exports.useTransition = function () {
14831483
return resolveDispatcher().useTransition();
14841484
};
1485-
exports.version = "19.3.0-www-classic-c0060cf2-20260224";
1485+
exports.version = "19.3.0-www-classic-074d96b9-20260225";
14861486
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
14871487
"function" ===
14881488
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-dev.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,7 @@ __DEV__ &&
14821482
exports.useTransition = function () {
14831483
return resolveDispatcher().useTransition();
14841484
};
1485-
exports.version = "19.3.0-www-modern-c0060cf2-20260224";
1485+
exports.version = "19.3.0-www-modern-074d96b9-20260225";
14861486
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
14871487
"function" ===
14881488
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-prod.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,4 +610,4 @@ exports.useSyncExternalStore = function (
610610
exports.useTransition = function () {
611611
return ReactSharedInternals.H.useTransition();
612612
};
613-
exports.version = "19.3.0-www-classic-c0060cf2-20260224";
613+
exports.version = "19.3.0-www-classic-074d96b9-20260225";

compiled/facebook-www/React-prod.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,4 +610,4 @@ exports.useSyncExternalStore = function (
610610
exports.useTransition = function () {
611611
return ReactSharedInternals.H.useTransition();
612612
};
613-
exports.version = "19.3.0-www-modern-c0060cf2-20260224";
613+
exports.version = "19.3.0-www-modern-074d96b9-20260225";

compiled/facebook-www/React-profiling.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ exports.useSyncExternalStore = function (
614614
exports.useTransition = function () {
615615
return ReactSharedInternals.H.useTransition();
616616
};
617-
exports.version = "19.3.0-www-classic-c0060cf2-20260224";
617+
exports.version = "19.3.0-www-classic-074d96b9-20260225";
618618
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
619619
"function" ===
620620
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-profiling.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ exports.useSyncExternalStore = function (
614614
exports.useTransition = function () {
615615
return ReactSharedInternals.H.useTransition();
616616
};
617-
exports.version = "19.3.0-www-modern-c0060cf2-20260224";
617+
exports.version = "19.3.0-www-modern-074d96b9-20260225";
618618
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
619619
"function" ===
620620
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/ReactART-dev.classic.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20404,10 +20404,10 @@ __DEV__ &&
2040420404
(function () {
2040520405
var internals = {
2040620406
bundleType: 1,
20407-
version: "19.3.0-www-classic-c0060cf2-20260224",
20407+
version: "19.3.0-www-classic-074d96b9-20260225",
2040820408
rendererPackageName: "react-art",
2040920409
currentDispatcherRef: ReactSharedInternals,
20410-
reconcilerVersion: "19.3.0-www-classic-c0060cf2-20260224"
20410+
reconcilerVersion: "19.3.0-www-classic-074d96b9-20260225"
2041120411
};
2041220412
internals.overrideHookState = overrideHookState;
2041320413
internals.overrideHookStateDeletePath = overrideHookStateDeletePath;
@@ -20442,7 +20442,7 @@ __DEV__ &&
2044220442
exports.Shape = Shape;
2044320443
exports.Surface = Surface;
2044420444
exports.Text = Text;
20445-
exports.version = "19.3.0-www-classic-c0060cf2-20260224";
20445+
exports.version = "19.3.0-www-classic-074d96b9-20260225";
2044620446
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
2044720447
"function" ===
2044820448
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

0 commit comments

Comments
 (0)