All writing

Model-checking a credential lifecycle with TLA+

This is a follow-up to the post about model-checking the reading app’s concurrency with TLA+, which has the short intro to TLA+ and TLC and the way the models are set up, so I won’t repeat that here. One habit from that post was to give every property a bug-* config that switches on one defect and must fail, and the model in this post showed a very plain way for that check to pass while the property checks almost nothing.

TLDR: a ghost variable that a property reads has to be set on every step that could breach the property, not only in the bug’s own branch, and its oracle shouldn’t just repeat the guard next to it, otherwise the bug-* config fails by construction and a defect outside the bug’s own branch goes unseen. And a step copied from one backend’s code can hide a bug in another backend that does it differently.

What the model is about

The app can read from self-hosted servers like Komga (comics) and Audiobookshelf (audiobooks). For each saved server it keeps a credential, a session token it gets by logging in with that credential, and a renewal that logs in again when the server starts answering 401. All of that lives behind one owner per saved catalog, and each owner is bound to one revision of the credential (in the model, bind[o] is that revision, and storeRev is the revision the store currently holds). The tricky part is editing the credential while reads are in flight, since an edit runs under a per-catalog lane (a queue that lets one writer at a time touch that catalog’s stored data), marks the owner as committing, and reads that land during the commit are held until it settles, after which they’re either delivered (the edit kept the binding) or failed (the edit replaced it). That part of the code had taken five separate fixes over about five weeks, for things like a request repeating with a token a 401 had already rejected, or a held response being thrown away when the edit didn’t change anything, which is the kind of history that makes a model worth the afternoon.

The model follows the same layout as the earlier ones, seven properties this time, with base configs that must pass and seven bug-* configs covering six of them (the failure-cause property has two, and the property that only one commit decides a catalog at a time has none). Four of those configs replay the code before four of the fixes, and the other three re-introduce a defect the code doesn’t have. On the first run every config gave the expected verdict, and the model found a real defect on the way, where a same-origin redirect on one read fails that read outright if another read sharing the token has just seen a 401, instead of renewing and trying again the way the normal path does.

The ghost flags

A lot of TLA+ properties are about history (“no token was ever sent after it was rejected”), and the usual way to check one is a ghost variable, a variable that exists only in the model and records whether the bad thing happened, so the invariant just says it’s still FALSE. Here are two of them, as they were in the first version. The first is the sync time, which the app records after a successful read and which should never be written for a credential an edit has replaced:

SyncWrite(q) ==
    /\ running /\ cpc = "idle"
    /\ synced' = [synced EXCEPT ![q] = TRUE]
    /\ LET writes == exists /\ (BugSyncIgnoresRevision \/ storeRev = bind[own[q]])
       IN badSync' = (badSync \/ (writes /\ storeRev # bind[own[q]]))
    ...

SyncMarksOwnBinding == ~badSync

Without the bug constant, the write only happens when storeRev = bind[own[q]], and the ghost is set only when the write happens and storeRev # bind[own[q]], so in the correct model the oracle is the negation of the step’s own guard, and the bug-* config fails only because the bug constant widens that guard. It checks the step against itself, reading storeRev and bind exactly the way the guard does, so if those two were already wrong coming in (an edit that rebinds the catalog to a new owner but forgets to move the store’s revision, say), the guard would admit a write from the replaced owner and the ghost would stay FALSE. The second one was even simpler, in the step where a response lands:

CASE adm[o] = "open" ->
        /\ pc' = [pc EXCEPT ![q] = "delivered"]
        /\ UNCHANGED badDeliver
  [] adm[o] = "committing" /\ BugDeliverDuringCommit ->
        /\ pc' = [pc EXCEPT ![q] = "delivered"]
        /\ badDeliver' = TRUE
  ...

DeliveredOnlyWhenOpen == ~badDeliver

Here badDeliver is set to TRUE in exactly one place, the bug arm, so the property is literally “the bug constant was never on”. Both bug-* configs failed as expected and both base configs passed, but if a later change let a result reach its caller during a commit through some other step (AfterCommit, which releases held responses, say), the model would have kept passing, since that step never touched the ghost. This is exactly the case the earlier post’s mutant rule is for (a property should fail under a mutant of each behaviour it guards, not only under its own bug-* config), and a mutant of AfterCommit would have caught it. It got caught in review before that, as it happens, by reviewer agents reading the model, one of which noticed the sync oracle repeating its guard.

For delivery, the fix is to set the ghost in one place that every delivering step goes through, so the bug arm doesn’t do anything special and a new or changed arm that delivers to a committing owner trips it too. Every arm that hands a result to the caller now goes through one helper, which records whether the read’s owner was open at that moment:

Deliveries(q) ==
    /\ pc' = [pc EXCEPT ![q] = "delivered"]
    /\ badDeliver' = (badDeliver \/ adm[own[q]] # "open")
    /\ UNCHANGED cause

For the sync write, we need the oracle to read something other than the two variables the guard reads, so there’s a new ghost replaced, the set of credential revisions that a committed edit has replaced, which Settle adds to when an edit rebinds the catalog:

Settle(k, unknown) ==
    ...
    /\ replaced' = IF k = "rebound" THEN replaced \cup {storeRev} ELSE replaced

SyncWrite(q) ==
    ...
    /\ LET writes == exists /\ (BugSyncIgnoresRevision \/ storeRev = bind[own[q]])
       IN badSync' = (badSync \/ (writes /\ bind[own[q]] \in replaced))

On correct code this still can’t fail, since revisions only go up and a write under the current binding can’t be under a replaced one, which is what we want from a property that passes. What it adds is that the oracle reads the history Settle keeps of replaced revisions, so the edit that rebinds the catalog without moving the store’s revision, which the old oracle couldn’t see, now fails it. The renewal’s failure path had the same problem in a smaller form (its bug arm wrote the failure cause directly instead of using the rule every other failed read uses), and it got the same treatment.

A quick check for any model: for each ghost, find every assignment that can make it TRUE. If they all sit under a Bug* guard, the property only fires on the bug switch, and if the oracle reads the same variables as the guard next to it, it only checks that step against itself. Then break the behaviour outside the bug arm (another step that delivers, or a step that feeds the guard bad state) and watch the property fail.

A step copied from one backend

The other thing the review caught was that the renewal install was modelled after Komga’s code only. Komga installs the new token with a call that waits through a commit, so a renewal that lands while an edit is saving just waits, and that’s what the model did for both servers. Audiobookshelf does it in two steps, first waiting through any commit the same way and then publishing the new refresh token under the catalog’s lane through a check that refuses an owner that is still committing.

The gap between those two steps matters in only one case, a commit whose outcome the store can’t report yet (it wrote, but can’t say whether the write landed). The commit releases the lane while the owner is still marked as committing, and the owner stays that way until a cleanup pass works out what happened. I added a constant to pick which backend the model runs, and a RenewPublish step that takes the lane and refuses a committing owner, the way the Audiobookshelf code does:

RenewPublish(o) ==
    /\ renew[o] = "publishing" /\ cpc = "idle"
    /\ \/ /\ adm[o] = "open"
          /\ Install(o)
       \/ /\ adm[o] = "committing"
          /\ renew' = [renew EXCEPT ![o] = "none"]
          /\ FailWaiters(o)
          /\ UNCHANGED <<tok, rejected, nextTok>>
    ...

TLC found the path in about a hundred states, where a read starts the session’s first renewal, the login lands and the publish passes its first admission check while the owner is open, then an edit takes the lane, begins its commit and settles with an unknown outcome (in the trace the edit itself had failed), and only then does the publish get the lane, see a committing owner, and fail every read waiting on the renewal. Nobody’s credential was replaced and the owner was never retired, so this breaks the property that a read fails only when its owner was retired or the server refused the credential twice, and it also throws away the freshly rotated token pair, so the next renewal may have to log in from scratch. The model keeps that path as a config that must keep failing until the code changes.

Summary

A bug-* config failing shows that the ghost can be set, not that a defect anywhere else would set it, and in this model two of seven properties were weaker than their bug configs made them look, one because its ghost was assigned only in the bug arm, so nothing else could set it, and one because its oracle read the same two variables as the guard next to it, so wrong state reaching that guard went unseen. What fixed them was setting the delivery ghost in one helper every delivering step goes through, and having the sync oracle read a history the guard doesn’t, and the thing that would have caught them earlier is the mutant habit from the previous post, a mutant outside the bug arm rather than the bug arm itself. The second bug came from a different gap than the earlier models’ too-big steps, a step that was the right size but copied from one backend, and modelling what each backend’s code does separately was enough to find it.

Email address copied [email protected]