| 1 | package grant |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "regexp" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | func TestBrowserConnectionsAreSubjectBoundAndRevocationIsSingleUse(t *testing.T) { |
| 14 | s, ts, _ := setup(t) |
| 15 | for _, g := range []Grant{{ID: "own", Subject: "opaque-person", ClientID: "oficina-cli", Teams: []string{"acme"}, Scopes: []string{"notes:write"}}, {ID: "other-person-grant", Subject: "other-person", ClientID: "oficina-cli", Teams: []string{"private-other-team"}, Scopes: []string{"notes:write"}}} { |
| 16 | if _, err := s.cfg.Store.CreateCode(t.Context(), g, Code{}); err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | } |
| 20 | c := ts.Client() |
| 21 | c.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse } |
| 22 | fetch := func() (string, []*http.Cookie) { |
| 23 | t.Helper() |
| 24 | r, _ := http.NewRequest("GET", ts.URL+"/connections", nil) |
| 25 | r.Header.Set("Test-Session", "signed-in") |
| 26 | resp, err := c.Do(r) |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | defer resp.Body.Close() |
| 31 | raw, _ := io.ReadAll(resp.Body) |
| 32 | if resp.StatusCode != 200 || strings.Contains(string(raw), "private-other-team") || strings.Contains(string(raw), "other-person-grant") { |
| 33 | t.Fatal("connection list leaked or failed", resp.StatusCode) |
| 34 | } |
| 35 | ticket := regexp.MustCompile(`name="ticket" value="([^"]+)"`).FindSubmatch(raw) |
| 36 | if len(ticket) != 2 { |
| 37 | t.Fatal("missing ticket") |
| 38 | } |
| 39 | return string(ticket[1]), resp.Cookies() |
| 40 | } |
| 41 | post := func(path, encoded, id, origin, session string, cookies []*http.Cookie) int { |
| 42 | t.Helper() |
| 43 | form := url.Values{"ticket": {encoded}, "id": {id}, "decision": {"approve"}, "team": {"acme"}} |
| 44 | r, _ := http.NewRequest("POST", ts.URL+path, strings.NewReader(form.Encode())) |
| 45 | r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 46 | r.Header.Set("Origin", origin) |
| 47 | r.Header.Set("Test-Session", session) |
| 48 | for _, cookie := range cookies { |
| 49 | r.AddCookie(cookie) |
| 50 | } |
| 51 | resp, err := c.Do(r) |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | resp.Body.Close() |
| 56 | return resp.StatusCode |
| 57 | } |
| 58 | ticket, cookies := fetch() |
| 59 | if code := post("/oauth/authorize", ticket, "own", ts.URL, "signed-in", cookies); code != 403 { |
| 60 | t.Fatal("revocation ticket authorized a grant", code) |
| 61 | } |
| 62 | if code := post("/connections/revoke", ticket, "own", "https://evil.test", "signed-in", cookies); code != 403 { |
| 63 | t.Fatal("foreign origin", code) |
| 64 | } |
| 65 | if code := post("/connections/revoke", ticket, "own", ts.URL, "", cookies); code != 403 { |
| 66 | t.Fatal("missing session", code) |
| 67 | } |
| 68 | if code := post("/connections/revoke", ticket, "other-person-grant", ts.URL, "signed-in", cookies); code != 303 { |
| 69 | t.Fatal("revoke response", code) |
| 70 | } |
| 71 | others, err := s.cfg.Store.Connections(context.Background(), "other-person") |
| 72 | if err != nil || len(others) != 1 { |
| 73 | t.Fatal("cross-person revocation", err) |
| 74 | } |
| 75 | ticket, cookies = fetch() |
| 76 | if code := post("/connections/revoke", ticket, "own", ts.URL, "signed-in", cookies); code != 303 { |
| 77 | t.Fatal("revoke", code) |
| 78 | } |
| 79 | if code := post("/connections/revoke", ticket, "own", ts.URL, "signed-in", cookies); code != 400 { |
| 80 | t.Fatal("replayed form accepted", code) |
| 81 | } |
| 82 | own, err := s.cfg.Store.Connections(context.Background(), "opaque-person") |
| 83 | if err != nil || len(own) != 0 { |
| 84 | t.Fatal("own grant survived", err) |
| 85 | } |
| 86 | resp, err := c.Get(ts.URL + "/connections") |
| 87 | if err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | resp.Body.Close() |
| 91 | if resp.StatusCode != 303 || !strings.HasPrefix(resp.Header.Get("Location"), "/signin?") { |
| 92 | t.Fatal("anonymous management", resp.StatusCode) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestConnectionsShowConsentLabelsNotScopeCodes(t *testing.T) { |
| 97 | s, ts, _ := setup(t) |
| 98 | g := Grant{ID: "labelled", Subject: "opaque-person", ClientID: "oficina-cli", Teams: []string{"acme"}, Scopes: []string{"notes:write", "offline_access", "mystery:scope"}} |
| 99 | if _, err := s.cfg.Store.CreateCode(t.Context(), g, Code{}); err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | r, _ := http.NewRequest("GET", ts.URL+"/connections", nil) |
| 103 | r.Header.Set("Test-Session", "signed-in") |
| 104 | resp, err := ts.Client().Do(r) |
| 105 | if err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | defer resp.Body.Close() |
| 109 | raw, _ := io.ReadAll(resp.Body) |
| 110 | page := string(raw) |
| 111 | for _, label := range []string{"Create and edit notes", "Keep this connection"} { |
| 112 | if !strings.Contains(page, "<li>"+label+"</li>") { |
| 113 | t.Fatalf("missing label %q in %s", label, page) |
| 114 | } |
| 115 | } |
| 116 | for _, code := range []string{"notes:write", "offline_access"} { |
| 117 | if strings.Contains(page, code) { |
| 118 | t.Fatalf("known scope code %q shown", code) |
| 119 | } |
| 120 | } |
| 121 | if !strings.Contains(page, "<li>mystery:scope</li>") { |
| 122 | t.Fatal("unknown scope did not fall back to its code") |
| 123 | } |
| 124 | } |
| 125 | |