Skip to content

Commit

Permalink
[Fix] no-unknown-property: avoid crash with prop named with Object.…
Browse files Browse the repository at this point in the history
…prototype key

Fixes #2879
  • Loading branch information
ljharb committed Dec 17, 2020
1 parent 9c1e652 commit 23da1ee
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -18,8 +18,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [`jsx-no-script-url`]: avoid crash with boolean `href` ([#2871][] @ljharb, @AriPerkkio)
* [`no-typos`]: avoid crash with computed method name ([#2870][] @ljharb, @AriPerkkio)
* [`jsx-max-depth`]: avoid crash with childless jsx child ([#2869][] @ljharb, @AriPerkkio)
* [`no-unknown-property`]: avoid crash with prop named with Object.prototype key ([#2879][] @ljharb, @AriPerkkio)

[#2871]: https://github.com/yannickcr/eslint-plugin-react/issues/2875
[#2879]: https://github.com/yannickcr/eslint-plugin-react/issues/2879
[#2875]: https://github.com/yannickcr/eslint-plugin-react/issues/2875
[#2871]: https://github.com/yannickcr/eslint-plugin-react/issues/2871
[#2870]: https://github.com/yannickcr/eslint-plugin-react/issues/2870
[#2869]: https://github.com/yannickcr/eslint-plugin-react/issues/2869
Expand Down
7 changes: 4 additions & 3 deletions lib/rules/no-unknown-property.js
Expand Up @@ -5,6 +5,7 @@

'use strict';

const has = require('has');
const docsUrl = require('../util/docsUrl');
const versionUtil = require('../util/version');

Expand Down Expand Up @@ -198,10 +199,10 @@ function tagNameHasDot(node) {
* @returns {String | undefined} The standard name of the attribute, or undefined if no standard name was found.
*/
function getStandardName(name, context) {
if (DOM_ATTRIBUTE_NAMES[name]) {
if (has(DOM_ATTRIBUTE_NAMES, name)) {
return DOM_ATTRIBUTE_NAMES[name];
}
if (SVGDOM_ATTRIBUTE_NAMES[name]) {
if (has(SVGDOM_ATTRIBUTE_NAMES, name)) {
return SVGDOM_ATTRIBUTE_NAMES[name];
}
const names = getDOMPropertyNames(context);
Expand Down Expand Up @@ -258,7 +259,7 @@ module.exports = {
const tagName = getTagName(node);

// 1. Some attributes are allowed on some tags only.
const allowedTags = ATTRIBUTE_TAGS_MAP[name];
const allowedTags = has(ATTRIBUTE_TAGS_MAP, name) ? ATTRIBUTE_TAGS_MAP[name] : null;
if (tagName && allowedTags && /[^A-Z]/.test(tagName.charAt(0)) && allowedTags.indexOf(tagName) === -1) {
context.report({
node,
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/rules/no-unknown-property.js
Expand Up @@ -46,7 +46,8 @@ ruleTester.run('no-unknown-property', rule, {
options: [{ignore: ['class']}]
},
{code: '<script crossOrigin />'},
{code: '<audio crossOrigin />'}
{code: '<audio crossOrigin />'},
{code: '<div hasOwnProperty="should not be allowed tag" />'}
],
invalid: [{
code: '<div class="bar"></div>;',
Expand Down

0 comments on commit 23da1ee

Please sign in to comment.