package fichastest import ( "context" "io" "log/slog" "os" "path/filepath" "strings" "testing" fichas "amadan.net/oficina/fichas/internal/fichas" "amadan.net/rastrillo/rastrillo/db" ) // canaries are plaintexts the app seals. None may appear in any file it // writes. // // The first version of this list had seven entries and excluded "Customer" // with a comment claiming the opaque key `t_customer` "legitimately // contains it". **That was simply false** — strings.Contains is // case-sensitive and `t_customer` does not contain `Customer` — so the // exclusion weakened the gate for no reason and taught the next reader that // weakening it was normal. It is back. var canaries = []string{ // Card type and field labels, sealed by Seed. "Customer", "Call log", "Name", "Email", "Phone", "Stage", "Next action", "Notes", "When", "What was said", "Duration", // Sealed enum options. "Lead", "Contacted", "Proposal", "Won", "Lost", // Values written through the store below. "Cnoc Teoranta", "ana@example.com", "renewal is due in March", "talked about the renewal", // The change payload's own JSON keys, which are sealed with it. `"before"`, `"after"`, } // scan reads every file in dir and returns the (file, canary) pairs found. // 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.Fatalf("reading %s: %v", dir, err) } var hits []string read := 0 for _, e := range entries { if e.IsDir() { continue } raw, err := os.ReadFile(filepath.Join(dir, e.Name())) if err != nil { t.Fatalf("reading %s: %v", e.Name(), err) } read++ for _, c := range canaries { if strings.Contains(string(raw), c) { hits = append(hits, e.Name()+": "+c) } } } if read == 0 { t.Fatal("no files were read: this scan would pass vacuously") } return hits } // TestPlaintextNeverReachesTheDatabase is the gate this app exists to pass. // memoria has one of the same shape; Sheets has the schema-side twin. // // It boots the real app, **writes through the real store path**, then reads // every byte on disk — database AND write-ahead log, because a value can // sit in the WAL for a long time before it is checkpointed, and a gate that // greps only the .db passes while the plaintext is on disk beside it. // // The earlier version only booted and seeded, so it never saw a single // value a person typed — in the app whose entire premise is what happens to // those. func TestPlaintextNeverReachesTheDatabase(t *testing.T) { dir := t.TempDir() logger := slog.New(slog.NewTextHandler(io.Discard, nil)) d, err := db.Open(filepath.Join(dir, "fichas.db"), logger) if err != nil { t.Fatalf("db.Open: %v", err) } if _, err := fichas.AppForTest(d, testConfig(t), logger, TestOwner); err != nil { t.Fatalf("App: %v", err) } s := testSealer(t) pr, err := s.Print("f_member", []byte("ana@example.com")) if err != nil { t.Fatalf("print: %v", err) } if err := d.G.Create(&fichas.Member{AddressPrint: pr}).Error; err != nil { t.Fatalf("creating a member: %v", err) } card, err := fichas.CreateCard(context.Background(), d.G, s, fichas.TypeCustomer, fichas.Person(1), map[string]string{ fichas.FieldName: "Cnoc Teoranta", fichas.FieldEmail: "ana@example.com", fichas.FieldStage: "Lead", }) if err != nil { t.Fatalf("CreateCard: %v", err) } if err := fichas.UpdateCard(context.Background(), d.G, s, card.ID, fichas.Person(1), map[string]string{fichas.FieldNotes: "renewal is due in March"}); err != nil { t.Fatalf("UpdateCard: %v", err) } if _, err := fichas.AddEntry(context.Background(), d.G, s, card.ID, fichas.Person(1), fichas.TypeCallLog, "manual", map[string]string{fichas.FieldBody: "talked about the renewal"}); err != nil { t.Fatalf("AddEntry: %v", err) } // Close so the WAL is checkpointed and every byte is on disk to read. if err := d.Close(); err != nil { t.Fatalf("Close: %v", err) } for _, hit := range scan(t, dir, canaries) { t.Errorf("plaintext on disk — %s", hit) } } // The gate is only worth something if it can fail, and it must be THIS // gate that fails, not a hand-rolled grep standing in for it. So: plant // every canary in a file in a directory and assert scan finds them all. func TestTheGateCanFail(t *testing.T) { dir := t.TempDir() planted := "header " + strings.Join(canaries, " ") + " footer" if err := os.WriteFile(filepath.Join(dir, "planted.db"), []byte(planted), 0o600); err != nil { t.Fatalf("WriteFile: %v", err) } if hits := scan(t, dir, canaries); len(hits) != len(canaries) { t.Fatalf("scan found %d of %d planted canaries; the gate does not look for what it claims", len(hits), len(canaries)) } }