Skip to content
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

Check write-access on prototype. #818

Closed
wants to merge 4 commits into from
Closed
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
23 changes: 21 additions & 2 deletions client/patch-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@

import React from 'react'

/**
* Traverses the prototype chain, returning the prototype object
* on which the property is defined.
*/
const getWritableProto = (obj, p) => {
const descr = Object.getOwnPropertyDescriptor(obj, p)
if (descr) {
return descr.writable ? obj : null
}
const proto = Object.getPrototypeOf(obj)
return proto ? getWritableProto(proto, p) : null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This traverses the prototype chain, returning the prototype object on which the property is defined. getOwnPropertyDescriptor returns the descriptor for the immediate/own properties only.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a comment in the code to explain the motivation for this? Since it's a bit obscure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rauchg ok?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

let patched = false

export default (handleError = () => {}) => {
Expand All @@ -16,7 +29,7 @@ export default (handleError = () => {}) => {

React.createElement = function (Component, ...rest) {
if (typeof Component === 'function') {
const { prototype } = Component
let { prototype } = Component

// assumes it's a class component if render method exists.
const isClassComponent = Boolean(prototype && prototype.render) ||
Expand All @@ -28,7 +41,13 @@ export default (handleError = () => {}) => {

if (isClassComponent) {
if (prototype.render) {
prototype.render = wrapRender(prototype.render)
/**
* Get the prototype object on which `render` is defined.
*/
prototype = getWritableProto(prototype, 'render')
if (prototype) {
prototype.render = wrapRender(prototype.render)
}
}

// wrap the render method in runtime when the component initialized
Expand Down