package server import ( "context" "errors" "html" "net/http" "net/http/httptest" "net/url" "os" "os/exec" "strings" "testing" "time" "pageswiki/internal/store" ) func TestProfileContactPrivacyAndRevocation(t *testing.T) { s, token, domain := configFixture(t) ctx := context.Background() admin, err := s.store.PrincipalForSession(domain, token) if err != nil { t.Fatal(err) } owner, err := s.store.CreatePasswordAccount(domain, "phone-owner", "strong-password-for-test", "Phone Owner", "") if err != nil { t.Fatal(err) } contact := store.ProfileContact{PhoneDisplay: "+81 (90) 1234-5678", Visibility: "private"} if err := s.store.SaveProfileContact(ctx, domain, owner.UUID, contact, false); err != nil { t.Fatal(err) } get := func(viewer string, want bool) store.ProfileContact { t.Helper() got, err := s.store.ProfileContactForViewer(ctx, domain, owner.UUID, viewer) if err != nil || (got.PhoneNumber != "") != want { t.Fatalf("viewer=%q visible=%v want=%v err=%v", viewer, got.PhoneNumber != "", want, err) } if !want && got != (store.ProfileContact{}) { t.Fatal("hidden contact metadata leaked") } return got } contact = get(owner.UUID, true) get(admin.UUID, false) // domain administrator is not consent get("", false) get("foreign-user", false) stale := contact stale.Version = 0 if err := s.store.SaveProfileContact(ctx, domain, owner.UUID, stale, false); !errors.Is(err, store.ErrGovernanceConflict) { t.Fatalf("stale write: %v", err) } contact.Visibility = "domain" if err := s.store.SaveProfileContact(ctx, domain, owner.UUID, contact, false); err != nil { t.Fatal(err) } get(admin.UUID, true) get("", false) contact = get(owner.UUID, true) contact.Visibility = "public" if err := s.store.SaveProfileContact(ctx, domain, owner.UUID, contact, true); !errors.Is(err, store.ErrGovernanceCeiling) { t.Fatalf("ceiling bypass: %v", err) } enableDomainPublicCeiling(t, s, domain, admin.UUID) policy, err := s.store.UserSpacePolicy(ctx, domain, owner.UUID) if err != nil { t.Fatal(err) } policy.ProfileVisibility = "public" policy, err = s.store.SaveUserSpacePolicy(ctx, domain, owner.UUID, policy) if err != nil { t.Fatal(err) } if err := s.store.SaveProfileContact(ctx, domain, owner.UUID, contact, false); err == nil { t.Fatal("public without confirmation") } if err := s.store.SaveProfileContact(ctx, domain, owner.UUID, contact, true); err != nil { t.Fatal(err) } get("", true) changed := get(owner.UUID, true) changed.PhoneDisplay = "03-1234-5678" if err := s.store.SaveProfileContact(ctx, domain, owner.UUID, changed, false); err == nil { t.Fatal("new public phone did not require renewed confirmation") } governance, err := s.store.CityGovernance(ctx, domain, admin.UUID, "domain", domain) if err != nil { t.Fatal(err) } closed := "authenticated_acl" if _, err = s.store.ChangeCityGovernance(ctx, domain, admin.UUID, "domain", domain, store.CityGovernanceChange{Epoch: governance.Epoch, TopologyVersion: governance.TopologyVersion, Policy: &closed}, false); err != nil { t.Fatal(err) } get("", false) enableDomainPublicCeiling(t, s, domain, admin.UUID) get("", true) policy.ProfileVisibility = "private" if _, err = s.store.SaveUserSpacePolicy(ctx, domain, owner.UUID, policy); err != nil { t.Fatal(err) } get("", false) get(admin.UUID, false) contact = get(owner.UUID, true) if err = s.store.SetPrincipalState(domain, owner.UUID, "readonly"); err != nil { t.Fatal(err) } if err = s.store.SaveProfileContact(ctx, domain, owner.UUID, contact, false); err == nil { t.Fatal("readonly writer accepted") } if err = s.store.SetPrincipalState(domain, owner.UUID, "active"); err != nil { t.Fatal(err) } contact.PhoneDisplay = "" if err = s.store.SaveProfileContact(ctx, domain, owner.UUID, contact, false); err != nil { t.Fatal(err) } cleared, err := s.store.ProfileContactForViewer(ctx, domain, owner.UUID, owner.UUID) if err != nil || cleared.PhoneNumber != "" || cleared.PhoneDisplay != "" || cleared.Visibility != "private" { t.Fatalf("not cleared: %+v %v", cleared, err) } if err = s.store.SaveProfileContact(ctx, "unrelated-domain", owner.UUID, store.ProfileContact{Visibility: "private", PhoneDisplay: "12345"}, false); err == nil { t.Fatal("cross-domain write accepted") } } func TestProfileContactChromium(t *testing.T) { if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" { t.Skip("opt-in Chromium") } s, token, _ := configFixture(t) s.mux.HandleFunc("/u/", s.handleUserProfile) script, err := os.ReadFile("internal/server/testdata/profile-contact.js") if err != nil { t.Fatal(err) } server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r.Host = "localhost" r.Header.Set("Authorization", "Bearer "+token) if r.URL.Path == "/fixture" { w.Header().Set("Content-Type", "text/html; charset=utf-8") _, _ = w.Write([]byte("")) return } s.withAuthorization(s.mux).ServeHTTP(w, r) })) defer server.Close() for _, width := range []string{"1440", "375"} { ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) screenshot := "" if base := os.Getenv("PAGESWIKI_CONTACT_SCREENSHOT"); base != "" { screenshot = base + "-" + width + ".png" } out, err := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+"/fixture", t.TempDir(), screenshot, width, "900").CombinedOutput() cancel() if err != nil { t.Fatalf("contact width=%s: %v\n%s", width, err, out) } } } func TestProfileContactHTTP(t *testing.T) { s, token, domain := configFixture(t) owner, err := s.store.PrincipalForSession(domain, token) if err != nil { t.Fatal(err) } s.mux.HandleFunc("/u/", s.handleUserProfile) call := func(method, target string, form url.Values, authenticated bool) *httptest.ResponseRecorder { r := httptest.NewRequest(method, "http://localhost"+target, strings.NewReader(form.Encode())) if method == "POST" { r.Header.Set("Content-Type", "application/x-www-form-urlencoded") } if authenticated { r.Header.Set("Authorization", "Bearer "+token) } w := httptest.NewRecorder() s.withAuthorization(s.mux).ServeHTTP(w, r) return w } form := url.Values{"action": {"contact"}, "contact_version": {"0"}, "phone": {"+81 (90) 1234-5678"}, "contact_visibility": {"private"}} w := call("POST", "/u/admin/_/edit", form, true) if w.Code != 303 || w.Header().Get("Location") != "/u/admin/_/edit?contact=saved#contact" { t.Fatalf("save: %d %s", w.Code, w.Body.String()) } if _, err := s.store.CreatePasswordAccount(domain, "other-phone", "strong-password-test", "Other", ""); err != nil { t.Fatal(err) } if other := call("POST", "/u/other-phone/_/edit", form, true); other.Code != 403 { t.Fatalf("editing another account: %d", other.Code) } w = call("GET", "/u/admin", nil, true) if w.Code != 200 || !strings.Contains(html.UnescapeString(w.Body.String()), `href="tel:+819012345678"`) || w.Header().Get("Cache-Control") != "private, no-store" { t.Fatalf("owner view: %d %s", w.Code, w.Body.String()) } if w = call("POST", "/u/admin/_/edit", form, true); w.Code != 409 { t.Fatalf("conflict: %d", w.Code) } form.Set("contact_version", "1") form.Set("phone", "123;ext=456") if w = call("POST", "/u/admin/_/edit", form, true); w.Code != 400 { t.Fatalf("invalid: %d", w.Code) } enableDomainPublicCeiling(t, s, domain, owner.UUID) policy, err := s.store.UserSpacePolicy(context.Background(), domain, owner.UUID) if err != nil { t.Fatal(err) } policy.ProfileVisibility = "public" if _, err = s.store.SaveUserSpacePolicy(context.Background(), domain, owner.UUID, policy); err != nil { t.Fatal(err) } w = call("GET", "/u/admin", nil, false) if w.Code != 200 || strings.Contains(w.Body.String(), "1234-5678") || strings.Contains(w.Body.String(), "tel:") { t.Fatal("private phone leaked in public profile", w.Code) } form.Set("phone", "+81 (90) 1234-5678") form.Set("contact_visibility", "public") form.Set("contact_confirm_public", "on") if w = call("POST", "/u/admin/_/edit", form, true); w.Code != 303 { t.Fatalf("public save: %d %s", w.Code, w.Body.String()) } w = call("GET", "/u/admin", nil, false) if w.Code != 200 || !strings.Contains(html.UnescapeString(w.Body.String()), `href="tel:+819012345678"`) { t.Fatal("public phone missing", w.Code) } if got := profileContactHref(store.ProfileContact{PhoneNumber: "javascript:alert(1)"}); got != "" { t.Fatal("unsafe phone href") } }