Aditya Dutt Pandey

Near miss · early tunyt

Published · 3 min read

The bug that scared me most never reached a single user

In tunyt's early days, during our own testing, an event dashboard showed one host another host's data. The application code was fine. The bug was a regex in an nginx cache rule, in a layer that didn't exist on my laptop. How I looked in every wrong place first, and how I think about caching now.

The bug that scared me most never reached a user. It was the very start of tunyt. We were testing internally, someone opened an event dashboard, and it showed another host's data.

We caught it ourselves. For a few seconds that didn't help, because all I could think was how many other places this was happening, and for how long.

Caching at the edge

Not long before, I'd been working on caching, and part of it was in nginx, in front of the app.

The idea was sound. A public event page looks the same for everyone. If nginx answers it from memory, the request never touches the app or the database. During a ticket sale, when everyone is refreshing the same page, that's the cheapest response you can serve.

So I wrote a rule to cache event pages.

I went to the code

When private data shows up in the wrong place, every instinct says the application is leaking it. So I went all the way down: how the dashboard loads its data, how every query is scoped to the logged-in host, how sessions work. All of it was correct.

I went back to the browser and watched the network calls again and again. One thing kept bugging me: the dashboard response was too fast for a page that should be doing real work for one specific host.

I ran it locally with debuggers, and the right host saw the right data every time. There was no nginx on my machine.

The click

Around the tenth pass it clicked: I'd done caching on nginx too.

I opened the config. The regex I'd written for public event pages also matched the dashboard pages, so both were cached as public. Something like this, simplified and not our real config:

# Written to cache public event pages.
# Also matched /events/<id>/dashboard, which is different for every host.
location ~* ^/events/ {
    proxy_cache events;
    proxy_cache_valid 200 5m;
    add_header Cache-Control "public, max-age=300";
    proxy_pass http://app;
}

Why it leaks

By default nginx treats two requests as the same if they have the same scheme, host and URL. That's the cache key. Your cookies aren't in it, and neither is who you're logged in as.

So two hosts opening the same dashboard URL look identical to the cache. Whoever loads it first fills it, and the next person gets their copy. The app never sees the second request, which is why it was so fast.

Cache-Control: public makes it worse. It tells every cache along the way, not only nginx, that the response is safe to store and share.

The fix and the rule

The immediate fix was narrowing the rule to the public pages it was written for.

The rule I kept: anything behind a login never goes into a shared cache. Never. A personalised response says private or no-store, and if a cache rule could ever match it, the rule is wrong.

It's absolute because the risk is lopsided. Get a public page's caching wrong and it's a bit slower. Get a private page's caching wrong and you've handed one customer's data to another.

What I took from it

A response that's too fast is a clue. Real work takes time, so when a personalised page comes back instantly, something in front of the app answered for it.

When local and production disagree, look at what's different between them first. The bug lived in a layer that didn't exist on my laptop. I could have debugged the app for a week and found nothing.

A caching rule is a security rule. It decides who sees a response, and here one regex decided whether a private page was public. It deserves the same review as an auth check. I'd stopped thinking of nginx as part of the system because I didn't write it every day, and config is code without tests.

If I could add one thing to every proxy config I've written, it would be a header saying whether the cache answered. One X-Cache-Status: HIT in the network tab would have pointed me at nginx in seconds.

By , CTO of tunyt

← All writing