| 1 | //go:build browser |
| 2 | |
| 3 | package equipotest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "fmt" |
| 8 | "net/http" |
| 9 | "net/http/httptest" |
| 10 | "net/url" |
| 11 | "strings" |
| 12 | "sync/atomic" |
| 13 | "testing" |
| 14 | "time" |
| 15 | |
| 16 | "amadan.net/oficina/equipo/internal/browsertest" |
| 17 | "amadan.net/oficina/equipo/internal/equipo" |
| 18 | "amadan.net/oficina/equipo/internal/runner/forge/forgetest" |
| 19 | "amadan.net/rastrillo/rastrillo" |
| 20 | "amadan.net/rastrillo/rastrillo/harness" |
| 21 | "github.com/chromedp/cdproto/cdp" |
| 22 | "github.com/chromedp/cdproto/network" |
| 23 | "github.com/chromedp/chromedp" |
| 24 | ) |
| 25 | |
| 26 | func TestBrowserGitHubManifestCSP(t *testing.T) { |
| 27 | const origin = "https://equipo.example.test" |
| 28 | d, logger := foundationDB(t) |
| 29 | mail := &capturingMailer{} |
| 30 | g := forgetest.NewGitHub(t) |
| 31 | cfg := testConfig(origin) |
| 32 | cfg.AllowLoggedMail = false |
| 33 | cfg.MailConfigured = true |
| 34 | cfg.GitHubEnabled = true |
| 35 | cfg.GitHubTransport = g.Transport() |
| 36 | router, e := equipo.Router(d, cfg, mail, logger) |
| 37 | if e != nil { |
| 38 | t.Fatal(e) |
| 39 | } |
| 40 | handler, closeHandler, e := rastrillo.Handler(rastrillo.Options{Mux: equipo.Mux(router), Logger: logger}) |
| 41 | if e != nil { |
| 42 | t.Fatal(e) |
| 43 | } |
| 44 | defer closeHandler() |
| 45 | var posts atomic.Int32 |
| 46 | server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 47 | if r.Host == "github.com" { |
| 48 | if r.Method == "POST" && strings.HasSuffix(r.URL.Path, "/settings/apps/new") { |
| 49 | posts.Add(1) |
| 50 | if r.ParseForm() != nil || r.Form.Get("manifest") == "" { |
| 51 | t.Error("manifest POST missing") |
| 52 | } |
| 53 | http.Redirect(w, r, origin+"/github/connect/manifest?"+url.Values{"code": {g.Code}, "state": {r.URL.Query().Get("state")}}.Encode(), http.StatusSeeOther) |
| 54 | return |
| 55 | } |
| 56 | if strings.HasSuffix(r.URL.Path, "/installations/new") { |
| 57 | http.Redirect(w, r, origin+"/github/connect/installation?"+url.Values{"state": {r.URL.Query().Get("state")}, "installation_id": {"999999"}}.Encode(), http.StatusSeeOther) |
| 58 | return |
| 59 | } |
| 60 | http.NotFound(w, r) |
| 61 | return |
| 62 | } |
| 63 | handler.ServeHTTP(w, r) |
| 64 | })) |
| 65 | defer server.Close() |
| 66 | client := signinClient(t, server.URL) |
| 67 | client.Transport = server.Client().Transport |
| 68 | signinEmail(t, client, server.URL, origin, mail, ownerAddress) |
| 69 | source, _ := url.Parse(server.URL) |
| 70 | opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.ExecPath(harness.ChromePath(t)), chromedp.NoSandbox, chromedp.UserDataDir(t.TempDir()), chromedp.Flag("ignore-certificate-errors", true), chromedp.Flag("no-proxy-server", true), chromedp.Flag("host-resolver-rules", "MAP equipo.example.test "+source.Host+", MAP github.com "+source.Host)) |
| 71 | alloc, stop := chromedp.NewExecAllocator(context.Background(), opts...) |
| 72 | defer stop() |
| 73 | ctx, cancel := chromedp.NewContext(alloc) |
| 74 | defer cancel() |
| 75 | ctx, deadline := context.WithTimeout(ctx, 45*time.Second) |
| 76 | defer deadline() |
| 77 | run := func(actions ...chromedp.Action[chromedp.Void]) { |
| 78 | t.Helper() |
| 79 | if err := chromedp.Do(ctx, actions...); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | } |
| 83 | yes := true |
| 84 | for _, cookie := range client.Jar.Cookies(source) { |
| 85 | run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 86 | _, e := cdp.Call(ctx, target, network.SetCookie, network.SetCookieParams{Name: cookie.Name, Value: cookie.Value, URL: origin + "/", Path: "/", Secure: &yes, HTTPOnly: &yes}) |
| 87 | return e |
| 88 | })) |
| 89 | } |
| 90 | run(chromedp.Navigate(origin+"/conversation/settings/github"), browsertest.Wait(`!!document.querySelector('#github-account')`)) |
| 91 | run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 92 | _, e := chromedp.Evaluate[any](fmt.Sprintf(`document.querySelector('#github-account').value=%q`, g.Login))(ctx, target) |
| 93 | return e |
| 94 | }), browsertest.Click("#github-start button"), browsertest.Wait(`(()=>{const b=document.querySelector('form[action^="https://github.com/"] button');if(!b)return false;const r=b.getBoundingClientRect();return document.elementFromPoint(r.x+r.width/2,r.y+r.height/2)===b})()`), browsertest.Click(`form[action^="https://github.com/"] button`), browsertest.Wait(`location.origin==="https://equipo.example.test" && location.pathname==="/conversation/settings/github" && location.search==="" && !!document.querySelector('form[action="/github/connect/confirm"]')`)) |
| 95 | if posts.Load() != 1 { |
| 96 | t.Fatal("manifest POST did not reach GitHub") |
| 97 | } |
| 98 | // A normal Settings document still cannot submit to GitHub. |
| 99 | run(chromedp.Navigate(origin+"/conversation/settings/thinking"), browsertest.Wait(`!!document.querySelector('form')`), chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 100 | _, e := chromedp.Evaluate[any](`(()=>{window.addEventListener('securitypolicyviolation',()=>window.githubBlocked=true,{once:true});const f=document.createElement('form');f.method='post';f.action='https://github.com/organizations/test/settings/apps/new';document.body.append(f);f.submit()})()`)(ctx, target) |
| 101 | return e |
| 102 | })) |
| 103 | run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 104 | current, e := chromedp.Evaluate[string](`location.origin+location.pathname`)(ctx, target) |
| 105 | if e != nil { |
| 106 | return e |
| 107 | } |
| 108 | if current != origin+"/conversation/settings/thinking" { |
| 109 | return fmt.Errorf("unrelated Settings page submitted externally") |
| 110 | } |
| 111 | return nil |
| 112 | })) |
| 113 | run(browsertest.Wait(`window.githubBlocked===true`)) |
| 114 | if posts.Load() != 1 { |
| 115 | t.Fatal("CSP exception spread to other Settings") |
| 116 | } |
| 117 | } |
| 118 | |