oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package idtest
4
5import (
6 "context"
7 "fmt"
8 "html/template"
9 "net/http"
10 "net/http/httptest"
11 "net/url"
12 "os"
13 "path/filepath"
14 "strings"
15 "sync/atomic"
16 "testing"
17 "time"
18
19 "amadan.net/oficina/equipo/internal/browsertest"
20 "amadan.net/rastrillo/rastrillo/harness"
21 "github.com/chromedp/cdproto/cdp"
22 "github.com/chromedp/cdproto/emulation"
23 "github.com/chromedp/chromedp"
24)
25
26// The test Home uses the production assertion signer and closed authorize
27// contract. Its identity is fixed; Home's own WebAuthn browser tests exercise
28// earning that identity. This test covers Equipo's real cross-site door.
29func TestBrowserOficinaIDDoor(t *testing.T) {
30 const origin = "https://equipo.example.test"
31 const home = "https://id.example.org"
32 f := fixtureAt(t, true, origin, home)
33 local := f.link(t)
34 var callbacks atomic.Int32
35 var nonce string
36 continuePage := template.Must(template.New("continue").Parse(`<!doctype html><title>Oficina ID</title><form method="post" action="/continue"><button id="continue">Continue</button></form>`))
37 receiptPage := template.Must(template.New("receipt").Parse(`<!doctype html><title>Oficina ID</title><form method="post" action="{{.Target}}"><input type="hidden" name="assertion" value="{{.Token}}"><button id="return">Continue to Equipo</button></form>`))
38 server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
39 if r.Host == "equipo.example.test" {
40 if r.URL.Path == "/auth/assertion" {
41 callbacks.Add(1)
42 if r.Header.Get("Origin") != home {
43 t.Error("callback lost issuer Origin")
44 }
45 for _, c := range r.Cookies() {
46 if c.Name == "__Host-equipo-id-pending" {
47 t.Error("Lax pending cookie sent on cross-site POST")
48 }
49 }
50 }
51 f.h.ServeHTTP(w, r)
52 return
53 }
54 if r.Host != "id.example.org" {
55 http.NotFound(w, r)
56 return
57 }
58 w.Header().Set("Cache-Control", "no-store")
59 w.Header().Set("Referrer-Policy", "strict-origin-when-cross-origin")
60 switch r.URL.Path {
61 case "/authorize":
62 q := r.URL.Query()
63 if r.Method != "GET" || len(q) != 2 || len(q["consumer"]) != 1 || q.Get("consumer") != "equipo" || len(q["nonce"]) != 1 {
64 http.Error(w, "invalid test authorization", 400)
65 return
66 }
67 nonce = q.Get("nonce")
68 _ = continuePage.Execute(w, nil)
69 case "/continue":
70 if r.Method != "POST" || r.Header.Get("Origin") != home || nonce == "" {
71 http.Error(w, "invalid test continuation", 400)
72 return
73 }
74 token := f.signed(t, nonce, person, time.Now())
75 nonce = ""
76 _ = receiptPage.Execute(w, struct{ Target, Token string }{origin + "/auth/assertion", token})
77 default:
78 http.NotFound(w, r)
79 }
80 }))
81 defer server.Close()
82 u, _ := url.Parse(server.URL)
83 profile := t.TempDir()
84 opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.ExecPath(harness.ChromePath(t)), chromedp.NoSandbox, chromedp.UserDataDir(profile), chromedp.Flag("ignore-certificate-errors", true), chromedp.Flag("no-proxy-server", true), chromedp.Flag("host-resolver-rules", "MAP equipo.example.test "+u.Host+", MAP id.example.org "+u.Host))
85 alloc, stop := chromedp.NewExecAllocator(context.Background(), opts...)
86 defer stop()
87 ctx, cancel := chromedp.NewContext(alloc)
88 defer cancel()
89 ctx, deadline := context.WithTimeout(ctx, 60*time.Second)
90 defer deadline()
91 run := func(actions ...chromedp.Action[chromedp.Void]) {
92 t.Helper()
93 if e := chromedp.Do(ctx, actions...); e != nil {
94 t.Fatal(e)
95 }
96 }
97 run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
98 _, e := cdp.Call(ctx, target, emulation.SetScriptExecutionDisabled, emulation.SetScriptExecutionDisabledParams{Value: true})
99 return e
100 }))
101 run(chromedp.Navigate(origin+"/signin"), browsertest.Wait(`!!document.querySelector('a[href^="/signin/oficina"]')`))
102 capture := func(name string) {
103 for _, width := range []int64{390, 1280} {
104 run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
105 _, e := cdp.Call(ctx, target, emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: width, Height: 900, DeviceScaleFactor: 1, Mobile: width == 390})
106 if e != nil {
107 return e
108 }
109 overflow, e := chromedp.Evaluate[bool](`document.documentElement.scrollWidth>innerWidth`)(ctx, target)
110 if e != nil {
111 return e
112 }
113 if overflow {
114 return fmt.Errorf("%s overflows at %d", name, width)
115 }
116 if dir := os.Getenv("EQUIPO_BROWSER_ARTIFACTS"); dir != "" {
117 shot, e := chromedp.CaptureScreenshot()(ctx, target)
118 if e != nil {
119 return e
120 }
121 if e = os.MkdirAll(dir, 0700); e != nil {
122 return e
123 }
124 return os.WriteFile(filepath.Join(dir, fmt.Sprintf("%s-%d.png", name, width)), shot, 0600)
125 }
126 return nil
127 }))
128 }
129 }
130 capture("oficina-id-door")
131 run(browsertest.Click(`a[href^="/signin/oficina"]`), browsertest.Wait(`location.origin==="https://id.example.org" && !!document.querySelector('#continue')`), browsertest.Click("#continue"), browsertest.Wait(`!!document.querySelector('#return')`), browsertest.Click("#return"), browsertest.Wait(`location.origin==="https://equipo.example.test" && location.pathname==="/conversation"`))
132 if callbacks.Load() != 1 {
133 t.Fatal("full callback did not run", callbacks.Load())
134 }
135 run(chromedp.Navigate(origin+"/conversation/settings/oficina-id"), browsertest.Wait(`document.body.textContent.includes("pr_aaaaaaaaaaaaaaaaaaaaaaaaaa")`))
136 capture("oficina-id-settings")
137 run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
138 body, e := chromedp.Evaluate[string](`document.body.textContent`)(ctx, target)
139 if e != nil {
140 return e
141 }
142 if strings.Contains(body, homeAddress) {
143 return fmt.Errorf("Home address leaked")
144 }
145 return nil
146 }))
147 // Local credentials remain valid after the browser signs in through Home.
148 if r := f.request("GET", "/work", "", nil, local); r.Code != 200 {
149 t.Fatal("email session changed", r.Code)
150 }
151}
152