Where a bearer token lives in the browser
A token in local storage is readable by any script on the page. A token in the URL is readable by logs. An HttpOnly cookie is the smaller exposure for a browser session.
Diagram
From a login response to a place the token sits
Step 1 of 4
Issue
The server creates a short-lived access token and, if needed, a refresh token.
# The token is how the browser will call the API. # It should live for minutes, not months.
localStorage or a query string.
Any script on the origin can copy it.
URLs are stored by servers and proxies.
The example on the open step is one illustration, or the fix you can paste into an application you own.
What this page is made of
4 pieces. Each one says what fails, who is affected, what you would notice, and what you change.
localStorage is the page’s memory
- What fails
- Any script that runs on the origin can read local storage. A single XSS becomes a stolen token, and HttpOnly cannot help a value that is not a cookie.
- Who is affected
- The account holder, for as long as the token works.
- What you would notice
- A front end that saves accessToken in localStorage after login.
- What you change
- Put the browser credential in an HttpOnly cookie. Keep using headers only for non-browser clients that can store secrets.
URLs and referrers
- What fails
- A token in a query string is copied into access logs, browser history, and the next site’s referrer.
- Who is affected
- The account holder, and anyone who can read those logs.
- What you would notice
- A redirect that appends the token, or a support bundle that includes full URLs.
- What you change
- Put tokens in headers or cookies. Strip them from log formatters.
Refresh is the longer secret
- What fails
- A short access token limits a theft. A long-lived refresh token in the browser undoes that if it is readable and not rotated.
- Who is affected
- The same person, for the refresh token’s whole life.
- What you would notice
- A refresh token in localStorage with a 90-day life, and no rotation on use.
- What you change
- Rotate refresh tokens. Detect reuse. Keep them in the HttpOnly cookie or on the server.
Non-browser clients
- What fails
- A mobile app or a server can store a token in a platform secret store. That is a different design from a web origin. Do not copy the web localStorage pattern into the app because the web app did it.
- Who is affected
- Users of that client if its storage is world-readable.
- What you would notice
- One authentication library used unchanged in the browser and on the server, dumping tokens to disk.
- What you change
- Split the storage advice by client. Browsers get cookies. Servers get a secret store and a short lifetime.
The place is the exposure
A bearer token is a session. Where you put it decides who can copy it. Browsers offer localStorage and sessionStorage, which any script on the origin can read, and cookies, which can be marked unreadable to script. Choosing storage because the tutorial used localStorage chooses the XSS blast radius on purpose.
Non-browser clients are allowed to hold a token if the platform gives them a secret store. Do not let that pattern leak back into the website. The website already has a cookie jar designed for this. Use it, with HttpOnly, Secure, and SameSite, and a short max age.
The URL is a log line
Redirects that append a token were a workaround for browsers that could not set cookies across a flow. They put the secret in a place every proxy and the next page’s referrer can store. If a flow still needs a one-time code in the URL, make it single-use, bound to the client, and exchange it immediately for a cookie. Do not let the long-lived token touch the address bar.
Scrub your log format. A request logger that stores the full URL will keep collecting tokens after you think you stopped issuing them, because old clients and old emails still have links. The scrub belongs in the logger, not only in the issuer.
Refresh tokens are the real session
An access token that lives for ten minutes is a small theft if you can revoke the refresh token that mints the next one. A refresh token in localStorage for months is the session an attacker wanted. Rotate it on use. If a used refresh token appears again, revoke the family. That detects a copied token without waiting for the user to notice.
You would notice the token in the application tab of the browser’s storage, in a redirect Location header, and in an access log sample. The person affected is the account holder until revocation. The finding names the storage and the lifetime. It does not paste a token.
What you change
Move the browser credential into an HttpOnly cookie. Keep CSRF protection, because a cookie will be sent by the browser. Delete localStorage writes. Exchange any URL code for the cookie and then drop the code. Rotate refresh tokens and revoke on reuse.
Add a test that the login response sets the cookie flags and that the response body does not contain the token. Add a log-scrub test if the logger is yours. The CSRF and rotation pages are the pair to read with this one.
Check yourself
Recognition and the fix. Nothing here is a procedure.
The single-page app stores the access token in localStorage. What failed?
Sibling techniques
Pages at the same depth, plus a lab and a glossary card for this one.