You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Defaults to `false`. When set to `true`, treats spread attributes as dangerous unless explicitly overriden.
e.g. the following is safe:
<a {...dangerousObject} rel="noreferrer" target="_blank"></a>
This change also extends target="_blank" detection to include conditional expressions whose alternate or consequent is the "_blank" string (case-insensitive).
Fixes#2827
Copy file name to clipboardExpand all lines: CHANGELOG.md
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
11
11
*[`jsx-no-constructed-context-values`]: add new rule which checks when the value passed to a Context Provider will cause needless rerenders ([#2763][]@dylanOshima)
12
12
*[`jsx-wrap-multilines`]: fix crash with `declaration`s that are on a new line after `=` ([#2875][]@ljharb)
Copy file name to clipboardExpand all lines: docs/rules/jsx-no-target-blank.md
+26-8Lines changed: 26 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,11 @@
1
1
# Prevent usage of unsafe `target='_blank'` (react/jsx-no-target-blank)
2
2
3
-
When creating a JSX element that has an `a` tag, it is often desired to have
4
-
the link open in a new tab using the `target='_blank'` attribute. Using this
5
-
attribute unaccompanied by `rel='noreferrer'`, however, is a severe
6
-
security vulnerability ([see here for more details](https://html.spec.whatwg.org/multipage/links.html#link-type-noopener))
3
+
When creating a JSX element that has an `a` tag, it is often desired to have the link open in a new tab using the `target='_blank'` attribute. Using this attribute unaccompanied by `rel='noreferrer'`, however, is a severe security vulnerability ([see here for more details](https://html.spec.whatwg.org/multipage/links.html#link-type-noopener))
7
4
This rules requires that you accompany `target='_blank'` attributes with `rel='noreferrer'`.
8
5
9
6
## Rule Details
10
7
11
-
This rule aims to prevent user generated links from creating security vulnerabilities by requiring
12
-
`rel='noreferrer'` for external links, and optionally any dynamically generated links.
8
+
This rule aims to prevent user generated links from creating security vulnerabilities by requiring `rel='noreferrer'` for external links, and optionally any dynamically generated links.
13
9
14
10
## Rule Options
15
11
```json
@@ -20,7 +16,8 @@ This rule aims to prevent user generated links from creating security vulnerabil
20
16
21
17
* allow-referrer: optional boolean. If `true` does not require `noreferrer`. Defaults to `false`.
22
18
* enabled: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to 0.
23
-
* enforce: optional string, 'always' or 'never'
19
+
* enforceDynamicLinks: optional string, 'always' or 'never'
20
+
* warnOnSpreadAttributes: optional boolean. Defaults to `false`.
24
21
25
22
### `enforceDynamicLinks`
26
23
@@ -56,6 +53,27 @@ Examples of **correct** code for this rule, when configured with `{ "enforceDyna
56
53
var Hello =<a target='_blank' href={dynamicLink}></a>
57
54
```
58
55
56
+
### `warnOnSpreadAttributes`
57
+
58
+
Spread attributes are a handy way of passing programmatically-generated props to components, but may contain unsafe props e.g.
59
+
60
+
```jsx
61
+
constunsafeProps= {
62
+
href:"http://example.com",
63
+
target:"_blank",
64
+
};
65
+
66
+
<a {...unsafeProps}></a>
67
+
```
68
+
69
+
Defaults to false. If false, this rule will ignore all spread attributes. If true, this rule will treat all spread attributes as if they contain an unsafe combination of props, unless specifically overridden by props _after_ the last spread attribute prop e.g. the following would not be violations:
70
+
71
+
```jsx
72
+
<a {...unsafeProps} rel="noreferrer"></a>
73
+
<a {...unsafeProps} target="_self"></a>
74
+
<a {...unsafeProps} href="/some-page"></a>
75
+
```
76
+
59
77
### Custom link components
60
78
61
79
This rule supports the ability to use custom components for links, such as `<Link />` which is popular in libraries like `react-router`, `next.js` and `gatsby`. To enable this, define your custom link components in the global [shared settings](https://github.com/yannickcr/eslint-plugin-react/blob/master/README.md#configuration) under the `linkComponents` configuration area. Once configured, this rule will check those components as if they were `<a />` elements.
@@ -81,4 +99,4 @@ For links to a trusted host (e.g. internal links to your own site, or links to a
81
99
82
100
## When Not To Use It
83
101
84
-
If you do not have any external links, you can disable this rule.
102
+
If you do not have any external links, you can disable this rule.
0 commit comments