Skip to content

Commit

Permalink
[Fix] jsx-no-script-url: avoid crash with boolean href
Browse files Browse the repository at this point in the history
Fixes #2871
  • Loading branch information
ljharb committed Dec 7, 2020
1 parent edbbd79 commit 63d5a1b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -14,7 +14,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [`display-name`]/component detection: avoid a crash on anonymous components ([#2840][] @ljharb)
* [`prop-types`]: function in class that returns a component causes false warning in typescript ([#2843][] @SyMind)
* [`jsx-no-target-blank`]: avoid a crash with a non-string literal ([#2851][] @ljharb)
* [`jsx-no-script-url`]: avoid crash with boolean `href` ([#2871][] @ljharb)

[#2871]: https://github.com/yannickcr/eslint-plugin-react/issues/2871
[#2851]: https://github.com/yannickcr/eslint-plugin-react/issues/2851
[#2843]: https://github.com/yannickcr/eslint-plugin-react/pull/2843
[#2840]: https://github.com/yannickcr/eslint-plugin-react/issues/2840
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/jsx-no-script-url.js
Expand Up @@ -16,7 +16,7 @@ const docsUrl = require('../util/docsUrl');
const isJavaScriptProtocol = /^[\u0000-\u001F ]*j[\r\n\t]*a[\r\n\t]*v[\r\n\t]*a[\r\n\t]*s[\r\n\t]*c[\r\n\t]*r[\r\n\t]*i[\r\n\t]*p[\r\n\t]*t[\r\n\t]*:/i;

function hasJavaScriptProtocol(attr) {
return attr.value.type === 'Literal'
return attr.value && attr.value.type === 'Literal'
&& isJavaScriptProtocol.test(attr.value.value);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/lib/rules/jsx-no-script-url.js
Expand Up @@ -37,7 +37,8 @@ ruleTester.run('jsx-no-script-url', rule, {
{code: '<a href=""></a>'},
{code: '<a name="foo"></a>'},
{code: '<a href={"javascript:"}></a>'},
{code: '<Foo href="javascript:"></Foo>'}
{code: '<Foo href="javascript:"></Foo>'},
{code: '<a href />'}
],
invalid: [{
code: '<a href="javascript:"></a>',
Expand Down

0 comments on commit 63d5a1b

Please sign in to comment.