| 1 | package docs |
| 2 | |
| 3 | import ( |
| 4 | "html" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "net/url" |
| 8 | "regexp" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | // browser is a cookie jar over the real router, asking for pages the way a |
| 14 | // browser does (Accept: text/html, the app's own Host). |
| 15 | type browser struct { |
| 16 | t *testing.T |
| 17 | app *App |
| 18 | jar map[string]*http.Cookie |
| 19 | } |
| 20 | |
| 21 | func newBrowser(t *testing.T, app *App) *browser { |
| 22 | return &browser{t: t, app: app, jar: map[string]*http.Cookie{}} |
| 23 | } |
| 24 | |
| 25 | func (b *browser) do(method, path, form string) *httptest.ResponseRecorder { |
| 26 | req := httptest.NewRequest(method, DefaultOrigin+path, strings.NewReader(form)) |
| 27 | req.Header.Set("Accept", "text/html") |
| 28 | req.Header.Set("Sec-Fetch-Site", "same-origin") |
| 29 | if form != "" { |
| 30 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 31 | } |
| 32 | for _, c := range b.jar { |
| 33 | req.AddCookie(c) |
| 34 | } |
| 35 | rec := httptest.NewRecorder() |
| 36 | b.app.Handler().ServeHTTP(rec, req) |
| 37 | for _, c := range rec.Result().Cookies() { |
| 38 | if c.MaxAge < 0 { |
| 39 | delete(b.jar, c.Name) |
| 40 | } else { |
| 41 | b.jar[c.Name] = c |
| 42 | } |
| 43 | } |
| 44 | return rec |
| 45 | } |
| 46 | |
| 47 | func (b *browser) signIn(address string) { |
| 48 | link, err := b.app.TestSignIn(b.t, address) |
| 49 | if err != nil { |
| 50 | b.t.Fatal(err) |
| 51 | } |
| 52 | verifyPath, verifyForm := confirmForm(link) |
| 53 | b.do("POST", verifyPath, verifyForm) |
| 54 | b.do("GET", localLinkCompletePath, "") |
| 55 | } |
| 56 | |
| 57 | // Review finding (Astra, #65): a guest whose last share is revoked still |
| 58 | // holds a valid session, and admission rightly refuses it — so GET / |
| 59 | // sends them to /signin. The already-signed-in shortcut must not send |
| 60 | // them straight back to /, or the two redirect to each other forever. |
| 61 | // Skipping the form needs a session the signed-in routes would admit. |
| 62 | func TestARevokedGuestIsNotBouncedBetweenTheListAndSignIn(t *testing.T) { |
| 63 | app, err := NewForTestWith(t, Config{AllowedAddresses: "alice@example.com"}) |
| 64 | if err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | alice := newBrowser(t, app) |
| 68 | alice.signIn("alice@example.com") |
| 69 | created := alice.do("POST", "/documents", "title=Minutes") |
| 70 | sqid := strings.TrimPrefix(created.Header().Get("Location"), "/d/") |
| 71 | if rec := alice.do("POST", "/d/"+sqid+"/share", "address=guest@example.com&role=reader"); rec.Code != http.StatusSeeOther { |
| 72 | t.Fatalf("share: %d", rec.Code) |
| 73 | } |
| 74 | |
| 75 | guest := newBrowser(t, app) |
| 76 | guest.signIn("guest@example.com") |
| 77 | if rec := guest.do("GET", "/d/"+sqid, ""); rec.Code != http.StatusOK { |
| 78 | t.Fatalf("the guest cannot read the shared document before revocation: %d", rec.Code) |
| 79 | } |
| 80 | // Already signed in and admitted: /signin goes straight back. |
| 81 | if rec := guest.do("GET", "/signin", ""); rec.Code != http.StatusSeeOther { |
| 82 | t.Fatalf("an admitted guest was shown the sign-in form again: %d", rec.Code) |
| 83 | } |
| 84 | |
| 85 | if rec := alice.do("POST", "/d/"+sqid+"/share/revoke", "address=guest@example.com"); rec.Code != http.StatusSeeOther { |
| 86 | t.Fatalf("revoke: %d", rec.Code) |
| 87 | } |
| 88 | list := guest.do("GET", "/", "") |
| 89 | if list.Code != http.StatusSeeOther || !strings.HasPrefix(list.Header().Get("Location"), "/signin") { |
| 90 | t.Fatalf("a revoked guest reached the list: %d %q", list.Code, list.Header().Get("Location")) |
| 91 | } |
| 92 | signin := guest.do("GET", list.Header().Get("Location"), "") |
| 93 | if signin.Code != http.StatusOK { |
| 94 | t.Fatalf("GET %s: %d to %q — the list and sign-in redirect to each other", list.Header().Get("Location"), signin.Code, signin.Header().Get("Location")) |
| 95 | } |
| 96 | if !strings.Contains(signin.Body.String(), `<form rst-signin-form method="post" action="/signin"`) { |
| 97 | t.Fatal("the revoked guest is not offered a way to sign in as someone else") |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Review finding (Astra, #65): "Use a different address" on the sent page |
| 102 | // went to a bare /signin, which a fresh start treats as "remember the |
| 103 | // list", so the deep link was lost. The retry keeps the remembered page: |
| 104 | // on the smart door it is "Use a different email", a POST to Forget that |
| 105 | // carries return_to back to the door. |
| 106 | func TestUseADifferentAddressKeepsTheDeepLink(t *testing.T) { |
| 107 | app, _ := NewForTest(t) |
| 108 | owner := newBrowser(t, app) |
| 109 | owner.signIn("alice@example.com") |
| 110 | created := owner.do("POST", "/documents", "title=Minutes") |
| 111 | deep := created.Header().Get("Location") + "?from=mail" |
| 112 | |
| 113 | b := newBrowser(t, app) |
| 114 | signin := b.do("GET", deep, "").Header().Get("Location") |
| 115 | door := b.do("GET", signin, "") |
| 116 | if !strings.Contains(door.Body.String(), `name="return_to" value="`+html.EscapeString(deep)+`"`) { |
| 117 | t.Fatalf("the door's form does not post the deep link %q", deep) |
| 118 | } |
| 119 | sent := b.do("POST", "/signin", url.Values{"address": {"typo@example.com"}, "return_to": {deep}}.Encode()) |
| 120 | sentPage := b.do("GET", sent.Header().Get("Location"), "") |
| 121 | m := regexp.MustCompile(`(?s)<form rst-signin-form method="post" action="(/signin/forget)">(.*?)</form>`).FindStringSubmatch(sentPage.Body.String()) |
| 122 | if m == nil || !strings.Contains(m[2], "Use a different email") { |
| 123 | t.Fatalf("no \"Use a different email\" on the sent page (%d)", sentPage.Code) |
| 124 | } |
| 125 | if !strings.Contains(m[2], `name="return_to" value="`+html.EscapeString(deep)+`"`) { |
| 126 | t.Fatalf("\"Use a different email\" does not keep the deep link: %s", m[2]) |
| 127 | } |
| 128 | again := b.do("POST", m[1], url.Values{"return_to": {deep}}.Encode()) |
| 129 | if again.Code != http.StatusSeeOther { |
| 130 | t.Fatalf("POST %s: %d", m[1], again.Code) |
| 131 | } |
| 132 | if rec := b.do("GET", again.Header().Get("Location"), ""); rec.Code != http.StatusOK { |
| 133 | t.Fatalf("GET %s: %d", again.Header().Get("Location"), rec.Code) |
| 134 | } |
| 135 | // The right address this time, in the same browser. |
| 136 | link, err := app.TestSignIn(t, "alice@example.com") |
| 137 | if err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | verifyPath, verifyForm := confirmForm(link) |
| 141 | b.do("POST", verifyPath, verifyForm) |
| 142 | landed := b.do("GET", localLinkCompletePath, "") |
| 143 | if landed.Header().Get("Location") != deep { |
| 144 | t.Fatalf("after \"Use a different email\" the sign-in landed on %q, want %q", landed.Header().Get("Location"), deep) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // The same loop through Home's /signin, which has the same shortcut: a |
| 149 | // guest admitted only by a share, signed in locally, then revoked. |
| 150 | func TestARevokedGuestIsNotBouncedThroughHomeSignIn(t *testing.T) { |
| 151 | f := newRouterHomeFixture(t) |
| 152 | alice := f.browser() |
| 153 | alice.localSignin(t, "alice@example.com") |
| 154 | created := alice.do("POST", "/documents", "title=Minutes") |
| 155 | sqid := strings.TrimPrefix(created.Header().Get("Location"), "/d/") |
| 156 | if rec := alice.do("POST", "/d/"+sqid+"/share", "address=carol@example.com&role=reader"); rec.Code != http.StatusSeeOther { |
| 157 | t.Fatalf("share: %d", rec.Code) |
| 158 | } |
| 159 | carol := f.browser() |
| 160 | carol.localSignin(t, "carol@example.com") |
| 161 | if rec := carol.do("GET", "/signin", ""); rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/" { |
| 162 | t.Fatalf("an admitted guest was not sent straight back: %d %q", rec.Code, rec.Header().Get("Location")) |
| 163 | } |
| 164 | if rec := alice.do("POST", "/d/"+sqid+"/share/revoke", "address=carol@example.com"); rec.Code != http.StatusSeeOther { |
| 165 | t.Fatalf("revoke: %d", rec.Code) |
| 166 | } |
| 167 | list := carol.do("GET", "/", "") |
| 168 | if list.Code != http.StatusSeeOther || !strings.HasPrefix(list.Header().Get("Location"), "/signin") { |
| 169 | t.Fatalf("a revoked guest reached the list: %d %q", list.Code, list.Header().Get("Location")) |
| 170 | } |
| 171 | next := carol.do("GET", list.Header().Get("Location"), "") |
| 172 | if next.Code == http.StatusSeeOther && next.Header().Get("Location") == "/" { |
| 173 | t.Fatal("Home's /signin sent a refused session straight back to / — the two redirect to each other") |
| 174 | } |
| 175 | // The door, saying why: the refusal in the app-problem slot, with the |
| 176 | // email field rather than carol's one-tap. |
| 177 | if body := next.Body.String(); next.Code != http.StatusOK || !strings.Contains(body, `id="rst-signin-app-problem"`) || !strings.Contains(body, "docs.signin.error.refused") || !strings.Contains(body, `id="rst-signin-email" name="address"`) || strings.Contains(body, "Continue as") { |
| 178 | t.Fatalf("a refused session is not shown the door with the refusal: %d\n%s", next.Code, body) |
| 179 | } |
| 180 | // Continue with Oficina ID has the same shortcut, and the same check. |
| 181 | if rec := carol.do("GET", "/signin/oficina", ""); rec.Code == http.StatusSeeOther && rec.Header().Get("Location") == "/" { |
| 182 | t.Fatal("/signin/oficina sent a refused session straight back to /") |
| 183 | } |
| 184 | } |
| 185 | |