Client-Side Storage Overview and Threat Model
What you'll learn: How browsers let websites store data locally, and why this creates security risks when attackers can read, modify, or inject malicious content into your application's persistent storage.
The Client-Side Storage Landscape
Modern browsers offer several mechanisms for websites to store data on the user's device:
- localStorage: Persistent key-value storage that survives browser restarts, scoped to the origin
- sessionStorage: Similar to localStorage but cleared when the browser tab closes
- IndexedDB: A full NoSQL database in the browser for complex data structures
- Cookies: Small text files sent with every HTTP request, with various scoping options
Think of these like filing cabinets in your office. Some drawers (cookies) get checked every time you leave the building, while others (localStorage) just sit there until you explicitly open them.
The Core Security Problem
Unlike server-side databases protected behind firewalls, client-side storage lives in hostile territory. Any JavaScript running in your origin can read and write to it. This creates three primary threats:
- XSS-based data theft: If an attacker injects malicious scripts (XSS), they can steal authentication tokens, personal data, or session information stored client-side
- Data tampering: Attackers can modify stored data to bypass client-side validation, manipulate application state, or escalate privileges
- Persistent malware: Malicious code stored in IndexedDB or localStorage can survive page reloads, creating persistent attack vectors
The fundamental challenge: Never trust data from client-side storage. An attacker with JavaScript execution can manipulate any client-side storage mechanism since they all share the same origin boundary you've already learned about.
Key Takeaway: Client-side storage mechanisms are convenient but inherently insecure—treat all data retrieved from localStorage, sessionStorage, IndexedDB, or cookies as untrusted user input that requires validation, and never store sensitive secrets like passwords or API keys there.