//go:build browser package browsertest import ( "fmt" "slices" "strings" "sync" "testing" "github.com/chromedp/cdproto/network" "github.com/chromedp/chromedp" "amadan.net/rastrillo/rastrillo/harness" "amadan.net/oficina/base/internal/consumer" ) // headersSeen keeps the headers the browser received with responses whose URL // starts with prefix. type headersSeen struct { mu sync.Mutex seen []map[string]string } func watchHeaders(rig *harness.Rig, prefix string) *headersSeen { h := &headersSeen{} harness.Listen(rig, rig.Context(), network.ResponseReceived, func(e *network.EventResponseReceived) { if !strings.HasPrefix(e.Response.URL, prefix) { return } got := map[string]string{} for k, v := range e.Response.Headers { if s, ok := v.(string); ok { got[strings.ToLower(k)] = s } } h.mu.Lock() h.seen = append(h.seen, got) h.mu.Unlock() }) return h } func (h *headersSeen) all() []map[string]string { h.mu.Lock() defer h.mu.Unlock() return append([]map[string]string(nil), h.seen...) } // Signed in to Base, the owner opens Scratchpad from Home and lands on it // signed in. Base and Scratchpad are different sites, both served through // rastrillo's production stack, so this is the browser's own verdict on // Base's handoff page (its form-action override and Referrer-Policy: origin) // and on the app's cookies across the cross-site POST, 303 and GET. func TestHandoffInRealBrowser(t *testing.T) { for _, scripts := range []bool{true, false} { name := "scripts on" if !scripts { name = "scripts off" } t.Run(name, func(t *testing.T) { w := newWorld(t) if !scripts { drive(t, w.rig, scriptsDisabled(true)) } authorize := watchHeaders(w.rig, w.baseOrigin+"/authorize?") signInByEmail(t, w.rig, w.baseOrigin, w.base.mail) tile := chromedp.CSS(fmt.Sprintf(`a.app[href=%q]`, w.padOrigin+"/")) note := chromedp.CSS(`textarea[name="note"]`) if scripts { // The handoff page's script posts its form; nobody presses anything. drive(t, w.rig, chromedp.Click(tile), chromedp.WaitVisible(note)) } else { button := chromedp.CSS(`form#handoff button[type="submit"]`) drive(t, w.rig, chromedp.Click(tile), chromedp.WaitVisible(button)) w.rig.Screen(".card", "the handoff page, scripts off") if here := harness.Read(w.rig, chromedp.Location()); !strings.HasPrefix(here, w.baseOrigin+"/authorize?") { t.Fatalf("the Continue button is on %s, not Base's /authorize", here) } drive(t, w.rig, chromedp.Click(button), chromedp.WaitVisible(note)) } w.rig.Screen("main", "Scratchpad") if here := harness.Read(w.rig, chromedp.Location()); here != w.padOrigin+"/" { t.Fatalf("the handoff ended at %s, want %s/", here, w.padOrigin) } want := "Signed in as " + ownerName + ", " + baseName if got := harness.Evaluate[string](w.rig, `document.querySelector(".lead").textContent`); got != want { t.Fatalf("Scratchpad says %q, want %q", got, want) } // What the browser was told on Base's handoff page. pages := authorize.all() if len(pages) != 1 { t.Fatalf("the browser loaded Base's /authorize %d times, want once", len(pages)) } if got := pages[0]["referrer-policy"]; got != "origin" { t.Errorf("handoff page Referrer-Policy %q, want origin", got) } if got := pages[0]["content-security-policy"]; !strings.HasSuffix(got, "; form-action "+w.padOrigin) || strings.Contains(got, "form-action 'self'") { t.Errorf("handoff page CSP %q, want form-action %s only", got, w.padOrigin) } // What Scratchpad received. var posts, finishes []seen for _, r := range w.pad.all() { switch { case r.method == "POST" && r.path == consumer.AssertionPath: posts = append(posts, r) case r.method == "GET" && r.path == consumer.FinishPath: finishes = append(finishes, r) } } if len(posts) != 1 || len(finishes) != 1 { t.Fatalf("Scratchpad got %d assertion POSTs and %d finish GETs, want one each: %+v", len(posts), len(finishes), w.pad.all()) } post, finish := posts[0], finishes[0] t.Logf("handoff page: Referrer-Policy %q; CSP %q", pages[0]["referrer-policy"], pages[0]["content-security-policy"]) t.Logf("assertion POST: Origin %q, Sec-Fetch-Site %q, cookies %v", post.origin, post.fetchSite, post.cookies) t.Logf("finish GET: Sec-Fetch-Site %q, cookies %v", finish.fetchSite, finish.cookies) if post.origin != w.baseOrigin { t.Errorf("the assertion POST carried Origin %q, want Base's %q", post.origin, w.baseOrigin) } // The control for everything about cookies below: the browser // itself counts Base and Scratchpad as different sites. if post.fetchSite != "cross-site" { t.Errorf("the assertion POST is %q to the browser, want cross-site", post.fetchSite) } // SameSite=Lax keeps the app's cookies off a cross-site POST, which // is why the assertion waits in an inbox for the same-site GET. if slices.Contains(post.cookies, "__Host-base-pending") { t.Errorf("the cross-site POST carried the pending cookie: %v", post.cookies) } for _, c := range []string{"__Host-base-pending", "__Host-base-inbox"} { if !slices.Contains(finish.cookies, c) { t.Errorf("GET %s after the 303 lacks %s: it carried %v", consumer.FinishPath, c, finish.cookies) } } }) } }