Skip to content

Commit 23bbbd9

Browse files
gaearonsegoddnja
authored andcommitted
Fix SSR crash on a hasOwnProperty attribute (facebook#13303)
1 parent d61eb5d commit 23bbbd9

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

packages/react-dom/src/__tests__/ReactServerRendering-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,19 @@ describe('ReactDOMServer', () => {
174174
(__DEV__ ? '\n in iframe (at **)' : ''),
175175
);
176176
});
177+
178+
it('should not crash on poisoned hasOwnProperty', () => {
179+
let html;
180+
expect(
181+
() =>
182+
(html = ReactDOMServer.renderToString(
183+
<div hasOwnProperty="poison">
184+
<span unknown="test" />
185+
</div>,
186+
)),
187+
).toWarnDev(['React does not recognize the `hasOwnProperty` prop']);
188+
expect(html).toContain('<span unknown="test">');
189+
});
177190
});
178191

179192
describe('renderToStaticMarkup', () => {

packages/react-dom/src/server/ReactPartialRenderer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ function processContext(type, context) {
349349
return maskedContext;
350350
}
351351

352+
const hasOwnProperty = Object.prototype.hasOwnProperty;
352353
const STYLE = 'style';
353354
const RESERVED_PROPS = {
354355
children: null,
@@ -368,7 +369,7 @@ function createOpenTagMarkup(
368369
let ret = '<' + tagVerbatim;
369370

370371
for (const propKey in props) {
371-
if (!props.hasOwnProperty(propKey)) {
372+
if (!hasOwnProperty.call(props, propKey)) {
372373
continue;
373374
}
374375
let propValue = props[propKey];

packages/react-dom/src/shared/DOMProperty.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,15 @@ export const VALID_ATTRIBUTE_NAME_REGEX = new RegExp(
6666
'^[' + ATTRIBUTE_NAME_START_CHAR + '][' + ATTRIBUTE_NAME_CHAR + ']*$',
6767
);
6868

69+
const hasOwnProperty = Object.prototype.hasOwnProperty;
6970
const illegalAttributeNameCache = {};
7071
const validatedAttributeNameCache = {};
7172

7273
export function isAttributeNameSafe(attributeName: string): boolean {
73-
if (validatedAttributeNameCache.hasOwnProperty(attributeName)) {
74+
if (hasOwnProperty.call(validatedAttributeNameCache, attributeName)) {
7475
return true;
7576
}
76-
if (illegalAttributeNameCache.hasOwnProperty(attributeName)) {
77+
if (hasOwnProperty.call(illegalAttributeNameCache, attributeName)) {
7778
return false;
7879
}
7980
if (VALID_ATTRIBUTE_NAME_REGEX.test(attributeName)) {

0 commit comments

Comments
 (0)