Skip to content

Commit d922ed2

Browse files
committed
Fix SSR crash on a hasOwnProperty attribute
1 parent 5b19684 commit d922ed2

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

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

+13
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ function processContext(type, context) {
308308
return maskedContext;
309309
}
310310

311+
const hasOwnProperty = Object.prototype.hasOwnProperty;
311312
const STYLE = 'style';
312313
const RESERVED_PROPS = {
313314
children: null,
@@ -327,7 +328,7 @@ function createOpenTagMarkup(
327328
let ret = '<' + tagVerbatim;
328329

329330
for (const propKey in props) {
330-
if (!props.hasOwnProperty(propKey)) {
331+
if (!hasOwnProperty.call(props, propKey)) {
331332
continue;
332333
}
333334
let propValue = props[propKey];

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

+3-2
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)