Thirty candidates in one classroom share one IP address, so our rate limiter counts the link before the client
Most rate limiting advice assumes one address is one person. CogniPrep Assess is sat by candidates, and a university careers session or a recruitment day puts thirty of them behind one address, all opening the same kind
Most rate limiting advice assumes one address is one person. CogniPrep Assess is sat by candidates, and a university careers session or a recruitment day puts thirty of them behind one address, all opening the same kind of link at the same minute, all reloading after each task. A limiter tuned for the open internet would lock out the room.
The candidate-facing pages, the invitation guide and the sample, are the ones this shaped. Here is how the limiter is arranged.
A preset for every place a request can come from
Instead of one limit and a lot of exceptions, there is a table of named presets, each with a count and a window, and each route says which preset it uses. The presets are grouped by who is asking: signed-in employers, the customer API, sign-in and recovery, candidate entry points, task play, public forms, background jobs, and the live call.
A preset's name is part of its Redis key, and all of them sit under an rl:assess: prefix because the Redis instance is shared with another product. A bucket shared between products is the kind of bug you only find when one of them is under load, so nothing is shared.
Two limits for opening a link
A candidate's link is the credential, so guessing links has to be slow. But the people opening links might be in one room. The answer is to count twice.
const link = createHash('sha256').update(token).digest('hex').slice(0, 16);
const [perLink, perClient] = await Promise.all([
rateLimit(`${client}:${link}`, 'inviteOpen'),
rateLimit(client, 'inviteOpenShared'),
]);
return perLink.success ? perClient : perLink;
The first limit is per client and link, so one link cannot be hammered. The second is per client alone, and is about ten times looser, which leaves room for a group at one site. A raw candidate token is never used as a key. It is hashed first, so a Redis dump or a log of keys cannot be used to open anyone's assessment.
Task play follows the same thinking. There is an outer guard for the whole site, a looser one for task play because of rooms of candidates, and a per-link limit underneath that is sized to how often a task legitimately sends a turn.
Unknown links get the tightest limit of all
If a link does not exist, the request is a guess, and a guess is the thing we most want to slow down. A separate preset applies to unknown links, much tighter than the rest, and it is applied only after the lookup fails. A real candidate never touches it, and someone enumerating tokens runs into it almost at once.
Fail open or fail closed, chosen per preset
Redis can be down. Each preset says what should happen then:
if (config.failClosed) return { success: false, ... }; // refuse
return { success: true, ... }; // allow
Most presets fail open. A candidate in the middle of a task must not be stopped because a cache is unreachable, and an employer's work is not worth interrupting for it either. The few that fail closed are the ones where letting everything through would hurt more than refusing: unknown links, unknown API keys, public forms, sign-ups, and anything that sends an email to a person. A Redis outage should not become a way to send our support inbox ten thousand messages.
With no Redis configured at all, for example on a laptop, an in-memory counter takes over, and sweeps itself every few minutes so it does not grow.
Which address is the client?
The most common mistake in this area is trusting X-Forwarded-For. The first entry in it is whatever the client wrote. The code prefers x-real-ip, which the platform sets and the client cannot spoof. If it has to use x-forwarded-for, it uses the last entry, the one added by the infrastructure we trust, and never the first.
When there is no trustworthy address at all, requests are not lumped into one shared bucket, which would let one anonymous client exhaust it for every other. They are split by a coarse fingerprint of the user agent, language and platform, hashed, with a single fallback name for requests that carry nothing.
What I would carry to another project
Write the presets down in one place, and name them for who is asking rather than for the route. Decide fail-open or fail-closed once per preset, on purpose, and write the reason beside it. And before you set a limit for a human flow, ask where those humans are physically sitting.
If a candidate does hit a limit, or anything else looks wrong, the help contact page is where they can say so, and it is itself behind one of these presets.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.