ADR: The one write a stranger can make
- Status
- accepted
- Date
- 2026-09-03
The security page has said there is no rate
limiter since it was written. This is not the limiter it meant, and that is the
point.
In plain words
Creating an account was the only thing a stranger could do that the database kept, and each one costs about 120 ms of CPU to hash the password. Registration is now capped at 120 accounts per sliding hour across the whole site, counted without telling one visitor from another.
What that is worth: a developer sees a limit that works behind the edge because it never needs to know who is asking, and the organization bounds its bill and keeps the container responsive while everything other than registration keeps working.
Context
Ask a narrow question instead of a broad one: what can somebody with no account
cause this application to keep?
GET everything. Reads, and they cost a query.
POST /api/errors/client. Writes into a ring buffer with fifty slots, each
one bounded in size, and the ring is the browser's own so a flood of them
cannot push server errors out of the operator's view. That was fixed in
1.0.0.64 and it is the shape this record copies.
POST /api/auth/register. Writes a row to AspNetUsers, in Azure SQL
Database, and keeps it.
That is the whole list, and only the last one persists. Bids need an account,
and a bid is keyed on the person and the vehicle together, so a signed-in
visitor can hold at most one row per vehicle. Which means the number of rows
anybody can add to this database is a function of the number of accounts, and
the number of accounts had no ceiling at all.
The rows are the smaller half. Identity hashes a password on the way in, on
purpose, at a cost measured here at about 120 ms of CPU. One container serves
this site. A loop against this one endpoint is a hundred and twenty milliseconds
of somebody else's CPU per request, which is a better attack on this site than
anything else it exposes, and it needs no account, no session and no cleverness.
What the security page actually argued
It says:
Behind the edge, the origin sees one address for every visitor, so an
IP-partitioned limit is a global cap rather than a per-attacker one, and
because the origin is directly reachable an attacker can bypass the edge and
forge whatever address they like.
Every word of that is true and none of it is about this. It is an argument
about partitioning: any limit that has to tell one visitor from another is
either lying to itself behind the edge or being lied to in front of it.
A limit that does not partition has nothing to forge. It counts registrations,
full stop, and an attacker with ten thousand addresses gets exactly the same
answer as an attacker with one.
That distinction was available the whole time and was not made, because "there
is no rate limiter" was written once and then read as settled. It is the same
species as the three findings in 1.0.0.61 through 1.0.0.63: a sentence that was
correct about the question it was answering, left standing over a question it
was never asked.
Decision
A sliding hour, counted across the whole site, of 120 registrations.
Sliding rather than a bucket that resets on the hour. A bucket hands the
whole allowance back at a known instant, so an attacker takes it twice in two
minutes by arriving either side of the reset. The window holds the times of the
last accepted registrations and ages them out one at a time.
A refusal is not recorded. Only an accepted registration takes a slot. If
refusals counted, fifty of them in the fifty-ninth minute would hold the window
open for another hour, and a limiter that never lets go while anybody keeps
knocking is the outage it was built to prevent.
Checked after the request is read and before the password is hashed. A
malformed request should not spend the allowance, and a refused one should not
spend the CPU, which is the thing being protected.
The refusal says nothing with a number in it. It says the demo is not taking
new accounts and to try in an hour. A reply that reported how much of the
allowance remained would be a counter anybody could poll, and the test asserts
no digit reaches the words.
120. This site has never seen more than a handful of registrations in a day
and most of those were its own tests, so the ceiling sits three orders of
magnitude above real use. Nobody meets it who is not trying to.
What this costs, said plainly
While an attacker is spending the hour's allowance, a real visitor cannot
register either. That is a denial of service, in the small, and it is the trade
being made on purpose: an hour of no new accounts is cheaper than an unbounded
bill and a saturated container, and everything else keeps working. Browsing,
signing in, bidding, and every existing account are untouched, and one test
exists only to say so.
The honest version of the alternative is worth writing down: with no limit, the
same attacker gets the CPU and the rows and nobody is refused, because there is
nothing to refuse them with.
Alternatives
A total cap on accounts. Simpler, and it bounds the bill forever rather than
per hour. It also converts a temporary refusal into a permanent one: fill it
once and this site never takes another account without a deploy. For a portfolio
whose entire purpose is that a stranger can try it, that is the wrong failure to
choose.
Per-address limiting. The security page explains why this does not work
here, and it still does not.
A CAPTCHA. It would work, and it would put a third party's script on a page
whose whole argument is that a reader can see everything it does.
Nothing, and say so. This was the position until today and it was defensible
while the origin held no data. Accounts and a database arrived underneath it.
What is still owed
The window lives in memory, in one container. A second instance would get its
own, and the site's real ceiling would be 120 times the instance count. That is
correct for what runs today, one container, and it is written here rather than
discovered later. A durable counter belongs with the origin lock, which needs a
paid subscription, and those two are one piece of work.
Consequences
- The only anonymous write this application accepts has a ceiling, and so, by
the key on the bids table, does everything else in the database.
- A visitor who has never seen it will never see it.
- The security page's rate limiter paragraph is now half true, and its addendum
says which half.
The window, read from this build:
public sealed class RegistrationLimit(int perHour, Func<DateTimeOffset> now) : IAllowance
{
public const int DefaultPerHour = 120;
private static readonly TimeSpan Window = TimeSpan.FromHours(1);
private readonly LinkedList<DateTimeOffset> _taken = new();
private readonly Lock _gate = new();
public int PerHour { get; } = perHour;
public bool TryTake()
{
DateTimeOffset moment = now();
lock (_gate)
{
while (_taken.Count > 0 && moment - _taken.First!.Value >= Window)
{
_taken.RemoveFirst();
}
if (_taken.Count >= PerHour)
{
return false;
}
_taken.AddLast(moment);
return true;
}
}
public void GiveBack()
{
lock (_gate)
{
if (_taken.Count > 0)
{
_taken.RemoveLast();
}
}
}
}
Live from api/TheYard.Api/Features/Accounts/RegistrationLimit.cs, region registration-limit, read from this build at 99df7a8.
Where it sits
The limit lives in the Api host: RegistrationLimit holds the sliding hour, and the register endpoint in Endpoints/AccountEndpoints.SignIn.cs calls TryTake after reading the request and GiveBack when Identity refuses the account. Single responsibility, one reason to change per class, fits here because the window logic sits in its own class and the endpoint only asks it for a slot. The cost is that a real visitor cannot register while somebody else spends the hour's 120 slots. The window also lives in one process's memory, so running a second instance, or adding the origin lock that a durable counter would need, is when this should move into a shared store.
Addendum, 2026-10-20 (1.0.3.188): the other store is refused, not skipped
Until 1.0.3.187 the stamp check passed a token whose store was not the request's store, on the argument that the endpoints refuse it. After 19 October one process serves both stores, and its own address is one origin for both, so a cookie issued on one store reaches the other with the header that picks it; a session ended on its own store still opened the other one with its old claims. From 1.0.3.188 the check is RefuseSession, handed the request's store and accounts by constructor, and it refuses a token from the other store before any endpoint runs. RefuseSessionTests holds the refusal with fake ports, and AuthTests signs out on one store and sends the dead cookie to both.
Addendum, 2026-09-09: the second write a stranger could make, and the slot that leaked
Two things a review with no context found in this record's own territory,
both fixed in 1.0.0.109. First, the proof (ADR: Same performance, proven)
had a second anonymous write: POST /api/admin/proof started a run that
registered two accounts and placed sixteen bids, and a loop of one start a
minute, which its cooldown allowed, spent exactly this record's 120
registrations an hour, so a stranger could close registration by asking the
site to measure itself. Starting a run takes a signed-in visitor now, and the
proof's accounts are made once per process and reused, so the loop that
remains costs nothing this record guards. Second, the slot was taken before
Identity had its say: a request the limiter accepted and Identity then
refused, a short password or an address already registered, kept its slot,
so a stranger could spend the hour's allowance with requests that were never
going to make an account. RegistrationLimit.GiveBack returns the slot on
that path, and two tests hold it, one on the window and one through the
endpoint. The rule this record stated stands, and is true again: one write a
stranger can make, and only the ones that happen are counted.
Addendum, 2026-10-18: the list was not the whole list
The list above stopped being the whole list as the Admin tab grew, and a review on 2026-10-18 found three more things a stranger could cause. Two were routes: GET /api/admin/selftest/exception, which throws on purpose and lands in the server's error ring and the kept log on every call, so a loop of them pushed the operator's real errors off the page; and POST /api/admin/pages, which starts a sweep that dials every served address on the loopback. The third was a bid with no ceiling: int.MaxValue was accepted and the next minimum wrapped to a negative number on the wire.
From 1.0.3.169 both routes need a signed-in session, like the proof's start, and the self-test runs once a minute per account. A bid above BidRules.MaxBid, ten million dollars, is refused before any sum is made, and arithmetic in Domain is checked, so a price that would wrap throws instead. The operator's key is read from the X-Admin-Key header and never from the address, because an address is written into the request logs of App Service, Front Door and Application Insights. And a session can now be ended: each token carries a fingerprint of the account's security stamp, every request checks it against the account, and both a password change and a sign-out move the stamp, so a copied cookie stops working the moment its owner signs out or resets.
What a stranger can cause is the list above again: reads, the browser's error ring, and an account.
public sealed class RefuseSession(CurrentBackend current, RequestAccounts accounts)
{
public async Task<string?> HandleAsync(ClaimsPrincipal? principal, CancellationToken cancellation)
{
string? id = principal?.FindFirstValue(ClaimTypes.NameIdentifier);
string? store = principal?.FindFirstValue(TokenIssuer.StoreClaim);
string? stamp = principal?.FindFirstValue(TokenIssuer.StampClaim);
if (id is null)
{
return "the session names no account";
}
if (!string.Equals(store, current.Backend.Key, StringComparison.Ordinal))
{
return "the session belongs to the other store";
}
if (accounts.Users is not { } users)
{
return null;
}
cancellation.ThrowIfCancellationRequested();
var user = await users.FindByIdAsync(id);
if (user is null)
{
return "the account no longer exists";
}
return stamp is not null && string.Equals(stamp, TokenIssuer.Fingerprint(user.SecurityStamp), StringComparison.Ordinal)
? null
: "the session was ended by a sign-out or a password change";
}
}
public sealed class SessionEvents(RefuseSession refuse) : JwtBearerEvents
{
public override Task MessageReceived(MessageReceivedContext context)
{
if (context.Request.Cookies.TryGetValue(TokenIssuer.CookieName, out string? cookie))
{
context.Token = cookie;
}
return Task.CompletedTask;
}
public override async Task TokenValidated(TokenValidatedContext context)
{
if (await refuse.HandleAsync(context.Principal, context.HttpContext.RequestAborted) is { } reason)
{
context.Fail(reason);
}
}
}
Live from api/TheYard.Api/Features/Accounts/RefuseSession.cs, region session-stamp, read from this build at 99df7a8.
Enforced by
Not machine-checked. Held by review.