| new file mode 100644 |
| index 000000000..1872f4244 |
| --- /dev/null |
| +++ b/internal/edge/tunnel_publish_guard_test.go |
| @@ -0,0 +1,206 @@ |
| +package edge |
| + |
| +// Tunnel WebSockets (#471) Task 4: a dynamic channel's registration checks |
| +// its credential again as it publishes the connection, and the check and |
| +// the publish sit inside one TunnelHub.mu hold; SetDynamicAuth replaces the |
| +// credential map under that same mutex. Between them, a publish and a |
| +// replacement exclude each other, so a detach can never leave a revoked box |
| +// with a live channel. A source check over tunnel.go, in the manner of |
| +// reload_guard_test.go, so moving the re-check out of the lock, unlocking |
| +// between it and the publish, or swapping the map outside the lock fails |
| +// here by name. |
| + |
| +import ( |
| + "go/ast" |
| + "go/parser" |
| + "go/token" |
| + "testing" |
| +) |
| + |
| +// tunnelHubFuncs parses tunnel.go and returns its methods by funcName. |
| +func tunnelHubFuncs(t *testing.T) (*token.FileSet, map[string]*ast.FuncDecl) { |
| + t.Helper() |
| + fset := token.NewFileSet() |
| + af, err := parser.ParseFile(fset, "tunnel.go", nil, 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + fns := map[string]*ast.FuncDecl{} |
| + for _, d := range af.Decls { |
| + if fd, ok := d.(*ast.FuncDecl); ok && fd.Body != nil { |
| + fns[funcName(fd)] = fd |
| + } |
| + } |
| + return fset, fns |
| +} |
| + |
| +// lockedSpan finds the first top-level `h.mu.Lock()` statement in body and |
| +// the first top-level `h.mu.Unlock()` after it (len(body) when the lock is |
| +// never released by a top-level statement). lock is -1 when there is none. |
| +func lockedSpan(body []ast.Stmt) (lock, unlock int) { |
| + lock, unlock = -1, len(body) |
| + for i, s := range body { |
| + switch callSel(s) { |
| + case "h.mu.Lock": |
| + if lock < 0 { |
| + lock = i |
| + } |
| + case "h.mu.Unlock": |
| + if lock >= 0 && i > lock && unlock == len(body) { |
| + unlock = i |
| + } |
| + } |
| + } |
| + return lock, unlock |
| +} |
| + |
| +// unlocksOnlyToLeave reports every h.mu.Unlock() call inside n that is not |
| +// a statement of a block whose last statement is a return: the refusal |
| +// branch may drop the lock on its way out, but nothing may drop it and |
| +// carry on toward the publish. |
| +func unlocksOnlyToLeave(t *testing.T, fset *token.FileSet, what string, n ast.Node) { |
| + t.Helper() |
| + allowed := map[ast.Stmt]bool{} |
| + ast.Inspect(n, func(n ast.Node) bool { |
| + b, ok := n.(*ast.BlockStmt) |
| + if !ok || len(b.List) == 0 { |
| + return true |
| + } |
| + if _, ret := b.List[len(b.List)-1].(*ast.ReturnStmt); !ret { |
| + return true |
| + } |
| + for _, s := range b.List { |
| + if callSel(s) == "h.mu.Unlock" { |
| + allowed[s] = true |
| + } |
| + } |
| + return true |
| + }) |
| + ast.Inspect(n, func(n ast.Node) bool { |
| + switch v := n.(type) { |
| + case *ast.FuncLit: |
| + return false // a closure's unlock runs elsewhere; none is expected |
| + case ast.Stmt: |
| + if callSel(v) == "h.mu.Unlock" && !allowed[v] { |
| + t.Errorf("%s: h.mu.Unlock() at %s between the lock and the publish, not on a branch that returns", what, fset.Position(v.Pos())) |
| + } |
| + } |
| + return true |
| + }) |
| +} |
| + |
| +// isConnsInsert reports whether s is `h.conns[...] = ...`. |
| +func isConnsInsert(s ast.Stmt) bool { |
| + as, ok := s.(*ast.AssignStmt) |
| + if !ok || as.Tok != token.ASSIGN || len(as.Lhs) != 1 { |
| + return false |
| + } |
| + ix, ok := as.Lhs[0].(*ast.IndexExpr) |
| + return ok && selChain(ix.X) == "h.conns" |
| +} |
| + |
| +func TestTunnelRegistrationRecheckAndPublishShareOneLock(t *testing.T) { |
| + fset, fns := tunnelHubFuncs(t) |
| + serve := fns["(*TunnelHub).ServeHTTP"] |
| + if serve == nil { |
| + t.Fatal("no (*TunnelHub).ServeHTTP in tunnel.go") |
| + } |
| + body := serve.Body.List |
| + |
| + // Exactly one publish, as a statement of ServeHTTP's own body. |
| + insert, inserts := -1, 0 |
| + for i, s := range body { |
| + if isConnsInsert(s) { |
| + insert = i |
| + } |
| + } |
| + ast.Inspect(serve.Body, func(n ast.Node) bool { |
| + if s, ok := n.(ast.Stmt); ok && isConnsInsert(s) { |
| + inserts++ |
| + } |
| + return true |
| + }) |
| + if insert < 0 || inserts != 1 { |
| + t.Fatalf("ServeHTTP has %d h.conns[...] assignment(s) (one a statement of its body: %v); want exactly one, a statement of the body", inserts, insert >= 0) |
| + } |
| + |
| + lock, unlock := lockedSpan(body) |
| + if lock < 0 { |
| + t.Fatal("ServeHTTP does not call h.mu.Lock() as a statement of its body") |
| + } |
| + if !(lock < insert && insert < unlock) { |
| + t.Fatalf("ServeHTTP's publish (statement %d, %s) is not inside its h.mu hold (Lock at statement %d, first Unlock at %d)", |
| + insert, fset.Position(body[insert].Pos()), lock, unlock) |
| + } |
| + |
| + // The re-check: ServeHTTP calls verifyDynamicLocked once, and that call |
| + // sits between the Lock and the publish. |
| + var rechecks []token.Pos |
| + ast.Inspect(serve.Body, func(n ast.Node) bool { |
| + if c, ok := n.(*ast.CallExpr); ok && selChain(c.Fun) == "h.verifyDynamicLocked" { |
| + rechecks = append(rechecks, c.Pos()) |
| + } |
| + return true |
| + }) |
| + if len(rechecks) != 1 { |
| + t.Fatalf("ServeHTTP calls h.verifyDynamicLocked %d times, want once (the re-check under h.mu)", len(rechecks)) |
| + } |
| + if p := rechecks[0]; p < body[lock].End() || p > body[insert].Pos() { |
| + t.Fatalf("ServeHTTP's credential re-check at %s is not between its h.mu.Lock() (%s) and its publish (%s)", |
| + fset.Position(p), fset.Position(body[lock].Pos()), fset.Position(body[insert].Pos())) |
| + } |
| + |
| + // No unlock between the Lock and the publish, but on a branch that |
| + // returns (the refusal). |
| + for _, s := range body[lock+1 : insert] { |
| + unlocksOnlyToLeave(t, fset, "ServeHTTP", s) |
| + } |
| + |
| + // SetDynamicAuth: the map is replaced, and the old one read, inside |
| + // one h.mu hold; nothing else in tunnel.go assigns h.dynamicAuth. |
| + set := fns["(*TunnelHub).SetDynamicAuth"] |
| + if set == nil { |
| + t.Fatal("no (*TunnelHub).SetDynamicAuth in tunnel.go") |
| + } |
| + sb := set.Body.List |
| + slock, sunlock := lockedSpan(sb) |
| + if slock < 0 { |
| + t.Fatal("SetDynamicAuth does not call h.mu.Lock() as a statement of its body") |
| + } |
| + replace, readOld := -1, -1 |
| + for i, s := range sb { |
| + as, ok := s.(*ast.AssignStmt) |
| + if !ok || len(as.Lhs) != 1 || len(as.Rhs) != 1 { |
| + continue |
| + } |
| + if as.Tok == token.ASSIGN && selChain(as.Lhs[0]) == "h.dynamicAuth" { |
| + replace = i |
| + } |
| + if selChain(as.Rhs[0]) == "h.dynamicAuth" { |
| + readOld = i |
| + } |
| + } |
| + if replace < 0 { |
| + t.Fatal("SetDynamicAuth does not assign h.dynamicAuth as a statement of its body") |
| + } |
| + if !(slock < replace && replace < sunlock) { |
| + t.Fatalf("SetDynamicAuth replaces h.dynamicAuth (statement %d) outside its h.mu hold (Lock at %d, Unlock at %d)", replace, slock, sunlock) |
| + } |
| + if !(slock < readOld && readOld < sunlock) { |
| + t.Fatalf("SetDynamicAuth reads the old h.dynamicAuth (statement %d) outside the hold that replaces it (Lock at %d, Unlock at %d)", readOld, slock, sunlock) |
| + } |
| + for name, fd := range fns { |
| + ast.Inspect(fd.Body, func(n ast.Node) bool { |
| + as, ok := n.(*ast.AssignStmt) |
| + if !ok { |
| + return true |
| + } |
| + for _, l := range as.Lhs { |
| + if sel, ok := l.(*ast.SelectorExpr); ok && sel.Sel.Name == "dynamicAuth" && name != "(*TunnelHub).SetDynamicAuth" { |
| + t.Errorf("%s assigns %s at %s; only SetDynamicAuth may, under h.mu", name, selChain(sel), fset.Position(l.Pos())) |
| + } |
| + } |
| + return true |
| + }) |
| + } |
| +} |