Security rules
NGB014: Insecure token storage
Reports authentication or session credentials (access/refresh/ID tokens, JWTs, session IDs, API keys, passwords) written to browser-accessible persistent storage: localStorage, sessionStorage and IndexedDB object stores.
| Property | Value |
|---|---|
| Rule ID | NGB014 |
| Category | Sensitive data & payment security |
| Default severity | high |
| CWE | CWE-922 |
| Built-in presets | recommended: error, strict: error, payments: error |
Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.
What it checks
Reports authentication or session credentials (access/refresh/ID tokens, JWTs, session IDs, API keys, passwords) written to browser-accessible persistent storage: localStorage, sessionStorage and IndexedDB object stores. Evidence comes from the storage key, the stored value's name, its type, and names along its data flow.
Why it matters
Anything in Web Storage or IndexedDB is readable by every script running in the origin. A single XSS bug or compromised third-party script can exfiltrate long-lived tokens, which then work from the attacker's machine. This is a design concern rather than a vulnerability by itself.
Detection behavior and supported patterns
localStorage.setItem(key, value),sessionStorage.setItem(…),localStorage.token = v,localStorage["token"] = v- IndexedDB
objectStore.put(value, key)/add(value, key)onIDBObjectStore - Key evidence: constant keys (including
const TOKEN_KEY = "access_token") classified by head noun (access_token,jwt,refreshToken,sessionId,apiKey) - Value evidence: identifier/property names (
res.accessToken), object literal keys (JSON.stringify({ token })), declared type properties, and names along the traced data flow - Not reported: CSRF/XSRF tokens, metadata (
tokenExpiry,tokenType), push/device/pagination tokens, opaque payment tokens, hashed or masked values
Example
this.http.post<{ accessToken: string }>('/api/login', creds) .subscribe(res => localStorage.setItem('access_token', res.accessToken));Safer alternative
// Server sets: Set-Cookie: session=…; HttpOnly; Secure; SameSite=Strictthis.http.post('/api/login', creds, { withCredentials: true }).subscribe();Remediation
Keep session credentials in HttpOnly, Secure, SameSite cookies set by the server, or hold access tokens only in memory and refresh them via an HttpOnly refresh cookie (backend-for-frontend pattern).
False-positive considerations
- Short-lived, audience-restricted tokens may be an accepted trade-off; document it with an exception and an expiry date.
Known limitations
- Name-based evidence: credentials stored under neutral names (
localStorage.setItem("a", x)with an untypedx) are not detected. - Storage wrappers from libraries (ngx-webstorage, localForage, idb) are not modeled.
- Cookies set from JavaScript are not covered by this rule.
References
Run it
ngbastion explain NGB014