Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lovely-clouds-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@biomejs/biome": patch
---

Fixed [#9433](https://github.com/biomejs/biome/issues/9433): [`noBlankTarget`](https://biomejs.dev/linter/rules/no-blank-target/) now correctly handles dynamic href attributes, such as `<a href={company?.website} target="_blank">`.
5 changes: 3 additions & 2 deletions crates/biome_js_analyze/src/lint/security/no_blank_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,15 @@ impl Rule for NoBlankTarget {
.then(|| node.find_attribute_by_name(attr_name))
.flatten()
})?;
let href = href.as_static_value()?;

let target_attribute = node.find_attribute_by_name("target")?;
if target_attribute.as_static_value()?.text() != "_blank" {
return None;
}

if !ctx.options().allow_domains.is_empty() {
if let Some(href) = href.as_static_value()
&& !ctx.options().allow_domains.is_empty()
{
let allow_domains: Vec<&str> = ctx
.options()
.allow_domains
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<a target="_blank" href="//example.com/17" rel></a>
<a target="_blank" href={dynamicLink}></a>
<a target={'_blank'} href="//example.com/18"></a>
<a href={company?.website} target="_blank"></a>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Please add an allowDomains-configured invalid fixture as well.

The bug being fixed only shows up once allowDomains is enabled, but this new case still runs under default options. A config-backed case here would pin the actual regression, not just its nearby cousin.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/biome_js_analyze/tests/specs/security/noBlankTarget/invalid.jsx` at
line 13, Add a new invalid fixture that replicates this
anchor-with-target="_blank" case but runs with a config enabling allowDomains so
the regression is pinned; locate the existing invalid.jsx test (the <a
href={company?.website} target="_blank"></a> case) and duplicate it into a new
spec that loads a config object or meta enabling allowDomains (matching how
other tests set options) so the analyzer runs with allowDomains=true rather than
default options.

<area target="_blank" href="https://example.com"></area>
<form target="_blank" action="https://example.com"></form>
</>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ expression: invalid.jsx
<a target="_blank" href="//example.com/17" rel></a>
<a target="_blank" href={dynamicLink}></a>
<a target={'_blank'} href="//example.com/18"></a>
<a href={company?.website} target="_blank"></a>
<area target="_blank" href="https://example.com"></area>
<form target="_blank" action="https://example.com"></form>
</>
Expand Down Expand Up @@ -191,6 +192,27 @@ invalid.jsx:10:8 lint/security/noBlankTarget ━━━━━━━━━━━

```

```
invalid.jsx:11:8 lint/security/noBlankTarget FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× Avoid using target="_blank" without rel="noopener" or rel="noreferrer".

9 │ <a target="_blank" href="//example.com/14" rel={"noopenernoreferrer"}></a>
10 │ <a target="_blank" href="//example.com/17" rel></a>
> 11 │ <a target="_blank" href={dynamicLink}></a>
│ ^^^^^^^^^^^^^^^
12 │ <a target={'_blank'} href="//example.com/18"></a>
13 │ <a href={company?.website} target="_blank"></a>

i Opening external links in new tabs without rel="noopener" is a security risk. See the explanation for more details.

i Safe fix: Add the rel="noopener" attribute.

11 │ ····<a·target="_blank"·href={dynamicLink}·rel="noopener"></a>
│ +++++++++++++++

```

```
invalid.jsx:12:8 lint/security/noBlankTarget FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Expand All @@ -200,8 +222,8 @@ invalid.jsx:12:8 lint/security/noBlankTarget FIXABLE ━━━━━━━━
11 │ <a target="_blank" href={dynamicLink}></a>
> 12 │ <a target={'_blank'} href="//example.com/18"></a>
│ ^^^^^^^^^^^^^^^^^
13 │ <area target="_blank" href="https://example.com"></area>
14 │ <form target="_blank" action="https://example.com"></form>
13 │ <a href={company?.website} target="_blank"></a>
14 │ <area target="_blank" href="https://example.com"></area>

i Opening external links in new tabs without rel="noopener" is a security risk. See the explanation for more details.

Expand All @@ -213,23 +235,23 @@ invalid.jsx:12:8 lint/security/noBlankTarget FIXABLE ━━━━━━━━
```

```
invalid.jsx:13:11 lint/security/noBlankTarget FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
invalid.jsx:13:32 lint/security/noBlankTarget FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× Avoid using target="_blank" without rel="noopener" or rel="noreferrer".

11 │ <a target="_blank" href={dynamicLink}></a>
12 │ <a target={'_blank'} href="//example.com/18"></a>
> 13 │ <area target="_blank" href="https://example.com"></area>
│ ^^^^^^^^^^^^^^^
14 │ <form target="_blank" action="https://example.com"></form>
15 │ </>
> 13 │ <a href={company?.website} target="_blank"></a>
^^^^^^^^^^^^^^^
14 │ <area target="_blank" href="https://example.com"></area>
15 │ <form target="_blank" action="https://example.com"></form>

i Opening external links in new tabs without rel="noopener" is a security risk. See the explanation for more details.

i Safe fix: Add the rel="noopener" attribute.

13 │ ····<area·target="_blank"·href="https://example.com"·rel="noopener"></area>
+++++++++++++++
13 │ ····<a·href={company?.website}·target="_blank"·rel="noopener"></a>
│ +++++++++++++++

```

Expand All @@ -239,17 +261,38 @@ invalid.jsx:14:11 lint/security/noBlankTarget FIXABLE ━━━━━━━━
× Avoid using target="_blank" without rel="noopener" or rel="noreferrer".

12 │ <a target={'_blank'} href="//example.com/18"></a>
13 │ <area target="_blank" href="https://example.com"></area>
> 14 │ <form target="_blank" action="https://example.com"></form>
13 │ <a href={company?.website} target="_blank"></a>
> 14 │ <area target="_blank" href="https://example.com"></area>
│ ^^^^^^^^^^^^^^^
15 │ <form target="_blank" action="https://example.com"></form>
16 │ </>

i Opening external links in new tabs without rel="noopener" is a security risk. See the explanation for more details.

i Safe fix: Add the rel="noopener" attribute.

14 │ ····<area·target="_blank"·href="https://example.com"·rel="noopener"></area>
│ +++++++++++++++

```

```
invalid.jsx:15:11 lint/security/noBlankTarget FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× Avoid using target="_blank" without rel="noopener" or rel="noreferrer".

13 │ <a href={company?.website} target="_blank"></a>
14 │ <area target="_blank" href="https://example.com"></area>
> 15 │ <form target="_blank" action="https://example.com"></form>
│ ^^^^^^^^^^^^^^^
15 │ </>
16
16 │ </>
17

i Opening external links in new tabs without rel="noopener" is a security risk. See the explanation for more details.

i Safe fix: Add the rel="noopener" attribute.

14 │ ····<form·target="_blank"·action="https://example.com"·rel="noopener"></form>
15 │ ····<form·target="_blank"·action="https://example.com"·rel="noopener"></form>
│ +++++++++++++++

```
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<a target={'targetValue'} href="/absolute/path"></a>
<a target={"targetValue"} href="/absolute/path"></a>
<a target={null} href="//example.com"></a>
<a href={company?.website} target="_blank" rel="noreferrer"></a>
<area target="_blank" rel="noopener" href="https://example.com"></area>
<form target="_blank" rel="noopener" action="https://example.com"></form>
<Link href="foobar" target="_blank" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ expression: valid.jsx
<a target={'targetValue'} href="/absolute/path"></a>
<a target={"targetValue"} href="/absolute/path"></a>
<a target={null} href="//example.com"></a>
<a href={company?.website} target="_blank" rel="noreferrer"></a>
<area target="_blank" rel="noopener" href="https://example.com"></area>
<form target="_blank" rel="noopener" action="https://example.com"></form>
<Link href="foobar" target="_blank" />
Expand Down