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.
| Finding | Why it mattered | Status |
|---|---|---|
Admin login tokens stored in the browser's localStorage | Any script on the page could read and steal them, which means full admin takeover | Fixed by me Keycloak SSO redesign, this page |
| Email-log search was slow | About 3 seconds per lookup across 3.3M records slowed down support work | Fixed by me 2.96s → 0.30s |
| An SQL injection risk in the template form | Form input could reach a database query unsafely | Found and raised |
| Environment configuration not handled safely | Sensitive settings were more exposed than they should be | Found 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.
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
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.
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 browser | Server-side session (what I built) | |
|---|---|---|
| If a script runs on the page | It can read the token and send it anywhere | It finds nothing it can read |
| Cancel a stolen login | Not possible until the token expires | One update, effective immediately |
| Logout from Keycloak | Token stays valid in the browser | Session revoked through back-channel logout |
| Cost | No lookup per request | One 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
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.