Skip to content

Commit

Permalink
Fix SSR crash on a hasOwnProperty attribute (facebook#13303)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon authored and segoddnja committed Aug 1, 2018
1 parent d61eb5d commit 23bbbd9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
13 changes: 13 additions & 0 deletions packages/react-dom/src/__tests__/ReactServerRendering-test.js
Expand Up @@ -174,6 +174,19 @@ describe('ReactDOMServer', () => {
(__DEV__ ? '\n in iframe (at **)' : ''),
);
});

it('should not crash on poisoned hasOwnProperty', () => {
let html;
expect(
() =>
(html = ReactDOMServer.renderToString(
<div hasOwnProperty="poison">
<span unknown="test" />
</div>,
)),
).toWarnDev(['React does not recognize the `hasOwnProperty` prop']);
expect(html).toContain('<span unknown="test">');
});
});

describe('renderToStaticMarkup', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/react-dom/src/server/ReactPartialRenderer.js
Expand Up @@ -349,6 +349,7 @@ function processContext(type, context) {
return maskedContext;
}

const hasOwnProperty = Object.prototype.hasOwnProperty;
const STYLE = 'style';
const RESERVED_PROPS = {
children: null,
Expand All @@ -368,7 +369,7 @@ function createOpenTagMarkup(
let ret = '<' + tagVerbatim;

for (const propKey in props) {
if (!props.hasOwnProperty(propKey)) {
if (!hasOwnProperty.call(props, propKey)) {
continue;
}
let propValue = props[propKey];
Expand Down
5 changes: 3 additions & 2 deletions packages/react-dom/src/shared/DOMProperty.js
Expand Up @@ -66,14 +66,15 @@ export const VALID_ATTRIBUTE_NAME_REGEX = new RegExp(
'^[' + ATTRIBUTE_NAME_START_CHAR + '][' + ATTRIBUTE_NAME_CHAR + ']*$',
);

const hasOwnProperty = Object.prototype.hasOwnProperty;
const illegalAttributeNameCache = {};
const validatedAttributeNameCache = {};

export function isAttributeNameSafe(attributeName: string): boolean {
if (validatedAttributeNameCache.hasOwnProperty(attributeName)) {
if (hasOwnProperty.call(validatedAttributeNameCache, attributeName)) {
return true;
}
if (illegalAttributeNameCache.hasOwnProperty(attributeName)) {
if (hasOwnProperty.call(illegalAttributeNameCache, attributeName)) {
return false;
}
if (VALID_ATTRIBUTE_NAME_REGEX.test(attributeName)) {
Expand Down

0 comments on commit 23bbbd9

Please sign in to comment.