One keyword that could break my live dashboard
A security review of this site found that a single URL could make the server-health page report the server as down. The cause was the word in.
- Security
- TypeScript
- Next.js
- Code review
The live page on this site shows the server's own metrics. It takes one input from the URL, a time range, and the code was careful about it: the range had to be one of three known keys, and the comment above the check said it could only ever produce one of those three.
It could produce others. Adding ?range=constructor to the address made every chart on the page report that the metrics source was unreachable.
Why
The check was written as value in RANGES. In JavaScript, in does not only look at the object's own keys. It walks the prototype chain, so "toString" in RANGES and "constructor" in RANGES are both true.
The check passed, and RANGES["constructor"] returned a function rather than a range. The next guard only asked whether the value was truthy, and a function is. The query was then built with a start time of NaN, the metrics server rejected it, and the page showed every chart as failed.
// before
return value in RANGES ? value : "24h";
// after
const RANGE_KEYS = Object.keys(RANGES);
return RANGE_KEYS.includes(value) ? value : "24h";How bad was it
No data was exposed and nothing could be executed. But the page exists to show that the server is healthy, and anyone could share a link that made it look broken. On a portfolio, that is about as close to defacement as a read-only page gets.
What I took from it
The comment claimed something the code did not do, and I believed the comment. Validation code needs testing with hostile input, not reading. And a guard that checks whether something is truthy is not a check on whether it is the thing you expected.