Browser applications
The photo editor has no server of its own, so its OAuthA framework that lets a person give an application limited access to their resources at another service, using tokens instead of sharing their password. OAuth is about access; OpenID Connect adds sign-in. client is the JavaScript in Ava's browser. It runs the code flow with PKCEProof Key for Code Exchange, pronounced pixie: the client creates a secret for each authorization attempt, sends only a hash of it at the start, and proves it holds the secret when redeeming the code, so a stolen authorization code is useless to anyone else. as a public clientAn OAuth client, such as code running in a browser or an app on a phone, that cannot keep a credential secret because the people using it can inspect it. It relies on protections such as PKCE instead of a client secret., which means its tokens live where any script in the page can reach them, and the rest of its design limits what that costs.
The client in the page
The photo editor at editor.example is built differently. It is a set of files served from a static host, and once Ava's browser loads them, the editor runs entirely in the page, with no server of its own behind it.
It still needs Ava's photos. It asks for photos.read to open them and photos.write to save edited copies into a new album. With no backend, the only place left for its OAuth client is the JavaScript in her browser, registered with the photo service as the public client photo-editor.
The editor runs the authorization code flowThe main OAuth flow: the browser visits the authorization server, where the person signs in and approves access, then carries an authorization code back to the client, which exchanges it for tokens directly. When the client runs on a server, the tokens never pass through the browser. with PKCE in JavaScript. When Ava selects Open from photo account, it generates a fresh verifier with the browser's cryptographic random number generator and sends the tab to the photo service's authorizationDeciding whether a requested action is permitted, such as whether a signed-in account may delete a particular photo. Authentication says which account is asking; authorization decides what it can do. endpoint. The page is replaced while she is away, so the editor keeps its pending attempt, including state and the verifier, in the tab's sessionThe ongoing connection between later requests and an earlier sign-in, so you are not asked for your password on every click. It is often tracked with a cookie holding an unpredictable session identifier. storage until its callback page reads and removes them.
There is no client secretA shared secret that a confidential client uses to prove itself to the authorization server, much like a password for an application. It belongs on a backend, never in code sent to browsers or phones.. Anyone can read the editor's JavaScript, so a secret placed there would prove nothing. PKCE binds the code to this attempt, and the photo service requires it from public clients.
The code exchange meets a rule of the browser. A page may not read responses from a different origin, meaning a different combination of scheme, host, and port, unless that server allows it. This is the same-origin policy, and a server relaxes it with Cross-Origin Resource Sharing (CORS) by naming the origins whose scripts may read its responses, as What browsers add showed. The editor's code exchange shows both sides of that arrangement. All domains, tokens and keys in these examples are fictional.
POST /token HTTP/1.1
Host: auth.photos.example
Origin: https://editor.example
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&code=demo-code-21
&redirect_uri=https%3A%2F%2Feditor.example%2Foauth%2Fcallback
&code_verifier=btl_training_verifier_for_the_photo_editor_2026
&client_id=photo-editorHTTP/1.1 200 OK
Access-Control-Allow-Origin: https://editor.example
Content-Type: application/json
Cache-Control: no-store
{
"access_token": "demo-editor-access-token-1",
"token_type": "Bearer",
"expires_in": 600,
"refresh_token": "demo-editor-refresh-token-1",
"scope": "photos.read photos.write"
}The browser adds the Origin header itself, and the page cannot change it. The Access-Control-Allow-Origin header in the response is what lets the editor's script read the tokens. Some authorization servers allow any origin at the token endpointThe authorization server address where clients exchange an authorization code, refresh token, or other grant for tokens, in a direct request rather than through a browser redirect., others only origins registered for the client, and the photo APIAn application programming interface: a defined way for software to interact with a service. A web API offers endpoints where other programs send HTTP requests. has to allow editor.example as well. The authorization endpoint is different. The editor sends the whole tab there instead of fetching it, and current guidance says that endpoint must not allow cross-origin reads at all.
Where the tokens live
The editor now holds its tokens in the same environment as everything else on the page. What happens if someone else's script runs there?
Suppose a bug in the editor inserts photo captions into the page as HTML instead of plain text, and one caption contains a fragment of script. Or suppose a library the editor loads from another site is compromised. Either way, the attacker's code runs inside the editor's page with the same powers as the editor's own code. This is cross-site scripting, often shortened to XSS, from Cross-site scripting and token storage. Whatever the editor can do with its tokens, the injected script can do too, and often it can read them outright.
The editor has a few places to keep its tokens. Besides a variable in the page and the browser's session and local storage, it can start a web worker, a script that runs in the background beside the page, keeps its own variables and talks to the page only by passing messages. A service worker is a similar background script that the browser keeps for the whole site. The table compares them:
| Where the editor keeps tokens | How long they last | What injected script can do |
|---|---|---|
| A variable in the page's memory | Until the tab reloads or closes | Harder to find directly, but it can replace the functions that use the token, or simply call them. |
| Session storage | Until the tab closes, and only in that tab | Read the tokens with one line of code. |
| Local storage | Until removed, shared by every tab of the origin | Read the tokens with one line of code, in any editor tab, long after Ava finished editing. |
| A web worker or service worker | While the worker runs | It cannot read the worker's variables, but it can ask the worker for anything the page may ask for, such as a fresh access token. |
Writing a tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret. into a cookie from JavaScript adds a problem of its own: the browser then also sends it with every request to the editor's own host, where it was never meant to go.
Isolation helps. A refresh tokenA longer-lived credential that a client sends to the authorization server, never to an API, to get new access tokens without asking the person again. kept inside a worker cannot simply be copied out, and an access tokenA token that a client presents to an API to use access it has been granted, usually limited in what it allows and how long it lasts. It authorizes API calls; it does not tell the client who signed in. held in memory disappears with the tab. But no storage choice stops the most capable version of the attack. Injected script can start an authorization request of its own, in a hidden frame or a small pop-up window. If Ava is still signed in at the photo service and has approved the editor before, the photo service may answer without showing her anything. It has no reason to hesitate, because the code can only be delivered to the editor's registered HTTPS callback. But that callback is on the editor's origin, so the script can read the code there and redeem it with the client ID and a verifier it generated itself. It leaves with a complete set of tokens of its own, however carefully the editor hid the existing ones.
The editor's own tokens
- Refresh token
- Inside a web worker
- Injected script
- Cannot simply copy it out
Isolation helps
Injected script in editor.example
- 1. Hidden frame
- Authorization request for photo-editor
- 2. Photo service
- Answers without showing Ava anything
- 3. Callback
- On the editor origin: the script reads the code
- 4. Token request
- A verifier it generated itself
A complete set of tokens of its own
No storage choice stops the most capable version of the attack.
Refreshing without a backend
The editor's access tokens last ten minutes, and a long editing session needs new ones without sending Ava back through the photo service each time.
Browser applications used to manage this with a hidden frame that loaded the authorization endpoint. The photo service recognized Ava's sign-in from its own session cookie and returned a new code without anything appearing on screen. Inside a frame on editor.example, that cookie from auth.photos.example is a third-party cookie. Several browsers now block or partition third-party cookies by default, and people can block them in others. In those browsers the photo service sees a frame with no sign-in, and silent renewal fails. A pop-up window is a top-level page rather than a frame, so it still carries the photo service's cookie, which is why the injected script's own authorization request keeps working there.
Refresh tokens have largely taken over the job, which puts a long-lived credential where injected script can reach it. Current guidance for browser applications therefore sets firm rules for refresh tokens issued to them:
- As for any public client, the authorization server must rotate them or bind them to a key that the browser holds, as the lesson on the refresh token grant described.
- They must stop working after a maximum lifetime, or after a period without use.
- When the first refresh token has a fixed expiry, the replacements issued by rotation must not outlast it.
The photo service gives the editor ten-minute access tokens and refresh tokens that end eight hours after Ava connects. The next morning, the editor sends her through the authorization endpoint again, which is quick if she is still signed in at the photo service. The fixed limit matters for the reason Refresh tokens and rotation gave for capping every family: rotation alone never ends a chain that only a thief is using.
Defending the page
The first defense against injected script is keeping it out: inserting untrusted text such as captions as text rather than markup, and choosing carefully which outside scripts the editor loads. A Content Security Policy adds a second layer. It is a response header that tells the browser which scripts the page may run and which addresses its scripts may contact:
Content-Security-Policy: script-src 'self'; connect-src 'self' https://auth.photos.example https://api.photos.exampleWith this policy, the browser runs script only from the editor's own origin, so a fragment smuggled into the page as markup does nothing, and it stops the page's scripts from sending requests anywhere else, which leaves stolen data fewer ways out. It is defense in depth, not a cure. It cannot help once the attacker's code arrives from an allowed source, and it does not stop that code from using the editor's tokens while the page is open.
Narrow scopes and short access token lifetimes limit the rest. Even so, a browser-only client keeps working tokens in the page. The IETF's best current practice for browser applications, RFC 10017, recommends against this design for business applications, sensitive applications, and applications that handle personal data, and a photo library is personal data. Backends for frontends moves the editor's tokens out of the browser by giving it a small backend of its own, without changing what Ava sees in the page.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Run a public client in a browser page and defend its tokensRun a small local page that signs Ava in with PKCE and calls the photo API, then inspect CORS, compare token storage, cap refresh at eight hours and see the page's CSP refuse an injected script.