ADR-0017: Targeting reference data is served, and derived from the matcher
- Status: Accepted
- Date: 2026-08-14
- Deciders: Renaissance workstream (001), phase 04 task 4
Context
A display rule narrows a link or a biolink block to an audience. It is a JSONB
blob on links and biolinks_blocks (migration 000013) naming countries,
continents, cities, browsers, operating systems, device types and languages,
each with a show/hide condition, and it is evaluated in exactly one place:
DisplayRulesService.Match (apps/api/internal/services/display_rules.go).
Audit 03 finding 03-D4 recorded that the vocabulary an operator picks from was
compiled into the browser bundle — six literal arrays in
apps/web/src/components/ui/DisplayRulesEditor.tsx, 89 lines of a 242-line
component — "beside a MaxMind GeoIP service and a languages API the same app
already calls". The plan
(.station/jobs/001-renaissance/plans/04-system-integrity.md
task 4) added the sentence that turned out to be the whole finding: they
silently disagree with what GeoIP can actually resolve.
They did, and in three different ways. Counted against the tree:
| Offered | Server's domain | Verdict |
|---|---|---|
| 7 continents | MaxMind continent.code, 7 | agrees |
| 3 device types | parseDeviceType, 3 | agrees |
| 44 countries | MaxMind country.iso_code, ~250 | a hand-picked 18% — most of the world could not be named |
| 20 languages | any ISO 639-1 primary subtag a browser sends, ~190 | likewise |
| 8 browsers | parseBrowserName, 7 | samsung_internet is unmatchable |
| 7 operating systems | parseOSName, 7 | chrome_os is unmatchable |
The last two are the reason this is a defect and not untidiness. Match
compares whole strings after lowercasing both sides
(containsIgnoreCase). No branch of parseBrowserName has ever returned
anything like samsung_internet — a Samsung Internet user-agent contains
chrome and is reported as Chrome. And parseOSName returns "Chrome OS",
which lowercases to chrome os; an underscore is not a space.
Neither failed loudly, and nothing could have made them. No validation runs on
a display rule at write time. At evaluation time an unmatchable entry in a
show list means nobody matched, so the block disappeared for every
visitor, with no error in any log. An operator who targeted ChromeOS users
got the exact opposite of what they asked for, permanently, and the surface
that told them the value existed was the surface that could not know it
didn't.
There was no server-side list to reconcile the browser's copy against. There
still is no countries table, and there should not be one.
Decision
Reference data for display-rule targeting is served by the API, and the browser holds none of it. Where the server can already produce the vocabulary, the served list is derived from the code that produces it rather than declared beside it.
Three parts.
1. The user-agent parser is a table, and the options come out of it. The
three switch statements in internal/handlers/redirect.go moved to
internal/services/useragent.go as ordered uaRule tables. ParseBrowserName
and its two siblings walk the table; targetingOptionsFrom maps the same table
to {value, label} options with strings.ToLower(rule.Value) as the value —
which is precisely the expression Match applies to the visitor's side. "The
picker offers it" and "the matcher can produce it" are now the same statement
rather than two lists that have to be kept in step. A switch cannot be
enumerated, and a vocabulary that cannot be enumerated has to be copied by hand
to be used; that copy is what 03-D4 found.
2. Countries and languages are derived from CLDR, via
golang.org/x/text (already in the module graph), not hand-written. Countries
are the two-letter regions that IsCountry(), canonicalise to themselves, and
have an English CLDR name; languages are the two-letter bases CLDR can name.
The canonicalisation filter is what keeps UK out beside GB and ZR out
beside CD — a deprecated alias is another option MaxMind would never produce.
Eight further codes are excluded by name with a reason each
(notResolvableCountries): CLDR carries EZ (Eurozone), UN, and six
exceptional reservations that ISO 3166-1 does not assign, and MaxMind reports
those territories under their assigned code instead.
3. GET /api/v1/reference/targeting serves all six lists, publicly, keyed
by the display_rules key each populates, so a client never maps between the
two. Public because the answer names no user, no workspace and nothing about
how the install is configured, and is identical for every caller — the same
reasoning that already puts GET /api/v1/languages on the public router. The
scope table records the deliberate absence rather than leaving a reader to
wonder.
The seven continents are the one declared list, and the ADR states why
rather than hiding it: CLDR names continents with UN M.49 numerics, and
MaxMind's two-letter codes collide with ISO 3166-1 — AF is Africa in one
vocabulary and Afghanistan in the other. Any "derivation" would have been a
lookup table wearing a function's clothes. A Go test holds the seven to
MaxMind's set.
Consequences
Easier. An operator can target any country MaxMind resolves and any
language a browser sends, which is most of the point. A new parser branch —
say, a real Samsung Internet rule — becomes targetable by adding one table row,
with no browser change at all. And the class of defect that produced
chrome_os is closed by construction on the server and by
scripts/check-hardcoded-data.mjs in the browser.
Harder. The targeting editor now depends on a network read, so it has three states where it had one. It must not blank when the API is slow and must say so when the API is down — an empty picker reads as "no results", which is exactly how a failed fetch comes to look like a successful one. The editor renders a named failure state rather than an empty list, and a live spec drives it with the request blocked.
Accepted costs, stated rather than discovered later.
- CLDR is not MaxMind. The derivation is filtered to approximate ISO
3166-1, and the approximation is asserted in
targeting_test.go(deprecated aliases absent, canonical codes present,XKpresent because MaxMind does return it) rather than assumed. If a future CLDR release adds a region that is not ISO-assigned, the test will not catch it; the exclusion list is the place to put it, with a reason. - Rules already stored naming
chrome_osorsamsung_internetstay inert. They never worked. Repairing them would mean a migration that makes previously-invisible blocks start hiding themselves — a live behaviour change on data whose author cannot be asked. The picker no longer offers either value,MultiSelectstill renders an unknown stored value as its raw string so it is visible rather than silently dropped, and a save preserves it. It is on the ROADMAP under LATER. - The list is ~14 KB of JSON. It is fetched once per session with an hour of client-side staleness and it is the same bytes for everyone.
Not decided here. The other 60-odd module-level arrays in apps/web/src.
Task 4's own wording is "DisplayRulesEditor's five arrays first … then the
rest of the 73, as each is touched", and the gate is seeded with these six
domains so the rest arrive with their own slices. One pre-existing duplicate
is allowlisted by name with a reason — AdminPushNotificationsPage's
CONTINENT_OPTIONS, byte-identical to the array this slice deleted — and the
gate prints the allowlist size on every green run so it can only go down.
Also not decided here, and recorded because this slice is what surfaced
them. Link-level display_rules are stored and never evaluated: Match has
exactly one caller, PublicBiolinkHandler, so the identical editor on
/links/:id writes rules the redirect path does not read. And the editor's
scheduling half writes scheduled_start while Match reads scheduled_at, so
the start of a schedule window has never been enforced. Both are on the ROADMAP
under LATER: each is a behaviour change on stored data, and folding either into
a slice about reference data would have made this ADR unreviewable.
Alternatives considered
- Serve the language options from
GET /api/v1/languages— rejected, and worth recording because it is the obvious move. That table is the catalogue of admin UI locales (migration000009, legacyAdminLanguages.php) and ships seeded with exactly one row,en. Alanguagesdisplay rule is compared against the primary subtag of the visitor'sAccept-Languageheader. Sourcing one from the other would have collapsed the picker from 20 options to 1 while looking like progress. - Hand-write the ~250 countries and ~190 languages in Go — rejected. It is the same 89-line literal block moved one layer down, with the same drift and a slower feedback loop. The data is already in a dependency, maintained by people whose job it is.
- Store the vocabulary in Postgres with an admin CRUD — rejected. It invents an editable copy of ISO 3166 that an operator can put out of step with what MaxMind returns, which is 03-D4 with a migration attached.
- Keep the arrays and add a parity script comparing them to the Go source —
rejected. It would have caught
chrome_os, and left the 44-country subset standing as "consistent". Consistency with a wrong list is not the goal. - Normalise
chrome_ostochrome osat match time — rejected. It repairs one spelling of one value while leaving the general problem (a picker that can offer anything) untouched, and it silently changes who sees blocks whose rules have never fired. - Put the endpoint behind the token scope gate — rejected. It would have
meant inventing a grantable
referencescope, putting a consent question in front of a list of countries.
Source: docs/decisions/0017-targeting-reference-data-is-served-and-derived-from-the-matcher.md