Skip to content

Commit

Permalink
Allow rel="noreferrer" when allowReferrer is true
Browse files Browse the repository at this point in the history
  • Loading branch information
edemaine committed Feb 16, 2021
1 parent 3885641 commit 02a54e7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/rules/jsx-no-target-blank.js
Expand Up @@ -88,7 +88,8 @@ function hasSecureRel(node, allowReferrer, warnOnSpreadAttributes, spreadAttribu
const relAttribute = node.attributes[relIndex];
const value = getStringFromValue(relAttribute.value);
const tags = value && typeof value === 'string' && value.toLowerCase().split(' ');
return tags && (allowReferrer ? tags.indexOf('noopener') >= 0 : tags.indexOf('noreferrer') >= 0);
const noreferrer = tags && tags.indexOf('noreferrer') >= 0;
return (allowReferrer ? noreferrer || (tags && tags.indexOf('noopener') >= 0) : noreferrer);
}

module.exports = {
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/rules/jsx-no-target-blank.js
Expand Up @@ -103,6 +103,10 @@ ruleTester.run('jsx-no-target-blank', rule, {
code: '<a href="foobar" target="_blank" rel="noopener"></a>',
options: [{allowReferrer: true}]
},
{
code: '<a href="foobar" target="_blank" rel="noreferrer"></a>',
options: [{allowReferrer: true}]
},
{
code: '<a target={3} />'
}
Expand Down Expand Up @@ -212,6 +216,12 @@ ruleTester.run('jsx-no-target-blank', rule, {
output: '<a target={"_blank"} href="//example.com/19" rel="noreferrer"></a>',
errors: defaultErrors
},
{
code: '<a href="http://example.com/20" target="_blank"></a>',
output: '<a href="http://example.com/20" target="_blank" rel="noreferrer"></a>',
options: [{allowReferrer: true}],
errors: defaultErrors
},
{
code: '<a target="_blank" href={ dynamicLink }></a>',
output: '<a target="_blank" href={ dynamicLink } rel="noreferrer"></a>',
Expand Down

0 comments on commit 02a54e7

Please sign in to comment.