Sensitive Data Handling and Storage Alternatives
What you'll learn: You'll discover what data should never live client-side and how to use server-side sessions as safer alternatives to browser storage.
The Golden Rule: Don't Store What You Shouldn't
Not all data belongs in the browser. Think of client-side storage like a public notebook anyone can read—even if you write in code, it's still vulnerable. Authentication tokens, passwords, API keys, personal identification numbers, and financial data should either avoid client storage entirely or be handled with extreme care.
What never to store client-side:
- Raw passwords or password hashes
- Social Security numbers, credit card numbers
- Private API keys or secrets
- Long-lived authentication tokens without encryption
- Sensitive personal health or financial information
Token Storage Best Practices
When you must store authentication tokens:
- Use
httpOnlycookies for session tokens—JavaScript cannot access them, blocking XSS attacks - Never store tokens in
localStoragefor critical applications (XSS can steal them instantly) - Prefer short-lived tokens with refresh mechanisms on the server
- sessionStorage is slightly better than localStorage (cleared on tab close) but still vulnerable to XSS
Server-Side Sessions: The Safer Alternative
Instead of storing sensitive data client-side, keep it on your server with session-based authentication. The browser only holds a random session ID (in an httpOnly, Secure, SameSite cookie), while your server maintains all sensitive user data. Think of it like a coat check: you get a ticket (session ID), but your valuable coat (sensitive data) stays safely behind the counter.
This pattern eliminates client-side data exposure entirely—even if an attacker compromises the browser, they only get a meaningless session ID with built-in expiration.
Key Takeaway: Assume client-side storage is always readable by attackers—keep sensitive data on the server, use httpOnly cookies for tokens, and choose server-side sessions over client storage whenever security matters.