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

Add JSXExpressionContainer as the href value type for target=_blank #1737

Closed
ksdmitrieva opened this issue Mar 22, 2018 · 3 comments
Closed

Comments

@ksdmitrieva
Copy link

ksdmitrieva commented Mar 22, 2018

Right now the jsx-no-target-blank rule only fires if the href is a literal string. What about the cases when the value of the href comes from a JSX expression? For example:

<a target='_blank' href={this.props.url}></a>

This might be even more dangerous if the URL property is provided by the user or the third-party.

Would it make sense to add a function hasDynamicLink() like this one:
function hasDynamicLink(element) {
return element.attributes.some(attr => attr.name &&
attr.name.name === 'href' &&
attr.value.type === 'JSXExpressionContainer');
}

And then include it in the condition for the rule:

if (
isTargetBlank(node) &&
(hasExternalLink(node.parent) || hasDynamicLink(node.parent)) &&
!hasSecureRel(node.parent)
)
I tested it out locally. Will be happy to help with a PR.

@ljharb
Copy link
Member

ljharb commented Mar 23, 2018

This seems reasonable; but what about the false positive case, where the dynamic link is internal?

I'd hate to force people to disable the entire rule in that case.

@ksdmitrieva
Copy link
Author

ksdmitrieva commented Mar 27, 2018 via email

@ljharb
Copy link
Member

ljharb commented May 15, 2018

This seems like it could be closed by #1784?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants