package mapatest import ( "os" "path/filepath" "sort" "strings" "testing" ) // Gate 4, the plaintext gate (spec §11): dump the database file, and no // title, alias, date, body, link, reference or ^id appears in the clear; // and the plaintext columns are exactly §4's list. Fichas' gate, ported, // with the schema half reading the real tables rather than the models. // plaintextColumns is §4's list, column by column, each with the §4 row // that allows it. Adding a column to the schema without adding it here // fails TestPlaintextColumnsAreExactlySection4; adding one here is a // change to the leak list and needs the same review the spec had. var plaintextColumns = map[string]string{ "members.id": "§4: member id", "members.address_print": "§4: address print", "pages.id": "§4: ids", "pages.nonce": "§4: page nonces", "pages.kind": "§4: kind", "pages.rung": "§4: rung", "pages.owner_id": "§4: owner", "pages.revision": "§4: revision", "pages.gone": "§4: ids, kind, rung row (S1 answers gone); build note", "pages.created_at": "§4: created_at", "pages.updated_at": "§4: updated_at", "aliases.id": "§4: ids", "aliases.page_id": "§4: ids (the alias's page)", "title_prints.print": "§4: title prints", "title_prints.page_id": "§4: title prints (print, page_id)", "daily_prints.print": "§4: daily prints", "daily_prints.page_id": "§4: daily prints (print, page_id)", "revisions.page_id": "§4: revision store", "revisions.revision": "§4: revision store", "revisions.at": "§4: revision store", "revisions.actor_id": "§4: revision store", "drafts.id": "§4: ids", "drafts.page_id": "§4: drafts", "drafts.daily_print": "§4: drafts", "drafts.person_id": "§4: drafts", "drafts.base_revision": "§4: drafts", "drafts.updated_at": "§4: drafts", "drafts.version": "§4: drafts, the tab check (§5); build note", "events.id": "§4: event ids", "events.nonce": "§4: event nonces", "events.at": "§4: events", "events.actor_id": "§4: events", "events.object_kind": "§4: events (object); build note", "events.object_id": "§4: events (object)", "events.verb": "§4: events", "events.revision": "§4: created_at, updated_at, revision row", // The images spec's one gate-4 row (images §1.3): serving, scope, // quota and retention without unsealing. "images.id": "images §1.3: the row's own key", "images.public_id": "images §1.3: the URL's id", "images.owner_id": "images §1.3: who uploaded it", "images.page_id": "images §1.3: the page it is attached to", "images.created_at": "images §1.3: the pending window", "images.unreferenced_at": "images §1.3: retention", "images.stored_bytes": "images §1.3: padded size, for the quota", "images.moved_from": "images §1.3: the page a Restore moved it from", "images.object_key": "images §1.3: the ciphertext's hash", "image_trash.object_key": "images §1.3: the delete queue, keys only", "image_trash.queued_at": "images §1.3: the delete queue", // The Notion import's tables, its spec §15 (gate I9). "notion_connections.id": "notion §15: connection", "notion_connections.created_by": "notion §15: connection", "notion_connections.created_at": "notion §15: connection", "notion_connections.verified_at": "notion §15: connection", "notion_runs.id": "notion §15: runs", "notion_runs.actor": "notion §15: runs", "notion_runs.state": "notion §15: runs", "notion_runs.lease": "notion §15: runs (random)", "notion_runs.started_at": "notion §15: runs", "notion_runs.heartbeat_at": "notion §15: runs", "notion_runs.progress_at": "notion §15: runs", "notion_runs.finished_at": "notion §15: runs", "notion_runs.surveyed": "notion §15: runs, the counters", "notion_runs.total": "notion §15: runs, the counters", "notion_runs.written": "notion §15: runs, the counters", "notion_runs.created": "notion §15: runs, the counters", "notion_runs.updated": "notion §15: runs, the counters", "notion_runs.unchanged": "notion §15: runs, the counters", "notion_runs.kept": "notion §15: runs, the counters", "notion_runs.failed": "notion §15: runs, the counters", "notion_runs.error_code": "notion §15: runs", "notion_pages.id": "notion §15: the map", "notion_pages.notion_print": "notion §15: the map (an HMAC)", "notion_pages.state": "notion §15: the map", "notion_pages.error_code": "notion §15: the map", "notion_pages.surveyed_run": "notion §15: the map", "notion_pages.seen_run": "notion §15: the map", "notion_images.id": "notion §15: images", "notion_images.place_print": "notion §15: images (an HMAC)", "notion_images.block_print": "notion §15: images (an HMAC)", "notion_images.state": "notion §15: images", } // sealedColumns hold ciphertext only; the file scan below proves it for // what the app writes. var sealedColumns = map[string]bool{ "members.sealed_address": true, "members.sealed_time_zone": true, "members.sealed_pins": true, "members.sealed_locale": true, "pages.sealed_record": true, "aliases.sealed_title": true, "revisions.sealed_record": true, "drafts.sealed_record": true, "images.sealed_record": true, "notion_connections.sealed_token": true, "notion_connections.sealed_workspace": true, "notion_pages.sealed_coordinate": true, "notion_pages.sealed_staged": true, "notion_images.sealed_record": true, } // inheritedTables are rastrillo's own (§4: "rastrillo session and sign-in // rows, inherited, as in every sibling"). A new one appearing after a // rastrillo upgrade fails the gate, so it is read before it is accepted. var inheritedTables = map[string]bool{ "rastrillo_migrations": true, "sessions": true, "auth_links": true, "auth_sessions": true, } func schemaColumns(t *testing.T, rig *rig) map[string]string { t.Helper() var tables []string err := rig.db.G.Raw("SELECT name FROM sqlite_master WHERE " + "type = 'table' AND name NOT LIKE 'sqlite_%'").Scan(&tables).Error if err != nil { t.Fatal(err) } columns := map[string]string{} for _, table := range tables { if inheritedTables[table] { continue } var info []struct{ Name, Type string } rig.db.G.Raw("SELECT name, type FROM pragma_table_info(?)", table).Scan(&info) for _, column := range info { columns[table+"."+column.Name] = column.Type } } return columns } func TestPlaintextColumnsAreExactlySection4(t *testing.T) { rig := newRig(t) columns := schemaColumns(t, rig) for name, kind := range columns { _, plain := plaintextColumns[name] switch { case sealedColumns[name] && !strings.EqualFold(kind, "blob"): t.Errorf("%s is listed sealed but is %s", name, kind) case !plain && !sealedColumns[name]: t.Errorf("%s is a column §4 does not list. If it must be "+ "plaintext, it needs a §4 row; else seal it", name) } } lists := []map[string]bool{sealedColumns, keys(plaintextColumns)} for _, list := range lists { for name := range list { if _, ok := columns[name]; !ok { t.Errorf("the list names %s, which is not in the schema", name) } } } } func TestOnlyKnownInheritedTablesExist(t *testing.T) { rig := newRig(t) var tables []string rig.db.G.Raw("SELECT name FROM sqlite_master WHERE type = 'table' " + "AND name NOT LIKE 'sqlite_%'").Scan(&tables) ours := map[string]bool{"members": true, "pages": true, "aliases": true, "title_prints": true, "daily_prints": true, "revisions": true, "drafts": true, "events": true, "images": true, "image_trash": true, // The Notion import's (its spec §15), read and listed above. "notion_connections": true, "notion_runs": true, "notion_pages": true, "notion_images": true} for _, table := range tables { if !ours[table] && !inheritedTables[table] { t.Errorf("table %s is neither Mapa's nor a known rastrillo "+ "table: read it, then list it", table) } } } func keys(list map[string]string) map[string]bool { out := map[string]bool{} for name := range list { out[name] = true } return out } // scan reads every file in dir (the database, its WAL and shm) and // returns each canary it finds. Shared by the gate and by the test that // proves the gate can fail. func scan(t *testing.T, dir string, canaries []string) []string { t.Helper() entries, err := os.ReadDir(dir) if err != nil { t.Fatal(err) } var hits []string read := 0 for _, entry := range entries { if entry.IsDir() { continue } raw, err := os.ReadFile(filepath.Join(dir, entry.Name())) if err != nil { t.Fatal(err) } read++ for _, canary := range canaries { if strings.Contains(string(raw), canary) { hits = append(hits, entry.Name()+": "+canary) } } } if read == 0 { t.Fatal("no files were read: the scan would pass vacuously") } sort.Strings(hits) return hits } // TestTheGateCanFail plants every canary and asserts scan finds them all: // the gate is only worth something if THIS gate can fail. func TestTheGateCanFail(t *testing.T) { dir := t.TempDir() planted := "head " + strings.Join(canaries, " ") + " tail" err := os.WriteFile(filepath.Join(dir, "planted.db"), []byte(planted), 0o600) if err != nil { t.Fatal(err) } if hits := scan(t, dir, canaries); len(hits) != len(canaries) { t.Fatalf("scan found %d of %d planted canaries", len(hits), len(canaries)) } }