Skip to content

The join and diary sign-in throttles count before they record, so a burst of parallel requests goes past the limit #163

Closed

Description

@lumenpearson

What is broken. JoinThrottle (server/app/security.py) guards the doors that take a guess:

  • POST /api/v1/join (class codes);
  • POST /api/v1/diary/login, where ten failed sign-ins per address per fifteen minutes is the thing standing between whoever holds a URL and the diary's password check from our address.

It works as check, then act, then record. It counts the caller's recent attempts, lets the request through if the count is under the limit, and records the attempt afterwards. Nothing makes the count and the record one step.

Every request that arrives before the first one has recorded therefore sees the same count, and all of them pass.

The failure scenario (on main).

  1. From one address, send 40 wrong-password requests to /api/v1/diary/login at once, against an upstream that takes 0.2 s to answer.
  2. Every request reads the same count of zero before any has written its row.
  3. Far more than ten reach the upstream: 24 of 40 in a reproduction (assert 24 <= 10).

/join has the same shape, so a burst of guesses at eight-character class codes is limited only by how many requests fit before the first record commits. The limit is only as strong as the attacker's patience with parallelism.

Widened on PR #140's branch. POST /api/v1/diary/session shares /login's bucket, and the anonymous school directory (/api/v1/directory/school-regions) uses the same class. For the directory, the same race would let one address spend the whole anonymous share of DaData's daily allowance.

Fix.

  • Record first, then count. A new JoinThrottle.admit inserts the attempt, commits, then counts including it. It admits while the count is within the limit. Otherwise it deletes its own row and answers with a Retry-After of at least one second.
  • forgive hands back an attempt that should not count: a 403 invite-only /join, a lost invite race, a request where nothing upstream judged the password.
  • All four doors use it. The fix lands on PR Read «Сетевой город» as a second diary, and let a phone in through its own school's diary #140's branch.
  • The test fires 40 concurrent wrong passwords and fails on the old code with assert 24 <= 10.
  • Unverified: the atomicity is proven on SQLite. On PostgreSQL it rests on READ COMMITTED showing each statement every earlier commit, and has not been run against a live database.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status
      Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions