Skip to content

The «Сетевой город» keep-alive overwrites a credential that a diary read rotated while the ping was in flight #146

Closed

Description

@lumenpearson

What is broken.

The cron keep-alive in server/app/services/diary_keepalive.py works in three steps:

  1. It claims a batch of «Сетевой город» sessions, reads each row's sealed upstream_token and commits.
  2. It pings every session with no transaction open.
  3. It writes the results back.

The module docstring and the _apply docstring both say the write-back is a guarded UPDATE that "matches nothing if the row was signed out or its credential rotated meanwhile", so "a vanished or changed row is skipped, not a lost update". The code only guards the second half. The UPDATE in _apply matches on id and expired_at IS NULL. It never compares the stored credential with the one that was claimed; _Result does not even carry the claimed value.

Every successful ping also sets upstream_token, because _ping always returns connection.credential. For a NetSchool connection that is json.dumps of the session dict, which is never empty. So every ok result writes back the credential as it was at claim time, plus whatever the ping itself picked up. If a diary read stored a newer credential in the meantime, it is overwritten.

Reads do rotate credentials. NetSchoolClient._authed_json takes a new at response header and changed NSSESSIONID/ESRNSec cookies into the session dict. DiaryClient._remember_token then seals the new credential into the row and commits. Its own docstring explains why: "dropping it would replay the old one next call". The provider contract in providers/diary/base.py makes the same promise: "a token the upstream rotates mid-call is kept".

The failure scenario.

  1. A family is signed in to «Сетевой город». Their diary_sessions row holds credential A, with at = X and NSSESSIONID = S1.
  2. The cron tick reaches keep_alive. _claim reads A for that row, stamps keepalive_attempted_at and commits. From here on no lock is held.
  3. While the pings are in flight, the parent opens the diary in the bot or the app. The upstream's answer carries a new at: Y header, or a Set-Cookie with a new NSSESSIONID = S2. DiaryClient._remember_token seals credential B (with Y/S2) into upstream_token and commits. In a batch of 200 pinged 8 at a time against an 18-second deadline, with a 5-second read timeout, this window is roughly 20 seconds for every row in the batch, because _apply runs only after the slowest ping.
  4. The ping for that row succeeds. _apply runs UPDATE diary_sessions SET upstream_token = seal(A'), kept_alive_at = …, upstream_ok_at = … WHERE id = … AND expired_at IS NULL. Here A' is A plus only what the ping itself absorbed. The row is still live, so the UPDATE matches and B is replaced.
  5. The next read unseals A' and sends the old at = X / NSSESSIONID = S1. If the upstream retired them when it issued the new values, _authed_json gets a 401, a redirect or a login page and raises SessionExpired. The row is expired and the parent is told to sign in again, although they did nothing wrong. The keep-alive exists to prevent exactly this. In the milder case the upstream still accepts the old values, but the rotation that the read path, the provider contract and the docstring all promise to keep is silently lost.
  6. server/tests/test_diary_keepalive.py has no test that changes upstream_token between the claim and the apply, so nothing catches this.

Where.

  • server/app/services/diary_keepalive.py:18-22: the module docstring promises a guard for "its credential rotated meanwhile".
  • server/app/services/diary_keepalive.py:163-165: the _apply docstring repeats the promise ("matches on what was read … rotated between the claim and now simply matches nothing").
  • server/app/services/diary_keepalive.py:170-178: the ok branch sets upstream_token under WHERE id = … AND expired_at IS NULL only.
  • server/app/services/diary_keepalive.py:156: _ping always returns connection.credential, so every successful ping carries a credential to write.
  • server/app/services/diary_keepalive.py:66, :123: the claimed credential is captured in _Claim.credential but never compared at write-back. _Result (:70-73) does not carry it.
  • server/app/services/diary_keepalive.py:124: the claim commits before the pings, so no row lock covers the in-flight window.
  • server/app/services/diary.py:473-489, :502-504: DiaryClient._call / _remember_token store a rotated credential after every read.
  • server/app/providers/netschool/client.py:146-156, :233-236: the NetSchool client absorbs rotated cookies and a new at header into the credential.
  • server/app/providers/diary/base.py:48-50: the provider contract promises that a rotated token is kept.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status
      Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions