Skip to content

Added checks for incorrect usage of innerHTML. Fixes #1370 #2520

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

Merged
merged 1 commit into from
Jan 5, 2015

Conversation

jimfb
Copy link
Contributor

@jimfb jimfb commented Nov 13, 2014

This PR adds checks to prevent the incorrect setting of inner html, to make it easier for developers to spot hazards in their code and avoid security holes. There have been several discussions about how best to handle the innerHTML issue ( most notably, the public discussion here: #1515 ), and the conclusion of these discussions was the creation of issue #1370, which is fixed by this commit.

);
invariant(
props.innerHTML == null,
'Directly setting property `innerHTML` is not permitted. ' +
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does anyone try to do this? I've never heard of anyone trying to…

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's the intuitive thing to try, if you haven't read about dangerouslySetInnerHTML, so it's worth checking if for no other reason than to hint to the user where they might look for the supported function.

@jimfb jimfb force-pushed the warn-for-dangerouslySetInnerHtml branch from 18cb2ba to ef9fb89 Compare November 13, 2014 23:24
@sebmarkbage
Copy link
Collaborator

This == null check is already happening twice (now three). We still need the check once, so as long as these are within the block that only affects non-null values, I think we're fine adding invariants there.

@jimfb
Copy link
Contributor Author

jimfb commented Nov 14, 2014

Added if-check outside call to invariant call, as per @sebmarkbage

throw 'Invariant Violation: ' +
'Directly setting property `innerHTML` is not permitted. ' +
'For more information, lookup documentation on `dangerouslySetInnerHTML`.';
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

We never throw strings because the stack is attached to the Error object. We also compress invariant error messages. So we should use the invariant module even if the first argument is false, although in this case it should be props == null.

);
invariant(
props.dangerouslySetInnerHTML.__html != null,
'`props.dangerouslySetInnerHTML` must be in the form {__html: ...} ' +
Copy link
Collaborator

Choose a reason for hiding this comment

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

Double space at the end here. One is enough. :)

There should also be a period. You can use the backticks it indicate what is code and what is period.

@jimfb jimfb force-pushed the warn-for-dangerouslySetInnerHtml branch from 4d02264 to da3a900 Compare November 25, 2014 04:19
@jimfb
Copy link
Contributor Author

jimfb commented Nov 25, 2014

Ready to merge?

@jimfb jimfb self-assigned this Nov 25, 2014
'Can only set one of `children` or `props.dangerouslySetInnerHTML`.'
props.innerHTML == null,
'Directly setting property `innerHTML` is not permitted. ' +
'For more information, lookup documentation on `dangerouslySetInnerHTML`.'
Copy link
Member

Choose a reason for hiding this comment

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

This should be a warning, not an invariant. There's no reason to stop execution flow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@zpao
Copy link
Member

zpao commented Nov 26, 2014

Let's a get a better description in the summary here explaining the rational. It actually wasn't clear to me that we had settled on what we were doing with innerHTML.

@jimfb jimfb force-pushed the warn-for-dangerouslySetInnerHtml branch from da3a900 to 557e95f Compare November 26, 2014 23:25
warning(
props.innerHTML == null,
'Directly setting property `innerHTML` is not permitted. ' +
'For more information, lookup documentation on `dangerouslySetInnerHTML`.'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Warnings should move into the __DEV__ block so that they never execute in prod.

We don't do that automatically, partially because we don't have good dead-code elimination but mostly because we don't know if there is extra work that can be removed in production rather than just this statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@jimfb jimfb force-pushed the warn-for-dangerouslySetInnerHtml branch from 557e95f to aef7c4d Compare December 1, 2014 19:10
@jimfb
Copy link
Contributor Author

jimfb commented Dec 1, 2014

Better description added, any other feedback or ready to merge?

jimfb added a commit that referenced this pull request Jan 5, 2015
Added checks for incorrect usage of innerHTML. Fixes #1370
@jimfb jimfb merged commit e5b3f9a into facebook:master Jan 5, 2015
@jimfb jimfb deleted the warn-for-dangerouslySetInnerHtml branch January 5, 2015 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants