Skip to content
MSO CloudDocumentation

ADR 0033 — org-owned skins + the brand override layer

On this page
  • Status: Accepted (CI configurable-deck programme, wave C, 2026-08-20). Code in tree; migrations 0173/0174 apply at the next deploy.
  • Date: 2026-08-20
  • Owners: Platform (Design system / tenancy).
  • Related: §69 (ONE skin injection path), inv 1 + packages/schema/rls-exceptions.json, §68 (NULLS NOT DISTINCT), §82 (brand partitioning), ADR 0016 (anonymous tenant surfaces).

Context#

site_themes shipped as a GLOBAL catalogue: one flat namespace, every custom skin offered to every tenant by orgs.availableThemes. In an org whose brands are separate CLIENTS (§81) that is a cross-tenant leak inside the picker — one agency's bespoke client palette, with its name, description and full payload, listed to another — and a tenant had nowhere to keep a skin of its own, so a client's colours had to be authored by a super admin as a platform row.

Selecting a skin is also all-or-nothing (brands.theme_slug, migration 0168): making one client's deck a little greener meant authoring a whole second skin, a payload copy that drifts from the theme it was cloned from the next time that theme is retuned.

Decisions#

D1 — site_themes gets an OWNER

Migration 0173: org_id nullable, FK organizations ON DELETE CASCADE. NULL = a PLATFORM skin offered to every org (every existing row); set = that org's own skin, visible only there. CASCADE is the honest lifetime — a skin authored for one tenant has no meaning after the org is deleted.

Uniqueness moves from slug to UNIQUE NULLS NOT DISTINCT (org_id, slug) (§68): without the clause two PLATFORM rows could take one slug and the renderer would have two candidates for one data-skin value; with it, two orgs may each own a skin called acb. Safe on live data by construction — every existing row is NULL-owned with a globally distinct slug.

D2 — Outside RLS, deliberately; the isolation is ONE shared predicate

site_themes gets NO policy, and this ADR is where that is on the record. The renderer reads the table for ANONYMOUS /share/<slug> requests — a white-label deck opened by a client with no session, and therefore no org context to pin. RLS context exists only inside runWithRlsContext (§66), so under FORCE RLS that read returns zero rows and an org's own skin vanishes from the exact surface it was authored for. The table holds a colour palette: no money, no PII, no facts.

Isolation is therefore application-layer, stated once in apps/web/src/server/themes/scope.ts (siteThemeOrgScope = platform rows OR this org's; siteThemeSlugInOrgScope; themeVisibleToOrg; preferOwnOverPlatform, which makes an org's own acb beat a platform acb — a total order, never a coin flip). Its three chokepoints ARE the read surface of the table:

  1. orgs.availableThemes — the picker;
  2. assertSelectableThemeSlug — the org and brand updateTheme writers;
  3. resolveSlug — the renderer, per request.

A slug one chokepoint refuses cannot be accepted by another; a brand still holding a foreign slug resolves to NOTHING and falls through the tier chain exactly as a deleted row does. The exception is registered in packages/schema/rls-exceptions.json with its threat model, compensating controls and reviewBy: 2027-02-15 (enforced by rls-policy-inventory.ts).

D3 — TWO write authorities, one table

Platform rows: superAdminProcedure only, now with an optional cohort selector (absent = the platform cohort, i.e. pre-0173 behaviour). Org rows: the self* cohort in routers/appearance.ts (selfList, selfCreate/Update/DeleteTheme, selfExport/ImportThemes) — owner or org admin, rate-limited, audited org.theme.*, and every one stamps ctx.activeOrgId rather than reading an org id from input. Row addressing uses the EXACT cohort, not the visible scope, so an org admin editing acb can never reach a platform acb; the visible scope is used only for collision checks. An org-owned slug is not addressable as the site-wide default (appearance.activate pins the platform cohort). selfDeleteTheme refuses to orphan a pointer and names the brands still on it. The yng-theme-bundle schema is unchanged: ownership is a property of the deployment a row lives in, not of the palette.

D4 — Two optional token groups so a skin can reach the deck

panels (the five --panel-<role>-{bg,border,ink} tones a delivered deck is read in) and gradients (--gradient-accent / --gradient-accent-soft, as STOPS at the platform's fixed 135deg — never a free-form CSS string). Both follow the proven chrome/heatRamp contract: absent ⇒ no declaration ⇒ byte-identical CSS to the pre-group emitter, inheriting :root. panels is all five tones or none — a skin that could declare danger alone would paint a client-red caution beside a platform-blue note. Until this landed, a client skin could retint every chart and surface while the deck's own summary cards stayed platform blue.

D5 — A brand overrides INK, never STRUCTURE

Migration 0174: brands.theme_overrides jsonb, a brandThemeOverrideSchema document — per mode (light/dark), partial, .strict().

  • MAY carry: accent, text, status, charts, heatRamp, panels, gradients.
  • MAY NOT: brand, surface, sidebar, border, chrome — naming one is a REFUSAL, not a silently ignored key. Radius, elevation, the ground a card sits on and the shell around it are what a reader recognises as one product; a per-brand fork of those is how five brands in one org end up looking like five applications.

An EMPTY override is refused (a row claiming a brand is customised while rendering identically is the state nobody can debug); clearing is null. charts.series replaces whole (half a series is a chart mixing two palettes); a panel tone replaces whole (fill, edge and ink must agree); everything else merges field-level, with an explicitly undefined key treated as absent so a partial can never blank a required token.

brands.updateThemeOverrides (owner/org admin + brand access, audited brand.themeOverrides.update recording GROUP NAMES only) validates the MERGED payload — themePayloadSchema then validateThemePayload (WCAG contrast, the §29 tooltip rule, the injection sweep) — over the base resolved through the brand's real chain. A brand cannot save itself unreadable.

D6 — The merge happens INSIDE the one injection path (§69)

applyBrandThemeOverride runs server-side immediately before renderSkinCss, so the merged payload travels the same themePayloadSchema → renderSkinCss → <style id="yng-skin"> path as any other: no second style tag, no client-side merge. It costs ZERO extra queries — theme_overrides rides in the brand row the request already selects for theme_slug, share scope included. data-skin keeps naming the BASE slug: it identifies the skin, not the tint. The renderer re-parses a stored override defensively and DROPS an incomplete panels/gradients group rather than rendering a half-built one (tokensFor reads a present group unconditionally). The root layout now gates the <style> tag on the PAYLOAD alone: a brand whose ink sits on the default skin keeps data-skin='yng' and still has a payload to render, so a slug test would silently drop its colours. Overrides apply on tenant surfaces only; /admin renders the system skin untouched.

Consequences#

  • A tenant authors and exports its own skins without a super admin; a super admin can still author for a specific org by naming the cohort.
  • Cross-tenant palette exposure in the picker is closed by a predicate, not by RLS — a fourth read of site_themes that skips themes/scope.ts reopens it, which is what theme-single-source.py and the three-chokepoint docstring exist to make visible.
  • Client tone lives in two layers now: theme = structure + everything, brand = ink. "Make this client greener" is an override, never a cloned skin.
  • Built-in skins are untouched; their rendered CSS is pinned by test.

Implementation status (declared 2026-08-20)#

Decision Status Evidence
D1 owner column Landed packages/schema/drizzle/0173_site_theme_org_owner.sql; packages/schema/src/tables/site-themes.ts
D2 RLS exception + scope Landed apps/web/src/server/themes/scope.ts; routers/orgs.ts, themes/selectable.ts, themes/active-theme.ts; rls-exceptions.json
D3 two authorities Landed routers/appearance.ts (self* ×6 + cohort-selecting super-admin procedures); apps/web/src/__tests__/theme-org-ownership.test.ts
D4 panels + gradients Landed packages/ui/src/theme-schema.ts (themePanelsSchema, themeGradientsSchema, emitter); theme-system.test.ts
D5 brand override Landed packages/schema/drizzle/0174_brand_theme_overrides.sql; brandThemeOverrideSchema; brands.updateThemeOverrides
D6 one injection path Landed applyBrandThemeOverride in themes/active-theme.ts + themes/resolve-payload.ts; apps/web/src/app/layout.tsx payload gate
Migrations applied + QA Pending Deploy runs db:migrate + applyRuntimeAcl; rls-policy-inventory runs in CI; browser check of a brand-overridden deck and share