SKY-D270 · Security in code
Token or password saved in browser storage
localStorage and sessionStorage keep values in the browser where any script on the page can read them. SKY-D270 reports a localStorage.setItem() or sessionStorage.setItem() call whose key names a token, password, secret, API key or session identifier.
- Languages
- TypeScript/JavaScript
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
- Catalog name
- Sensitive data in storage
Why it matters
One cross-site scripting bug, or one compromised npm package running on the page, can read localStorage.getItem("accessToken") and send it away. The attacker can then call your API as that user until the token expires, from any machine. An httpOnly cookie cannot be read by script at all.
What Skylos flags
interface LoginResponse { accessToken: string; expiresAt: number;} export function rememberLogin(login: LoginResponse): void { localStorage.setItem("accessToken", login.accessToken); localStorage.setItem("tokenExpiresAt", String(login.expiresAt));}What Skylos leaves alone
interface LoginResponse { csrfToken: string; expiresAt: number;} export function rememberLogin(login: LoginResponse): void { localStorage.setItem("tokenExpiresAt", String(login.expiresAt)); sessionStorage.setItem("csrf_token", login.csrfToken);}Why Skylos leaves it alone
tokenExpiresAt mentions a token but ends in ExpiresAt, and only keys that end in a sensitive word count. Keys starting with csrf or xsrf are skipped on purpose: a CSRF token is meant to be readable by the page's own script.
How Skylos decides
- Runs with
--dangeron TypeScript and JavaScript files. Finds calls written exactly aslocalStorage.setItem(…)orsessionStorage.setItem(…). - Reads the first argument only when it is a plain quoted string. The key is lower-cased and
_and-are removed, soauth_token,auth-tokenandauthTokenare the same. - Skips keys that start with
csrforxsrf. - Reports the call when the key is, or ends with, one of:
token,auth,jwt,secret,password,passwd,credential,apikey,bearer,accesstoken,refreshtoken,sessionid,sessionkey,privatekey. - The stored value is not looked at: the key name is the only evidence.
How to fix it
Let the server put the session in a cookie marked HttpOnly, Secure and SameSite, and send requests with credentials: "include". The browser then attaches the token without page script ever seeing it. Keep only harmless facts, such as the expiry time, in storage.
interface LoginResponse {Removed: accessToken: string; expiresAt: number;} Removed: export function rememberLogin(login: LoginResponse): void {Removed: localStorage.setItem("accessToken", login.accessToken);Added: // The server sets the session cookie with HttpOnly; Secure; SameSite=Lax.Added: export async function logIn(email: string, password: string): Promise<void> {Added: const response = await fetch("/api/login", {Added: method: "POST",Added: credentials: "include",Added: headers: { "Content-Type": "application/json" },Added: body: JSON.stringify({ email, password }),Added: });Added: const login: LoginResponse = await response.json(); localStorage.setItem("tokenExpiresAt", String(login.expiresAt));}When it can be wrong
- Keys that end in a sensitive word but hold nothing secret are reported:
oauthholding the chosen login provider ends inauth, andpushTokenholding a public push-subscription id ends intoken. - Missed: a key held in a constant (
setItem(TOKEN_KEY, t)), a template-literal key,window.localStorage.setItem(…), bracket assignment (localStorage["token"] = t) and storage wrappers such aslocalforageare not checked.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
Mark a reviewed line with a comment on the same line.
TypeScript localStorage.setItem("pushToken", sub.token); // skylos: ignore[SKY-D270] public id[tool.skylos] ignore = ["SKY-D270"]turns the rule off for the whole project.skylos baseline .and then--baselineaccept the existing calls and report only new ones.