oficina / fichas Public

Clone
git clone https://amadan.net/oficina/fichas

Plain git — no account needed to clone.

Download

Download this file

1package fichas
2
3import (
4 "reflect"
5 "testing"
6)
7
8// plaintextColumns is the allowlist of string columns permitted to reach
9// the durable file, and **it is this app's leak list**. Adding a column
10// here is a deliberate act with a reason; adding one to a model without
11// editing this map fails the build of the gate below.
12//
13// §4.1 permits plaintext in three cases and no others:
14//
15// 1. the suite contract's feed row, which must be answerable without the
16// key and must pass a plaintext grep unaided;
17// 2. referential integrity and routing;
18// 3. the reach mark, which a sibling evaluates with no asker and no
19// unsealing.
20//
21// Everything below is case 2 or case 3, plus keyed one-way prints, which
22// are not plaintext content: a print is an HMAC under a purpose key and
23// yields nothing to someone holding the file.
24//
25// Sheets and memoria each have a gate of this shape
26// (TestNoPlaintextValueColumns, TestPlaintextNeverReachesTheDatabase).
27// fichas had neither until this test, which meant `Name string` could be
28// added to Card and CI would pass — in the app whose defining property is
29// that it does not do that.
30var plaintextColumns = map[string]string{
31 // Keyed one-way prints. Not content.
32 "Member.AddressPrint": "print: finds a member without holding an address",
33 "CardValue.Print": "print: equality on a TierPrint field",
34 "EntryValue.Print": "print: equality on a TierPrint field",
35 "Ref.Print": "print: dedupe on re-import",
36
37 // Case 2 — routing and referential integrity. Every one of these is
38 // this app's own vocabulary, chosen by us, never the team's words.
39 "CardType.Key": "opaque constant, ours; code selects on it",
40 "FieldDef.Key": "opaque constant, ours; the print purpose derives from it",
41 "FieldDef.Kind": "the value's shape — text, date, enum — our vocabulary",
42 "FieldDef.Tier": "which tier a field's values live in; see the note below",
43 "Entry.Kind": "routing: call, note",
44 "Entry.Source": "routing: manual, calls",
45 "Event.Kind": "routing: created, updated, deleted",
46 "Event.ObjectKind": "routing: card, entry",
47 "Event.ChangedFields": "JSON array of FieldDef ids — opaque, and §5 keeps it permanently",
48 "CardType.FaceKeys": "the card face: a JSON list of this app's opaque field keys, never a value",
49 "CardType.Scope": "card or entry — this app's vocabulary, and what stops an entry taking a card type",
50 "Event.ActorReason": "why the app acted as itself — our words, never a team's; required for an app actor",
51}
52
53// nonStringPlaintext is the same list for columns that are not strings.
54//
55// The first version of this gate walked reflect.String only, which meant
56// `Amount int64` or `Birthday time.Time` — a salary, a date of birth —
57// could be added to a model and pass. Numbers and dates are content as
58// readily as text is; the tier table seals next_action_date for exactly
59// that reason.
60var nonStringPlaintext = map[string]string{
61 "Member.ID": "row id", "Member.CreatedAt": "row timestamp", "Member.UpdatedAt": "row timestamp",
62 "CardType.ID": "row id", "CardType.CreatedAt": "row timestamp", "CardType.UpdatedAt": "row timestamp",
63 "FieldDef.ID": "row id", "FieldDef.CardTypeID": "foreign key", "FieldDef.Ordinal": "display order, ours",
64 "FieldDef.CreatedAt": "row timestamp", "FieldDef.UpdatedAt": "row timestamp",
65 "Card.ID": "row id", "Card.CardTypeID": "foreign key", "Card.OwnerID": "foreign key to members",
66 "Card.Revision": "S1 feed row", "Card.Gone": "S1 feed row", "Card.Offered": "the reach mark",
67 "Card.Nonce": "half of the suite item id (§6.2)", "Event.Nonce": "half of the suite feed cursor (§6.2)",
68 "Card.CreatedAt": "row timestamp", "Card.UpdatedAt": "S1 feed row (touched_at)",
69 "CardValue.CardID": "foreign key", "CardValue.FieldDefID": "foreign key",
70 "Entry.ID": "row id", "Entry.CardID": "foreign key", "Entry.TypeID": "foreign key",
71 "Entry.ActorID": "foreign key to members", "Entry.CreatedAt": "row timestamp",
72 "EntryValue.EntryID": "foreign key", "EntryValue.FieldDefID": "foreign key",
73 "Ref.ID": "row id", "Ref.CardID": "foreign key", "Ref.EntryID": "foreign key", "Ref.CreatedAt": "row timestamp",
74 "Event.ID": "row id", "Event.ObjectID": "foreign key by value; no constraint, the log outlives its object",
75 "Event.ChildID": "the entry a card-scoped event is about", "Event.ActorID": "member id, NULL for the app",
76 "Event.Revision": "the object's revision after this event", "Event.TriggerID": "the event that provoked this one",
77 "Event.PayloadErasedAt": "when the payload was erased; its absence is what §5 forbids being ambiguous",
78 "Event.CreatedAt": "row timestamp",
79}
80
81// FieldDef.Tier is plaintext for a reason worth stating because the design
82// does not give it: this gate, and any reviewer, must be able to read the
83// leak list off the file WITHOUT the key. A sealed tier would mean the only
84// way to audit what is exposed is to hold the DEK, which inverts the point
85// of writing a leak list down.
86
87// TestNoPlaintextColumnEscapesTheAllowlist walks every model and fails on a
88// string column that is not on the list above.
89//
90// []byte columns are not checked: by convention in this app every one of
91// them is sealed, and the file gate in fichastest proves that for the
92// values this app actually writes.
93func TestNoPlaintextColumnEscapesTheAllowlist(t *testing.T) {
94 for _, m := range Models {
95 st := reflect.TypeOf(m).Elem()
96 for i := 0; i < st.NumField(); i++ {
97 f := st.Field(i)
98 ft := f.Type
99 for ft.Kind() == reflect.Pointer {
100 ft = ft.Elem()
101 }
102 // []byte is sealed by this app's convention, and the file
103 // gate proves it for what the app writes. Associations are
104 // not columns.
105 if ft.Kind() == reflect.Slice || (ft.Kind() == reflect.Struct && ft.String() != "time.Time") {
106 continue
107 }
108 name := st.Name() + "." + f.Name
109 list, kind := plaintextColumns, "string"
110 if ft.Kind() != reflect.String {
111 list, kind = nonStringPlaintext, ft.Kind().String()
112 }
113 if _, ok := list[name]; !ok {
114 t.Errorf("%s is a plaintext %s column and is not on the allowlist.\n"+
115 "If it must be plaintext, add it with the §4.1 case it falls under.\n"+
116 "If it holds anything a person typed — a number and a date are content too — seal it.", name, kind)
117 }
118 }
119 }
120}
121
122// The allowlist must not rot: an entry naming a field that no longer exists
123// makes the list stop being a description of the schema.
124func TestAllowlistHasNoStaleEntries(t *testing.T) {
125 live := map[string]bool{}
126 for _, m := range Models {
127 st := reflect.TypeOf(m).Elem()
128 for i := 0; i < st.NumField(); i++ {
129 live[st.Name()+"."+st.Field(i).Name] = true
130 }
131 }
132 for _, list := range []map[string]string{plaintextColumns, nonStringPlaintext} {
133 for name := range list {
134 if !live[name] {
135 t.Errorf("the allowlist names %s, which no longer exists", name)
136 }
137 }
138 }
139}
140