package web import ( "context" "net/http" "net/url" "regexp" "sort" "strings" "testing" "time" "supermarket/internal/content" "supermarket/internal/inventory" "supermarket/internal/profile" "supermarket/internal/prospects" "supermarket/internal/signals" "supermarket/internal/users" ) // marker is in everything team B has: it must never reach team A. const marker = "ZEBRAMARK" // seedTeam fills a team with data carrying a marker, in many tables. func seedTeam(t *testing.T, ts *Server, mark string) { t.Helper() d := ts.DB must := func(err error) { t.Helper() if err != nil { t.Fatal(err) } } must(profile.Save(d, &profile.Profile{Name: mark + " Inc", Description: mark + " widgets", Keywords: []string{mark}})) for i := 0; i < 3; i++ { must(inventory.Save(d, &inventory.Entry{Name: mark + " rival " + string(rune('a'+i)), Relationship: "competitor", Description: mark})) } sig := &signals.Signal{Name: mark + " signal", Hypothesis: mark} must(signals.Save(d, sig)) _, _, err := prospects.AddCompany(d, prospects.Company{Name: mark + " Corp", Domain: strings.ToLower(mark) + ".example", Source: "manual"}, time.Now()) must(err) _, _, err = prospects.AddContact(d, prospects.Contact{FirstName: mark, LastName: "Lee", Email: "kim@" + strings.ToLower(mark) + ".example", Source: "manual"}, time.Now()) must(err) _, err = content.CreateDrafts(d, []content.Draft{{Channel: content.ChannelX, Content: mark + " post"}, {Channel: content.ChannelLinkedIn, Content: mark + " update"}}) must(err) must(prospects.Suppress(d, "domain", strings.ToLower(mark)+"-blocked.example", mark, time.Now())) must(d.PutJSON("seo:indexnow-key", mark)) must(ts.Secrets.Set(map[string]string{"hunter.api_key": mark})) } // sampleValues fill a route's wildcards: ids and names that exist in the // other team's schema. var wildcard = regexp.MustCompile(`\{([a-z]+)(\.\.\.)?\}`) func fillPattern(pattern, v string) string { return wildcard.ReplaceAllStringFunc(pattern, func(w string) string { switch { case strings.Contains(w, "provider"): return "x" case strings.Contains(w, "kind"): return "company" case strings.Contains(w, "month"): return "2026-11" case strings.Contains(w, "token"): return "nothing" } return v }) } // TestTeamsIsolatedEverywhere is Phase 13's proof: team A, signed in as its // owner and through its agent with every scope, reaches every page, // fragment and tool, and never sees team B's data, nor changes it. func TestTeamsIsolatedEverywhere(t *testing.T) { s, h := newMultiEdition(t) a, _, ac := newTeam(t, s, "Acme", "olu@acme.example") b, _, bc := newTeam(t, s, "Bolt", "bea@bolt.example") ta, _ := s.forTeam(a.ID) tb, _ := s.forTeam(b.ID) seedTeam(t, ta, "OWNMARK") seedTeam(t, tb, marker) // Both prove their domains, so nothing is held back by that. users.Prove(s.platform(), a.ID, "dns", time.Now()) users.Prove(s.platform(), b.ID, "dns", time.Now()) leaks := func(where, body string) { t.Helper() if strings.Contains(body, marker) || strings.Contains(strings.ToLower(body), strings.ToLower(marker)) { i := strings.Index(strings.ToLower(body), strings.ToLower(marker)) t.Errorf("%s shows team B's data: …%s…", where, body[max(0, i-80):min(len(body), i+80)]) } } // The check can see a leak: B's own owner sees the marker. for _, p := range []string{"/market", "/signals", "/posts", "/prospects", "/settings/profile", "/search?q=" + marker[:5]} { if body := as(h, bc, http.MethodGet, p, nil).Body.String(); !strings.Contains(body, marker) { t.Errorf("control: B's own %s lacks the marker", p) } } // Every page and fragment (GET), with its ids filled in. patterns := append([]string(nil), s.st().patterns...) sort.Strings(patterns) n := 0 for _, p := range patterns { method, path, _ := strings.Cut(p, " ") if method != http.MethodGet || strings.HasPrefix(path, "/static") || path == "/mcp" { continue } for _, v := range []string{"1", "2", "3"} { u := strings.TrimSuffix(fillPattern(path, v), "{$}") for _, q := range []string{"", "?site=0", "?q=" + url.QueryEscape(marker[:5])} { rec := as(h, ac, http.MethodGet, u+q, nil) leaks("GET "+u+q, rec.Body.String()) n++ } } } if n < 300 { t.Errorf("only %d requests", n) } // Every MCP tool, as A's agent with every scope. all := map[string]users.Access{} for _, g := range agentGrants() { all[g.ID] = users.Edit } _, key := newAgentIn(t, ta, "A's agent", all) _, tools := mcpTools(h, key) if len(tools) < 30 { t.Fatalf("only %d tools", len(tools)) } for _, tool := range tools { for _, args := range []map[string]any{{}, {"id": 1, "site": 0, "status": "waiting", "entry": marker + " rival a", "tab": "drafts"}, {"id": 2, "draft_id": 1, "message_id": 1, "contact_id": 1, "change_id": 1}} { text, _ := mcpCall(t, h, key, tool, args) leaks("tool "+tool, text) } } // Changes by A, aimed at B's ids, change nothing of B's. for _, p := range []string{"/market/1/delete", "/market/2/delete", "/signals/1/delete", "/drafts/1/discard", "/drafts/2/discard", "/prospects/company/1/reject", "/prospects/contact/1/delete"} { as(h, ac, http.MethodPost, p, url.Values{}) } if list, _ := inventory.List(tb.DB, ""); len(list) != 3 { t.Errorf("B's Market: %d", len(list)) } if list, _ := signals.List(tb.DB); len(list) != 1 { t.Errorf("B's signals: %d", len(list)) } if list, _ := content.ListDrafts(tb.DB, 10, content.StatusDraft); len(list) != 2 { t.Errorf("B's drafts: %d", len(list)) } if cs, ps, _ := prospects.Review(tb.DB, prospects.StatusNew, 0, 10); len(cs) != 1 || len(ps) != 1 { t.Errorf("B's prospects: %d %d", len(cs), len(ps)) } if tb.Secrets.Get("hunter.api_key") != marker { t.Error("B's key changed") } // And the background jobs: one round of each, for both teams, leaves // B's data B's (and A's job never ran on B's schema: B's drafts are // where they were). sent := &fakePosts{} s.testPublisher = nil _ = sent for _, name := range []string{"scheduler", "accounts"} { s.jobDo(t.Context(), name, func(ctx context.Context, ts *Server) { if name == "scheduler" { ts.publishDue(ctx, time.Now()) } }) } if list, _ := inventory.List(ta.DB, ""); strings.Contains(entryNames(list), marker) { t.Error("B's entries in A's schema") } } func entryNames(list []inventory.Entry) string { var b strings.Builder for _, e := range list { b.WriteString(e.Name + " ") } return b.String() }