Skip to content

Remove special casing of toString values when enableTrustedTypesIntegration #17774

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 2 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions packages/react-dom/src/client/DOMPropertyOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import {
OVERLOADED_BOOLEAN,
} from '../shared/DOMProperty';
import sanitizeURL from '../shared/sanitizeURL';
import {toStringOrTrustedType} from './ToStringValue';
import {disableJavaScriptURLs} from 'shared/ReactFeatureFlags';
import {setAttribute, setAttributeNS} from './setAttribute';
import {
disableJavaScriptURLs,
enableTrustedTypesIntegration,
} from 'shared/ReactFeatureFlags';

import type {PropertyInfo} from '../shared/DOMProperty';

Expand Down Expand Up @@ -144,7 +145,10 @@ export function setValueForProperty(
if (value === null) {
node.removeAttribute(attributeName);
} else {
setAttribute(node, attributeName, toStringOrTrustedType(value));
node.setAttribute(
attributeName,
enableTrustedTypesIntegration ? (value: any) : '' + (value: any),
);
}
}
return;
Expand Down Expand Up @@ -176,15 +180,19 @@ export function setValueForProperty(
} else {
// `setAttribute` with objects becomes only `[object]` in IE8/9,
// ('' + value) makes it output the correct toString()-value.
attributeValue = toStringOrTrustedType(value);
if (enableTrustedTypesIntegration) {
attributeValue = (value: any);
} else {
attributeValue = '' + (value: any);
}
if (propertyInfo.sanitizeURL) {
sanitizeURL(attributeValue.toString());
}
}
if (attributeNamespace) {
setAttributeNS(node, attributeNamespace, attributeName, attributeValue);
node.setAttributeNS(attributeNamespace, attributeName, attributeValue);
} else {
setAttribute(node, attributeName, attributeValue);
node.setAttribute(attributeName, attributeValue);
}
}
}
6 changes: 1 addition & 5 deletions packages/react-dom/src/client/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ import possibleStandardNames from '../shared/possibleStandardNames';
import {validateProperties as validateARIAProperties} from '../shared/ReactDOMInvalidARIAHook';
import {validateProperties as validateInputProperties} from '../shared/ReactDOMNullInputValuePropHook';
import {validateProperties as validateUnknownProperties} from '../shared/ReactDOMUnknownPropertyHook';
import {toStringOrTrustedType} from './ToStringValue';

import {
enableDeprecatedFlareAPI,
Expand Down Expand Up @@ -787,10 +786,7 @@ export function diffProperties(
const lastHtml = lastProp ? lastProp[HTML] : undefined;
if (nextHtml != null) {
if (lastHtml !== nextHtml) {
(updatePayload = updatePayload || []).push(
propKey,
toStringOrTrustedType(nextHtml),
);
(updatePayload = updatePayload || []).push(propKey, nextHtml);
}
} else {
// TODO: It might be too late to clear this if we have children
Expand Down
33 changes: 0 additions & 33 deletions packages/react-dom/src/client/ToStringValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
* @flow
*/

import {enableTrustedTypesIntegration} from 'shared/ReactFeatureFlags';

export opaque type ToStringValue =
| boolean
| number
Expand Down Expand Up @@ -37,34 +35,3 @@ export function getToStringValue(value: mixed): ToStringValue {
return '';
}
}

/** Trusted value is a wrapper for "safe" values which can be assigned to DOM execution sinks. */
export opaque type TrustedValue: {toString(): string, valueOf(): string} = {
toString(): string,
valueOf(): string,
};

/**
* We allow passing objects with toString method as element attributes or in dangerouslySetInnerHTML
* and we do validations that the value is safe. Once we do validation we want to use the validated
* value instead of the object (because object.toString may return something else on next call).
*
* If application uses Trusted Types we don't stringify trusted values, but preserve them as objects.
*/
export let toStringOrTrustedType: any => string | TrustedValue = toString;
if (enableTrustedTypesIntegration && typeof trustedTypes !== 'undefined') {
toStringOrTrustedType = value => {
if (
typeof value === 'object' &&
(trustedTypes.isHTML(value) ||
trustedTypes.isScript(value) ||
trustedTypes.isScriptURL(value) ||
/* TrustedURLs are deprecated and will be removed soon: https://github.com/WICG/trusted-types/pull/204 */
(trustedTypes.isURL && trustedTypes.isURL(value)))
) {
// Pass Trusted Types through.
return value;
}
return toString(value);
};
}
35 changes: 0 additions & 35 deletions packages/react-dom/src/client/setAttribute.js

This file was deleted.

3 changes: 1 addition & 2 deletions packages/react-dom/src/client/setInnerHTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import {Namespaces} from '../shared/DOMNamespaces';
import createMicrosoftUnsafeLocalFunction from '../shared/createMicrosoftUnsafeLocalFunction';
import type {TrustedValue} from './ToStringValue';
import {enableTrustedTypesIntegration} from 'shared/ReactFeatureFlags';

// SVG temp container for IE lacking innerHTML
Expand All @@ -24,7 +23,7 @@ let reusableSVGContainer;
*/
const setInnerHTML = createMicrosoftUnsafeLocalFunction(function(
node: Element,
html: string | TrustedValue,
html: {valueOf(): {toString(): string}},
): void {
if (node.namespaceURI === Namespaces.svg) {
if (__DEV__) {
Expand Down