Typescript Local Storage With Sensitive Data
Description
Detects when sensitive data from HTTP responses is stored in the browser's localStorage, which is an insecure client-side storage mechanism. This poses a security risk since localStorage data is stored in plaintext and accessible via JavaScript, potentially exposing sensitive information to cross-site scripting (XSS) attacks.
Detection Strategy
• Identifies calls to localStorage.setItem() or similar localStorage methods
• Checks if the data being stored comes from HTTP responses or contains sensitive information
• Reports a vulnerability when sensitive HTTP response data is saved to localStorage without proper encryption or security measures
Vulnerable code example
import axios, { AxiosResponse } from 'axios';
async function fetchAndStore(): Promise<void> {
// Vulnerable: Storing raw HTTP response object in localStorage by force-casting to string
const response: AxiosResponse = await axios.get('https://api.example.com/data');
// This cast bypasses TypeScript checks and may store sensitive data unsafely
localStorage.setItem('apiData', response as unknown as string);
...✅ Secure code example
import axios, { AxiosResponse } from 'axios';
async function fetchAndStore(): Promise<void> {
// Safe: Extract and store only the response data as JSON string
const response: AxiosResponse = await axios.get('https://api.example.com/data');
localStorage.setItem('apiData', JSON.stringify(response.data));
// Safe: Convert fetch response to JSON before storing and type the result...Search for vulnerabilities in your apps for free with Fluid Attacks' automated security testing! Start your 21-day free trial and discover the benefits of the Continuous Hacking Essential plan. If you prefer the Advanced plan, which includes the expertise of Fluid Attacks' hacking team, fill out this contact form.