Auth token in localStorage
Auth token in localStorage. Stores a credential where any script on the page can read it.
This is how the comment reads inline, under the flagged line.
Why it matters
One XSS bug, or one bad npm package, can read localStorage and take the session.
What to do
Keep sessions in HttpOnly, Secure, SameSite cookies.
Examples
localStorage.setItem("authToken", token);// the server sets an HttpOnly session cookie
What it catches
It reads a pattern on each line, aware of strings and comments. Languages: TypeScript and JavaScript. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.
Flagged
localStorage.setItem("authToken", t);Left alone
localStorage.setItem("theme", "dark");Research
Auth tokens in `localStorage`. Any injected script can read them.
Turn it off or change it
In a config file for the CLI, or in the API's rules field:
{
"rules": {
"disable": [
"token-in-localstorage"
],
"severity": {
"token-in-localstorage": "low"
}
}
}