Skip to main content

SKY-D270 · Security in code

Token or password saved in browser storage

Medium

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
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

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

src/auth/session.ts · TypeScriptReported on line 7
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

src/auth/session.ts · TypeScript
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

  1. Runs with --danger on TypeScript and JavaScript files. Finds calls written exactly as localStorage.setItem(…) or sessionStorage.setItem(…).
  2. Reads the first argument only when it is a plain quoted string. The key is lower-cased and _ and - are removed, so auth_token, auth-token and authToken are the same.
  3. Skips keys that start with csrf or xsrf.
  4. 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.
  5. 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.

src/auth/session.ts · TypeScript · the fix, against the flagged file
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: oauth holding the chosen login provider ends in auth, and pushToken holding a public push-subscription id ends in token.
  • 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 as localforage are 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 --baseline accept the existing calls and report only new ones.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.