-
-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lint): add
noDocumentCookie
rule (#4204)
- Loading branch information
1 parent
2e5e3f2
commit 2e5b656
Showing
13 changed files
with
429 additions
and
82 deletions.
There are no files selected for viewing
This file contains 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
8 changes: 8 additions & 0 deletions
8
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.
183 changes: 101 additions & 82 deletions
183
crates/biome_configuration/src/analyzer/linter/rules.rs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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
147 changes: 147 additions & 0 deletions
147
crates/biome_js_analyze/src/lint/nursery/no_document_cookie.rs
This file contains 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,147 @@ | ||
use biome_analyze::{context::RuleContext, declare_lint_rule, Rule, RuleDiagnostic, RuleSource}; | ||
use biome_console::markup; | ||
use biome_js_semantic::SemanticModel; | ||
use biome_js_syntax::{ | ||
global_identifier, AnyJsAssignment, AnyJsExpression, JsAssignmentExpression, | ||
}; | ||
use biome_rowan::AstNode; | ||
|
||
use crate::services::semantic::Semantic; | ||
|
||
declare_lint_rule! { | ||
/// Disallow direct assignments to `document.cookie`. | ||
/// | ||
/// It's not recommended to use document.cookie directly as it's easy to get the string wrong. | ||
/// Instead, you should use the [Cookie Store API](https://developer.mozilla.org/en-US/docs/Web/API/CookieStore). | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// document.cookie = "foo=bar"; | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// document.cookie += "; foo=bar"; | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```js | ||
/// const array = document.cookie.split("; "); | ||
/// ``` | ||
/// | ||
/// ```js | ||
/// await cookieStore | ||
/// .set({ | ||
/// name: "foo", | ||
/// value: "bar", | ||
/// expires: Date.now() + 24 * 60 * 60, | ||
/// domain: "example.com", | ||
/// }) | ||
/// ``` | ||
/// | ||
/// ```js | ||
/// import Cookies from 'js-cookie'; | ||
/// | ||
/// Cookies.set('foo', 'bar'); | ||
/// ``` | ||
/// | ||
pub NoDocumentCookie { | ||
version: "next", | ||
name: "noDocumentCookie", | ||
language: "js", | ||
recommended: false, | ||
sources: &[RuleSource::EslintUnicorn("no-document-cookie")], | ||
} | ||
} | ||
|
||
/// Check `expr` is `document` | ||
fn is_global_document(expr: &AnyJsExpression, model: &SemanticModel) -> Option<()> { | ||
let (reference, name) = global_identifier(expr)?; | ||
|
||
// Check identifier is `document` | ||
if name.text() != "document" { | ||
return None; | ||
}; | ||
|
||
// TODO: Verify that the variable is assigned the global `document` to be closer to the original rule. | ||
model.binding(&reference).is_none().then_some(()) | ||
} | ||
|
||
/// Check member is `cookie` | ||
fn is_cookie(assignment: &AnyJsAssignment) -> Option<()> { | ||
const COOKIE: &str = "cookie"; | ||
match assignment { | ||
// `document.cookie` | ||
AnyJsAssignment::JsStaticMemberAssignment(static_assignment) => { | ||
let property = static_assignment.member().ok()?; | ||
|
||
if property.text() != COOKIE { | ||
return None; | ||
}; | ||
} | ||
// `document["cookie"]` | ||
AnyJsAssignment::JsComputedMemberAssignment(computed_assignment) => { | ||
let any_expr = computed_assignment.member().ok()?; | ||
let string_literal = any_expr | ||
.as_any_js_literal_expression()? | ||
.as_js_string_literal_expression()?; | ||
let inner_string = string_literal.inner_string_text().ok()?; | ||
|
||
if inner_string.text() != COOKIE { | ||
return None; | ||
} | ||
} | ||
_ => { | ||
return None; | ||
} | ||
} | ||
|
||
Some(()) | ||
} | ||
|
||
impl Rule for NoDocumentCookie { | ||
type Query = Semantic<JsAssignmentExpression>; | ||
type State = (); | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let node = ctx.query(); | ||
let left = node.left().ok()?; | ||
|
||
let any_assignment = left.as_any_js_assignment()?; | ||
|
||
let expr = match any_assignment { | ||
AnyJsAssignment::JsStaticMemberAssignment(assignment) => assignment.object().ok()?, | ||
AnyJsAssignment::JsComputedMemberAssignment(assignment) => assignment.object().ok()?, | ||
_ => { | ||
return None; | ||
} | ||
}; | ||
|
||
is_global_document(&expr, ctx.model())?; | ||
|
||
is_cookie(any_assignment)?; | ||
|
||
Some(()) | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { | ||
let node = ctx.query(); | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
node.range(), | ||
markup! { | ||
"Direct assigning to "<Emphasis>"document.cookie"</Emphasis>" is not recommended." | ||
}, | ||
) | ||
.note(markup! { | ||
"Consider using the "<Hyperlink href = "https://developer.mozilla.org/en-US/docs/Web/API/CookieStore">"Cookie Store API"</Hyperlink>"." | ||
}), | ||
) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
crates/biome_js_analyze/tests/specs/nursery/noDocumentCookie/invalid.js
This file contains 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 @@ | ||
document.cookie = "foo=bar"; | ||
document.cookie += ";foo=bar" | ||
|
||
window.document.cookie = "foo=bar"; | ||
globalThis.window.document.cookie = "foo=bar"; | ||
|
||
document["cookie"] = "foo=bar" |
94 changes: 94 additions & 0 deletions
94
crates/biome_js_analyze/tests/specs/nursery/noDocumentCookie/invalid.js.snap
This file contains 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,94 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalid.js | ||
--- | ||
# Input | ||
```jsx | ||
document.cookie = "foo=bar"; | ||
document.cookie += ";foo=bar" | ||
|
||
window.document.cookie = "foo=bar"; | ||
globalThis.window.document.cookie = "foo=bar"; | ||
|
||
document["cookie"] = "foo=bar" | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.js:1:1 lint/nursery/noDocumentCookie ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Direct assigning to document.cookie is not recommended. | ||
> 1 │ document.cookie = "foo=bar"; | ||
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
2 │ document.cookie += ";foo=bar" | ||
3 │ | ||
i Consider using the Cookie Store API. | ||
``` | ||
|
||
``` | ||
invalid.js:2:1 lint/nursery/noDocumentCookie ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Direct assigning to document.cookie is not recommended. | ||
1 │ document.cookie = "foo=bar"; | ||
> 2 │ document.cookie += ";foo=bar" | ||
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
3 │ | ||
4 │ window.document.cookie = "foo=bar"; | ||
i Consider using the Cookie Store API. | ||
``` | ||
|
||
``` | ||
invalid.js:4:1 lint/nursery/noDocumentCookie ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Direct assigning to document.cookie is not recommended. | ||
2 │ document.cookie += ";foo=bar" | ||
3 │ | ||
> 4 │ window.document.cookie = "foo=bar"; | ||
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
5 │ globalThis.window.document.cookie = "foo=bar"; | ||
6 │ | ||
i Consider using the Cookie Store API. | ||
``` | ||
|
||
``` | ||
invalid.js:5:1 lint/nursery/noDocumentCookie ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Direct assigning to document.cookie is not recommended. | ||
4 │ window.document.cookie = "foo=bar"; | ||
> 5 │ globalThis.window.document.cookie = "foo=bar"; | ||
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
6 │ | ||
7 │ document["cookie"] = "foo=bar" | ||
i Consider using the Cookie Store API. | ||
``` | ||
|
||
``` | ||
invalid.js:7:1 lint/nursery/noDocumentCookie ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Direct assigning to document.cookie is not recommended. | ||
5 │ globalThis.window.document.cookie = "foo=bar"; | ||
6 │ | ||
> 7 │ document["cookie"] = "foo=bar" | ||
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
i Consider using the Cookie Store API. | ||
``` |
22 changes: 22 additions & 0 deletions
22
crates/biome_js_analyze/tests/specs/nursery/noDocumentCookie/valid.js
This file contains 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,22 @@ | ||
document.cookie | ||
|
||
const foo = document.cookie; | ||
|
||
const array = document.cookie.split("; "); | ||
|
||
cookieStore | ||
.set({ | ||
name: "foo", | ||
value: "bar", | ||
expires: Date.now() + 24 * 60 * 60, | ||
domain: "example.com", | ||
}) | ||
|
||
function document_is_not_global1(document){ | ||
document.cookie = "bar=foo" | ||
} | ||
|
||
function document_is_not_global2(){ | ||
const document = "foo"; | ||
document.cookie = "bar=foo" | ||
} |
29 changes: 29 additions & 0 deletions
29
crates/biome_js_analyze/tests/specs/nursery/noDocumentCookie/valid.js.snap
This file contains 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,29 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: valid.js | ||
--- | ||
# Input | ||
```jsx | ||
document.cookie | ||
|
||
const foo = document.cookie; | ||
|
||
const array = document.cookie.split("; "); | ||
|
||
cookieStore | ||
.set({ | ||
name: "foo", | ||
value: "bar", | ||
expires: Date.now() + 24 * 60 * 60, | ||
domain: "example.com", | ||
}) | ||
|
||
function document_is_not_global1(document){ | ||
document.cookie = "bar=foo" | ||
} | ||
|
||
function document_is_not_global2(){ | ||
const document = "foo"; | ||
document.cookie = "bar=foo" | ||
} | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.