QA
Intermediate LevelWeb Security & Cyber Defense

Compare security profiles of localStorage, sessionStorage, httpOnly Cookies, and IndexedDB.

❓ Interview Prompt: Compare client storage mechanisms regarding XSS vulnerability, CSRF exposure, tab scoping, and secure token storage recommendations.

Evaluating client-side storage security profiles:

| Storage Mechanism | XSS Vulnerable? | CSRF Vulnerable? | Scoping & Lifetime |

|---|---|---|---|

| 'localStorage' | ❌ YES (Any JS can read) | ✅ NO (Not sent automatically) | Persistent across tabs & restarts |

| 'sessionStorage' | ❌ YES (Any JS can read) | ✅ NO (Not sent automatically) | Scoped strictly to current tab |

| 'HttpOnly' Cookie | ✅ NO (JS cannot access!) | ❌ YES (Mitigated via 'SameSite') | Sent automatically on requests |

| 'IndexedDB' | ❌ YES (Any JS can read) | ✅ NO (Not sent automatically) | Persistent structured DB storage |

•
Recommendation: Store authentication JWTs in 'HttpOnly', 'Secure', 'SameSite=Strict' cookies to block XSS token theft.

Code Implementation Example

typescript
// Setting secure HttpOnly cookie response
res.cookie('jwt', token, {
  httpOnly: true, // Prevents XSS script access!
  secure: true,   // Forces HTTPS transmission
  sameSite: 'strict' // Mitigates CSRF attacks
});
💡Setting secure HttpOnly, Secure, and SameSite=Strict authentication cookies in Express.
🎯 Interview Key Takeaway

Store sensitive authentication tokens in HttpOnly, Secure, SameSite cookies to protect against XSS token theft.

#Storage Security#HttpOnly Cookies#localStorage#Intermediate
Back to all Web Security & Cyber Defense Questions→

More Web Security & Cyber Defense Interview Questions

Beginner LevelSecurity & Web Security

Explain OWASP Top 10 vulnerabilities (XSS, CSRF, SQL Injection) and their defenses.

Beginner LevelSecurity & Web Security

What are the 3 types of XSS (Stored, Reflected, DOM-based) and how do they differ?

Beginner LevelSecurity & Web Security

How do Parameterized Queries (Prepared Statements) prevent SQL Injection attacks?