Assume everything sent to the browser is public: users (and malware, extensions, XSS payloads) can read your bundle, network traffic in DevTools, storage and memory. So: never put secrets in client code, minimise the sensitive data you send, keep session credentials in HttpOnly; Secure; SameSite cookies rather than localStorage, use HTTPS everywhere (HSTS), don't log or cache sensitive data, clear it when it's no longer needed, and harden against XSS, because XSS can read anything JavaScript can.
// ✗ shipped to every visitor, visible in the bundle and network tab
const stripe = new Stripe('sk_live_…');
fetch('https://api.openai.example/v1', { headers: { Authorization: `Bearer ${import.meta.env.VITE_SECRET}` } });
VITE_*, NEXT_PUBLIC_*, REACT_APP_*) are inlined into the bundle. Only public, publishable keys belong there..env files out of git; scan for leaked keys (GitHub secret scanning, gitleaks) and rotate immediately if one leaks.Referer headers and analytics). Use POST bodies or headers, and set Referrer-Policy: strict-origin-when-cross-origin (or stricter).| Storage | Readable by JS (and XSS) | Sent automatically | Notes |
|---|---|---|---|
HttpOnly cookie | No | Yes (needs CSRF protection) | Best for session/refresh tokens |
| Memory (variable/closure) | Yes, while page lives | No | Good for short-lived access tokens |
sessionStorage | Yes | No | Per-tab, cleared when the tab closes |
localStorage / IndexedDB | Yes | No | Persists indefinitely; avoid for credentials |
Set-Cookie: __Host-session=…; Path=/; Secure; HttpOnly; SameSite=Lax
The __Host- prefix forces Secure, Path=/ and no Domain, preventing subdomain overrides. For SPAs, the backend-for-frontend (BFF) pattern keeps OAuth tokens on the server and gives the browser only a session cookie.
If you must persist sensitive data offline, encrypt it with Web Crypto (crypto.subtle) using a non-extractable key, and understand that this mainly protects data at rest, not against XSS running in the page.
Strict-Transport-Security. No mixed content.Cache-Control: no-store on responses with personal data so they aren't stored by browsers or shared caches.crypto.getRandomValues()/crypto.randomUUID(), never Math.random(), for anything security-related.autocomplete values (current-password, new-password, one-time-code, cc-number) help password managers; autocomplete="off" on passwords mostly hurts users.beforeSend hooks, deny-lists for fields like password, authorization).console.log user objects in production.XSS gives attackers everything JavaScript can reach, so the controls above depend on XSS defences: output encoding, strict CSP, Trusted Types, dependency hygiene, and Subresource Integrity for third-party scripts (or self-host them). Limit third-party scripts on pages with sensitive data (login, checkout).
HttpOnly cookies or a BFF are preferred.Spotted a mistake or a rights concern? Report an issue