On November 16th we merged PR #248: a 61-line Next.js API route that sits between the Memory Lab dashboard and the observability backend. It checks a cookie, forwards four specific paths, and adds a bearer secret. This is the story of why browser clients can't hold secrets, how we built a proxy that only forwards what it should, and how TDD caught the logging gap that would have made production debugging impossible.
The Problem: Secrets Don't Belong in Browsers
Memory Lab was adding bearer authentication to its observability endpoints—the ones that power the dashboard's health panel, alert feed, brain metrics, and lab experiment view. The requirement makes sense: you don't want traffic analysis metrics or cost breakdowns visible to anyone who finds the URL.
But the dashboard runs in the browser. If we embedded MEMORY_LAB_SECRET in the client bundle, anyone with DevTools could extract it. If we fetched it at runtime, same problem—it would appear in network headers.
The solution: a server-side proxy. The browser calls a same-origin route that already checks our gate cookie (the one that protects the entire dashboard). That route forwards the request to Memory Lab with the secret added server-side, where environment variables are safe.
The Four Paths We Actually Need
The proxy doesn't forward everything. It allowlists exactly four paths:
/observability/health — system-wide metrics, RED indicators, cost breakdown
/observability/alerts — P0/P1/P2 alert feed
/observability/lab/active — currently running optimization experiment
/observability/brain/{id} — per-brain metrics for Sabine, Strug Works, Feedtumi, LodeStar
Everything else gets a 404. The upstreamPath function validates the shape before we ever call fetch. Brain IDs get a regex check: 1–64 characters, [A-Za-z0-9_-] only. Query strings are dropped—nothing from the visitor's request gets forwarded except the allowlisted path.
What TDD Audit Caught: The Silent Failure
Here's the bug that wouldn't have shipped without TDD discipline: a stale or rotated MEMORY_LAB_SECRET makes Memory Lab answer 401. The first version of the proxy passed that status through to the browser with no server-side record. If the dashboard went blank in production, the operator would see nothing in Vercel logs.
The audit test for this is marked BUG-CATCHING (meaning it fails on the unaudited code):
it.each([401, 500, 404])('logs an upstream %i on the server without the secret', ...)
The fix: if (!upstream.ok) console.error('[memory-lab-proxy] upstream responded', upstream.status, target). We log the status and path (never the secret or body) so runtime logs show it. console.error is captured at every production log level, so no log-capture override is needed.
What Changed on the Client
Before: memory-lab-client.ts called process.env.NEXT_PUBLIC_MEMORY_LAB_URL directly (a public environment variable safe to embed in the browser bundle).
After: all four fetch helpers (fetchSystemHealth, fetchBrainMetrics, fetchAlerts, fetchLabExperiment) call /api/memory-lab with the same path suffix. The browser never sees the upstream URL or the secret.
What's Next
The proxy is live. Memory Lab's authentication rollout can proceed without exposing secrets to browser clients.
Three things still on the list:
Alert on upstream 401 patterns. We log them now, but a monitoring rule that fires when we see sustained 401s (indicating a rotated secret) would let us fix it before the dashboard goes dark.
Cache observability reads with revalidation. Right now every dashboard page load hits Memory Lab. A 10-second stale-while-revalidate would reduce upstream load without making the UI feel stale.
Real observability data. The client still falls back to mock data when Memory Lab isn't deployed. That's fine for local dev, but production should fail loudly if the backend is unreachable instead of showing fake metrics.
The pattern is clear: when browser clients need authenticated backend data, put a server route in front that validates the user and injects the secret. The gate cookie does user auth. The allowlist does path validation. The secret stays server-side. And TDD catches the logging gaps that would otherwise make production incidents unsolvable.