Skip to main content

Code reviews rule: Use of Session storage and Local storage

Written by David Martin

Use of Session storage and Local storage

Why is this an issue?

Data stored in localStorage and sessionStorage:

  • Persists beyond sessions: localStorage data remains after the browser is closed

  • Accessible to any script: Any JavaScript on the page can access stored data

  • Not encrypted: Data is stored in plain text

  • Vulnerable to XSS: If an attacker injects script, they can read all stored data

Storing sensitive information in browser storage creates security risks.

Examples

Example of incorrect code:

// Storing sensitive data in localStorage
localStorage.setItem('authToken', this.userToken);
localStorage.setItem('userData', JSON.stringify(this.sensitiveUserInfo));

How can I fix violations?

  1. Use server-side storage: Keep sensitive data on the server, not in the browser.

  2. Use Salesforce platform cache: For caching, use Platform Cache instead of browser storage.

  3. Minimize stored data: If browser storage is needed, store only non-sensitive, non-identifying information.

  4. Clear on logout: Ensure stored data is cleared when users log out.

When should I disable this rule?

You may dismiss specific violations when storing only non-sensitive preferences or UI state that has no security implications.

Resources

Did this answer your question?