| 1 | package guards |
| 2 | |
| 3 | import ( |
| 4 | "go/ast" |
| 5 | "go/parser" |
| 6 | "go/token" |
| 7 | "os" |
| 8 | "path" |
| 9 | "path/filepath" |
| 10 | "slices" |
| 11 | "strconv" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | ) |
| 15 | |
| 16 | // frameworkSignin are rastrillo's sign-in packages Home must not reach for |
| 17 | // beyond what it needs. Home is blind: its link store is sealed and its |
| 18 | // session subject is an opaque person ref, and auth's defaults would write |
| 19 | // a readable auth_links.address (AGENTS.md, Data). secondfactor's Schema |
| 20 | // copies the recovery codes out and drops the table Home's codes live in, |
| 21 | // so it is never imported. passkey links lastsignin and secondfactor |
| 22 | // itself, which is allowed: what is held here is that Home's own code |
| 23 | // never reaches for them. |
| 24 | var frameworkSignin = []string{ |
| 25 | "amadan.net/rastrillo/rastrillo/auth", |
| 26 | "amadan.net/rastrillo/rastrillo/lastsignin", |
| 27 | "amadan.net/rastrillo/rastrillo/secondfactor", |
| 28 | } |
| 29 | |
| 30 | // doorImports is the one carve-out (smart-door spec step 5, ruling 26): |
| 31 | // the door on id.* fills rastrillo's SigninState itself, renders its |
| 32 | // partial, and remembers the way in with lastsignin's jar. So auth and |
| 33 | // lastsignin may be imported, only by the door's two files, and only for |
| 34 | // these names: the state, its constants and the jar. Never auth.New, |
| 35 | // auth.Config or any handler, which would be the framework's sign-in, and |
| 36 | // never secondfactor. |
| 37 | var doorImports = map[string]struct { |
| 38 | files []string |
| 39 | names map[string]bool |
| 40 | }{ |
| 41 | "amadan.net/rastrillo/rastrillo/auth": { |
| 42 | files: []string{"internal/home/door.go", "internal/home/signin.go"}, |
| 43 | names: setOf("SigninState", "SigninProblem", "Remembered", "PasskeyDoor", "ParseWay", "WayEmail", |
| 44 | "StepAsk", "StepReturning", "StepSent", "StepContinue", |
| 45 | "ProblemNone", "ProblemAddress", "ProblemRate", "ProblemGeneric", "ProblemExpired", "ProblemKeymail", "ProblemReauth"), |
| 46 | }, |
| 47 | "amadan.net/rastrillo/rastrillo/lastsignin": { |
| 48 | files: []string{"internal/home/door.go"}, |
| 49 | names: setOf("New", "Config", "Jar", "Record", "On", "Valid", "Invalid", |
| 50 | "MethodMagicLink", "MethodKeymail", "MethodPasskey"), |
| 51 | }, |
| 52 | } |
| 53 | |
| 54 | func setOf(names ...string) map[string]bool { |
| 55 | set := make(map[string]bool, len(names)) |
| 56 | for _, n := range names { |
| 57 | set[n] = true |
| 58 | } |
| 59 | return set |
| 60 | } |
| 61 | |
| 62 | // frameworkSigninUses is every way the file at rel (relative to the repo |
| 63 | // root, slash-separated) reaches past the carve-out: a banned import, an |
| 64 | // allowed import from another file, or a name the carve-out does not |
| 65 | // list. |
| 66 | func frameworkSigninUses(t *testing.T, rel string, src []byte) []string { |
| 67 | t.Helper() |
| 68 | fset := token.NewFileSet() |
| 69 | f, err := parser.ParseFile(fset, rel, src, parser.SkipObjectResolution) |
| 70 | if err != nil { |
| 71 | t.Fatalf("parse %s: %v", rel, err) |
| 72 | } |
| 73 | var out []string |
| 74 | local := map[string]string{} // import name in this file -> package path |
| 75 | for _, imp := range f.Imports { |
| 76 | p, _ := strconv.Unquote(imp.Path.Value) |
| 77 | if !slices.Contains(frameworkSignin, p) { |
| 78 | continue |
| 79 | } |
| 80 | allowed, ok := doorImports[p] |
| 81 | if !ok || !slices.Contains(allowed.files, rel) { |
| 82 | out = append(out, "imports "+p) |
| 83 | continue |
| 84 | } |
| 85 | name := path.Base(p) |
| 86 | if imp.Name != nil { |
| 87 | name = imp.Name.Name |
| 88 | } |
| 89 | if name == "_" || name == "." { |
| 90 | out = append(out, "imports "+p+" as "+name) |
| 91 | continue |
| 92 | } |
| 93 | local[name] = p |
| 94 | } |
| 95 | ast.Inspect(f, func(n ast.Node) bool { |
| 96 | sel, ok := n.(*ast.SelectorExpr) |
| 97 | if !ok { |
| 98 | return true |
| 99 | } |
| 100 | x, ok := sel.X.(*ast.Ident) |
| 101 | if !ok { |
| 102 | return true |
| 103 | } |
| 104 | if p, ok := local[x.Name]; ok && !doorImports[p].names[sel.Sel.Name] { |
| 105 | out = append(out, "line "+strconv.Itoa(fset.Position(sel.Pos()).Line)+": "+x.Name+"."+sel.Sel.Name) |
| 106 | } |
| 107 | return true |
| 108 | }) |
| 109 | return out |
| 110 | } |
| 111 | |
| 112 | // passkeyPkg's New and Handlers build and hold passkey's HTTP handlers. |
| 113 | // Home runs its own ceremonies (internal/home/id_passkey.go) and keeps |
| 114 | // passkey.Config only as the record its RP-ID guard reads; passkey's |
| 115 | // sign-in and Gate handlers are never built, so never mounted. |
| 116 | const passkeyPkg = "amadan.net/rastrillo/rastrillo/passkey" |
| 117 | |
| 118 | // passkeyHandlerUses returns each use of passkey.New or passkey.Handlers |
| 119 | // in the file's code (comments are not code), under whatever name the |
| 120 | // file imports passkey as. |
| 121 | func passkeyHandlerUses(t *testing.T, path string) []string { |
| 122 | t.Helper() |
| 123 | fset := token.NewFileSet() |
| 124 | f, err := parser.ParseFile(fset, path, nil, parser.SkipObjectResolution) |
| 125 | if err != nil { |
| 126 | t.Fatalf("parse %s: %v", path, err) |
| 127 | } |
| 128 | name := "" |
| 129 | for _, imp := range f.Imports { |
| 130 | if p, _ := strconv.Unquote(imp.Path.Value); p == passkeyPkg { |
| 131 | name = "passkey" |
| 132 | if imp.Name != nil { |
| 133 | name = imp.Name.Name |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | if name == "" { |
| 138 | return nil |
| 139 | } |
| 140 | var uses []string |
| 141 | ast.Inspect(f, func(n ast.Node) bool { |
| 142 | sel, ok := n.(*ast.SelectorExpr) |
| 143 | if !ok { |
| 144 | return true |
| 145 | } |
| 146 | if x, ok := sel.X.(*ast.Ident); ok && x.Name == name && (sel.Sel.Name == "New" || sel.Sel.Name == "Handlers") { |
| 147 | uses = append(uses, "line "+strconv.Itoa(fset.Position(sel.Pos()).Line)+": passkey."+sel.Sel.Name) |
| 148 | } |
| 149 | return true |
| 150 | }) |
| 151 | return uses |
| 152 | } |
| 153 | |
| 154 | // TestHomeNeverMountsFrameworkSignin fails on a direct import of |
| 155 | // secondfactor; an import of auth or lastsignin outside the door's files, |
| 156 | // or any of their names beyond the door's carve-out (doorImports); or a |
| 157 | // passkey.New / passkey.Handlers, in any of Home's production files |
| 158 | // (tests may build fixtures with them). |
| 159 | func TestHomeNeverMountsFrameworkSignin(t *testing.T) { |
| 160 | root := repoRoot(t) |
| 161 | for _, path := range goFiles(t, root) { |
| 162 | if strings.HasSuffix(path, "_test.go") { |
| 163 | continue |
| 164 | } |
| 165 | rel := relPath(t, root, path) |
| 166 | src, err := os.ReadFile(path) |
| 167 | if err != nil { |
| 168 | t.Fatal(err) |
| 169 | } |
| 170 | for _, use := range frameworkSigninUses(t, filepath.ToSlash(rel), src) { |
| 171 | t.Errorf("%s: %s; Home does its own sign-in, and the door may use only its state and jar (AGENTS.md, Data)", rel, use) |
| 172 | } |
| 173 | for _, use := range doorMemoryUses(t, filepath.ToSlash(rel), src) { |
| 174 | t.Errorf("%s: %s; only the door (internal/home/door.go) may reach the jar or fill these fields (ruling 26)", rel, use) |
| 175 | } |
| 176 | for _, use := range passkeyHandlerUses(t, path) { |
| 177 | t.Errorf("%s: %s; Home mounts none of passkey's handlers", rel, use) |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // doorFile is the one production file that holds the jar. |
| 183 | const doorFile = "internal/home/door.go" |
| 184 | |
| 185 | // doorOnlyFields are SigninState fields that carry an address or a URL |
| 186 | // the door must decide itself: only door.go may fill them in a composite |
| 187 | // literal. |
| 188 | var doorOnlyFields = map[string]bool{"SentTo": true, "Federated": true, "Confirm": true, "Remembered": true} |
| 189 | |
| 190 | // doorMemoryUses is every way a production file other than door.go |
| 191 | // reaches the door's memory: the jar field (".jar", on whatever value), |
| 192 | // any identifier named lastsignin, or a door-only SigninState field set |
| 193 | // in a composite literal. The import allowlist above confines the |
| 194 | // packages; this confines the jar, which an import rule cannot see: a |
| 195 | // file that could reach the jar could seal any address it liked. |
| 196 | func doorMemoryUses(t *testing.T, rel string, src []byte) []string { |
| 197 | t.Helper() |
| 198 | if rel == doorFile { |
| 199 | return nil |
| 200 | } |
| 201 | fset := token.NewFileSet() |
| 202 | f, err := parser.ParseFile(fset, rel, src, parser.SkipObjectResolution) |
| 203 | if err != nil { |
| 204 | t.Fatalf("parse %s: %v", rel, err) |
| 205 | } |
| 206 | var out []string |
| 207 | at := func(n ast.Node) string { return "line " + strconv.Itoa(fset.Position(n.Pos()).Line) } |
| 208 | ast.Inspect(f, func(n ast.Node) bool { |
| 209 | switch n := n.(type) { |
| 210 | case *ast.SelectorExpr: |
| 211 | if n.Sel.Name == "jar" { |
| 212 | out = append(out, at(n)+": .jar") |
| 213 | } |
| 214 | case *ast.Ident: |
| 215 | if n.Name == "lastsignin" { |
| 216 | out = append(out, at(n)+": lastsignin") |
| 217 | } |
| 218 | case *ast.KeyValueExpr: |
| 219 | if k, ok := n.Key.(*ast.Ident); ok && doorOnlyFields[k.Name] { |
| 220 | out = append(out, at(n)+": "+k.Name+":") |
| 221 | } |
| 222 | } |
| 223 | return true |
| 224 | }) |
| 225 | return out |
| 226 | } |
| 227 | |
| 228 | // The jar is the door's alone: a .jar, a lastsignin name or a door-only |
| 229 | // field anywhere else is refused, and door.go itself passes. |
| 230 | func TestDoorMemoryGuardRefuses(t *testing.T) { |
| 231 | cases := []struct { |
| 232 | rel, src string |
| 233 | refused bool |
| 234 | }{ |
| 235 | {"internal/home/door.go", "package home\nfunc f(a *App) { a.door.jar.Clear(nil) }\n", false}, |
| 236 | {"internal/home/signin.go", "package home\nfunc f(a *App) { a.door.jar.Clear(nil) }\n", true}, |
| 237 | {"internal/home/app.go", "package home\nvar x = lastsignin.Record{}\n", true}, |
| 238 | {"internal/home/signin.go", "package home\nvar x = auth.SigninState{SentTo: \"a@b\"}\n", true}, |
| 239 | {"internal/home/signin.go", "package home\nvar x = auth.SigninState{Step: auth.StepSent}\n", false}, |
| 240 | } |
| 241 | for i, c := range cases { |
| 242 | if got := doorMemoryUses(t, c.rel, []byte(c.src)); (len(got) > 0) != c.refused { |
| 243 | t.Errorf("case %d (%s): uses %q, refused want %v", i, c.rel, got, c.refused) |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // The carve-out is narrow, and the guard says so on the shapes it must |
| 249 | // refuse: the framework's constructor and handlers from the door's own |
| 250 | // file, the jar from a file that is not the door's, and secondfactor |
| 251 | // anywhere. |
| 252 | func TestFrameworkSigninCarveOutRefuses(t *testing.T) { |
| 253 | const head = "package home\n\nimport (\n\t\"amadan.net/rastrillo/rastrillo/auth\"\n\t\"amadan.net/rastrillo/rastrillo/lastsignin\"\n)\n\n" |
| 254 | cases := []struct { |
| 255 | rel, body string |
| 256 | refused bool |
| 257 | }{ |
| 258 | {"internal/home/door.go", "var _ auth.SigninState\nvar _, _ = lastsignin.New(lastsignin.Config{})\n", false}, |
| 259 | {"internal/home/door.go", "var _, _ = auth.New(auth.Config{})\nvar _ lastsignin.Jar\n", true}, |
| 260 | {"internal/home/door.go", "var _ = auth.SigninState{}.Door\nvar _ = lastsignin.Forgetting\n", true}, |
| 261 | {"internal/home/signin.go", "var _ auth.SigninState\nvar _ lastsignin.Jar\n", true}, |
| 262 | {"internal/home/app.go", "var _ auth.SigninState\nvar _ lastsignin.Jar\n", true}, |
| 263 | } |
| 264 | for i, c := range cases { |
| 265 | if got := frameworkSigninUses(t, c.rel, []byte(head+c.body)); (len(got) > 0) != c.refused { |
| 266 | t.Errorf("case %d (%s): uses %q, refused want %v", i, c.rel, got, c.refused) |
| 267 | } |
| 268 | } |
| 269 | src := "package home\n\nimport _ \"amadan.net/rastrillo/rastrillo/secondfactor\"\n" |
| 270 | if got := frameworkSigninUses(t, "internal/home/door.go", []byte(src)); len(got) == 0 { |
| 271 | t.Error("secondfactor imported by the door's file passed") |
| 272 | } |
| 273 | } |
| 274 | |