Reviewing for leaks
By the end of this lesson you will have your screen's server code checked against the four findings below, three attacks on it predicted and then run, and tests/screen-leaks.test.ts keeping all four fixed on every pull request.
Lens one, on real screener code. Here is a proxy route an assistant produced for "let the page pass screener filters through".
export async function GET(req: NextRequest) {
const incoming = new URL(req.url).searchParams;
const upstream = new URL("https://eodhd.com/api/screener");
for (const [k, v] of incoming.entries()) {
upstream.searchParams.set(k, v);
}
upstream.searchParams.set("api_token", process.env.EODHD_API_KEY!);
const res = await fetch(upstream);
return Response.json(await res.json());
}
It works. It is also three separate leaks. Find them before reading on.
Leak one: the caller sets the token
The loop copies every parameter, including api_token. A caller who sends one has theirs overwritten by the server's on the next line, so that specific ordering saves you here — but reorder those two lines during a refactor and your proxy starts making requests with a stranger's token, or worse, with a token they control.
Build Your First Tool's proxy dropped api_token explicitly. This one relies on line order. Explicit beats lucky:
if (k.toLowerCase() === "api_token") continue;
Leak two: the caller sets the limit
Nothing clamps limit. limit=500 was verified to return 500 rows in one request, and a loop of those requests spends your daily allowance in minutes. Your quota is a resource a stranger can consume through a public URL, which makes this a denial-of-service against yourself.
Leak three, and this is the one people miss
The response is forwarded verbatim: Response.json(await res.json()).
Today that is screener rows and harmless. But the same pattern applied to /user forwards the account's email, payment method and invite token — which is precisely the mistake Build Your First Tool warned about, arriving here in a different costume. A proxy that reflects upstream responses unexamined is a proxy that will one day reflect something private.
Return what the page needs, not what the upstream said. For a successful answer that means rows cut down to the fields your columns use. An error body is the one exception, and the starter's proxy makes it on purpose: the body of a 403 is the only place that says which feed your plan is missing, so it passes through, with your key cut out of it.
And a fourth thing that is not a leak but will bite
process.env.EODHD_API_KEY! — the non-null assertion. With the variable unset, the request goes out with api_token=undefined, the API rejects it, and your page shows an empty screen. The person debugging it has no idea the key is missing, because nothing said so. Build Your First Tool threw a named error for exactly this.
What the review comment looks like
Three things. The parameter loop copies
api_token, so this only works because the server'ssethappens to run afterwards — drop it explicitly instead of relying on line order.limitis unclamped and a stranger can spend my quota through the public URL, so clamp it here rather than in the page. And the response is forwarded verbatim; return only the fields the table needs, because this pattern applied to another endpoint forwards an email address. Separately, the!on the env var turns a missing key into a blank page rather than a message.
Four sentences, each naming a consequence. That is the standard from lesson 1 of this unit, applied.
Try it now
Open your own screener proxy — for the screen page, the server code that builds its request — and check it against these four.
The prompt
Mock mode on, plan mode on, and paste:
Check how the screen page builds and answers its screener request
against four leaks: a caller-supplied api_token, an unclamped limit,
an upstream answer passed on whole, and a missing key that becomes a
blank page. For each, say whether our code has it, with the line. Then
write tests/screen-leaks.test.ts in the style of tests/proxy.test.ts:
api_token=fake and limit=99999 in, and assert what would go upstream;
rows keep only the fields our columns use; no key gives a message that
names it. If a test renders the page, mock any client component on it
(such as the saved screens list), which needs browser routing. Show me
which tests fail, then fix only those. No API call. Done means npm test,
typecheck, lint and check:leak green.
A good answer adds tests/screen-leaks.test.ts and changes code only where a test went red first, most often src/lib/screens.ts: rows cut down to the columns' fields, and a key check before any request.
Check it yourself
- The new tests assert on what would go upstream: no caller token, and
limitat 100 when 99999 was asked for. - A test feeds a row with an extra field and finds it gone from what the page receives.
- With the key removed from
.env.localand mock mode off, the screen page names the missing key instead of drawing an empty table. Put the key back. npm testis green, and every new test went red once.
Break it on purpose
Try the attacks from outside: with the dev server running, curl localhost:3000/screen (you deploy it in the last unit) with api_token=fake, with limit=99999, and with the key removed from the environment. Three attacks, three specific behaviours you should be able to predict before you run them.
Compare with ours
// tests/screen-leaks.test.ts
it("builds its own token and limit, whatever the caller sends", () => {
const req = screenRequest(new URLSearchParams(
"f=market_capitalization:gt:1000000000&api_token=fake&limit=99999"));
expect(req.params).not.toHaveProperty("api_token"); // eodhdFetch adds ours
expect(req.params.limit).toBe("100");
});
Ours builds the request from the page's query string. If yours takes parsed filters and a limit instead, the test feeds it the same two hostile values and asserts the same two things. The full state at the end of this unit is on the starter's branch ref/build-a-screener/u3.
Checkpoint
npm test && npm run typecheck
git status # only the files this step touched, no .env*
git add -A
git commit -m "Screen leaks pinned: own token, clamped limit, trimmed rows, named missing key"
Then answer this lesson's questions.