| 1 | package idtest |
| 2 | |
| 3 | // Fable's review of the door (oficina/equipo #45): /signed-out is never a |
| 4 | // landing, a stale session is signed out rather than refused, an Oficina |
| 5 | // ID settings page that cannot be read does not loop, and an invited |
| 6 | // person is left alone. |
| 7 | |
| 8 | import ( |
| 9 | "net/http" |
| 10 | "net/url" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "amadan.net/rastrillo/rastrillo/pow" |
| 16 | ) |
| 17 | |
| 18 | // emailLanding signs who in by email from the door at start, with form's |
| 19 | // fields over the door's, and with kept in place of the page the sign-in |
| 20 | // kept when it is set; it returns where the finished sign-in lands. |
| 21 | func (f *fixture) emailLanding(t *testing.T, start, who string, form url.Values, kept *http.Cookie) string { |
| 22 | t.Helper() |
| 23 | page := f.request("GET", start, "", nil) |
| 24 | fields := fields(page.Body.String(), "/auth/begin") |
| 25 | fields.Set("address", who) |
| 26 | for k, v := range form { |
| 27 | fields[k] = v |
| 28 | } |
| 29 | time.Sleep(pow.DefaultMinAge + 20*time.Millisecond) |
| 30 | begin := f.request("POST", "/auth/begin", f.origin, fields) |
| 31 | if begin.Code != http.StatusSeeOther { |
| 32 | t.Fatalf("begin %d", begin.Code) |
| 33 | } |
| 34 | jar := mergeCookies(page.Result().Cookies(), begin.Result().Cookies()) |
| 35 | if kept != nil { |
| 36 | jar = mergeCookies(jar, []*http.Cookie{kept}) |
| 37 | } |
| 38 | confirm := f.request("GET", f.mailedLink(t), "", nil, jar...) |
| 39 | done := f.request("POST", "/auth/verify", f.origin, fieldsOf(confirm.Body.String(), "/auth/verify"), mergeCookies(jar, confirm.Result().Cookies())...) |
| 40 | if done.Code != http.StatusSeeOther { |
| 41 | t.Fatalf("verify %s %d %q", who, done.Code, title(done.Body.String())) |
| 42 | } |
| 43 | cookie(t, done, sessionCookie) |
| 44 | return done.Header().Get("Location") |
| 45 | } |
| 46 | |
| 47 | func fieldsOf(body, action string) url.Values { return fields(body, action) } |
| 48 | |
| 49 | // A sign-in never lands on /signed-out (or a sign-in page), however the |
| 50 | // page it kept was spelled or reached it. |
| 51 | func TestDoorNeverLandsOnSignedOut(t *testing.T) { |
| 52 | t.Parallel() |
| 53 | modes(t, func(t *testing.T, f *fixture, home bool) { |
| 54 | for _, c := range []struct { |
| 55 | name, start string |
| 56 | form url.Values |
| 57 | kept *http.Cookie |
| 58 | }{ |
| 59 | {"a dot segment", "/signin?way=email&return_to=%2F.%2Fsigned-out", nil, nil}, |
| 60 | {"a dot-dot segment", "/signin?way=email&return_to=%2Fwork%2F..%2Fsigned-out", nil, nil}, |
| 61 | {"posted to Begin", "/signin?way=email", url.Values{"return_to": {"/signed-out"}}, nil}, |
| 62 | {"a tampered kept page", "/signin?way=email", nil, &http.Cookie{Name: keptCookie, Value: "%2Fsigned-out"}}, |
| 63 | {"a backslash", "/signin?way=email", url.Values{"return_to": {"/work/..\\signed-out"}}, nil}, |
| 64 | {"a sign-in page", "/signin?way=email", url.Values{"return_to": {"/work/../signin/local"}}, nil}, |
| 65 | } { |
| 66 | // One address each: a mail per address is rate-limited. |
| 67 | who := strings.ToLower(strings.ReplaceAll(c.name, " ", "-")) + "@example.test" |
| 68 | f.member(t, who) |
| 69 | if got := f.emailLanding(t, c.start, who, c.form, c.kept); got != "/" { |
| 70 | t.Errorf("%s: landed on %q, want /", c.name, got) |
| 71 | } |
| 72 | } |
| 73 | // The door does not even carry such a page on its form. |
| 74 | for _, start := range []string{"/signin?way=email&return_to=%2F.%2Fsigned-out", "/signin?way=email&return_to=%2Fwork%2F..%2Fsigned-out"} { |
| 75 | if got := fields(f.request("GET", start, "", nil).Body.String(), "/auth/begin").Get("return_to"); got != "" { |
| 76 | t.Errorf("%s: the door's form carries %q", start, got) |
| 77 | } |
| 78 | } |
| 79 | // A page that is a page still lands there. |
| 80 | f.member(t, "a-real-page@example.test") |
| 81 | if got := f.emailLanding(t, "/signin?way=email&return_to=%2Fwork", "a-real-page@example.test", nil, nil); got != "/work" { |
| 82 | t.Errorf("a real page: landed on %q", got) |
| 83 | } |
| 84 | if home { |
| 85 | f.link(t) |
| 86 | // handoff.SafePath refuses a dot segment before any exchange. |
| 87 | if w := f.request("GET", "/auth/home?return_to=%2F.%2Fsigned-out", "", nil); w.Code == http.StatusSeeOther { |
| 88 | t.Error("an Oficina ID exchange started for /./signed-out") |
| 89 | } |
| 90 | start := f.request("GET", "/auth/home?return_to=%2Fsigned-out", "", nil) |
| 91 | u, _ := url.Parse(start.Header().Get("Location")) |
| 92 | receipt := f.request("POST", "/auth/assertion", f.issuer, url.Values{"assertion": {f.signed(t, u.Query().Get("nonce"), person, time.Now())}}) |
| 93 | done := f.request("GET", "/auth/finish", "", nil, cookie(t, start, "__Host-equipo-id-pending"), cookie(t, receipt, "__Host-equipo-id-inbox")) |
| 94 | if done.Code != http.StatusSeeOther || done.Header().Get("Location") != "/" { |
| 95 | t.Errorf("an Oficina ID sign-in landed on %q", done.Header().Get("Location")) |
| 96 | } |
| 97 | } |
| 98 | }) |
| 99 | } |
| 100 | |
| 101 | // Removed and added back: the old session predates the member's session |
| 102 | // epoch, so the guard refuses it. The door ends it and says nothing of a |
| 103 | // refusal, because this person can sign in; a remembered way is kept. |
| 104 | func TestDoorStaleSessionIsSignedOutNotRefused(t *testing.T) { |
| 105 | t.Parallel() |
| 106 | modes(t, func(t *testing.T, f *fixture, home bool) { |
| 107 | const who = "readded@example.test" |
| 108 | f.member(t, who) |
| 109 | s, record := f.emailRecord(t, who) |
| 110 | sessions := map[string][2]*http.Cookie{"email": {s, record}} |
| 111 | if home { |
| 112 | f.link(t) |
| 113 | done := f.oficinaSignin(t) |
| 114 | sessions["oficina-id"] = [2]*http.Cookie{cookie(t, done, sessionCookie), cookie(t, done, jarCookie)} |
| 115 | } |
| 116 | epoch := time.Now().Add(5 * time.Second).Unix() |
| 117 | if _, err := f.db.Writer().Exec("UPDATE members SET session_epoch=? WHERE address IN (?,?)", epoch, who, address); err != nil { |
| 118 | t.Fatal(err) |
| 119 | } |
| 120 | for name, c := range sessions { |
| 121 | if w := f.request("GET", "/work", "", nil, c[0]); w.Code < 400 || w.Code >= 500 { |
| 122 | t.Fatalf("%s: the guard admits a stale session: %d", name, w.Code) |
| 123 | } |
| 124 | door := f.request("GET", "/signin", "", nil, c[0], c[1]) |
| 125 | if door.Code != http.StatusOK || strings.Contains(door.Body.String(), refusal) || title(door.Body.String()) != "Sign in to Equipo" { |
| 126 | t.Errorf("%s: %d %q, or told an active member they can't sign in", name, door.Code, title(door.Body.String())) |
| 127 | } |
| 128 | if _, clears := set(door, sessionCookie); !clears || f.sessionAlive(t, c[0]) { |
| 129 | t.Errorf("%s: the stale session lived on", name) |
| 130 | } |
| 131 | if c, _ := set(door, jarCookie); c != nil { |
| 132 | t.Errorf("%s: the door forgot how this person signs in", name) |
| 133 | } |
| 134 | } |
| 135 | }) |
| 136 | } |
| 137 | |
| 138 | // The Oficina ID settings page, when the session cannot be read, answers |
| 139 | // 503 rather than sending the browser to a door that sends it back. |
| 140 | func TestDoorOficinaSettingsUnreadableIsNoLoop(t *testing.T) { |
| 141 | t.Parallel() |
| 142 | f := newFixture(t, true) |
| 143 | s := f.local(t, address) |
| 144 | f.breakRoster(t) |
| 145 | w := f.request("GET", "/conversation/settings/oficina-id", "", nil, s) |
| 146 | if w.Code != http.StatusServiceUnavailable || w.Header().Get("Location") != "" { |
| 147 | t.Fatalf("settings on a storage failure: %d %s", w.Code, w.Header().Get("Location")) |
| 148 | } |
| 149 | signedOut := f.request("GET", "/conversation/settings/oficina-id", "", nil) |
| 150 | if signedOut.Code != http.StatusSeeOther || !strings.HasPrefix(signedOut.Header().Get("Location"), "/signin?") { |
| 151 | t.Fatalf("settings signed out: %d %s", signedOut.Code, signedOut.Header().Get("Location")) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // An invited person who has signed in to verify their address, and not yet |
| 156 | // joined, is not refused at the door and keeps their session: the real |
| 157 | // invitation, requireMember and MayVerifyInvitation. |
| 158 | func TestDoorLeavesAnInviteeAlone(t *testing.T) { |
| 159 | t.Parallel() |
| 160 | modes(t, func(t *testing.T, f *fixture, home bool) { |
| 161 | const invitee = "invitee@example.test" |
| 162 | owner := f.local(t, address) |
| 163 | created := f.request("POST", "/invites", f.origin, url.Values{"address": {invitee}, "role": {"member"}, "request_id": {"door-invite"}}, owner) |
| 164 | if created.Code != http.StatusOK { |
| 165 | t.Fatalf("invite %d %.300s", created.Code, created.Body.String()) |
| 166 | } |
| 167 | s := f.sessionAs(t, invitee, "magiclink") |
| 168 | if w := f.request("GET", "/work", "", nil, s); w.Code != http.StatusForbidden { |
| 169 | t.Fatalf("the guard admits an invitee: %d", w.Code) |
| 170 | } |
| 171 | door := f.request("GET", "/signin", "", nil, s) |
| 172 | if door.Code != http.StatusOK || strings.Contains(door.Body.String(), refusal) { |
| 173 | t.Fatalf("invitee door %d, or refused", door.Code) |
| 174 | } |
| 175 | if c, _ := set(door, sessionCookie); c != nil || !f.sessionAlive(t, s) { |
| 176 | t.Fatal("the door ended an invitee's session") |
| 177 | } |
| 178 | // Without the invitation the same person is refused. |
| 179 | stranger := f.sessionAs(t, "stranger@example.test", "magiclink") |
| 180 | if !strings.Contains(f.request("GET", "/signin", "", nil, stranger).Body.String(), refusal) { |
| 181 | t.Fatal("a stranger was not refused") |
| 182 | } |
| 183 | }) |
| 184 | } |
| 185 | |