-
-
Notifications
You must be signed in to change notification settings - Fork 794
feat: implement noScriptUrl rule #8232
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
Merged
Netail
merged 27 commits into
biomejs:main
from
ruidosujeira:feat/add-no-script-url-rule
Dec 7, 2025
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
9470f59
feat(biome_js_analyze): implement noScriptUrl rule
ruidosujeira a5e221d
feat(biome_js_analyze): implement noScriptUrl rule
ruidosujeira ba19a93
[autofix.ci] apply automated fixes
autofix-ci[bot] 335b6fa
chore(lint): update noScriptUrl severity and snapshots
ruidosujeira 1ac0f42
chore(security/noScriptUrl): resolve rebase conflict, enforce Severit…
ruidosujeira 3976669
[autofix.ci] apply automated fixes
autofix-ci[bot] cfe9818
feat(js-analyze): add noScriptUrl rule to disallow 'javascript:' URLs…
ruidosujeira 5e5c305
[autofix.ci] apply automated fixes
autofix-ci[bot] 60d6fc8
feat(security): add noScriptUrl lint rule to detect and error on java…
ruidosujeira 5218579
Update .changeset/add-no-script-url-rule.md
ruidosujeira a708f90
test: run and update snapshots; chore: move noScriptUrl to nursery, a…
ruidosujeira c00c728
Apply suggestion from @ematipico
ruidosujeira 9145019
fix(security): register noScriptUrl diagnostic category and fix clipp…
ruidosujeira 5fc2b33
[autofix.ci] apply automated fixes
autofix-ci[bot] ab3714c
Merge branch 'main' into feat/add-no-script-url-rule
ruidosujeira 1867fd9
[autofix.ci] apply automated fixes
autofix-ci[bot] 0587bd3
chore(changeset): update changeset to remove duplicate line and short…
ruidosujeira e1fe8dd
chore(changeset): commit latest changeset adjustment
ruidosujeira 0358506
chore(changeset): commit latest changes before push
ruidosujeira 1879ed7
refactor: apply review feedback for noScriptUrl rule
ruidosujeira 726b356
Merge branch 'main' into feat/add-no-script-url-rule
ruidosujeira a576948
[autofix.ci] apply automated fixes
autofix-ci[bot] 0201aaa
fix(noScriptUrl): remove security variant, use biome_rule_options and…
ruidosujeira f6d8dd7
[autofix.ci] apply automated fixes
autofix-ci[bot] 70c22e5
Merge branch 'main' into feat/add-no-script-url-rule
ruidosujeira 10a82fe
Merge branch 'main' into feat/add-no-script-url-rule
Netail 900c708
fix: cicd
Netail File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| "@biomejs/biome": patch | ||
| --- | ||
| Added the nursery rule [`noScriptUrl`](https://biomejs.dev/linter/rules/no-script-url/). | ||
|
|
||
| This rule disallows the use of `javascript:` URLs, which are considered a form of `eval` and can pose security risks such as XSS vulnerabilities. | ||
|
|
||
| ```jsx | ||
| <a href="javascript:alert('XSS')">Click me</a> | ||
| ``` |
60 changes: 60 additions & 0 deletions
60
crates/biome_cli/src/execute/migrate/eslint_any_rule_to_biome.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
194 changes: 108 additions & 86 deletions
194
crates/biome_configuration/src/analyzer/linter/rules.rs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
crates/biome_html_analyze/src/lint/nursery/no_script_url.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| use biome_analyze::{ | ||
| Ast, Rule, RuleDiagnostic, RuleSource, context::RuleContext, declare_lint_rule, | ||
| }; | ||
| use biome_console::markup; | ||
| use biome_diagnostics::Severity; | ||
| use biome_html_syntax::{AnyHtmlAttributeInitializer, HtmlOpeningElement, inner_string_text}; | ||
| use biome_rowan::{AstNode, TextRange}; | ||
| use biome_rule_options::no_script_url::NoScriptUrlOptions; | ||
| use biome_string_case::StrOnlyExtension; | ||
|
|
||
| declare_lint_rule! { | ||
| /// Disallow `javascript:` URLs in HTML. | ||
| /// | ||
| /// Using `javascript:` URLs is considered a form of `eval` and can be a security risk. | ||
| /// These URLs can execute arbitrary JavaScript code, which can lead to cross-site scripting (XSS) vulnerabilities. | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ### Invalid | ||
| /// | ||
| /// ```html,expect_diagnostic | ||
| /// <a href="javascript:void(0)">Click me</a> | ||
| /// ``` | ||
| /// | ||
| /// ```html,expect_diagnostic | ||
| /// <a href="javascript:alert('XSS')">Click me</a> | ||
| /// ``` | ||
| /// | ||
| /// ### Valid | ||
| /// | ||
| /// ```html | ||
| /// <a href="https://example.com">Click me</a> | ||
| /// <a href="/path/to/page">Click me</a> | ||
| /// <a href="#section">Click me</a> | ||
| /// <span href="javascript:void(0)">Not a real href</span> | ||
| /// ``` | ||
| /// | ||
| pub NoScriptUrl { | ||
| version: "next", | ||
| name: "noScriptUrl", | ||
| language: "html", | ||
| // Show equivalents from related ecosystems | ||
| sources: &[ | ||
| RuleSource::Eslint("no-script-url").same(), | ||
| RuleSource::EslintReact("jsx-no-script-url").same(), | ||
| RuleSource::EslintQwik("jsx-no-script-url").same(), | ||
| RuleSource::EslintSolid("jsx-no-script-url").same(), | ||
| RuleSource::EslintReactXyz("dom-no-script-url").same(), | ||
| ], | ||
| recommended: true, | ||
| severity: Severity::Error, | ||
| } | ||
| } | ||
|
|
||
| impl Rule for NoScriptUrl { | ||
| type Query = Ast<HtmlOpeningElement>; | ||
| type State = TextRange; | ||
| type Signals = Option<Self::State>; | ||
| type Options = NoScriptUrlOptions; | ||
|
|
||
| fn run(ctx: &RuleContext<Self>) -> Option<Self::State> { | ||
| let element = ctx.query(); | ||
|
|
||
| // Only check <a> elements for HTML (unlike JSX where components/custom elements exist) | ||
| let name = element.name().ok()?; | ||
| let token = name.value_token().ok()?; | ||
| let tag = token.text_trimmed(); | ||
| if !tag.eq_ignore_ascii_case("a") { | ||
| return None; | ||
| } | ||
|
|
||
| let attrs = element.attributes(); | ||
| let attr = attrs.find_by_name("href")?; | ||
| let initializer = attr.initializer()?; | ||
| let value = initializer.value().ok()?; | ||
|
|
||
| if let AnyHtmlAttributeInitializer::HtmlString(html_string) = value | ||
| && let Ok(token) = html_string.value_token() | ||
| { | ||
| let inner = inner_string_text(&token); | ||
| if inner.trim().to_lowercase_cow().starts_with("javascript:") { | ||
| return Some(initializer.range()); | ||
| } | ||
| } | ||
|
|
||
| None | ||
| } | ||
|
|
||
| fn diagnostic(_ctx: &RuleContext<Self>, range: &Self::State) -> Option<RuleDiagnostic> { | ||
| Some( | ||
| RuleDiagnostic::new( | ||
| rule_category!(), | ||
| *range, | ||
| markup! { | ||
| "Avoid using "<Emphasis>"javascript:"</Emphasis>" URLs, as they can be a security risk." | ||
| }, | ||
| ) | ||
| .note(markup! { | ||
| "Using "<Emphasis>"javascript:"</Emphasis>" URLs can lead to security vulnerabilities such as cross-site scripting (XSS)." | ||
| }) | ||
| .note(markup! { | ||
| "Consider using regular URLs, or if you need to handle click events, use event handlers instead." | ||
| }), | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
crates/biome_html_analyze/tests/specs/nursery/noScriptUrl/invalid.astro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| // Astro invalid cases - should trigger the rule | ||
| --- | ||
|
|
||
| <a href="javascript:void(0)">Void</a> | ||
| <a href=" javascript:prompt('XSS') ">Prompt</a> | ||
| <a href="JAVASCRIPT:doSomething()">Uppercase</a> |
71 changes: 71 additions & 0 deletions
71
crates/biome_html_analyze/tests/specs/nursery/noScriptUrl/invalid.astro.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| --- | ||
| source: crates/biome_html_analyze/tests/spec_tests.rs | ||
| expression: invalid.astro | ||
| --- | ||
| # Input | ||
| ```html | ||
| --- | ||
| // Astro invalid cases - should trigger the rule | ||
| --- | ||
|
|
||
| <a href="javascript:void(0)">Void</a> | ||
| <a href=" javascript:prompt('XSS') ">Prompt</a> | ||
| <a href="JAVASCRIPT:doSomething()">Uppercase</a> | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| invalid.astro:5:8 lint/nursery/noScriptUrl ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| × Avoid using javascript: URLs, as they can be a security risk. | ||
|
|
||
| 3 │ --- | ||
| 4 │ | ||
| > 5 │ <a href="javascript:void(0)">Void</a> | ||
| │ ^^^^^^^^^^^^^^^^^^^^^ | ||
| 6 │ <a href=" javascript:prompt('XSS') ">Prompt</a> | ||
| 7 │ <a href="JAVASCRIPT:doSomething()">Uppercase</a> | ||
|
|
||
| i Using javascript: URLs can lead to security vulnerabilities such as cross-site scripting (XSS). | ||
|
|
||
| i Consider using regular URLs, or if you need to handle click events, use event handlers instead. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid.astro:6:8 lint/nursery/noScriptUrl ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| × Avoid using javascript: URLs, as they can be a security risk. | ||
|
|
||
| 5 │ <a href="javascript:void(0)">Void</a> | ||
| > 6 │ <a href=" javascript:prompt('XSS') ">Prompt</a> | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 7 │ <a href="JAVASCRIPT:doSomething()">Uppercase</a> | ||
| 8 │ | ||
|
|
||
| i Using javascript: URLs can lead to security vulnerabilities such as cross-site scripting (XSS). | ||
|
|
||
| i Consider using regular URLs, or if you need to handle click events, use event handlers instead. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid.astro:7:8 lint/nursery/noScriptUrl ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| × Avoid using javascript: URLs, as they can be a security risk. | ||
|
|
||
| 5 │ <a href="javascript:void(0)">Void</a> | ||
| 6 │ <a href=" javascript:prompt('XSS') ">Prompt</a> | ||
| > 7 │ <a href="JAVASCRIPT:doSomething()">Uppercase</a> | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 8 │ | ||
|
|
||
| i Using javascript: URLs can lead to security vulnerabilities such as cross-site scripting (XSS). | ||
|
|
||
| i Consider using regular URLs, or if you need to handle click events, use event handlers instead. | ||
|
|
||
|
|
||
| ``` |
9 changes: 9 additions & 0 deletions
9
crates/biome_html_analyze/tests/specs/nursery/noScriptUrl/invalid.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <!-- Invalid cases - should trigger the rule --> | ||
|
|
||
| <a href="javascript:void(0)">Link</a> | ||
|
|
||
| <a href="javascript:alert('XSS')">Link</a> | ||
|
|
||
| <a href=" javascript:void(0)">Link</a> | ||
|
|
||
| <a href="JAVASCRIPT:void(0)">Link</a> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.