react/jsx-no-target-blank
Require rel="noreferrer" on links that open in a new tab
In older browsers, a page opened with target="_blank" can reach the page that opened it through window.opener and send it to another address, for example a fake login page. rel="noreferrer" cuts that connection.
The rule only reports links that can leave your site: an href that starts with a scheme such as https: or with //, or an href in braces.
--fix adds rel="noreferrer" to the link.
Configure#
Off by default. To turn it on:
[lint.rules]
"react/jsx-no-target-blank" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"react/jsx-no-target-blank" = ["error", {
allowReferrer = false,
enforceDynamicLinks = "always", # "always" or "never"
warnOnSpreadAttributes = false,
forms = false,
links = true,
}]Examples#
const docs = <a href="https://example.com/docs" target="_blank">Using target="_blank" without rel="noreferrer" (which implies rel="noopener") is a security risk in older browsers: see mathiasbynens.github.io/rel-noopener/#recommendationsDocs</a>;const profile = <a href={website} target="_blank">Using target="_blank" without rel="noreferrer" (which implies rel="noopener") is a security risk in older browsers: see mathiasbynens.github.io/rel-noopener/#recommendationsWebsite</a>;const docs = <a href="https://example.com/docs" target="_blank" rel="noreferrer">Docs</a>;
const help = <a href="/help" target="_blank">Help</a>;Options#
| Option | Default | What it does |
|---|---|---|
allowReferrer | false | Accept rel="noopener" as well, which keeps the Referer header. |
enforceDynamicLinks | "always" | "never" allows links with an href in braces. |
warnOnSpreadAttributes | false | Treat {...props} as if it set an unsafe target and href, unless props after it say otherwise. |
forms | false | Also check <form target="_blank"> and its action. |
links | true | Only with an oxlint config: false stops checking links. |
{ "allowReferrer": true }No problemsconst docs = <a href="https://example.com/docs" target="_blank" rel="noopener">Docs</a>;{ "enforceDynamicLinks": "never" }No problemsconst profile = <a href={website} target="_blank">Website</a>;{ "warnOnSpreadAttributes": true }1 problemconst link = <a {...props}>Using target="_blank" without rel="noreferrer" (which implies rel="noopener") is a security risk in older browsers: see mathiasbynens.github.io/rel-noopener/#recommendationsOpen</a>;{ "warnOnSpreadAttributes": true }No problemsconst link = <a {...props} rel="noreferrer">Open</a>;{ "forms": true }1 problemconst search = <form action="https://example.com/search" target="_blank" />Using target="_blank" without rel="noreferrer" (which implies rel="noopener") is a security risk in older browsers: see mathiasbynens.github.io/rel-noopener/#recommendations;The rule also checks your own link and form components if your settings list them under linkComponents and formComponents. Settings go under [lint.settings] in bunfig.toml, or under settings in an ESLint config.
When not to use it#
Current browsers treat target="_blank" as if it had rel="noopener". If you don't support older ones, you don't need this rule.