Skip to content

Commit

Permalink
Remove Numeric Fallback of Symbols
Browse files Browse the repository at this point in the history
This was already defeating the XSS issue that Symbols was meant to protect
against. So you were already supposed to use a polyfill for security.

We rely on real Symbol.for in Flight for Server Components so those require
real symbols anyway.

We also don't really support IE without additional polyfills anyway.
  • Loading branch information
sebmarkbage committed Feb 23, 2022
1 parent 4035157 commit b0ee098
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 62 deletions.
63 changes: 20 additions & 43 deletions packages/shared/ReactSymbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,27 @@
// When adding new symbols to this file,
// Please consider also adding to 'react-devtools-shared/src/backend/ReactSymbols'

// The Symbol used to tag the ReactElement-like types. If there is no native Symbol
// nor polyfill, then a plain number is used for performance.
export let REACT_ELEMENT_TYPE = 0xeac7;
export let REACT_PORTAL_TYPE = 0xeaca;
export let REACT_FRAGMENT_TYPE = 0xeacb;
export let REACT_STRICT_MODE_TYPE = 0xeacc;
export let REACT_PROFILER_TYPE = 0xead2;
export let REACT_PROVIDER_TYPE = 0xeacd;
export let REACT_CONTEXT_TYPE = 0xeace;
export let REACT_FORWARD_REF_TYPE = 0xead0;
export let REACT_SUSPENSE_TYPE = 0xead1;
export let REACT_SUSPENSE_LIST_TYPE = 0xead8;
export let REACT_MEMO_TYPE = 0xead3;
export let REACT_LAZY_TYPE = 0xead4;
export let REACT_SCOPE_TYPE = 0xead7;
export let REACT_DEBUG_TRACING_MODE_TYPE = 0xeae1;
export let REACT_OFFSCREEN_TYPE = 0xeae2;
export let REACT_LEGACY_HIDDEN_TYPE = 0xeae3;
export let REACT_CACHE_TYPE = 0xeae4;
export let REACT_TRACING_MARKER_TYPE = 0xeae5;
// The Symbol used to tag the ReactElement-like types.
export let REACT_ELEMENT_TYPE = Symbol.for('react.element');
export let REACT_PORTAL_TYPE = Symbol.for('react.portal');
export let REACT_FRAGMENT_TYPE = Symbol.for('react.fragment');
export let REACT_STRICT_MODE_TYPE = Symbol.for('react.strict_mode');
export let REACT_PROFILER_TYPE = Symbol.for('react.profiler');
export let REACT_PROVIDER_TYPE = Symbol.for('react.provider');
export let REACT_CONTEXT_TYPE = Symbol.for('react.context');
export let REACT_FORWARD_REF_TYPE = Symbol.for('react.forward_ref');
export let REACT_SUSPENSE_TYPE = Symbol.for('react.suspense');
export let REACT_SUSPENSE_LIST_TYPE = Symbol.for('react.suspense_list');
export let REACT_MEMO_TYPE = Symbol.for('react.memo');
export let REACT_LAZY_TYPE = Symbol.for('react.lazy');
export let REACT_SCOPE_TYPE = Symbol.for('react.scope');
export let REACT_DEBUG_TRACING_MODE_TYPE = Symbol.for('react.debug_trace_mode');
export let REACT_OFFSCREEN_TYPE = Symbol.for('react.offscreen');
export let REACT_LEGACY_HIDDEN_TYPE = Symbol.for('react.legacy_hidden');
export let REACT_CACHE_TYPE = Symbol.for('react.cache');
export let REACT_TRACING_MARKER_TYPE = Symbol.for('react.tracing_marker');

if (typeof Symbol === 'function' && Symbol.for) {
const symbolFor = Symbol.for;
REACT_ELEMENT_TYPE = symbolFor('react.element');
REACT_PORTAL_TYPE = symbolFor('react.portal');
REACT_FRAGMENT_TYPE = symbolFor('react.fragment');
REACT_STRICT_MODE_TYPE = symbolFor('react.strict_mode');
REACT_PROFILER_TYPE = symbolFor('react.profiler');
REACT_PROVIDER_TYPE = symbolFor('react.provider');
REACT_CONTEXT_TYPE = symbolFor('react.context');
REACT_FORWARD_REF_TYPE = symbolFor('react.forward_ref');
REACT_SUSPENSE_TYPE = symbolFor('react.suspense');
REACT_SUSPENSE_LIST_TYPE = symbolFor('react.suspense_list');
REACT_MEMO_TYPE = symbolFor('react.memo');
REACT_LAZY_TYPE = symbolFor('react.lazy');
REACT_SCOPE_TYPE = symbolFor('react.scope');
REACT_DEBUG_TRACING_MODE_TYPE = symbolFor('react.debug_trace_mode');
REACT_OFFSCREEN_TYPE = symbolFor('react.offscreen');
REACT_LEGACY_HIDDEN_TYPE = symbolFor('react.legacy_hidden');
REACT_CACHE_TYPE = symbolFor('react.cache');
REACT_TRACING_MARKER_TYPE = symbolFor('react.tracing_marker');
}

const MAYBE_ITERATOR_SYMBOL = typeof Symbol === 'function' && Symbol.iterator;
const MAYBE_ITERATOR_SYMBOL = Symbol.iterator;
const FAUX_ITERATOR_SYMBOL = '@@iterator';

export function getIteratorFn(maybeIterable: ?any): ?() => ?Iterator<*> {
Expand Down
15 changes: 0 additions & 15 deletions packages/shared/__tests__/ReactSymbols-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,4 @@ describe('ReactSymbols', () => {
it('Symbol values should be unique', () => {
expectToBeUnique(Object.entries(require('shared/ReactSymbols')));
});

it('numeric values should be unique', () => {
const originalSymbolFor = global.Symbol.for;
global.Symbol.for = null;
try {
const entries = Object.entries(require('shared/ReactSymbols')).filter(
// REACT_ASYNC_MODE_TYPE and REACT_CONCURRENT_MODE_TYPE have the same numeric value
// for legacy backwards compatibility
([key]) => key !== 'REACT_ASYNC_MODE_TYPE',
);
expectToBeUnique(entries);
} finally {
global.Symbol.for = originalSymbolFor;
}
});
});
5 changes: 1 addition & 4 deletions packages/shared/isValidElementType.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ import {
enableTransitionTracing,
} from './ReactFeatureFlags';

let REACT_MODULE_REFERENCE: number | Symbol = 0;
if (typeof Symbol === 'function') {
REACT_MODULE_REFERENCE = Symbol.for('react.module.reference');
}
let REACT_MODULE_REFERENCE: Symbol = Symbol.for('react.module.reference');

export default function isValidElementType(type: mixed) {
if (typeof type === 'string' || typeof type === 'function') {
Expand Down

0 comments on commit b0ee098

Please sign in to comment.