Chatter3 engineering notes

Note 4 · Cloudflare Workers · Routing

A wider route ate a GDPR export request

“Export my data” answered User not found for every signed‑in user. The handler was there, it worked, it was never reached.

Chatter3 API · the third finding of the AppGallery review

The symptom

The account page has a “Export my data” button, and it is not decoration: under the GDPR a user has the right to a copy of everything the service holds about them. In the shipped build the button returned this, for every account, every time:

{"success":false,"error":"User not found"}

The message is the tell. Nobody who wrote the export route would have written that. It came from a different route entirely — the one that looks up a user by id.

How a path becomes a user id

The API is a single Worker, and its routing is a long list of if statements in one file. Two of them matter here:

// The generic "GET /api/user/" handler.
if (p.startsWith('/api/user/') && req.method === 'GET') {
  const uid = p.split('/').pop();          // "data-export"
  const u = await env.DB.prepare('SELECT * FROM users WHERE id=?').bind(uid).first();
  if (!u) return json({success:false, error:'User not found'});
  ...
}

And, several hundred lines further down:

if (p === '/api/user/data-export' && req.method === 'GET') {
  const auth = await requireAuth(env, req);
  ...
  return json({success:true, exports:{ user, sessions, vocabulary, ratings, achievements }});
}

Both are in the same file. The first one is earlier. The first one matches /api/user/data-export, because startsWith('/api/user/') is true for it, and it takes the last piece of the path — data-export — as a user id. There is no user with that id, so it answers User not found and returns. The export handler below is never executed.

Why this one survived review and shipping

Both handlers were correct. The export was tested once, early, when it was the only route under /api/user/; the generic handler was added later, above it, and no test covered the pair. A route that is correct in isolation can still be wrong because of the routes around it.

The fix: order, and then a second lock

The specific route moved above the generic one. That is the necessary fix. But order alone is a promise that the next person has to remember, so the generic handler was also made to exclude the path explicitly:

// -- GDPR: DATA EXPORT -------------------------------------
// This route has to be ABOVE the generic /api/user/ handler below.
// That handler matches every GET under /api/user/, so while this one sat
// further down the file it never ran: "data-export" was taken for a user
// id, the lookup found nothing, and the export answered
// {"success":false,"error":"User not found"} to every signed-in user.
// The catch-all excludes the path as well, so the two cannot drift apart
// again if this block is ever moved.
if (p === '/api/user/data-export' && req.method === 'GET') {
  const auth = await requireAuth(env, req);
  if (auth instanceof Response) return auth;
  const uid = auth.userId;
  ...
}

// The generic "GET /api/user/" handler. It has to stay BELOW every
// specific route under /api/user/, and it must not swallow them.
if (p.startsWith('/api/user/') && req.method === 'GET'
    && !p.includes('/balances') && p !== '/api/user/data-export') {
  ...
}

Two mechanisms now protect the same thing. If the export block is ever moved down the file again, the catch‑all still refuses to swallow it: it will fall through to nothing, and the test will fail loudly — instead of answering User not found to every user in the world.

The client half

The bug was not only on the server. When the export worked, the web app still showed nothing: it fetched the JSON and handed it to a download that a WebView cannot always perform. The page now shows the exported data in place — the user can read it, copy it, and see that the export worked before the file leaves the screen.

The general rule

A catch‑all route and the specific routes under it are one unit. If you write if (p.startsWith('/api/user/')), you have taken a responsibility for every route that will ever live under /api/user/ — and you have to keep that responsibility in the same edit as the route itself. The exclusion list is not redundant with the order; it is what makes the order survive the next refactor.


← All notes · Next: “your network blocked the call” when the other person never arrived →