Catalog My learning Blog
Backend for Frontend (BFF) Course with Node and Fastify · Phase 2 · Lesson 3 · 5 min read

Why OAuth Tokens Belong on the Server (RFC 10017)

On this page
  1. The single-page app token problem
  2. Why no browser storage is safe enough
  3. What RFC 10017 says
  4. Why the BFF comes out on top
  5. What the BFF doesn't fix
  6. Check yourself

The single-page app token problem

A React app that calls an OAuth-protected API needs an access token, a credential that says "this request is from Ada, allowed to read tasks". The classic single-page app gets it directly from the identity provider and keeps it somewhere in the browser: memory, localStorage or sessionStorage. Then it adds Authorization: Bearer ... to every API call.

That works. The question is what happens when an attacker gets JavaScript running in your page, through cross-site scripting (XSS) or a compromised npm package in your bundle. That code runs with all the privileges of your app.

Why no browser storage is safe enough

People spend a lot of time debating where in the browser to keep tokens. The uncomfortable truth is that it barely matters once malicious code runs in the page:

  • localStorage and sessionStorage: the script reads the token and sends it to the attacker's server. The attacker can then use it from anywhere until it expires, and a stolen refresh token can last much longer.
  • In memory, inside a closure or a web worker: harder to read directly, but the script can still call your API *through* your app's own code, or start a fresh, silent authorization flow in a hidden iframe and receive its own brand-new tokens.

That last attack matters most. RFC 10017 describes how malicious code can run a new authorization code flow in the background, because the user already has a session at the identity provider. No token storage trick prevents that, since the attacker never touches your stored tokens at all.

What RFC 10017 says

In August 2026 the IETF published RFC 10017, "OAuth 2.0 for Browser-Based Applications", also known as BCP 212. It's the best current practice for OAuth in single-page apps, and it describes three architectures "in decreasing order of security":

  1. Backend for Frontend (BFF). A server-side component is the OAuth client. It gets the tokens, keeps them, and forwards API calls with them. The browser only has a session cookie.
  2. Token-mediating backend. A server-side component gets the tokens as a confidential client, but hands the access token to the browser, which calls APIs directly.
  3. Browser-based OAuth client. The browser does everything as a public client and holds all tokens.

The document also lists patterns to avoid entirely, such as the implicit grant and the resource owner password grant.

Why the BFF comes out on top

In the BFF pattern, as the RFC puts it, "since tokens are only available to the BFF, there are no tokens available to extract from the browser". Several properties follow:

  • No tokens to steal. Malicious JavaScript can't send a token to an attacker's server, because there isn't one in the page.
  • A confidential client. The BFF authenticates to the identity provider with its own credentials, like a client secret or a private key. A stolen authorization code is useless without them.
  • Refresh tokens stay server-side, and the BFF can use them without the browser ever seeing them.
  • The session is under your control. You can end it on the server immediately, which you can't do with a token already copied elsewhere.

Here's what that looked like in the TaskBoard lab after signing in through a real OpenID Connect provider:

Code
document.cookie in page:  ""                         (session cookie is HttpOnly)
/bff/user response:       {"authenticated":true,"user":{"sub":"ada","name":"Ada Lovelace",...}}
tasks API saw:            {"sub":"ada","scope":["tasks:read","tasks:write"],"authHeaderPresent":true}
tasks API saw cookie:     null

The page knows who's signed in, the API receives a proper bearer token, and nothing in between is readable by scripts.

What the BFF doesn't fix

Be precise about the limits. If an attacker runs code in your page, they can still make requests *through* the BFF while the user has the page open, because the browser attaches the session cookie for them. The RFC describes this as proxying requests through the user's browser.

What the BFF changes is the *scope* of the damage: the attacker is limited to what your BFF exposes, only while the victim's page is open, from the victim's browser. Stolen tokens would let them act from their own machine, for hours or days.

So the BFF doesn't replace XSS prevention. You still need output encoding, a Content Security Policy and careful dependency management. The BFF limits the blast radius when those fail.

TipWhen someone suggests the token-mediating pattern because it "saves a hop", remember the access token is back in the browser. Choose it consciously, for example when the API is on a different origin and proxying is impossible, and keep tokens short-lived.

Check yourself

Malicious code can still use the app's own code paths, or start a new silent authorization flow and get fresh tokens of its own, without ever reading the stored token.

Backend for Frontend, token-mediating backend, and browser-based OAuth client, which is the order the RFC lists them in decreasing security. Only the BFF keeps every token out of the browser. The token-mediating backend still hands the access token to the browser, and the browser-based client holds all tokens itself.

They can make requests through the BFF from the victim's open page, using it as a proxy. They can't steal tokens to use later from their own machine, because no tokens exist in the browser.

If the user still has a session at the identity provider, the script can start a new authorization code flow in a hidden iframe or popup and receive its own tokens. RFC 10017 describes this attack, and no storage trick prevents it, because the script never touches your stored tokens. Keeping the OAuth client on the server removes the attack.

It can be, as a conscious trade-off. The server still runs the OAuth flow as a confidential client and keeps the refresh token, but the access token reaches the browser, where injected scripts can use it. Keep access tokens short-lived, narrowly scoped and in memory, and invest heavily in XSS prevention.