Suyog Acharya
Case study · Message Center · Security

One malicious message could steal an admin's login. I made that impossible.

While learning a six-year-old codebase, I audited it on my own initiative. The most serious risk: admin login tokens sat where any script on the page could read them. I led the redesign to Keycloak single sign-on, built so the browser never holds a token at all.

Role
Found the risk, designed and built the fix
Stack
Keycloak, OAuth 2.0 + PKCE, OpenID Connect, Laravel
Tests
58 unit tests
Rolled out
2 deployments

In short: Message Center's admin panel kept each admin's login token in the browser's storage, where any script on the page can read it. If an attacker ever got a script onto a page an admin opens, they could copy that token and act as the admin.

No incident was ever reported. I found the risk myself and led the fix: login moved to Keycloak, and the tokens now live only on the server. The browser gets a cookie that scripts cannot read, so there is nothing left in the browser to steal.

The audit

When I joined the Message Center work, the codebase was six years old with around fifteen past contributors. Instead of only learning the parts my first tickets touched, I traced how requests, logins and data actually moved through it. I wrote down every risk I found and raised them with the team.

FindingWhy it matteredStatus
Admin login tokens stored in the browser's localStorageAny script on the page could read and steal them, which means full admin takeoverFixed by me Keycloak SSO redesign, this page
Email-log search was slowAbout 3 seconds per lookup across 3.3M records slowed down support workFixed by me 2.96s → 0.30s
An SQL injection risk in the template formForm input could reach a database query unsafelyFound and raised
Environment configuration not handled safelySensitive settings were more exposed than they should beFound and raised

The token risk was the most serious, because one weakness anywhere on the page would lead straight to admin access. So that is where I focused.

The risk in plain words

A login token is like a key card. Whoever holds it is treated as the admin. Storing it in localStorage is like leaving the key card on the desk: any JavaScript running on the page can pick it up.

Message Center shows content that users submit. If a submitted message ever contained a script and that script ran when an admin opened it, it could read the token and send it to an attacker. And because the token was a self-contained JWT, there was no way to cancel it. The attacker would stay logged in as that admin until the token expired on its own.

The real problem

Fixing one input field would close one door. The deeper flaw was that the browser held something worth stealing. I wanted a design where even a successful script injection finds nothing useful.

See the attack, before and after

A malicious script tries to steal the admin's login token

An illustration of the risk. No real attack is shown and no incident took place.

The decision

I moved authentication to the company's Keycloak identity server using the OAuth 2.0 Authorization Code flow with PKCE and OpenID Connect, in the pattern often called "backend for frontend":

  • The browser never sees an access token, ID token or refresh token.
  • The Message Center server holds the tokens, encrypted, and talks to Keycloak directly.
  • The browser gets one cookie that only identifies a server-side session, and scripts cannot read it.

The login flow, step by step

Step through the full flow. Each step shows which party talks to which, and what travels between them.

OAuth 2.0 Authorization Code flow with PKCE between the browser, Message Center and Keycloak

The details that make it hold

PKCE, so a stolen login code is useless

Before redirecting to Keycloak, the server creates a random secret (the code verifier) and sends only its SHA-256 hash (the challenge). When the code comes back, the server proves it knows the original secret. Someone who intercepts the code cannot exchange it. The verifier itself is stored encrypted, and a state value ties the callback to the login that started it.

Tokens are encrypted at rest, and session ids are hashed

The access, refresh and ID tokens are encrypted with a dedicated key before they are stored. The session id in the cookie is stored only as a hash, so even a copy of the database does not contain a usable session.

A cookie scripts cannot touch

The session cookie uses the __Host- prefix, httpOnly, Secure and SameSite=Lax. JavaScript cannot read it, it only travels over HTTPS, it is locked to this exact host, and other sites cannot make the browser send it in background requests. Both places that issue the cookie, login and refresh, build it from one shared function. If they ever disagreed on these flags, the browser would silently drop the cookie and users would be logged out with no visible cause.

No open redirects after login

After login, the user returns to the page they came from. That return path must be a local path. Full URLs like https://evil.example, protocol-relative //evil.example and the backslash variant some browsers rewrite are all rejected.

Refresh without races

When the access token is about to expire, the next request refreshes it. But an admin page fires several requests at once. If two of them refreshed together, Keycloak's refresh-token rotation would reject the second one and log the user out at random. So only one request refreshes, under a short lease lock, and the others wait and reuse its result. If waiting takes too long, the request fails cleanly instead of trying a second refresh.

Instant revocation

When a session ends in Keycloak, Keycloak notifies Message Center through back-channel logout, and every matching server session is revoked immediately. Revoking a session is a database update, which is exactly what a JWT in the browser could not offer.

Why server-side sessions over browser JWTs

JWT held by the browserServer-side session (what I built)
If a script runs on the pageIt can read the token and send it anywhereIt finds nothing it can read
Cancel a stolen loginNot possible until the token expiresOne update, effective immediately
Logout from KeycloakToken stays valid in the browserSession revoked through back-channel logout
CostNo lookup per requestOne session lookup per request, which is cheap and worth it for an admin panel

JWTs are a fine tool when you need stateless verification between services. For a browser session on an admin panel, the ability to revoke matters more than avoiding one lookup.

Results

0tokens reachable by any script in the browser
Instantrevocation of any session, from Keycloak or the server
58 tests · 2unit tests, and deployments using the same design

The design was reused for a second white-label deployment without changes to its core. What started as reading code to learn the system became the authentication layer the admin panel runs on.

What I learned

Understanding a system deeply is the best security tool I have. The most serious risks were not hidden in clever code. They were in ordinary decisions, like where a token is stored, that nobody had questioned in years.