Security & Configuration

Use Browser Storage with Clear Privacy and Expiration Rules

Choose cookies, session storage, local storage, or IndexedDB based on access needs, sensitivity, and lifecycle.

Use Browser Storage with Clear Privacy and Expiration Rules

Overview

Browser storage is accessible to scripts in its origin and should not be treated as a secure vault for secrets.

Choose storage by purpose

Cookies are sent with matching HTTP requests and can use security attributes. sessionStorage is scoped to a tab session, while localStorage persists across browser restarts. IndexedDB supports structured and larger client-side datasets. Select the smallest mechanism that satisfies the requirement.

Keep secrets out of script-readable storage

Do not store session secrets, long-lived access tokens, passwords, or sensitive personal data in local storage. An XSS vulnerability can read script-accessible storage. HttpOnly cookies can reduce direct JavaScript access to session identifiers, but they require CSRF-aware request design.

Define retention and migration

Give cached data an expiration strategy and a version. Handle quota errors and storage eviction. Provide logout cleanup for user-specific local state and avoid showing one account's cached data after another account signs in on the same browser.

Test privacy boundaries

Test shared devices, multiple accounts, private browsing, browser storage limits, and logout. Treat data stored in the browser as potentially inspectable by the device owner and by scripts executing in the origin.

Implementation checklist

  • Define the expected behavior and supported environments.
  • Keep security, accessibility, and failure states in the design.
  • Measure the result with representative user journeys.
  • Document operational assumptions and review them after changes.
Share this page Help other developers discover this resource.