package mapatest import ( "context" "fmt" "slices" "strings" "testing" "amadan.net/oficina/mapa/internal/mapa" ) // canaries are plaintexts the app seals. None may appear in any file it // writes: every title, alias, date, body, link, pending link and ^id of // §11 gate 4, and the member columns. writeEverything writes each one // through the real store. var canaries = []string{ // A member's address and time zone setting. Ana, "America/Argentina/Ushuaia", // A team page's title, its old title (an alias), and its body. "Quillfeather", "Oldquill", "renewal hinges on the grant", // A link, a pending link, a block link and its ^id marker. "[[Quillfeather", "Unwrittenpage", "^blockzq7", // A daily note's date and text, and a scratch page's text. "2031-07-19", "a private thought about wages", "scratchpad musing", // A shared block, now a team page of its own. "Sharedtitle", "the part worth sharing", // Drafts: one on a page, one for an unwritten day, and one kept by // a conflict. "an unsaved draft about budgets", "a day not yet written", "conflicting words kept", // A team page's icon (columns and icons spec, LOOK1): content, // sealed in the record. "🪼", } // writeEverything drives every write the app has, through the real // store, with text that holds every canary. func writeEverything(t *testing.T, store *mapa.Store, index *mapa.Index) { t.Helper() ctx := context.Background() must := func(err error) { t.Helper() if err != nil { t.Fatal(err) } } must(store.SetTimeZone(ctx, Ana, "America/Argentina/Ushuaia")) page, err := store.CreateTeamPage(ctx, Ana, "Oldquill", "The renewal hinges on the grant. ^blockzq7") must(err) must(store.Rename(ctx, Ana, page.ID, "Quillfeather")) _, err = store.SetLook(ctx, Ana, page.ID, mapa.LookChange{SetIcon: true, Icon: &mapa.Icon{Emoji: "🪼"}}) must(err) _, err = store.CreateTeamPage(ctx, Bo, "Linker", "See "+ "[[Quillfeather#^blockzq7]] and [[Unwrittenpage]].") must(err) daily, err := store.SaveDaily(ctx, Ana, "2031-07-19", 0, "a private thought about wages\n\nthe part worth sharing") must(err) _, err = store.CreateScratch(ctx, Ana, "scratchpad musing") must(err) _, err = store.MakeTeamPage(ctx, Ana, daily.ID, daily.Revision, []int{1}, "Sharedtitle") must(err) writeLater(t, store, index) } // writeLater is the writes step 5 added: drafts, and the draft a // conflict keeps. func writeLater(t *testing.T, store *mapa.Store, index *mapa.Index) { t.Helper() ctx := context.Background() pages, err := index.TeamPages(ctx) if err != nil || len(pages) == 0 { t.Fatalf("team pages: %v", err) } page := pages[0] _, err = store.SaveDraft(ctx, Bo, mapa.DraftKey{PageID: page.ID}, page.Revision, 0, "an unsaved draft about budgets") if err != nil { t.Fatal(err) } _, err = store.SaveDraft(ctx, Bo, mapa.DraftKey{Date: "2031-07-20"}, 0, 0, "a day not yet written") if err != nil { t.Fatal(err) } if _, err := store.Commit(ctx, Ana, page.ID, page.Revision, "changed by Ana"); err != nil { t.Fatal(err) } outcome, err := store.Commit(ctx, Ana, page.ID, page.Revision, "conflicting words kept") if err != nil || outcome != mapa.Conflicted { t.Fatalf("the conflict did not keep a draft: %v, %v", outcome, err) } } // signedIn are the addresses the gate signs in with, for real, and the // named exception to the file scan (review M4): rastrillo keeps a // signed-in person's address in the clear in its own auth_links and // auth_sessions rows, in every sibling. Spec §4 says so. Mapa's own // tables must still never hold one, which the table scan below checks, // and any other plaintext anywhere still fails the gate. var signedIn = []string{Ana, Bo} // TestNoPlaintextReachesTheDisk is gate 4's file half. It signs Ana and // Bo in through the real flow, writes everything, then reads every byte // on disk, the WAL included: a value can sit in the WAL for a long time, // and a gate that greps only the .db passes beside it. func TestNoPlaintextReachesTheDisk(t *testing.T) { dir := t.TempDir() app := wiredApp(t, dir, Ana+","+Bo) for _, address := range signedIn { signIn(t, app, address) } writeEverything(t, app.built.Store, app.built.Index) inMapasTables(t, app) if err := app.close(); err != nil { t.Fatal(err) } for _, hit := range scan(t, dir, withoutExceptions()) { t.Errorf("plaintext on disk: %s", hit) } for _, address := range signedIn { if len(scan(t, dir, []string{address})) == 0 { t.Errorf("%s is not on disk at all: rastrillo may seal it now; "+ "drop the exception and the §4 sentence", address) } } } // withoutExceptions is the canaries the file scan looks for. func withoutExceptions() []string { var out []string for _, canary := range canaries { if !slices.Contains(signedIn, canary) { out = append(out, canary) } } return out } // inMapasTables fails on any canary, the addresses included, in any of // Mapa's own tables: the exception is rastrillo's rows, and only those. func inMapasTables(t *testing.T, app wired) { t.Helper() database := app.database() for _, table := range []string{"members", "pages", "aliases", "title_prints", "daily_prints", "revisions", "drafts", "events"} { rows, err := database.Raw("SELECT * FROM " + table).Rows() if err != nil { t.Fatal(err) } for rows.Next() { columns, _ := rows.Columns() values := make([]any, len(columns)) pointers := make([]any, len(columns)) for at := range values { pointers[at] = &values[at] } rows.Scan(pointers...) for _, canary := range canaries { if strings.Contains(fmt.Sprint(values...), canary) { t.Errorf("%s holds %q in the clear", table, canary) } } } rows.Close() } }