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

Secure Session Cookies: HttpOnly, Secure and SameSite

On this page
  1. The cookie is the key
  2. HttpOnly and Secure: the two MUSTs
  3. The __Host-Http- prefix
  4. SameSite: Strict or Lax?
  5. The login cookie needs Lax
  6. What goes in the cookie
  7. Check yourself

With a BFF, the browser holds exactly one credential: a cookie containing a random session id. The BFF looks the id up and finds the user and their tokens. Whoever has that cookie *is* the user, so it deserves care.

RFC 10017 lists the attributes a BFF's cookies need. Here's the TaskBoard session cookie, as Fastify sends it with @fastify/cookie:

Code
reply.setCookie('__Host-Http-sid', sessionId, {
  path: '/',
  httpOnly: true,
  secure: true,
  sameSite: 'strict',
});
// Set-Cookie: __Host-Http-sid=<random>; Path=/; HttpOnly; Secure; SameSite=Strict

Let's take each attribute in turn.

HttpOnly and Secure: the two MUSTs

HttpOnly hides the cookie from JavaScript. document.cookie doesn't include it, so a malicious script can't read the session id and send it elsewhere. In the lab, document.cookie was an empty string even while signed in.

Secure makes the browser send the cookie only over HTTPS, so it can't leak over plain HTTP on a hostile network. Current browsers treat http://localhost as a secure context, which is why Secure cookies still work in local development.

RFC 10017 says a BFF MUST set both. There's no good reason to skip either.

The __Host-Http- prefix

Cookie names can carry a prefix that the browser enforces:

  • __Host- requires Secure, Path=/ and *no* Domain attribute. That locks the cookie to the exact host that set it, so a compromised sibling subdomain like blog.taskboard.dev can't set or overwrite it.
  • __Host-Http- adds one more rule: the cookie must also be HttpOnly. That guarantees it was set by a server, never by page JavaScript.

RFC 10017 recommends __Host-Http- for BFF cookies. In a test with Chromium 153, the browser simply dropped cookies that broke the rules:

Code
__Host-Http-good       Secure, HttpOnly, Path=/   -> stored
__Host-Http-nohttponly Secure, Path=/             -> rejected (not HttpOnly)
__Host-nosecure        HttpOnly, Path=/           -> rejected (not Secure)
__Host-withdomain      ... Domain=localhost       -> rejected (Domain set)

Browsers that don't know the newer __Host-Http- rule still see a name starting with __Host- and apply those rules, so it's safe to use everywhere.

There's a trap here. To delete a prefixed cookie you send it again with an expiry in the past, and that Set-Cookie must follow the same rules. In the same test, clearing __Host-ok with just Path=/; Max-Age=0 silently did nothing, and the user stayed signed in. Clearing it with Secure and HttpOnly worked. In Fastify, pass the same options you used to set it:

Code
reply.clearCookie('__Host-Http-sid', { path: '/', httpOnly: true, secure: true, sameSite: 'strict' });

SameSite: Strict or Lax?

SameSite controls whether the browser sends the cookie on requests that come from *other sites*:

  • Strict: never sent on cross-site requests, not even when the user clicks a link to your app from another site.
  • Lax: sent on top-level navigations (clicking a link, a redirect) using GET, but not on cross-site POSTs, iframes or fetch calls.
  • None: always sent. Only for cookies that genuinely need cross-site use.

RFC 10017 recommends Strict. There's a practical catch, and the lab shows it clearly. Sign-in ends with the identity provider, on another site, redirecting the browser back to your app. That navigation started on another site, so a Strict cookie isn't sent with it. Here's what the server saw on the page right after login:

Code
                          SameSite=Strict          SameSite=Lax
after login redirect:     server sees: anonymous   server sees: signed in
after a reload:           server sees: anonymous   server sees: signed in
fresh navigation:         server sees: signed in   server sees: signed in
SPA fetch('/bff/user'):   signed in                signed in

With Strict, the HTML the server rendered after login said "anonymous", while the React app's own fetch call, which is same-site, was signed in. For a single-page app that renders the user from a fetch, Strict works fine. If your BFF renders pages on the server, or users arrive from links in email and chat, Lax avoids a confusing "signed out" first page. You'll back it up with a CSRF defense on Day 8, which you need either way.

Sign-in uses a second, short-lived cookie that ties the outgoing login request to the callback (Day 5). The callback *is* the cross-site redirect from the identity provider, so that cookie must be Lax, or the BFF never sees it and every login fails:

Code
reply.setCookie('__Host-Http-login', txId, { path: '/', httpOnly: true, secure: true, sameSite: 'lax', maxAge: 600 });

Only the session id: 32 random bytes from crypto.randomBytes, encoded as base64url. Never a user id, a role or a token. The id means nothing on its own, and the session data stays on the server (Day 6).

Some frameworks store the whole session *inside* an encrypted cookie instead. RFC 10017 allows that, and says the BFF should encrypt the cookie if it contains access tokens. But tokens make cookies large, approaching browsers' roughly 4 KB per-cookie limit, and you can't revoke a session you don't store. This course uses server-side sessions.

TipDon't set Domain, and keep the app on its own host, like app.taskboard.dev. Sharing cookies across subdomains widens the circle of code that can affect your session.

Check yourself

The browser only stores a cookie with that prefix if it has Secure, Path=/, no Domain attribute, and HttpOnly. That locks it to the exact host that set it, so a sibling subdomain can't overwrite it, and proves it came from a server, never from page script. Chromium 153 dropped every test cookie that broke these rules.

The clearing Set-Cookie doesn't satisfy the prefix rules, because Secure and HttpOnly are missing, so the browser ignores it. Clear it with the same attributes used to set it.

The login ends with a redirect that started on the identity provider's site, so the browser doesn't send a Strict cookie with it. Either render the user from a same-site fetch, or use Lax together with a CSRF defense.

The callback arrives as a navigation that started on the identity provider's site, so the browser doesn't send a Strict cookie with it. The BFF never sees the login transaction id and can't complete the login. The short-lived login cookie must be SameSite=Lax.

The cookie is sent with every request and can be copied if it leaks, so it should mean nothing on its own and be impossible to guess or edit into something useful. A 32-byte random id points to data kept on the server, where you can change or revoke it at any time. A user id or role in the cookie would invite tampering.