Course contentsShow
← Security
Lesson 1062 of 3,05222. Web Security - Client-Side and SPA SecurityFree lesson

Browser Storage and Origin Isolation

How localStorage, sessionStorage, and IndexedDB are scoped to origins and implications for data isolation.

Browser Storage and Origin Isolation

You'll learn how browser storage mechanisms are confined to specific origins and what that means for keeping data separated between sites.

What Is Browser Storage?

Modern browsers provide three main client-side storage APIs: localStorage, sessionStorage, and IndexedDB. These let web applications store data directly in the user's browser—like saving preferences, caching content, or keeping temporary state.

Here's the critical security rule: Each of these storage mechanisms is strictly scoped to an origin (the scheme + host + port combination you learned earlier).

How Origin Isolation Works

When https://bank.example.com stores data in localStorage, only scripts running on that exact origin can read or modify it. A different site like https://evil.com cannot access that bank's storage—even though both run in the same browser.

Think of it like apartment buildings: each origin has its own locked storage unit in the building's basement. You can't peek into or steal from another tenant's unit, even though you're in the same building.

Storage Scoping Rules

  • localStorage: Persistent across browser sessions, scoped to origin
  • sessionStorage: Cleared when the tab closes, scoped to origin
  • IndexedDB: Database-like storage, scoped to origin

Even subdomains are isolated: https://shop.example.com and https://blog.example.com have completely separate storage spaces.

Security Implications

This isolation prevents:

  • One site from stealing another site's stored credentials or tokens
  • Malicious scripts from cross-site data theft
  • Attackers reading sensitive cached information from legitimate sites

However, remember: if an attacker can execute JavaScript on your origin (via XSS), they can access your storage freely—the Same-Origin Policy doesn't protect against same-origin attacks.

Key Takeaway: Browser storage APIs are isolated by origin—each origin gets its own private storage space that other origins cannot access, forming a fundamental security boundary in web applications.