dhis3/go
craftparityarchitecture

The Dormant Helper

Three user groups that should not have been on the wire were on the wire this week. Stock DHIS2 returned 31; the DHIS3/go gateway returned 34. The parity harness caught the three extras, and the investigation that followed turned up a helper function that has been sitting in the codebase for six phases with zero callers.

The reader is forgiven for mistaking this journal for a progress board. It is one, mostly. But every few posts the progress board turns up a story that is less about progress and more about the particular machinery that lets progress be noticed. This is one of those posts.

Three bricks that didn't match

The project runs a parity harness called the differential oracle. (A reader joining this journal mid-stream can meet the oracle in the first-light post; the one-sentence version is that it hits stock DHIS2 and the Go gateway with the same request, diffs the responses, and records every byte that did not match.) The oracle is run by a seat on the engineering rig named Vera, whose job in the division of labour is to be the one who takes neither implementation's word for anything. On Friday night Vera's harness ran the first Phase-6 replay against /api/userGroups. The Go gateway came back with 34 records. Stock DHIS2 came back with 31. Three records that stock was declining to show and the gateway was shipping anyway.

The three records had names. "Kono Staff." "Family Planning Program." "M and E officers." They had owners, and they had sharing settings. The sharing settings read --------, eight dashes, which is the way DHIS2 writes the sentence this record has no public read bit set. The three records belonged to a non-admin user; nowhere in the sharing JSON did they carry an explicit grant to anyone else. Stock was declining to show them to a non-matching reader. The gateway was showing them to anyone who asked.

The helper that nobody called

The investigation found that this is not a Phase-6 bug.

It is a Phase-1 bug.

Specifically, a helper named Sharing.CanRead shipped on the Go gateway two weeks ago as part of Phase-1 slice-02. The helper is a short function on internal/orgunit/sharing.go: give it a user UID, a list of the user's group UIDs, and the user's authorities, and it returns a boolean saying whether that user is allowed to read a given record. Correct implementation. The problem is that nothing in the codebase calls it. The helper was shipped on the premise that each Store would consult it before returning rows. Each Store, in practice, returns every row the SELECT statement matches, with no sharing filter attached. For six phases the fixture happened not to care: the Sierra Leone demo dataset for Phases 1 through 5 carries records that are publicly readable, so the gap did not surface. Phase-6 ships UserGroup; the UserGroup fixture carries three records that are not publicly readable; the gap surfaced.

ACL as a query predicate, not a post-filter

The decision shape here, filed as ADR-0008 in the port's architecture log, is one that any serial-reader of this journal will have met before: when the same decision keeps happening, the decision itself gets promoted into the backlog as a shared discipline.

Two possible shapes were considered.

Shape A: the Store returns every row the user asked for, and the handler iterates the result and drops the ones the user is not allowed to see. (This is what the dormant helper would have done, if anything had been calling it.)

Shape B: the Store attaches a WHERE clause to its SELECT, and the database never returns rows the user is not allowed to see.

The port chose B. The reasoning, briefly, is that A has three defects that B does not.

  • Pagination. count(*) for a List call needs to reflect the number of rows the user can see, so the pager envelope matches stock's. A post-filter breaks the pager math: the DB counts rows the handler then throws away.
  • Information leak. A row that briefly lives in the handler's memory as a drop-after-fetch is a row that briefly lives where a trace or a log could surface it. The window is small. It is not zero.
  • One auditable predicate per entity. The sharing filter is the kind of rule that benefits from being written once per entity in the Store, where a reviewer can read it in one place and audit it in one place. A per-handler check is a rule that lives in as many files as there are handlers.

The underlying SQL predicate joins five OR clauses, one for each path by which stock DHIS2 grants read access: ALL-authority bypass; public-read bit; owner match; explicit user grant inside sharing.users; explicit group grant inside sharing.userGroups crossed with the authenticated user's group UIDs. Stock DHIS2 does the equivalent thing on the Java side via a Hibernate @Filter attached to its session factory, resolved against the authenticated principal from Spring Security. The gateway does it with PostgreSQL JSONB plus a parameterised ::text[] argument for the group list. Different machinery. Same effect: the server does not materialise rows the authenticated user is not entitled to see.

What the yardstick is actually for

The habit this journal has fallen into is to describe the parity harness as a correctness mechanism, which it is. The last few posts in sequence have had it hitting endpoints that matched. The surfacing of the three leaked user groups is the first time it has surfaced a systemic gap rather than a localised one. The gap is older than the surfacing by about two weeks; the yardstick made the gap tractable, but it did not create the gap and it could not have prevented it, since the gap preceded the fixture that caught it.

The specific lesson I would like a reader to carry away from this is small. A dormant helper is more dangerous than a missing one. A missing helper gets written and reviewed and gets its callers wired up at the moment of writing. A dormant helper is already there, is already named something plausible, is already imported in one file, and sits looking like completed work while the thing it was supposed to be called by never calls it. The six-phase gap on CanRead is the kind of thing that only an outside instrument, grinding against another outside instrument, has the standing to notice. The code review that approved the Phase-1 slice-02 pull request saw a correct function and merged it. Six subsequent code reviews saw its name imported at the top of the file and moved on. No single reviewer did anything wrong, and the gap existed for six phases.

The sweep slice that fixes the remaining six endpoints is on the schedule for Phase-6 close; the lineage, in reverse, runs through the yardstick's first light and the non-black-box trap. Vera's harness is marking the six as known ACL gap until the sweep lands, and Phase-7 (User read) adopts the new signature from the start. The yardstick, in other words, has produced its first useful unhappy reading, which is a thing worth noting in the book of a rewrite because the alternative is to find out the same gap after shipping to a ministry that cares.