| 1 | package fichastest |
| 2 | |
| 3 | // The return memory: the page a sign-in began from, kept across the mail |
| 4 | // round trip, landed on once, and never kept when it should not be |
| 5 | // (signin.go's beginReturn and forget; rastrillo's keepReturnTo, admit |
| 6 | // and Signout). |
| 7 | |
| 8 | import ( |
| 9 | "net/http" |
| 10 | "net/http/httptest" |
| 11 | "net/url" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | ) |
| 15 | |
| 16 | // A signed-out page request goes to the door with the page; the door's |
| 17 | // form carries it; the landing goes there, and spends it. |
| 18 | func TestTheKeptPageIsLandedOn(t *testing.T) { |
| 19 | h, m, _ := wiredApp(t) |
| 20 | b := newBrowser() |
| 21 | first := b.get(t, h, "/cards/new") |
| 22 | if first.Code != http.StatusSeeOther || first.Header().Get("Location") != "/signin?return_to=%2Fcards%2Fnew" { |
| 23 | t.Fatalf("signed-out GET /cards/new: %d to %q", first.Code, first.Header().Get("Location")) |
| 24 | } |
| 25 | door := b.get(t, h, first.Header().Get("Location")).Body.String() |
| 26 | if !strings.Contains(door, `<input type="hidden" name="return_to" value="/cards/new">`) { |
| 27 | t.Fatalf("the door's form does not carry the page:\n%s", door) |
| 28 | } |
| 29 | landing := signIn(t, h, b, m, TestOwner, url.Values{"return_to": {"/cards/new"}}) |
| 30 | if landing.Header().Get("Location") != "/cards/new" { |
| 31 | t.Errorf("landed on %q, want /cards/new", landing.Header().Get("Location")) |
| 32 | } |
| 33 | if b.has(returnCookie) { |
| 34 | t.Error("the landing did not spend the kept page") |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Every landing that mints a session spends the kept page, so a second |
| 39 | // landing goes to the list, not to the first one's page. |
| 40 | func TestEveryLandingSpendsTheKeptPage(t *testing.T) { |
| 41 | h, m, _ := wiredApp(t) |
| 42 | b := newBrowser() |
| 43 | plain := linkFor(t, h, b, m, TestOwner, nil) |
| 44 | kept := linkFor(t, h, b, m, TestOwner, url.Values{"return_to": {"/cards/new"}}) |
| 45 | if rec := b.post(t, h, "/auth/verify", url.Values{"token": {plain}}); rec.Header().Get("Location") != "/cards/new" { |
| 46 | t.Fatalf("first landing: %q", rec.Header().Get("Location")) |
| 47 | } |
| 48 | if rec := b.post(t, h, "/auth/verify", url.Values{"token": {kept}}); rec.Header().Get("Location") != "/" { |
| 49 | t.Errorf("second landing went to %q, want /: the page was not spent", rec.Header().Get("Location")) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // A post without return_to forgets a page an earlier, abandoned attempt |
| 54 | // kept, owner and stranger alike. |
| 55 | func TestAPostWithoutAPageForgetsTheKeptOne(t *testing.T) { |
| 56 | h, m, _ := wiredApp(t) |
| 57 | for _, address := range []string{TestOwner, "zzz@example.com"} { |
| 58 | b := newBrowser() |
| 59 | b.post(t, h, "/auth/begin", url.Values{"address": {address}, "force": {"1"}, "return_to": {"/cards/new"}}) |
| 60 | if !b.has(returnCookie) { |
| 61 | t.Fatalf("%s: Begin kept no page", address) |
| 62 | } |
| 63 | b.post(t, h, "/auth/begin", url.Values{"address": {address}, "force": {"1"}}) |
| 64 | if b.has(returnCookie) { |
| 65 | t.Errorf("%s: a post without return_to kept the earlier page", address) |
| 66 | } |
| 67 | } |
| 68 | b := newBrowser() |
| 69 | b.post(t, h, "/auth/begin", url.Values{"address": {TestOwner}, "force": {"1"}, "return_to": {"/cards/new"}}) |
| 70 | if rec := signIn(t, h, b, m, TestOwner, nil); rec.Header().Get("Location") != "/" { |
| 71 | t.Errorf("landed on %q, want /", rec.Header().Get("Location")) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // A body that does not parse keeps nothing, even what parsed before the |
| 76 | // error, and forgets what was kept. |
| 77 | func TestABodyThatDoesNotParseKeepsNoPage(t *testing.T) { |
| 78 | h, _, _ := wiredApp(t) |
| 79 | b := newBrowser() |
| 80 | b.post(t, h, "/auth/begin", url.Values{"address": {TestOwner}, "force": {"1"}, "return_to": {"/t/x"}}) |
| 81 | rec := b.postRaw(t, h, "/auth/begin", "address="+url.QueryEscape(TestOwner)+"&force=1&return_to=%2Fcards%2Fnew&bad=%zz") |
| 82 | if rec.Code != http.StatusSeeOther { |
| 83 | t.Fatalf("POST: %d", rec.Code) |
| 84 | } |
| 85 | if b.has(returnCookie) { |
| 86 | t.Errorf("a body that did not parse kept %q", b.cookies[returnCookie].Value) |
| 87 | } |
| 88 | if strings.Contains(rec.Header().Get("Location"), "return_to") { |
| 89 | t.Errorf("the answer carries a page: %q", rec.Header().Get("Location")) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // A page that is no page to come back to is never kept: the signed-out |
| 94 | // page, the door, auth's routes, another site. The door drops it from its |
| 95 | // form, Begin keeps nothing, and the landing is the list. |
| 96 | func TestNoPageThatIsNoPageIsKept(t *testing.T) { |
| 97 | for _, to := range []string{"/signed-out", "/signed-out?x=1", "/signin", "/signin/forget", "/auth/verify", "//evil.example/x", "https://evil.example/", "/\\evil.example"} { |
| 98 | // A fresh app each time: the per-address budget is five. |
| 99 | h, m, _ := wiredApp(t) |
| 100 | door := get(t, h, "/signin?return_to="+url.QueryEscape(to)).Body.String() |
| 101 | if strings.Contains(door, `name="return_to"`) { |
| 102 | t.Errorf("%q: the door's form carries it", to) |
| 103 | } |
| 104 | b := newBrowser() |
| 105 | rec := signIn(t, h, b, m, TestOwner, url.Values{"return_to": {to}}) |
| 106 | if rec.Header().Get("Location") != "/" { |
| 107 | t.Errorf("%q: landed on %q, want /", to, rec.Header().Get("Location")) |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Forget and Sign out clear a kept page: on a shared computer it is the |
| 113 | // last person's, not the next one's. |
| 114 | func TestForgetAndSignOutClearTheKeptPage(t *testing.T) { |
| 115 | h, m, _ := wiredApp(t) |
| 116 | b := newBrowser() |
| 117 | b.post(t, h, "/auth/begin", url.Values{"address": {TestOwner}, "force": {"1"}, "return_to": {"/cards/new"}}) |
| 118 | b.post(t, h, "/signin/forget", url.Values{"return_to": {"/cards/new"}}) |
| 119 | if b.has(returnCookie) { |
| 120 | t.Error("Forget kept the page") |
| 121 | } |
| 122 | |
| 123 | b = newBrowser() |
| 124 | signIn(t, h, b, m, TestOwner, nil) |
| 125 | b.post(t, h, "/auth/begin", url.Values{"address": {TestOwner}, "force": {"1"}, "return_to": {"/cards/new"}}) |
| 126 | b.post(t, h, "/signout", url.Values{}) |
| 127 | if b.has(returnCookie) { |
| 128 | t.Error("Sign out kept the page") |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // The landing reads the kept page back only when it passes the same test |
| 133 | // Begin's does: a cookie edited by hand, one past its time, or two of |
| 134 | // them, and a return_to posted to the landing itself, each land on "/" |
| 135 | // (Fable on fichas #9). The cookie is cleared on that same response. |
| 136 | func TestTheLandingTrustsNoPageItWasNotKept(t *testing.T) { |
| 137 | for name, c := range map[string]struct { |
| 138 | cookies []string |
| 139 | posted []string |
| 140 | }{ |
| 141 | "tampered: signed-out": {cookies: []string{url.QueryEscape("/signed-out")}}, |
| 142 | "tampered: dot segments": {cookies: []string{url.QueryEscape("/x/../signed-out")}}, |
| 143 | "tampered: the door": {cookies: []string{url.QueryEscape("/./signin")}}, |
| 144 | "tampered: another site": {cookies: []string{url.QueryEscape("//evil.example/")}}, |
| 145 | "tampered: not escaped": {cookies: []string{"%zz"}}, |
| 146 | "duplicate cookies": {cookies: []string{url.QueryEscape("/cards/new"), url.QueryEscape("/t/t_customer")}}, |
| 147 | "posted: signed-out": {posted: []string{"/signed-out"}}, |
| 148 | "posted: a page": {posted: []string{"/cards/new"}}, |
| 149 | "posted: duplicate": {posted: []string{"/cards/new", "/cards/new"}}, |
| 150 | "posted over a kept cookie": {cookies: []string{url.QueryEscape("/signed-out")}, posted: []string{"/cards/new"}}, |
| 151 | } { |
| 152 | h, m, _ := wiredApp(t) |
| 153 | b := newBrowser() |
| 154 | tok := linkFor(t, h, b, m, TestOwner, nil) |
| 155 | form := url.Values{"token": {tok}, "return_to": c.posted} |
| 156 | req := httptest.NewRequest(http.MethodPost, "/auth/verify", strings.NewReader(form.Encode())) |
| 157 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 158 | req.Header.Set("Origin", testOrigin) |
| 159 | b.send(req) |
| 160 | for _, v := range c.cookies { |
| 161 | req.AddCookie(&http.Cookie{Name: returnCookie, Value: v}) |
| 162 | } |
| 163 | rec := httptest.NewRecorder() |
| 164 | h.ServeHTTP(rec, req) |
| 165 | if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/" { |
| 166 | t.Errorf("%s: %d to %q, want 303 to /", name, rec.Code, rec.Header().Get("Location")) |
| 167 | } |
| 168 | if len(c.cookies) > 0 && !clears(rec, returnCookie) { |
| 169 | t.Errorf("%s: the landing did not clear the kept page", name) |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // An expired cookie: rastrillo's kept page carries no issue time, so its |
| 175 | // lifetime is the Max-Age Begin gives it, the emailed link's own, after |
| 176 | // which a browser stops sending it. A cookie sent past that is one |
| 177 | // written by hand, and is judged as above. |
| 178 | func TestTheKeptPageLivesNoLongerThanItsLink(t *testing.T) { |
| 179 | h, _, _ := wiredApp(t) |
| 180 | rec := post(t, h, "/auth/begin", url.Values{"address": {TestOwner}, "force": {"1"}, "return_to": {"/cards/new"}}) |
| 181 | var kept *http.Cookie |
| 182 | for _, c := range rec.Result().Cookies() { |
| 183 | if c.Name == returnCookie { |
| 184 | kept = c |
| 185 | } |
| 186 | } |
| 187 | if kept == nil { |
| 188 | t.Fatal("Begin kept no page") |
| 189 | } |
| 190 | if kept.MaxAge <= 0 || kept.MaxAge > 900 || !kept.HttpOnly || kept.Path != "/" { |
| 191 | t.Errorf("kept page cookie: Max-Age %d, HttpOnly %v, Path %q; want at most the link's 15 minutes, HttpOnly, /", kept.MaxAge, kept.HttpOnly, kept.Path) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // clears reports whether rec sets name to nothing. |
| 196 | func clears(rec *httptest.ResponseRecorder, name string) bool { |
| 197 | for _, c := range rec.Result().Cookies() { |
| 198 | if c.Name == name && (c.MaxAge < 0 || c.Value == "") { |
| 199 | return true |
| 200 | } |
| 201 | } |
| 202 | return false |
| 203 | } |
| 204 | |