| 1 | package mapatest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "slices" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/mapa/internal/mapa" |
| 11 | ) |
| 12 | |
| 13 | // canaries are plaintexts the app seals. None may appear in any file it |
| 14 | // writes: every title, alias, date, body, link, pending link and ^id of |
| 15 | // §11 gate 4, and the member columns. writeEverything writes each one |
| 16 | // through the real store. |
| 17 | var canaries = []string{ |
| 18 | // A member's address and time zone setting. |
| 19 | Ana, "America/Argentina/Ushuaia", |
| 20 | // A team page's title, its old title (an alias), and its body. |
| 21 | "Quillfeather", "Oldquill", "renewal hinges on the grant", |
| 22 | // A link, a pending link, a block link and its ^id marker. |
| 23 | "[[Quillfeather", "Unwrittenpage", "^blockzq7", |
| 24 | // A daily note's date and text, and a scratch page's text. |
| 25 | "2031-07-19", "a private thought about wages", |
| 26 | "scratchpad musing", |
| 27 | // A shared block, now a team page of its own. |
| 28 | "Sharedtitle", "the part worth sharing", |
| 29 | // Drafts: one on a page, one for an unwritten day, and one kept by |
| 30 | // a conflict. |
| 31 | "an unsaved draft about budgets", "a day not yet written", |
| 32 | "conflicting words kept", |
| 33 | // A team page's icon (columns and icons spec, LOOK1): content, |
| 34 | // sealed in the record. |
| 35 | "🪼", |
| 36 | } |
| 37 | |
| 38 | // writeEverything drives every write the app has, through the real |
| 39 | // store, with text that holds every canary. |
| 40 | func writeEverything(t *testing.T, store *mapa.Store, index *mapa.Index) { |
| 41 | t.Helper() |
| 42 | ctx := context.Background() |
| 43 | must := func(err error) { |
| 44 | t.Helper() |
| 45 | if err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | } |
| 49 | must(store.SetTimeZone(ctx, Ana, "America/Argentina/Ushuaia")) |
| 50 | page, err := store.CreateTeamPage(ctx, Ana, "Oldquill", |
| 51 | "The renewal hinges on the grant. ^blockzq7") |
| 52 | must(err) |
| 53 | must(store.Rename(ctx, Ana, page.ID, "Quillfeather")) |
| 54 | _, err = store.SetLook(ctx, Ana, page.ID, mapa.LookChange{SetIcon: true, |
| 55 | Icon: &mapa.Icon{Emoji: "🪼"}}) |
| 56 | must(err) |
| 57 | _, err = store.CreateTeamPage(ctx, Bo, "Linker", "See "+ |
| 58 | "[[Quillfeather#^blockzq7]] and [[Unwrittenpage]].") |
| 59 | must(err) |
| 60 | daily, err := store.SaveDaily(ctx, Ana, "2031-07-19", 0, |
| 61 | "a private thought about wages\n\nthe part worth sharing") |
| 62 | must(err) |
| 63 | _, err = store.CreateScratch(ctx, Ana, "scratchpad musing") |
| 64 | must(err) |
| 65 | _, err = store.MakeTeamPage(ctx, Ana, daily.ID, daily.Revision, |
| 66 | []int{1}, "Sharedtitle") |
| 67 | must(err) |
| 68 | writeLater(t, store, index) |
| 69 | } |
| 70 | |
| 71 | // writeLater is the writes step 5 added: drafts, and the draft a |
| 72 | // conflict keeps. |
| 73 | func writeLater(t *testing.T, store *mapa.Store, index *mapa.Index) { |
| 74 | t.Helper() |
| 75 | ctx := context.Background() |
| 76 | pages, err := index.TeamPages(ctx) |
| 77 | if err != nil || len(pages) == 0 { |
| 78 | t.Fatalf("team pages: %v", err) |
| 79 | } |
| 80 | page := pages[0] |
| 81 | _, err = store.SaveDraft(ctx, Bo, mapa.DraftKey{PageID: page.ID}, |
| 82 | page.Revision, 0, "an unsaved draft about budgets") |
| 83 | if err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | _, err = store.SaveDraft(ctx, Bo, mapa.DraftKey{Date: "2031-07-20"}, |
| 87 | 0, 0, "a day not yet written") |
| 88 | if err != nil { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | if _, err := store.Commit(ctx, Ana, page.ID, page.Revision, |
| 92 | "changed by Ana"); err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | outcome, err := store.Commit(ctx, Ana, page.ID, page.Revision, |
| 96 | "conflicting words kept") |
| 97 | if err != nil || outcome != mapa.Conflicted { |
| 98 | t.Fatalf("the conflict did not keep a draft: %v, %v", outcome, err) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // signedIn are the addresses the gate signs in with, for real, and the |
| 103 | // named exception to the file scan (review M4): rastrillo keeps a |
| 104 | // signed-in person's address in the clear in its own auth_links and |
| 105 | // auth_sessions rows, in every sibling. Spec §4 says so. Mapa's own |
| 106 | // tables must still never hold one, which the table scan below checks, |
| 107 | // and any other plaintext anywhere still fails the gate. |
| 108 | var signedIn = []string{Ana, Bo} |
| 109 | |
| 110 | // TestNoPlaintextReachesTheDisk is gate 4's file half. It signs Ana and |
| 111 | // Bo in through the real flow, writes everything, then reads every byte |
| 112 | // on disk, the WAL included: a value can sit in the WAL for a long time, |
| 113 | // and a gate that greps only the .db passes beside it. |
| 114 | func TestNoPlaintextReachesTheDisk(t *testing.T) { |
| 115 | dir := t.TempDir() |
| 116 | app := wiredApp(t, dir, Ana+","+Bo) |
| 117 | for _, address := range signedIn { |
| 118 | signIn(t, app, address) |
| 119 | } |
| 120 | writeEverything(t, app.built.Store, app.built.Index) |
| 121 | inMapasTables(t, app) |
| 122 | if err := app.close(); err != nil { |
| 123 | t.Fatal(err) |
| 124 | } |
| 125 | for _, hit := range scan(t, dir, withoutExceptions()) { |
| 126 | t.Errorf("plaintext on disk: %s", hit) |
| 127 | } |
| 128 | for _, address := range signedIn { |
| 129 | if len(scan(t, dir, []string{address})) == 0 { |
| 130 | t.Errorf("%s is not on disk at all: rastrillo may seal it now; "+ |
| 131 | "drop the exception and the §4 sentence", address) |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // withoutExceptions is the canaries the file scan looks for. |
| 137 | func withoutExceptions() []string { |
| 138 | var out []string |
| 139 | for _, canary := range canaries { |
| 140 | if !slices.Contains(signedIn, canary) { |
| 141 | out = append(out, canary) |
| 142 | } |
| 143 | } |
| 144 | return out |
| 145 | } |
| 146 | |
| 147 | // inMapasTables fails on any canary, the addresses included, in any of |
| 148 | // Mapa's own tables: the exception is rastrillo's rows, and only those. |
| 149 | func inMapasTables(t *testing.T, app wired) { |
| 150 | t.Helper() |
| 151 | database := app.database() |
| 152 | for _, table := range []string{"members", "pages", "aliases", |
| 153 | "title_prints", "daily_prints", "revisions", "drafts", "events"} { |
| 154 | rows, err := database.Raw("SELECT * FROM " + table).Rows() |
| 155 | if err != nil { |
| 156 | t.Fatal(err) |
| 157 | } |
| 158 | for rows.Next() { |
| 159 | columns, _ := rows.Columns() |
| 160 | values := make([]any, len(columns)) |
| 161 | pointers := make([]any, len(columns)) |
| 162 | for at := range values { |
| 163 | pointers[at] = &values[at] |
| 164 | } |
| 165 | rows.Scan(pointers...) |
| 166 | for _, canary := range canaries { |
| 167 | if strings.Contains(fmt.Sprint(values...), canary) { |
| 168 | t.Errorf("%s holds %q in the clear", table, canary) |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | rows.Close() |
| 173 | } |
| 174 | } |
| 175 | |