| 1 | package guards |
| 2 | |
| 3 | import ( |
| 4 | "path/filepath" |
| 5 | "regexp" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | // addressWords are the identifier parts that name an address. |
| 11 | // §10: "people — person ref, blind index, created_at. No address column." |
| 12 | // |
| 13 | // It matches the runtime twin's list in internal/home/db_test.go on |
| 14 | // purpose. An earlier version of the two disagreed — this one had no |
| 15 | // bare "mail", so a column called sender_mail passed the static guard |
| 16 | // and was caught only at runtime — and two guards that disagree about |
| 17 | // what they forbid are one guard plus a surprise. |
| 18 | var addressWords = map[string]bool{ |
| 19 | "address": true, "addresses": true, "addr": true, |
| 20 | "email": true, "emails": true, "mail": true, |
| 21 | "recipient": true, "recipients": true, |
| 22 | } |
| 23 | |
| 24 | // identifier finds the word-shaped runs in a line: letters, digits and |
| 25 | // the separators SQL identifiers use. |
| 26 | var identifier = regexp.MustCompile(`[A-Za-z0-9_-]+`) |
| 27 | |
| 28 | // namesAnAddress reports whether a line of SQL declares a column whose |
| 29 | // name says it holds an address, and which word gave it away. |
| 30 | // |
| 31 | // It works per identifier, not per line, and that is the whole |
| 32 | // correction. The first version matched a regex against the line and |
| 33 | // exempted the ENTIRE line when the word "sealed" appeared anywhere on |
| 34 | // it. Three real leaks walked through, all found by a reviewer running |
| 35 | // them against a migration: |
| 36 | // |
| 37 | // unsealed_address TEXT NOT NULL -- "sealed" is a substring |
| 38 | // sealed_note BLOB, address TEXT NOT NULL -- one line, two columns |
| 39 | // address TEXT NOT NULL DEFAULT 'sealed' -- the word in a default |
| 40 | // |
| 41 | // The exemption is now a whole "sealed" part of one identifier, so it |
| 42 | // covers sealed_address and nothing standing next to it. |
| 43 | // |
| 44 | // Splitting on _ and - also fixes the earlier \b problem: \b counts _ as |
| 45 | // a word character, so \bemail\b never matched owner_email, which is |
| 46 | // exactly the column somebody would add. |
| 47 | func namesAnAddress(line string) (string, bool) { |
| 48 | // Match on the code, not the comment. A migration explaining why it |
| 49 | // has no address column is not an address column, and the first one |
| 50 | // written tripped this guard on its own prose. Nothing hides here: a |
| 51 | // column definition inside a SQL comment is not a column. |
| 52 | code, _, _ := strings.Cut(line, "--") |
| 53 | for _, word := range identifier.FindAllString(code, -1) { |
| 54 | parts := strings.FieldsFunc(strings.ToLower(word), func(r rune) bool { |
| 55 | return r == '_' || r == '-' |
| 56 | }) |
| 57 | if slicesContains(parts, "sealed") { |
| 58 | // A sealed blob is not an address, and says so in its name. |
| 59 | continue |
| 60 | } |
| 61 | for _, p := range parts { |
| 62 | if addressWords[p] { |
| 63 | return word, true |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | return "", false |
| 68 | } |
| 69 | |
| 70 | func slicesContains(parts []string, want string) bool { |
| 71 | for _, p := range parts { |
| 72 | if p == want { |
| 73 | return true |
| 74 | } |
| 75 | } |
| 76 | return false |
| 77 | } |
| 78 | |
| 79 | // TestNoAddressColumn reads every migration and fails on a column whose |
| 80 | // name says it holds an address. This is the cheap half of gate 2; the |
| 81 | // expensive half — a full sign-in leaves no readable address anywhere, |
| 82 | // WAL included — lives with the sign-in it needs. |
| 83 | // |
| 84 | // It is a tripwire, like gate 9: it would pass trivially with no |
| 85 | // migrations, and that is correct. A guard written after the schema it |
| 86 | // guards is a guard written to pass. |
| 87 | func TestNoAddressColumn(t *testing.T) { |
| 88 | root := repoRoot(t) |
| 89 | files := migrationFiles(t, root) |
| 90 | if len(files) == 0 { |
| 91 | t.Fatal("no migrations found: the guard would pass by not looking") |
| 92 | } |
| 93 | for _, path := range files { |
| 94 | rel := relPath(t, root, path) |
| 95 | for i, line := range strings.Split(readFile(t, path), "\n") { |
| 96 | if word, bad := namesAnAddress(line); bad { |
| 97 | t.Errorf("%s:%d: %q names an address column; §10 says there is none", |
| 98 | rel, i+1, word) |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // TestNamesAnAddress proves the matcher fires, and fires on the shapes a |
| 105 | // schema would actually use. A tripwire that passes today is only worth |
| 106 | // having if it would still fire tomorrow. |
| 107 | func TestNamesAnAddress(t *testing.T) { |
| 108 | shouldMatch := []string{ |
| 109 | " address TEXT NOT NULL,", |
| 110 | " email TEXT,", |
| 111 | " e_mail TEXT,", |
| 112 | " mail_to TEXT,", |
| 113 | " recipient TEXT,", |
| 114 | " EMAIL TEXT,", |
| 115 | " owner_email TEXT,", |
| 116 | " sender_mail TEXT,", |
| 117 | " emails TEXT,", |
| 118 | " addr TEXT,", |
| 119 | // The three the line-level exemption used to let through. |
| 120 | " unsealed_address TEXT NOT NULL,", |
| 121 | " sealed_note BLOB, address TEXT NOT NULL,", |
| 122 | " address TEXT NOT NULL DEFAULT 'sealed',", |
| 123 | } |
| 124 | for _, line := range shouldMatch { |
| 125 | if _, bad := namesAnAddress(line); !bad { |
| 126 | t.Errorf("namesAnAddress missed %q", line) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | shouldNotMatch := []string{ |
| 131 | " person_ref TEXT PRIMARY KEY,", |
| 132 | " blind_index TEXT NOT NULL UNIQUE,", |
| 133 | " created_at TEXT NOT NULL", |
| 134 | "CREATE TABLE people (", |
| 135 | " host TEXT NOT NULL,", |
| 136 | // The one real exemption: a sealed blob is not an address, and |
| 137 | // says so in its own name. |
| 138 | " sealed_address BLOB NOT NULL,", |
| 139 | // Prose about addresses is not a column. |
| 140 | "-- there is no address column here and that is the point", |
| 141 | " person_ref TEXT PRIMARY KEY, -- not the email address", |
| 142 | // "mailer" and "emailed" are words, not column names. |
| 143 | " mailer_id TEXT,", |
| 144 | " emailed_at TEXT,", |
| 145 | } |
| 146 | for _, line := range shouldNotMatch { |
| 147 | if word, bad := namesAnAddress(line); bad { |
| 148 | t.Errorf("namesAnAddress wrongly matched %q in %q", word, line) |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Every word this guard forbids must actually be matched by it. They are |
| 154 | // two pieces of one guard, and a word in the list that the matcher never |
| 155 | // sees is a rule nobody enforces. |
| 156 | func TestEveryForbiddenWordIsMatched(t *testing.T) { |
| 157 | for word := range addressWords { |
| 158 | if _, bad := namesAnAddress(" " + word + " TEXT,"); !bad { |
| 159 | t.Errorf("addressWords has %q but namesAnAddress does not match it", word) |
| 160 | } |
| 161 | if _, bad := namesAnAddress(" owner_" + word + " TEXT,"); !bad { |
| 162 | t.Errorf("addressWords has %q but namesAnAddress does not match owner_%s", word, word) |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Gate 2's static half, extended for the operator endpoint |
| 168 | // (team-provisioning-amendment.md §9): the walk must reach the HTTP |
| 169 | // operator migrations on both roles, whose plan and registry tables are the |
| 170 | // new address-adjacent schema (the owner's blind index, consumer origins). |
| 171 | func TestNoAddressColumnReachesOperatorHTTPMigrations(t *testing.T) { |
| 172 | root := repoRoot(t) |
| 173 | seen := map[string]bool{} |
| 174 | for _, path := range migrationFiles(t, root) { |
| 175 | seen[filepath.ToSlash(relPath(t, root, path))] = true |
| 176 | } |
| 177 | for _, want := range []string{ |
| 178 | "internal/operator/http_migrations/credential/migrations/0001_operator_http.sql", |
| 179 | "internal/operator/http_migrations/directory/migrations/0001_operator_http.sql", |
| 180 | } { |
| 181 | if !seen[want] { |
| 182 | t.Errorf("gate 2's static half does not read %s", want) |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // Gate 2's static half, extended for the Team page (team-members.md §3 rule |
| 188 | // 5, §4, §5): the walk reaches the Team set's migrations, and the one new |
| 189 | // column holding something a person typed, the member display name, is a |
| 190 | // sealed BLOB by name and by type. |
| 191 | func TestNoAddressColumnReachesTeamMigrations(t *testing.T) { |
| 192 | root := repoRoot(t) |
| 193 | seen := map[string]bool{} |
| 194 | for _, path := range migrationFiles(t, root) { |
| 195 | seen[filepath.ToSlash(relPath(t, root, path))] = true |
| 196 | } |
| 197 | dir := "internal/home/directorydata/team_migrations/migrations/" |
| 198 | for _, want := range []string{"0001_membership_added_at.sql", "0002_membership_names.sql", "0003_member_add.sql"} { |
| 199 | if !seen[dir+want] { |
| 200 | t.Errorf("gate 2's static half does not read %s", dir+want) |
| 201 | } |
| 202 | } |
| 203 | names := readFile(t, filepath.Join(root, dir+"0002_membership_names.sql")) |
| 204 | if !regexp.MustCompile(`(?m)^\s*sealed_name BLOB NOT NULL CHECK\(typeof\(sealed_name\) = 'blob'`).MatchString(names) { |
| 205 | t.Error("membership_names.sealed_name is not a sealed BLOB") |
| 206 | } |
| 207 | for _, line := range strings.Split(names, "\n") { |
| 208 | code, _, _ := strings.Cut(line, "--") |
| 209 | if strings.Contains(code, "name ") && !strings.Contains(code, "sealed_name") && strings.Contains(strings.ToUpper(code), "TEXT") { |
| 210 | t.Errorf("membership_names has a plaintext name column: %q", line) |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |