| 1 | package slides |
| 2 | |
| 3 | // The return memory, and the step-up, across the door. |
| 4 | |
| 5 | import ( |
| 6 | "encoding/base64" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "net/url" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/oficina/home/connect" |
| 15 | "amadan.net/rastrillo/rastrillo/sessions" |
| 16 | ) |
| 17 | |
| 18 | // The return memory, in both modes: a local sign-in begun from a page |
| 19 | // lands on it after the landing (/auth/local/complete with Home, where |
| 20 | // connect could resume a link; /auth/landed standalone), once; one begun |
| 21 | // from the plain door lands on the list. |
| 22 | func TestALocalSigninLandsOnThePageItBeganFrom(t *testing.T) { |
| 23 | for _, mk := range []func(*testing.T) (*homeBrowser, User){newHomeBrowser, newStandaloneBrowser} { |
| 24 | b, u := mk(t) |
| 25 | from := "/signin?return_to=%2Fd%2F7" |
| 26 | if b.a.home != nil { |
| 27 | from = "/signin/local?return_to=%2Fd%2F7" |
| 28 | } |
| 29 | card := door(t, b.get(from).Body.String()) |
| 30 | if !strings.Contains(card, `name="return_to" value="/d/7"`) { |
| 31 | t.Fatalf("the door's form does not post the page:\n%s", card) |
| 32 | } |
| 33 | if landed := b.emailSignin(url.Values{"address": {u.Email}, "return_to": {"/d/7"}}); landed != "/d/7" { |
| 34 | t.Fatalf("home=%v: landed on %q, want /d/7", b.a.home != nil, landed) |
| 35 | } |
| 36 | if _, kept := b.jar[returnCookie]; kept { |
| 37 | t.Fatal("the landing kept the remembered page") |
| 38 | } |
| 39 | c := b.browser() |
| 40 | c.get("/signin") |
| 41 | if landed := c.emailSignin(url.Values{"address": {u.Email}}); landed != "/" { |
| 42 | t.Fatalf("home=%v: a plain sign-in landed on %q, want /", b.a.home != nil, landed) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // A plain /signin/local (connect's failed, unknown and connect pages send |
| 48 | // people there) keeps the page the sign-in began from: it remembers |
| 49 | // nothing new, and its form posts the remembered page. |
| 50 | func TestSigninLocalKeepsTheRememberedPage(t *testing.T) { |
| 51 | b, _ := newHomeBrowser(t) |
| 52 | b.get("/signin?return_to=%2Fd%2F7") |
| 53 | kept := b.jar[returnCookie] |
| 54 | if kept == nil { |
| 55 | t.Fatal("the door did not remember the page") |
| 56 | } |
| 57 | rec := b.get("/signin/local") |
| 58 | for _, c := range rec.Result().Cookies() { |
| 59 | if c.Name == returnCookie { |
| 60 | t.Fatal("a plain /signin/local overwrote the remembered page") |
| 61 | } |
| 62 | } |
| 63 | if card := door(t, rec.Body.String()); !strings.Contains(card, `name="return_to" value="/d/7"`) { |
| 64 | t.Fatalf("/signin/local does not carry the remembered page:\n%s", card) |
| 65 | } |
| 66 | // With its own return_to it remembers that. |
| 67 | b.get("/signin/local?return_to=%2Fd%2F9") |
| 68 | if b.returnPath() != "/d/9" { |
| 69 | t.Fatalf("/signin/local?return_to= remembered %q", b.returnPath()) |
| 70 | } |
| 71 | // A plain /signin is a fresh start: the list. |
| 72 | b.get("/signin") |
| 73 | if b.returnPath() != "/" { |
| 74 | t.Fatalf("a plain /signin kept %q", b.returnPath()) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // A post with no return_to is a sign-in for the list: a page another tab |
| 79 | // remembered does not win. |
| 80 | func TestAPostWithoutAReturnForgetsAnotherTabsPage(t *testing.T) { |
| 81 | for _, mk := range []func(*testing.T) (*homeBrowser, User){newHomeBrowser, newStandaloneBrowser} { |
| 82 | b, u := mk(t) |
| 83 | b.get("/signin?return_to=%2Fd%2F7") |
| 84 | if landed := b.emailSignin(url.Values{"address": {u.Email}}); landed != "/" { |
| 85 | t.Fatalf("home=%v: landed on %q, want /", b.a.home != nil, landed) |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // return_to comes off Begin's form whether or not the body parses: Begin |
| 91 | // must never read a destination of its own (Fable on Docs #139, A5). The |
| 92 | // body's last pair fails to parse, after a return_to the parser has |
| 93 | // already put on the form: Begin does not see it, and a body that did not |
| 94 | // parse is a sign-in for the list. |
| 95 | func TestReturnToIsTakenOffEvenWhenTheBodyFailsToParse(t *testing.T) { |
| 96 | b, _ := newHomeBrowser(t) |
| 97 | r := httptest.NewRequest(http.MethodPost, homeTestOrigin+"/signin", strings.NewReader("return_to=%2Fd%2F7&address=a%zz")) |
| 98 | r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 99 | r.Header.Set("Origin", homeTestOrigin) |
| 100 | seen := "unread" |
| 101 | w := httptest.NewRecorder() |
| 102 | b.a.beginSignin(func(w http.ResponseWriter, r *http.Request) { |
| 103 | seen = r.FormValue("return_to") + r.PostFormValue("return_to") |
| 104 | http.Redirect(w, r, "/signin?err=address", http.StatusSeeOther) |
| 105 | })(w, r) |
| 106 | if seen != "" { |
| 107 | t.Fatalf("Begin read return_to %q", seen) |
| 108 | } |
| 109 | if r.PostForm == nil { |
| 110 | t.Fatal("the body was never parsed") |
| 111 | } |
| 112 | if loc := w.Header().Get("Location"); loc != "/signin?err=address" { |
| 113 | t.Fatalf("Begin's answer = %q, want it as Begin gave it", loc) |
| 114 | } |
| 115 | j := map[string]*http.Cookie{} |
| 116 | for _, c := range w.Result().Cookies() { |
| 117 | j[c.Name] = c |
| 118 | } |
| 119 | if got := (&homeBrowser{a: b.a, jar: j}).returnPath(); got != "/" { |
| 120 | t.Fatalf("remembered %q, want the list", got) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // What is remembered is only ever one safe local path: an off-site or |
| 125 | // protocol-relative return_to, a backslash, or the signed-out page (which |
| 126 | // would land a fresh sign-in on "You're signed out") lands on the list. |
| 127 | func TestTheReturnMemoryRefusesWhatIsNotAPage(t *testing.T) { |
| 128 | for _, bad := range []string{"//evil.example/x", "https://evil.example/", `/\evil.example`, "/signed-out", "/signed-out?x=1", "/signin", "/auth/landed"} { |
| 129 | b, u := newHomeBrowser(t) |
| 130 | b.get("/signin") |
| 131 | if landed := b.emailSignin(url.Values{"address": {u.Email}, "return_to": {bad}}); landed != "/" { |
| 132 | t.Errorf("return_to %q landed on %q, want /", bad, landed) |
| 133 | } |
| 134 | // And straight into the memory, as the door would remember it. |
| 135 | w := httptest.NewRecorder() |
| 136 | b.a.rememberReturn(w, bad) |
| 137 | j := map[string]*http.Cookie{} |
| 138 | for _, c := range w.Result().Cookies() { |
| 139 | j[c.Name] = c |
| 140 | } |
| 141 | if got := (&homeBrowser{a: b.a, jar: j}).returnPath(); got != "/" { |
| 142 | t.Errorf("rememberReturn(%q) kept %q, want /", bad, got) |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // A tampered, an expired and a duplicated return cookie each read as the |
| 148 | // list at the landing. |
| 149 | func TestABadReturnCookieLandsOnTheList(t *testing.T) { |
| 150 | b, u := newHomeBrowser(t) |
| 151 | session := homeSession(t, b.a, u.Email, "magiclink") |
| 152 | land := func(cookies ...*http.Cookie) string { |
| 153 | r := httptest.NewRequest(http.MethodGet, homeTestOrigin+connect.LocalCompletePath, nil) |
| 154 | r.AddCookie(session) |
| 155 | for _, c := range cookies { |
| 156 | r.AddCookie(c) |
| 157 | } |
| 158 | w := httptest.NewRecorder() |
| 159 | b.h.ServeHTTP(w, r) |
| 160 | if w.Code != http.StatusSeeOther { |
| 161 | t.Fatalf("landing: %d", w.Code) |
| 162 | } |
| 163 | return w.Header().Get("Location") |
| 164 | } |
| 165 | good := func(path string, now time.Time) *http.Cookie { |
| 166 | was := b.a.returnNow |
| 167 | b.a.returnNow = func() time.Time { return now } |
| 168 | defer func() { b.a.returnNow = was }() |
| 169 | w := httptest.NewRecorder() |
| 170 | b.a.rememberReturn(w, path) |
| 171 | return w.Result().Cookies()[0] |
| 172 | } |
| 173 | if got := land(good("/d/7", time.Now())); got != "/d/7" { |
| 174 | t.Fatalf("a good cookie landed on %q", got) |
| 175 | } |
| 176 | tampered := good("/d/7", time.Now()) |
| 177 | raw, _ := base64.RawURLEncoding.DecodeString(tampered.Value) |
| 178 | raw[len(raw)-1] ^= 1 |
| 179 | tampered.Value = base64.RawURLEncoding.EncodeToString(raw) |
| 180 | expired := good("/d/7", time.Now().Add(-11*time.Minute)) |
| 181 | first, second := good("/d/7", time.Now()), good("/d/8", time.Now()) |
| 182 | for name, cookies := range map[string][]*http.Cookie{"tampered": {tampered}, "expired": {expired}, "duplicated": {first, second}} { |
| 183 | if got := land(cookies...); got != "/" { |
| 184 | t.Errorf("%s: landed on %q, want /", name, got) |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // A step-up's reauth=1 survives Begin's answer and the |
| 190 | // page the sign-in began from, for a bad address and for Sent alike: the |
| 191 | // next door is still local ways only, for the same page. |
| 192 | func TestAStepUpPostKeepsReauthAndTheReturn(t *testing.T) { |
| 193 | b, u := newHomeBrowser(t) |
| 194 | for _, address := range []string{"not an address", u.Email} { |
| 195 | br := b.browser() |
| 196 | rec := br.do(http.MethodPost, signinPath, url.Values{"address": {address}, "reauth": {"1"}, "return_to": {"/d/7"}}, "") |
| 197 | loc := rec.Header().Get("Location") |
| 198 | target, err := url.Parse(loc) |
| 199 | if rec.Code != http.StatusSeeOther || err != nil || target.Path != signinPath || target.Query().Get("reauth") != "1" || target.Query().Get("return_to") != "/d/7" { |
| 200 | t.Fatalf("%q: %d %q, want the door with reauth=1 and return_to=/d/7", address, rec.Code, loc) |
| 201 | } |
| 202 | card := door(t, br.get(loc).Body.String()) |
| 203 | if strings.Contains(card, "Oficina ID") || !strings.Contains(card, `name="return_to" value="/d/7"`) || !strings.Contains(card, `name="reauth" value="1"`) { |
| 204 | t.Fatalf("%q: the step-up's next door:\n%s", address, card) |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // The link step-up (connect's Account, for a local sign-in too old to |
| 210 | // link) end to end: it lands on the email-only door with reauth=1, the |
| 211 | // fresh sign-in through it lands back where linking continues, and Oficina |
| 212 | // ID is never offered on the way. |
| 213 | func TestTheLinkStepUpEndToEnd(t *testing.T) { |
| 214 | b, u := newHomeBrowser(t) |
| 215 | token, err := b.a.homeSessions.Mint(sessions.Session{Subject: u.Email, Method: "magiclink", AuthTime: time.Now().Add(-time.Hour)}) |
| 216 | if err != nil { |
| 217 | t.Fatal(err) |
| 218 | } |
| 219 | // A sign-in an hour old is too old to link. |
| 220 | b.set(&http.Cookie{Name: b.a.homeSessions.CookieName(), Value: token}) |
| 221 | start := b.get("/account/home") |
| 222 | loc := start.Header().Get("Location") |
| 223 | target, _ := url.Parse(loc) |
| 224 | if start.Code != http.StatusSeeOther || target.Path != connect.SigninLocalPath || target.Query().Get("reauth") != "1" { |
| 225 | t.Fatalf("a stale link start: %d %q, want the email-only door with reauth=1", start.Code, loc) |
| 226 | } |
| 227 | card := door(t, b.get(loc).Body.String()) |
| 228 | if strings.Contains(card, "Oficina ID") || !strings.Contains(card, "Sign in again to continue.") || !strings.Contains(card, `name="reauth" value="1"`) { |
| 229 | t.Fatalf("the step-up door:\n%s", card) |
| 230 | } |
| 231 | if landed := b.emailSignin(url.Values{"address": {u.Email}, "reauth": {"1"}}); landed != "/account/home" { |
| 232 | t.Fatalf("the fresh sign-in landed on %q, want /account/home to go on linking", landed) |
| 233 | } |
| 234 | if page := b.get("/account/home"); page.Code != http.StatusOK || !strings.Contains(page.Body.String(), "Connect your Oficina account") { |
| 235 | t.Fatalf("the link page after the step-up: %d", page.Code) |
| 236 | } |
| 237 | } |
| 238 | |