oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1package mapatest
2
3import (
4 "os"
5 "path/filepath"
6 "sort"
7 "strings"
8 "testing"
9)
10
11// Gate 4, the plaintext gate (spec §11): dump the database file, and no
12// title, alias, date, body, link, reference or ^id appears in the clear;
13// and the plaintext columns are exactly §4's list. Fichas' gate, ported,
14// with the schema half reading the real tables rather than the models.
15
16// plaintextColumns is §4's list, column by column, each with the §4 row
17// that allows it. Adding a column to the schema without adding it here
18// fails TestPlaintextColumnsAreExactlySection4; adding one here is a
19// change to the leak list and needs the same review the spec had.
20var plaintextColumns = map[string]string{
21 "members.id": "§4: member id",
22 "members.address_print": "§4: address print",
23
24 "pages.id": "§4: ids",
25 "pages.nonce": "§4: page nonces",
26 "pages.kind": "§4: kind",
27 "pages.rung": "§4: rung",
28 "pages.owner_id": "§4: owner",
29 "pages.revision": "§4: revision",
30 "pages.gone": "§4: ids, kind, rung row (S1 answers gone); build note",
31 "pages.created_at": "§4: created_at",
32 "pages.updated_at": "§4: updated_at",
33
34 "aliases.id": "§4: ids",
35 "aliases.page_id": "§4: ids (the alias's page)",
36
37 "title_prints.print": "§4: title prints",
38 "title_prints.page_id": "§4: title prints (print, page_id)",
39 "daily_prints.print": "§4: daily prints",
40 "daily_prints.page_id": "§4: daily prints (print, page_id)",
41
42 "revisions.page_id": "§4: revision store",
43 "revisions.revision": "§4: revision store",
44 "revisions.at": "§4: revision store",
45 "revisions.actor_id": "§4: revision store",
46
47 "drafts.id": "§4: ids",
48 "drafts.page_id": "§4: drafts",
49 "drafts.daily_print": "§4: drafts",
50 "drafts.person_id": "§4: drafts",
51 "drafts.base_revision": "§4: drafts",
52 "drafts.updated_at": "§4: drafts",
53 "drafts.version": "§4: drafts, the tab check (§5); build note",
54
55 "events.id": "§4: event ids",
56 "events.nonce": "§4: event nonces",
57 "events.at": "§4: events",
58 "events.actor_id": "§4: events",
59 "events.object_kind": "§4: events (object); build note",
60 "events.object_id": "§4: events (object)",
61 "events.verb": "§4: events",
62 "events.revision": "§4: created_at, updated_at, revision row",
63
64 // The images spec's one gate-4 row (images §1.3): serving, scope,
65 // quota and retention without unsealing.
66 "images.id": "images §1.3: the row's own key",
67 "images.public_id": "images §1.3: the URL's id",
68 "images.owner_id": "images §1.3: who uploaded it",
69 "images.page_id": "images §1.3: the page it is attached to",
70 "images.created_at": "images §1.3: the pending window",
71 "images.unreferenced_at": "images §1.3: retention",
72 "images.stored_bytes": "images §1.3: padded size, for the quota",
73 "images.moved_from": "images §1.3: the page a Restore moved it from",
74 "images.object_key": "images §1.3: the ciphertext's hash",
75 "image_trash.object_key": "images §1.3: the delete queue, keys only",
76 "image_trash.queued_at": "images §1.3: the delete queue",
77 // The Notion import's tables, its spec §15 (gate I9).
78 "notion_connections.id": "notion §15: connection",
79 "notion_connections.created_by": "notion §15: connection",
80 "notion_connections.created_at": "notion §15: connection",
81 "notion_connections.verified_at": "notion §15: connection",
82 "notion_runs.id": "notion §15: runs",
83 "notion_runs.actor": "notion §15: runs",
84 "notion_runs.state": "notion §15: runs",
85 "notion_runs.lease": "notion §15: runs (random)",
86 "notion_runs.started_at": "notion §15: runs",
87 "notion_runs.heartbeat_at": "notion §15: runs",
88 "notion_runs.progress_at": "notion §15: runs",
89 "notion_runs.finished_at": "notion §15: runs",
90 "notion_runs.surveyed": "notion §15: runs, the counters",
91 "notion_runs.total": "notion §15: runs, the counters",
92 "notion_runs.written": "notion §15: runs, the counters",
93 "notion_runs.created": "notion §15: runs, the counters",
94 "notion_runs.updated": "notion §15: runs, the counters",
95 "notion_runs.unchanged": "notion §15: runs, the counters",
96 "notion_runs.kept": "notion §15: runs, the counters",
97 "notion_runs.failed": "notion §15: runs, the counters",
98 "notion_runs.error_code": "notion §15: runs",
99 "notion_pages.id": "notion §15: the map",
100 "notion_pages.notion_print": "notion §15: the map (an HMAC)",
101 "notion_pages.state": "notion §15: the map",
102 "notion_pages.error_code": "notion §15: the map",
103 "notion_pages.surveyed_run": "notion §15: the map",
104 "notion_pages.seen_run": "notion §15: the map",
105 "notion_images.id": "notion §15: images",
106 "notion_images.place_print": "notion §15: images (an HMAC)",
107 "notion_images.block_print": "notion §15: images (an HMAC)",
108 "notion_images.state": "notion §15: images",
109}
110
111// sealedColumns hold ciphertext only; the file scan below proves it for
112// what the app writes.
113var sealedColumns = map[string]bool{
114 "members.sealed_address": true,
115 "members.sealed_time_zone": true,
116 "members.sealed_pins": true,
117 "members.sealed_locale": true,
118 "pages.sealed_record": true,
119 "aliases.sealed_title": true,
120 "revisions.sealed_record": true,
121 "drafts.sealed_record": true,
122 "images.sealed_record": true,
123
124 "notion_connections.sealed_token": true,
125 "notion_connections.sealed_workspace": true,
126 "notion_pages.sealed_coordinate": true,
127 "notion_pages.sealed_staged": true,
128 "notion_images.sealed_record": true,
129}
130
131// inheritedTables are rastrillo's own (§4: "rastrillo session and sign-in
132// rows, inherited, as in every sibling"). A new one appearing after a
133// rastrillo upgrade fails the gate, so it is read before it is accepted.
134var inheritedTables = map[string]bool{
135 "rastrillo_migrations": true, "sessions": true, "auth_links": true,
136 "auth_sessions": true,
137}
138
139func schemaColumns(t *testing.T, rig *rig) map[string]string {
140 t.Helper()
141 var tables []string
142 err := rig.db.G.Raw("SELECT name FROM sqlite_master WHERE " +
143 "type = 'table' AND name NOT LIKE 'sqlite_%'").Scan(&tables).Error
144 if err != nil {
145 t.Fatal(err)
146 }
147 columns := map[string]string{}
148 for _, table := range tables {
149 if inheritedTables[table] {
150 continue
151 }
152 var info []struct{ Name, Type string }
153 rig.db.G.Raw("SELECT name, type FROM pragma_table_info(?)",
154 table).Scan(&info)
155 for _, column := range info {
156 columns[table+"."+column.Name] = column.Type
157 }
158 }
159 return columns
160}
161
162func TestPlaintextColumnsAreExactlySection4(t *testing.T) {
163 rig := newRig(t)
164 columns := schemaColumns(t, rig)
165 for name, kind := range columns {
166 _, plain := plaintextColumns[name]
167 switch {
168 case sealedColumns[name] && !strings.EqualFold(kind, "blob"):
169 t.Errorf("%s is listed sealed but is %s", name, kind)
170 case !plain && !sealedColumns[name]:
171 t.Errorf("%s is a column §4 does not list. If it must be "+
172 "plaintext, it needs a §4 row; else seal it", name)
173 }
174 }
175 lists := []map[string]bool{sealedColumns, keys(plaintextColumns)}
176 for _, list := range lists {
177 for name := range list {
178 if _, ok := columns[name]; !ok {
179 t.Errorf("the list names %s, which is not in the schema",
180 name)
181 }
182 }
183 }
184}
185
186func TestOnlyKnownInheritedTablesExist(t *testing.T) {
187 rig := newRig(t)
188 var tables []string
189 rig.db.G.Raw("SELECT name FROM sqlite_master WHERE type = 'table' " +
190 "AND name NOT LIKE 'sqlite_%'").Scan(&tables)
191 ours := map[string]bool{"members": true, "pages": true,
192 "aliases": true, "title_prints": true, "daily_prints": true,
193 "revisions": true, "drafts": true, "events": true,
194 "images": true, "image_trash": true,
195 // The Notion import's (its spec §15), read and listed above.
196 "notion_connections": true, "notion_runs": true,
197 "notion_pages": true, "notion_images": true}
198 for _, table := range tables {
199 if !ours[table] && !inheritedTables[table] {
200 t.Errorf("table %s is neither Mapa's nor a known rastrillo "+
201 "table: read it, then list it", table)
202 }
203 }
204}
205
206func keys(list map[string]string) map[string]bool {
207 out := map[string]bool{}
208 for name := range list {
209 out[name] = true
210 }
211 return out
212}
213
214// scan reads every file in dir (the database, its WAL and shm) and
215// returns each canary it finds. Shared by the gate and by the test that
216// proves the gate can fail.
217func scan(t *testing.T, dir string, canaries []string) []string {
218 t.Helper()
219 entries, err := os.ReadDir(dir)
220 if err != nil {
221 t.Fatal(err)
222 }
223 var hits []string
224 read := 0
225 for _, entry := range entries {
226 if entry.IsDir() {
227 continue
228 }
229 raw, err := os.ReadFile(filepath.Join(dir, entry.Name()))
230 if err != nil {
231 t.Fatal(err)
232 }
233 read++
234 for _, canary := range canaries {
235 if strings.Contains(string(raw), canary) {
236 hits = append(hits, entry.Name()+": "+canary)
237 }
238 }
239 }
240 if read == 0 {
241 t.Fatal("no files were read: the scan would pass vacuously")
242 }
243 sort.Strings(hits)
244 return hits
245}
246
247// TestTheGateCanFail plants every canary and asserts scan finds them all:
248// the gate is only worth something if THIS gate can fail.
249func TestTheGateCanFail(t *testing.T) {
250 dir := t.TempDir()
251 planted := "head " + strings.Join(canaries, " ") + " tail"
252 err := os.WriteFile(filepath.Join(dir, "planted.db"), []byte(planted),
253 0o600)
254 if err != nil {
255 t.Fatal(err)
256 }
257 if hits := scan(t, dir, canaries); len(hits) != len(canaries) {
258 t.Fatalf("scan found %d of %d planted canaries", len(hits),
259 len(canaries))
260 }
261}
262