IndexedDB Security Considerations
You'll learn how IndexedDB protects data through same-origin isolation, where XSS attacks pose the greatest risk, and best practices for secure structured data storage.
What Is IndexedDB?
IndexedDB is a browser-based database for storing large amounts of structured data client-side. Unlike localStorage's simple key-value pairs, IndexedDB handles complex objects, indices, and transactions—think of it as a mini-database living in your browser.
Same-Origin Isolation
Like other browser storage mechanisms, IndexedDB enforces same-origin policy. Each origin (scheme + host + port) gets its own isolated database namespace. https://example.com cannot access IndexedDB data from https://evil.com or even http://example.com (different scheme).
This isolation is your first line of defense—malicious sites can't steal your stored data directly.
The XSS Threat
Here's the critical vulnerability: if an attacker injects JavaScript via XSS into your origin, they bypass all IndexedDB protections. Since the malicious script runs in your origin's context, it has full access to read, modify, or delete any IndexedDB data.
// Malicious XSS payload can do:
const request = indexedDB.open('UserDatabase');
request.onsuccess = function(event) {
const db = event.target.result;
// Attacker now has full database access
};
Best Practices
- Never store sensitive data unencrypted: Treat IndexedDB like you would
localStorage—assume XSS is possible - Encrypt sensitive values before storing (though key management remains challenging client-side)
- Validate all data retrieved from IndexedDB before use (it could be tampered)
- Implement CSP to reduce XSS attack surface
- Clear databases on logout for sensitive applications
Key Takeaway: IndexedDB provides same-origin isolation but offers zero protection against XSS attacks—your primary defense is preventing script injection and never trusting client-side storage with unencrypted sensitive data.