Skip to content

Commit

Permalink
[New] jsx-no-target-blank: add forms option
Browse files Browse the repository at this point in the history
  • Loading branch information
jaaberg committed Aug 10, 2021
1 parent 30ae98b commit 893fee3
Show file tree
Hide file tree
Showing 5 changed files with 469 additions and 159 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,13 @@ You should also specify settings that will be shared across all the plugin rules
{"property": "observer", "object": "Mobx"},
{"property": "observer", "object": "<pragma>"} // sets `object` to whatever value `settings.react.pragma` is set to
],
"formComponents": [
// Components used as alternatives to <form> for forms, eg. <Form endpoint={url} />
"CustomForm",
{"name": "Form", "formAttribute": "endpoint"}
]
"linkComponents": [
// Components used as alternatives to <a> for linking, eg. <Link to={ url } />
// Components used as alternatives to <a> for linking, eg. <Link to={url} />
"Hyperlink",
{"name": "Link", "linkAttribute": "to"}
]
Expand Down
91 changes: 65 additions & 26 deletions docs/rules/jsx-no-target-blank.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
# Prevent usage of unsafe `target='_blank'` (react/jsx-no-target-blank)

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))
When creating a JSX element that has an `a` tag or a `form` 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))
This rules requires that you accompany `target='_blank'` attributes with `rel='noreferrer'`.

## Rule Details

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.
This rule aims to prevent user generated link hrefs and form actions from creating security vulnerabilities by requiring `rel='noreferrer'` for external link hrefs and form actions, and optionally any dynamically generated link hrefs and form actions.

## Rule Options

```json
...
"react/jsx-no-target-blank": [<enabled>, { "allowReferrer": <allow-referrer>, "enforceDynamicLinks": <enforce> }]
"react/jsx-no-target-blank": [<enabled>, {
"allowReferrer": <allow-referrer>,
"enforceDynamicLinks": <enforce>,
"links": <boolean>,
"forms": <boolean>,
}]
...
```

* allow-referrer: optional boolean. If `true` does not require `noreferrer`. Defaults to `false`.
* enabled: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to 0.
* enforceDynamicLinks: optional string, 'always' or 'never'
* warnOnSpreadAttributes: optional boolean. Defaults to `false`.
- `allowReferrer`: optional boolean. If `true` does not require `noreferrer`. Defaults to `false`.
- `enabled`: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to 0.
- `enforceDynamicLinks`: optional string, 'always' or 'never'
- `warnOnSpreadAttributes`: optional boolean. Defaults to `false`.
- `enforceDynamicLinks` - enforce: optional string, 'always' or 'never'
- `links` - Prevent usage of unsafe `target='_blank'` inside links, defaults to `true`
- `forms` - Prevent usage of unsafe `target='_blank'` inside forms, defaults to `false`

### `enforceDynamicLinks`

Expand All @@ -28,19 +40,21 @@ This rule aims to prevent user generated links from creating security vulnerabil
Examples of **incorrect** code for this rule, when configured with `{ "enforceDynamicLinks": "always" }`:

```jsx
var Hello = <a target='_blank' href="http://example.com/"></a>
var Hello = <a target='_blank' href={dynamicLink}></a>
var Hello = <a target="_blank" href="http://example.com/"></a>;
var Hello = <a target="_blank" href={dynamicLink}></a>;
```

Examples of **correct** code for this rule:

```jsx
var Hello = <p target="_blank"></p>
var Hello = <a target="_blank" rel="noreferrer" href="http://example.com"></a>
var Hello = <a target="_blank" rel="noopener noreferrer" href="http://example.com"></a>
var Hello = <a target="_blank" href="relative/path/in/the/host"></a>
var Hello = <a target="_blank" href="/absolute/path/in/the/host"></a>
var Hello = <a></a>
var Hello = <p target="_blank"></p>;
var Hello = <a target="_blank" rel="noreferrer" href="http://example.com"></a>;
var Hello = (
<a target="_blank" rel="noopener noreferrer" href="http://example.com"></a>
);
var Hello = <a target="_blank" href="relative/path/in/the/host"></a>;
var Hello = <a target="_blank" href="/absolute/path/in/the/host"></a>;
var Hello = <a></a>;
```

#### never
Expand All @@ -50,7 +64,7 @@ var Hello = <a></a>
Examples of **correct** code for this rule, when configured with `{ "enforceDynamicLinks": "never" }`:

```jsx
var Hello = <a target='_blank' href={dynamicLink}></a>
var Hello = <a target="_blank" href={dynamicLink}></a>;
```

### `warnOnSpreadAttributes`
Expand All @@ -59,11 +73,11 @@ Spread attributes are a handy way of passing programmatically-generated props to

```jsx
const unsafeProps = {
href: "http://example.com",
target: "_blank",
href: 'http://example.com',
target: '_blank',
};

<a {...unsafeProps}></a>
<a {...unsafeProps}></a>;
```

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:
Expand All @@ -74,29 +88,54 @@ Defaults to false. If false, this rule will ignore all spread attributes. If tru
<a {...unsafeProps} href="/some-page"></a>
```

### `links` / `forms`

When option `forms` is set to `true`, the following is considered an error:

```jsx
var Hello = <form target="_blank" action="http://example.com/"></form>;
```

When option `links` is set to `true`, the following is considered an error:

```jsx
var Hello = <a target='_blank' href="http://example.com/"></form>
```

### Custom link components

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.

Examples of **incorrect** code for this rule:

```jsx
var Hello = <Link target="_blank" to="http://example.com/"></Link>
var Hello = <Link target="_blank" to={dynamicLink}></Link>
var Hello = <Link target="_blank" to="http://example.com/"></Link>;
var Hello = <Link target="_blank" to={dynamicLink}></Link>;
```

Examples of **correct** code for this rule:

```jsx
var Hello = <Link target="_blank" rel="noopener noreferrer" to="http://example.com"></Link>
var Hello = <Link target="_blank" to="relative/path/in/the/host"></Link>
var Hello = <Link target="_blank" to="/absolute/path/in/the/host"></Link>
var Hello = <Link />
var Hello = (
<Link
target="_blank"
rel="noopener noreferrer"
to="http://example.com"
></Link>
);
var Hello = <Link target="_blank" to="relative/path/in/the/host"></Link>;
var Hello = <Link target="_blank" to="/absolute/path/in/the/host"></Link>;
var Hello = <Link />;
```

### Custom form components

This rule supports the ability to use custom components for forms. To enable this, define your custom form components in the global [shared settings](https://github.com/yannickcr/eslint-plugin-react/blob/master/README.md#configuration) under the `formComponents` configuration area. Once configured, this rule will check those components as if they were `<form />` elements.

## When To Override It

For links to a trusted host (e.g. internal links to your own site, or links to a another host you control, where you can be certain this security vulnerability does not exist), you may want to keep the HTTP Referer header for analytics purposes.

## When Not To Use It

If you do not have any external links, you can disable this rule.
If you do not have any external links or forms, you can disable this rule.
Loading

0 comments on commit 893fee3

Please sign in to comment.