| 1 | package memoria |
| 2 | |
| 3 | // The return memory across the door (return.go): the page a sign-in began |
| 4 | // from, kept Memoria's way so the landing (/auth/landed standalone, |
| 5 | // connect's /auth/local/complete with Home, where a pending account link |
| 6 | // resumes) is never skipped, with every protection the earlier reviews |
| 7 | // asked for. |
| 8 | |
| 9 | import ( |
| 10 | "net/http" |
| 11 | "net/http/httptest" |
| 12 | "net/url" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | "time" |
| 16 | |
| 17 | "amadan.net/oficina/home/connect" |
| 18 | ) |
| 19 | |
| 20 | // The door's form posts return_to; Begin never sees it; the sign-in lands |
| 21 | // on the page through the landing. |
| 22 | func TestTheDoorLandsOnThePageItBeganFrom(t *testing.T) { |
| 23 | doorModes(t, func(t *testing.T, f *homeRouterFixture) { |
| 24 | page := f.get("/signin?return_to=%2Fconnectors") |
| 25 | // The one way in carries the page: the email form standalone, and |
| 26 | // Continue with Oficina ID with Home. |
| 27 | if body := page.Body.String(); !strings.Contains(body, `name="return_to" value="/connectors"`) && !strings.Contains(body, `/signin/oficina?return_to=%2Fconnectors`) { |
| 28 | t.Fatalf("the door does not carry the page:\n%s", door(t, body)) |
| 29 | } |
| 30 | if got := f.emailSignin(t, url.Values{"address": {"alice@example.test"}, "return_to": {"/connectors"}}); got != "/connectors" { |
| 31 | t.Fatalf("landed on %q, want /connectors", got) |
| 32 | } |
| 33 | }) |
| 34 | } |
| 35 | |
| 36 | // Begin's redirect to the door gets return_to back, so the next form posts |
| 37 | // it again; and Begin itself is never handed it (rastrillo would land the |
| 38 | // session there, skipping the landing). |
| 39 | func TestBeginNeverSeesReturnToButTheDoorKeepsIt(t *testing.T) { |
| 40 | doorModes(t, func(t *testing.T, f *homeRouterFixture) { |
| 41 | sent := f.do(http.MethodPost, "/auth/begin", url.Values{"address": {"alice@example.test"}, "force": {"1"}, "return_to": {"/connectors"}}.Encode()) |
| 42 | loc := sent.Header().Get("Location") |
| 43 | if u, _ := url.Parse(loc); u == nil || u.Query().Get("return_to") != "/connectors" || u.Query().Get("sent") != "1" { |
| 44 | t.Fatalf("Sent = %q, want the page carried", loc) |
| 45 | } |
| 46 | if f.returnPath() != "/connectors" { |
| 47 | t.Fatalf("the return memory holds %q", f.returnPath()) |
| 48 | } |
| 49 | // rastrillo's own kept destination is never set. |
| 50 | for _, c := range sent.Result().Cookies() { |
| 51 | if strings.Contains(c.Name, "rastrillo_return") && c.MaxAge >= 0 && c.Value != "" { |
| 52 | t.Fatalf("Begin kept a destination of its own: %s", c.Name) |
| 53 | } |
| 54 | } |
| 55 | // The Sent page's "Use a different email" carries it on. |
| 56 | if body := f.get(loc).Body.String(); !strings.Contains(body, `name="return_to" value="/connectors"`) { |
| 57 | t.Fatalf("Sent's Use a different email loses the page:\n%s", door(t, body)) |
| 58 | } |
| 59 | for _, address := range []string{"not-an-email@", "paul@localhost"} { |
| 60 | bad := f.do(http.MethodPost, "/auth/begin", url.Values{"address": {address}, "return_to": {"/connectors"}}.Encode()) |
| 61 | if u, _ := url.Parse(bad.Header().Get("Location")); u == nil || u.Query().Get("return_to") != "/connectors" { |
| 62 | t.Fatalf("%s = %q, want the page carried", address, bad.Header().Get("Location")) |
| 63 | } |
| 64 | } |
| 65 | }) |
| 66 | } |
| 67 | |
| 68 | // A post without return_to is a sign-in for the landing's own "/": a page |
| 69 | // an earlier, abandoned attempt kept does not win. |
| 70 | func TestAPostWithoutReturnToForgetsTheKeptPage(t *testing.T) { |
| 71 | doorModes(t, func(t *testing.T, f *homeRouterFixture) { |
| 72 | f.get("/signin?return_to=%2Fconnectors") |
| 73 | if f.returnPath() != "/connectors" { |
| 74 | t.Fatalf("the door did not keep the page: %q", f.returnPath()) |
| 75 | } |
| 76 | if got := f.emailSignin(t, url.Values{"address": {"alice@example.test"}}); got != "/" { |
| 77 | t.Fatalf("landed on %q, want /", got) |
| 78 | } |
| 79 | }) |
| 80 | } |
| 81 | |
| 82 | // Off the form even when the body does not parse: nothing reaches Begin, |
| 83 | // and nothing is kept. |
| 84 | func TestReturnToComesOffEvenOnAParseError(t *testing.T) { |
| 85 | doorModes(t, func(t *testing.T, f *homeRouterFixture) { |
| 86 | f.get("/signin?return_to=%2Fconnectors") |
| 87 | w := f.do(http.MethodPost, "/auth/begin", "return_to=%2Fconnectors&address=%zz") |
| 88 | if strings.Contains(w.Header().Get("Location"), "return_to") || f.returnPath() != "" { |
| 89 | t.Fatalf("a malformed post kept the page: %q, memory %q", w.Header().Get("Location"), f.returnPath()) |
| 90 | } |
| 91 | }) |
| 92 | } |
| 93 | |
| 94 | // A plain /signin forgets a kept page; a plain /signin/local (where connect |
| 95 | // sends people mid-way through a sign-in) keeps it and carries it on its |
| 96 | // form. |
| 97 | func TestAPlainSigninLocalCarriesTheKeptPage(t *testing.T) { |
| 98 | f := newHomeRouterFixture(t) |
| 99 | f.get("/signin?return_to=%2Fconnectors") |
| 100 | page := f.get("/signin/local") |
| 101 | if !strings.Contains(page.Body.String(), `name="return_to" value="/connectors"`) || f.returnPath() != "/connectors" { |
| 102 | t.Fatalf("/signin/local lost the page (memory %q):\n%s", f.returnPath(), door(t, page.Body.String())) |
| 103 | } |
| 104 | f.get("/signin") |
| 105 | if f.returnPath() != "" { |
| 106 | t.Fatalf("a plain /signin kept %q", f.returnPath()) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Only a same-origin page is ever kept: anything else, and the signed-out |
| 111 | // page, read as no page. |
| 112 | func TestOnlyASafePageIsKept(t *testing.T) { |
| 113 | doorModes(t, func(t *testing.T, f *homeRouterFixture) { |
| 114 | for _, bad := range []string{"//evil.example/x", "https://evil.example/", `/\evil.example`, "/signed-out", "/signed-out?x=1", "/signin", "/auth/verify"} { |
| 115 | br := f.fresh() |
| 116 | br.get("/signin?return_to=" + url.QueryEscape(bad)) |
| 117 | if br.returnPath() != "" { |
| 118 | t.Errorf("GET /signin?return_to=%s kept %q", bad, br.returnPath()) |
| 119 | } |
| 120 | br.do(http.MethodPost, "/auth/begin", url.Values{"address": {"alice@example.test"}, "force": {"1"}, "return_to": {bad}}.Encode()) |
| 121 | if br.returnPath() != "" { |
| 122 | t.Errorf("POST return_to=%s kept %q", bad, br.returnPath()) |
| 123 | } |
| 124 | w := httptest.NewRecorder() |
| 125 | f.app.rememberReturn(w, bad) |
| 126 | if len(w.Result().Cookies()) != 0 { |
| 127 | t.Errorf("rememberReturn(%q) set a cookie", bad) |
| 128 | } |
| 129 | } |
| 130 | }) |
| 131 | } |
| 132 | |
| 133 | // A tampered, expired or duplicated memory reads as none. |
| 134 | func TestABrokenMemoryReadsAsNone(t *testing.T) { |
| 135 | f := newStandaloneDoor(t) |
| 136 | w := httptest.NewRecorder() |
| 137 | f.app.rememberReturn(w, "/connectors") |
| 138 | kept := w.Result().Cookies()[0] |
| 139 | read := func(cookies ...*http.Cookie) string { |
| 140 | r := httptest.NewRequest(http.MethodGet, homeTestOrigin+"/", nil) |
| 141 | for _, c := range cookies { |
| 142 | r.AddCookie(c) |
| 143 | } |
| 144 | p, _ := f.app.rememberedReturn(r) |
| 145 | return p |
| 146 | } |
| 147 | if read(kept) != "/connectors" { |
| 148 | t.Fatal("the memory does not read back") |
| 149 | } |
| 150 | tampered := *kept |
| 151 | tampered.Value = kept.Value[:len(kept.Value)-2] + "AA" |
| 152 | if read(&tampered) != "" || read(kept, kept) != "" { |
| 153 | t.Fatal("a tampered or duplicated memory was read") |
| 154 | } |
| 155 | f.app.clock = func() time.Time { return time.Now().Add(time.Hour) } |
| 156 | if read(kept) != "" { |
| 157 | t.Fatal("an expired memory was read") |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Every landing spends the kept page before it answers, so the clearing |
| 162 | // cookie is on the landing's own response (Calls' /continue, where it was |
| 163 | // written after the redirect and never sent: Fable on oficina/meet #46), |
| 164 | // and a second landing goes to the index. |
| 165 | func TestTheLandingSpendsTheKeptPage(t *testing.T) { |
| 166 | doorModes(t, func(t *testing.T, f *homeRouterFixture) { |
| 167 | landing := landedPath |
| 168 | if f.app.home != nil { |
| 169 | landing = "/auth/local/complete" |
| 170 | f.session(t, "alice@example.test", "magiclink", time.Now()) |
| 171 | } |
| 172 | f.get("/signin/local?return_to=%2Fconnectors") |
| 173 | if f.app.home == nil { |
| 174 | f.get("/signin?return_to=%2Fconnectors") |
| 175 | } |
| 176 | if f.returnPath() != "/connectors" { |
| 177 | t.Fatalf("nothing kept: %q", f.returnPath()) |
| 178 | } |
| 179 | first := f.get(landing) |
| 180 | if first.Header().Get("Location") != "/connectors" || !clears(first, f.app.returnCookieName()) { |
| 181 | t.Fatalf("the landing = %d %q, clears %v; want the page, spent on this response", first.Code, first.Header().Get("Location"), clears(first, f.app.returnCookieName())) |
| 182 | } |
| 183 | if got := f.get(landing).Header().Get("Location"); got != "/" { |
| 184 | t.Fatalf("a second landing went to %q, want /", got) |
| 185 | } |
| 186 | }) |
| 187 | } |
| 188 | |
| 189 | // Forget and Sign out end a kept page: it was this person's, not the next |
| 190 | // one's. |
| 191 | func TestForgetAndSignOutClearTheKeptPage(t *testing.T) { |
| 192 | doorModes(t, func(t *testing.T, f *homeRouterFixture) { |
| 193 | f.get("/signin?return_to=%2Fconnectors") |
| 194 | w := f.do(http.MethodPost, signinForgetPath, "") |
| 195 | if !clears(w, f.app.returnCookieName()) { |
| 196 | t.Fatal("Forget kept the page") |
| 197 | } |
| 198 | f.get("/signin?return_to=%2Fconnectors") |
| 199 | f.session(t, "alice@example.test", "magiclink", time.Now()) |
| 200 | w = f.do(http.MethodPost, "/signout", "") |
| 201 | if !clears(w, f.app.returnCookieName()) { |
| 202 | t.Fatal("Sign out kept the page") |
| 203 | } |
| 204 | }) |
| 205 | } |
| 206 | |
| 207 | // The link step-up end to end: a stale local session asked to connect its |
| 208 | // Oficina account goes to the email-only door with reauth=1, a bad address |
| 209 | // and Sent both keep it and the page, and the fresh sign-in lands back on |
| 210 | // the link page. |
| 211 | func TestTheLinkStepUpStaysLocalEndToEnd(t *testing.T) { |
| 212 | f := newHomeRouterFixture(t) |
| 213 | f.session(t, "alice@example.test", "magiclink", time.Now().Add(-time.Hour)) |
| 214 | step := f.get("/account/home") |
| 215 | loc := step.Header().Get("Location") |
| 216 | if step.Code != http.StatusSeeOther || !strings.HasPrefix(loc, "/signin/local?") || !strings.Contains(loc, "reauth=1") { |
| 217 | t.Fatalf("a stale session at /account/home = %d %q, want the local door with reauth=1", step.Code, loc) |
| 218 | } |
| 219 | card := door(t, f.get(loc).Body.String()) |
| 220 | if strings.Contains(card, oficinaButton) || !strings.Contains(card, `name="reauth" value="1"`) { |
| 221 | t.Fatalf("the step-up door:\n%s", card) |
| 222 | } |
| 223 | f.mail.body = "" |
| 224 | for _, address := range []string{"not-an-email@", "alice@example.test"} { |
| 225 | w := f.do(http.MethodPost, "/auth/begin", url.Values{"address": {address}, "force": {"1"}, "reauth": {"1"}, "return_to": {"/account/home"}}.Encode()) |
| 226 | u, _ := url.Parse(w.Header().Get("Location")) |
| 227 | if u == nil || u.Query().Get("reauth") != "1" || u.Query().Get("return_to") != "/account/home" { |
| 228 | t.Fatalf("POST %s = %q, want reauth=1 and the page kept", address, w.Header().Get("Location")) |
| 229 | } |
| 230 | if card := door(t, f.get(w.Header().Get("Location")).Body.String()); strings.Contains(card, oficinaButton) { |
| 231 | t.Fatalf("the step-up's next door offers Oficina ID:\n%s", card) |
| 232 | } |
| 233 | } |
| 234 | link := signinLink.FindString(f.mail.body) |
| 235 | if link == "" { |
| 236 | t.Fatal("no link") |
| 237 | } |
| 238 | u, _ := url.Parse(link) |
| 239 | next := homeRedirect(t, f.openLink(t, u.RequestURI())) |
| 240 | for range 3 { |
| 241 | if next == "/account/home" { |
| 242 | break |
| 243 | } |
| 244 | next = homeRedirect(t, f.get(next)) |
| 245 | } |
| 246 | if next != "/account/home" { |
| 247 | t.Fatalf("the fresh sign-in landed on %q", next) |
| 248 | } |
| 249 | if w := f.get("/account/home"); w.Code != http.StatusOK { |
| 250 | t.Fatalf("the link page after the step-up = %d", w.Code) |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // A finished Oficina ID exchange (/auth/finish) and the member choice's |
| 255 | // "I'm new here" spend a page a local sign-in kept, on the response that |
| 256 | // mints the session, as every landing does; an answer that sends the |
| 257 | // browser on to the choice, or "Connect my existing account" to the |
| 258 | // email-only door, keeps it (Fable on #49, finding 1). |
| 259 | func TestAFinishedExchangeSpendsTheKeptPage(t *testing.T) { |
| 260 | f := newHomeRouterFixture(t) |
| 261 | f.link(t) |
| 262 | f.signOut(t) |
| 263 | f.cookies = map[string]*http.Cookie{} |
| 264 | f.get("/signin/local?return_to=%2Fconnectors") |
| 265 | if f.returnPath() != "/connectors" { |
| 266 | t.Fatalf("nothing kept: %q", f.returnPath()) |
| 267 | } |
| 268 | fin := f.oficinaSignin(t, "/signin/oficina", "opaque-home-alice") |
| 269 | if fin.Code != http.StatusSeeOther || !sets(fin, f.app.sessionCookie()) || !clears(fin, f.app.returnCookieName()) { |
| 270 | t.Fatalf("/auth/finish = %d, mints %v, clears %v; want the kept page spent with the session", fin.Code, sets(fin, f.app.sessionCookie()), clears(fin, f.app.returnCookieName())) |
| 271 | } |
| 272 | if f.returnPath() != "" { |
| 273 | t.Fatalf("the kept page survived the exchange: %q", f.returnPath()) |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | func TestTeamTheChoiceSpendsTheKeptPageOnlyWhenItSignsIn(t *testing.T) { |
| 278 | for _, c := range []struct { |
| 279 | choice string |
| 280 | spent bool |
| 281 | }{{"new", true}, {"connect", false}} { |
| 282 | t.Run(c.choice, func(t *testing.T) { |
| 283 | f := newTeamFixture(t, "carol@example.test") |
| 284 | subject := teamSubject('k') |
| 285 | f.dir.Set(subject, "member") |
| 286 | f.refreshed() |
| 287 | f.cookies = map[string]*http.Cookie{} |
| 288 | f.get("/signin/local?return_to=%2Fconnectors") |
| 289 | fin := f.signInChoosing(subject) |
| 290 | redirectsTo(t, fin, connect.ChoosePath) |
| 291 | if clears(fin, f.app.returnCookieName()) || f.returnPath() != "/connectors" { |
| 292 | t.Fatal("/auth/finish spent the kept page on its way to the choice") |
| 293 | } |
| 294 | w := f.choose(c.choice) |
| 295 | if got := clears(w, f.app.returnCookieName()); got != c.spent { |
| 296 | t.Fatalf("choice %s: clears the kept page %v, want %v (%d %q)", c.choice, got, c.spent, w.Code, w.Header().Get("Location")) |
| 297 | } |
| 298 | if (f.returnPath() == "") != c.spent { |
| 299 | t.Fatalf("choice %s: memory now %q", c.choice, f.returnPath()) |
| 300 | } |
| 301 | }) |
| 302 | } |
| 303 | } |
| 304 | |