| 1 | // Package sealtest is the plaintext gate: it proves, by reading raw |
| 2 | // bytes, that a database file never holds a sealed value in the clear. |
| 3 | // |
| 4 | // It lives in its own package — not seal, and not a _test.go file — so |
| 5 | // that store and handle tests, which are the ones that actually write |
| 6 | // rows, can import it directly. AssertNoPlaintext takes a *testing.T, so |
| 7 | // this package must never be imported from non-test code: doing so would |
| 8 | // drag the testing package into the production binary. |
| 9 | package sealtest |
| 10 | |
| 11 | import ( |
| 12 | "bytes" |
| 13 | "fmt" |
| 14 | "os" |
| 15 | "path/filepath" |
| 16 | "testing" |
| 17 | ) |
| 18 | |
| 19 | // Canaries are the strings a test seeds into a database before running |
| 20 | // AssertNoPlaintext. Any of them appearing in the raw file bytes is a |
| 21 | // plaintext leak. |
| 22 | var Canaries = [][]byte{ |
| 23 | []byte("CANARY-CELL-VALUE-8f3a"), |
| 24 | []byte("CANARY-SHEET-TITLE-2b71"), |
| 25 | []byte("CANARY-TAB-NAME-c40d"), |
| 26 | } |
| 27 | |
| 28 | // leaked reports the first canary found in b, or nil if none is present. |
| 29 | // Split out from AssertNoPlaintext so the detection logic can be |
| 30 | // unit-tested directly, without needing a live *testing.T failure to |
| 31 | // observe. |
| 32 | func leaked(b []byte) []byte { |
| 33 | for _, c := range Canaries { |
| 34 | if bytes.Contains(b, c) { |
| 35 | return c |
| 36 | } |
| 37 | } |
| 38 | return nil |
| 39 | } |
| 40 | |
| 41 | // findLeak scans dbPath's main file, its WAL and its SHM for a canary. It |
| 42 | // checks the WAL because Litestream ships WAL frames continuously, and a |
| 43 | // value written plainly reaches the bucket before any delete would remove |
| 44 | // it from the main file. A nil canary means no leak was found. |
| 45 | // |
| 46 | // Split out from AssertNoPlaintext so the scan itself — including the |
| 47 | // WAL/SHM handling — can be unit-tested directly, without routing a |
| 48 | // deliberately-failing case through a live *testing.T (a failing subtest |
| 49 | // always fails its parent, which would make this package's own tests |
| 50 | // unable to ever go green). |
| 51 | func findLeak(dbPath string) (canary []byte, file string, err error) { |
| 52 | for _, suffix := range []string{"", "-wal", "-shm"} { |
| 53 | p := dbPath + suffix |
| 54 | b, err := os.ReadFile(p) |
| 55 | if os.IsNotExist(err) { |
| 56 | continue |
| 57 | } |
| 58 | if err != nil { |
| 59 | return nil, "", fmt.Errorf("read %s: %w", filepath.Base(p), err) |
| 60 | } |
| 61 | if c := leaked(b); c != nil { |
| 62 | return c, filepath.Base(p), nil |
| 63 | } |
| 64 | } |
| 65 | return nil, "", nil |
| 66 | } |
| 67 | |
| 68 | // AssertNoPlaintext greps a database file, its WAL and its SHM for |
| 69 | // canaries. Any of them appearing in raw bytes is a plaintext leak. |
| 70 | func AssertNoPlaintext(t *testing.T, dbPath string) { |
| 71 | t.Helper() |
| 72 | c, f, err := findLeak(dbPath) |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | if c != nil { |
| 77 | t.Fatalf("plaintext %q found in %s", c, f) |
| 78 | } |
| 79 | } |
| 80 | |