Backends for frontends
A backend for frontend gives the photo editor a small server of its own that runs the 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. flow and calls 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. for it. Ava's browser keeps only a session cookieA small value that a website asks the browser to store and send back with later matching requests. Cookies commonly carry a session identifier, so the site can recognize an ongoing sign-in without your password., 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 clientAn OAuth client, typically running on a backend, that can keep a credential such as a client secret or private key protected, and so can authenticate itself to the authorization server., 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 authenticationChecking evidence that someone, or some application, controls an account, such as a password or a passkey. It establishes which account is being used, not what that account may do. and keeps the tokens in a server-side 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. 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: 1The backend finds the session from the cookie, takes the 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. stored with it, and forwards the request:
GET /albums/42/photos HTTP/1.1
Host: api.photos.example
Authorization: Bearer demo-editor-access-token-2When 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.
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 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. as a confidential client and keeps the 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., 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 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 scriptingAn attack in which a site runs script an attacker supplied inside its own pages, so the script acts with the site's privileges. It can read cookies and tokens kept where scripts can reach them and send requests as the signed-in user. 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=StrictSameSite=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 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 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.
Try it in the Lab
- Move the browser client's tokens into a small backend for frontendRun a local backend that is a confidential client, gives the page only an HttpOnly cookie, forwards to one fixed API and demands a custom header, then compare all three designs.