No token in JavaScript: cookie sessions and two API clients
Our website keeps every sign-in in a cookie no script can read, and a cookie only counts when the request says which part of the site sent it. This is the lesson that made us build it that way.
Lodge Internet · 14 November 2026 · 5 min read
An admin signs in to our admin console in a browser. Later, in the same browser, someone opens the customer registration page. The page loads the list of hostels so the new customer can pick theirs, as it always does.
For a while, that list came back with every hostel's Wi-Fi password in it.
Nothing on the page showed the passwords. But the browser had received them, on a customer page, because an admin had once signed in there. This post is about why that happened and what we built so it can't happen again.
What went wrong
Back then, the admin's session token sat in the browser's local storage. The website had one API client, and it attached that token to any request whose path was on a list of admin routes.
That list had a subtle member. The hostel list is public to read, but creating and editing hostels is admin-only, and both live on the same route. So the hostel list was on the list. The client attached the admin token whenever it found one, even on a plain read from a customer page. A comment beside the entry called this harmless, because the public read would ignore the token.
On the backend, the hostel list includes each hostel's Wi-Fi password when the caller is a signed-in admin, because the admin hostels screen needs it. The backend did exactly what it was told. The customer page told it the wrong thing.
Two mistakes were stacked here:
- a request's authority came from what the browser happened to hold, not from what the page meant to do;
- the token was in storage any script on the page could read.
Cookies no script can read
Every sign-in, customer and admin, is now a cookie the backend sets. It is HttpOnly, so no JavaScript on the page can read it. It is Secure, so it travels only over HTTPS. It is SameSite=Lax, and it is scoped to the API's routes, so nothing else on that host ever receives it.
The browser sends it with every request to the API. No script in the website ever holds a token. A test pins this down from the backend side: the sign-in response sets the cookie with those attributes and returns no token in its body.
That fixes the second mistake. A script injected into the page can't carry a session away.
A cookie counts only with a label
The first mistake needed something else. A cookie goes with every request, wanted or not, so the backend can't tell from the cookie alone which page sent the request.
So every request from the website carries a header that says which part of the site sent it: the customer pages or the admin pages. The backend reads the customer cookie only on a request labelled as the customer pages, and the admin cookie only on one labelled as the admin pages. From backend/app/core/cookies.py:
def admin_cookie(request: Request) -> str:
"""The admin's session token, if this request came from the admin pages."""
if request.headers.get(CLIENT_HEADER) != ADMIN:
return ""
return request.cookies.get(ADMIN_COOKIE, "")Everything that asks "is this an admin?" goes through that function. That includes the check behind the hostel list, and the one that lets admins past a lockdown. So in an admin's browser, a customer page is still just a customer page. It gets no Wi-Fi passwords, and no way past the maintenance screen.
The label does a second job. A browser can send a cookie on requests our pages never made: SameSite=Lax still allows some cross-site navigations. But a page on another site can't add a custom header to a request to our API without our CORS permission. A form on someone else's site sends the cookie and no label, so the backend treats it as signed out. A test checks both cases: the customer cookie with no label, and the customer cookie labelled as admin, are each refused.
Two clients, on purpose
The website has two API clients in frontend/lib/apiClient.ts, and they never share a code path for the label:
function from(client: "site" | "admin", init?: RequestInit): RequestInit {
const headers = new Headers(init?.headers);
headers.set(CLIENT_HEADER, client);
return { ...init, headers, credentials: "include" };
}apiFetch is the customer client. It always labels its requests as the customer pages. It never acts as an admin, even in a browser where an admin is signed in. A comment at the top of the file says so, and records the hostel password lesson as the reason, so the next person to touch it knows what the rule protects.
adminFetch is the admin client. It labels its requests as the admin pages, and it does one more thing. A 401 from any admin request becomes a single console-wide "session ended" signal, which a small component in the admin console turns into a trip to the login screen, with a reason and the page to come back to. Before, every admin screen handled an expired session on its own, or mostly didn't, so an expired session looked like an admin page where every table was empty. A 403 is left alone: the session is fine, this admin simply isn't allowed to do that.
The customer client has a lighter version. A 401 fires an event that makes the sign-in state check whether the session has really ended, rather than each page guessing.
The bot is different
The bot is a server talking to our backend, not a browser. It holds its own token and sends it as a bearer token. Cookies would add nothing there, and the risks they solve (scripts in a page, other sites riding the session) don't exist.
The trade-off
The custom header makes every API request a non-simple request, so the browser checks with the API before each new address it calls. We let the browser cache that answer, and the customer layout opens the connection to the API early. It still costs a round trip the first time.
The website and the API sit on different subdomains of one domain. The cookie works because they are the same site, and every request is sent with credentials included. Moving the API to an unrelated domain would break this design, and we'd have to rethink it.
HttpOnly stops a script stealing a session. It doesn't stop a script that's already running in our page from making requests while the customer has it open. Keeping our own pages free of injected script still matters.
And the label is a convention the backend enforces, not a secret. It works because the browser won't let another site set it, not because nobody knows its name.
If you are building this
- Put sessions in HttpOnly cookies set by the server, and keep tokens out of anything JavaScript can read.
- Make the client say what it is, and let the server decide what that request may see. Don't let the client decide by attaching whatever credentials it can find.
- Keep the customer client and the admin client separate in code, so neither can borrow the other's authority by accident.
- Write the reason down where the next engineer will read it. Ours is at the top of the file that would have to change.
Last checked against the product on 14 November 2026.