Beta

Create a tenant

A new tenant starts with its own users, OAuth settings, audit history and logs. You are its first Tenant Admin.

Backends for frontends

A backend for frontend gives the photo editor a small server of its own that runs the OAuth flow and calls the photo API for it. Ava's browser keeps only a session cookie, so injected script can no longer leave with a token, and the backend has to defend its cookie-based endpoints against forged requests instead.

A backend that holds the tokens

The editor's team takes that guidance seriously but does not want to rebuild the editor. The interface, the files, and the address stay the same. What changes is underneath: a small server at editor.example that takes over every OAuth responsibility the JavaScript used to carry.

This design is called a backend for frontend, often shortened to BFF: a server-side component that belongs to one frontend application, acts as its OAuth client, and makes API calls for it. From the photo service's point of view, the editor now looks much like the printer's website.

The backend can keep a credential on a server, so it is registered as a confidential client, photo-editor-backend, separate from the browser-only photo-editor. When Ava selects Open from photo account, the page navigates to the backend's /login address. The backend creates the pending attempt and redirects her browser to the photo service, and the callback returns to the backend at https://editor.example/oauth/callback. The backend exchanges the code with its own client authentication and keeps the tokens in a server-side session record.

The browser receives only a session cookie. From then on, the editor's JavaScript calls the backend instead of the photo API. All domains, tokens and keys in these examples are fictional.

GET /api/albums/42/photos HTTP/1.1
Host: editor.example
Cookie: __Host-editor-session=demo-session-5
Editor-Request: 1

The backend finds the session from the cookie, takes the access token stored with it, and forwards the request:

GET /albums/42/photos HTTP/1.1
Host: api.photos.example
Authorization: Bearer demo-editor-access-token-2

When the access token has expired, the backend refreshes it first, authenticating as a confidential client, and the page never notices.

A backend that forwards requests needs firm rules about where they go. The editor's backend maps each of its routes to one fixed destination, such as /api/albums/{id}/photos to https://api.photos.example/albums/{id}/photos, and checks that the value it places in the path is only an album identifier. A backend that forwarded to an address taken from the request would attach Ava's access token to a request for whoever supplied that address.

The editor's backend is the OAuth client. The browser carries the authorization request and the code, then holds only a session cookie. Tokens travel only between the backend, the authorization server, and the photo API.

Three designs side by side

A third design sits between the browser-only editor and this one. A token-mediating backend also runs the authorization code flow as a confidential client and keeps the refresh token, but instead of forwarding API calls, it hands access tokens to the page, which calls the photo API directly. That spares the backend from carrying every request, at the cost of putting access tokens back in the browser.

The editor alone

What reaches the browser
Access and refresh tokens
What injected script can carry away
The existing tokens, and new ones from an authorization it starts itself
What it needs
Only static hosting

Tokens in the page

Backend keeps refresh

What reaches the browser
Access tokens and a session cookie
What injected script can carry away
Access tokens the page holds or asks the backend for
What it needs
A server for sign-in and refresh

Tokens in the page

Backend makes every call

What reaches the browser
A session cookie only
What injected script can carry away
No tokens
What it needs
A server that every API call passes through

No tokens in the page

Moving the tokens does not make cross-site scripting harmless.

The three designs for the photo editor, compared on what reaches Ava's browser, what injected script in the page can carry away, and what each design needs to run.

The costs run the other way. The browser-only editor needs only static hosting. A token-mediating backend adds a server for sign-in and refresh. A backend for frontend adds a server that every API call passes through, which has to be operated and kept secure, and which sees all the data flowing between the editor and the photo API. Current guidance strongly recommends the backend for frontend for business applications, sensitive applications, and applications that handle personal data, and a token-mediating backend only where proxying every call is impractical.

What injected script can still do

Moving the tokens does not make cross-site scripting harmless. Suppose the caption bug from Browser applications is still there, and the attacker's script runs in the editor's page as before. What changes is what it can reach.

An injected script still runs with the page's authority. It can send allowed requests, read their responses and copy returned information. A custom header or CSRF defense cannot distinguish that script from the application's own code. These defenses block requests from another origin; they do not repair script injection on the trusted origin.

What the script can no longer do is leave with a credential. The session cookie is HttpOnly, so the script cannot read it, and there is no token anywhere in the page to copy. Everything it does has to go through the backend, which calls only the API in its own configuration and only for the operations it exposes. When Ava closes the tab, the script stops with it, but anything it already copied stays copied. So the backend's routes and the session's lifetime still decide how much damage one injected script can do.

Protecting the backend's endpoints

The backend now accepts requests authenticated by a cookie, which exposes it to the attack the Correlating requests and responses lesson described at the printer's callback: cross-site request forgery, or CSRF, in which another site causes a browser to send a request that carries its cookies. Cookies were designed to be attached according to where a request is going, not which page caused it. A page on another site could contain a hidden form that posts to https://editor.example/api/albums. If Ava's browser attached the editor's session cookie, the backend would create an album in her photo account.

The editor's backend sets its session cookie like this:

Set-Cookie: __Host-editor-session=demo-session-5; Path=/; Secure; HttpOnly; SameSite=Strict

SameSite=Strict tells the browser to send the cookie only with requests that start from the editor's own site, so the forged form arrives without it. The __Host- prefix, which the printer's website also used, keeps the cookie to editor.example alone. Just as important, it means no other host, including a subdomain such as blog.editor.example, can set a cookie by that name that editor.example would receive. A careless or compromised neighbor cannot plant a session identifier the attacker already knows, the session fixation described in Staying signed in.

Current guidance for this design also recommends a name prefix showing that the server, not a script, set the cookie. The proposed __Host-Http- prefix would do that by adding HttpOnly to the __Host- requirements, and because such a name still begins with __Host-, browsers that do not recognize it still apply the __Host- rules.

One cookie needs different treatment. The callback from the photo service is a navigation that starts on the photo service's site, so a SameSite=Strict cookie would not arrive with it. The short-lived cookie that ties the callback to its pending attempt therefore uses SameSite=Lax, and the backend checks the response with state and PKCE as usual.

SameSite also has a gap. It compares sites, and a site here means the registrable domain, so every subdomain of editor.example counts as the same site. If an attacker took over a forgotten subdomain, its pages could send requests that the browser treats as same-site. The backend closes that gap by requiring a header on every API request: the Editor-Request: 1 line in the page's request to the backend shown earlier. A form cannot add a custom header. A script on another origin that tries to add one triggers a CORS preflight: the browser first asks the backend whether that origin may send such a request, and sends the real request only if the backend approves. The editor's backend approves no other origin, and it rejects any request that arrives without the header.

The editor now has the security properties of the printer's website: a confidential client on a server, tokens that never reach the browser, and a cookie that other sites cannot make Ava's browser send. It keeps what made it a browser application, an interface that runs in the page, and pays for the difference with a server that every API call passes through.

Check your understanding

Try these questions before moving on. If an answer isn't right, use the feedback and try again.

The editor now uses a backend for frontend, and injected script is running in its page. Which of these can the script do? 1 of 3

Choose one answer.

An attacker takes over the forgotten subdomain blog.editor.example, and a script there posts to https://editor.example/api/albums. What stops it from creating an album with Ava's session? 2 of 3

Choose one answer.

A developer proposes that the editor's backend forward each page request to whatever API address the page names, attaching Ava's access token. What is the problem? 3 of 3

Choose one answer.

Try it in the Lab

We value your privacy

We use cookies and similar technologies to enhance your browsing experience, and analytics to understand our traffic. By clicking "Allow all", you consent to optional analytics. Cookie Policy

Learn identity