package idtest // Fable's review of the door (oficina/equipo #45): /signed-out is never a // landing, a stale session is signed out rather than refused, an Oficina // ID settings page that cannot be read does not loop, and an invited // person is left alone. import ( "net/http" "net/url" "strings" "testing" "time" "amadan.net/rastrillo/rastrillo/pow" ) // emailLanding signs who in by email from the door at start, with form's // fields over the door's, and with kept in place of the page the sign-in // kept when it is set; it returns where the finished sign-in lands. func (f *fixture) emailLanding(t *testing.T, start, who string, form url.Values, kept *http.Cookie) string { t.Helper() page := f.request("GET", start, "", nil) fields := fields(page.Body.String(), "/auth/begin") fields.Set("address", who) for k, v := range form { fields[k] = v } time.Sleep(pow.DefaultMinAge + 20*time.Millisecond) begin := f.request("POST", "/auth/begin", f.origin, fields) if begin.Code != http.StatusSeeOther { t.Fatalf("begin %d", begin.Code) } jar := mergeCookies(page.Result().Cookies(), begin.Result().Cookies()) if kept != nil { jar = mergeCookies(jar, []*http.Cookie{kept}) } confirm := f.request("GET", f.mailedLink(t), "", nil, jar...) done := f.request("POST", "/auth/verify", f.origin, fieldsOf(confirm.Body.String(), "/auth/verify"), mergeCookies(jar, confirm.Result().Cookies())...) if done.Code != http.StatusSeeOther { t.Fatalf("verify %s %d %q", who, done.Code, title(done.Body.String())) } cookie(t, done, sessionCookie) return done.Header().Get("Location") } func fieldsOf(body, action string) url.Values { return fields(body, action) } // A sign-in never lands on /signed-out (or a sign-in page), however the // page it kept was spelled or reached it. func TestDoorNeverLandsOnSignedOut(t *testing.T) { t.Parallel() modes(t, func(t *testing.T, f *fixture, home bool) { for _, c := range []struct { name, start string form url.Values kept *http.Cookie }{ {"a dot segment", "/signin?way=email&return_to=%2F.%2Fsigned-out", nil, nil}, {"a dot-dot segment", "/signin?way=email&return_to=%2Fwork%2F..%2Fsigned-out", nil, nil}, {"posted to Begin", "/signin?way=email", url.Values{"return_to": {"/signed-out"}}, nil}, {"a tampered kept page", "/signin?way=email", nil, &http.Cookie{Name: keptCookie, Value: "%2Fsigned-out"}}, {"a backslash", "/signin?way=email", url.Values{"return_to": {"/work/..\\signed-out"}}, nil}, {"a sign-in page", "/signin?way=email", url.Values{"return_to": {"/work/../signin/local"}}, nil}, } { // One address each: a mail per address is rate-limited. who := strings.ToLower(strings.ReplaceAll(c.name, " ", "-")) + "@example.test" f.member(t, who) if got := f.emailLanding(t, c.start, who, c.form, c.kept); got != "/" { t.Errorf("%s: landed on %q, want /", c.name, got) } } // The door does not even carry such a page on its form. for _, start := range []string{"/signin?way=email&return_to=%2F.%2Fsigned-out", "/signin?way=email&return_to=%2Fwork%2F..%2Fsigned-out"} { if got := fields(f.request("GET", start, "", nil).Body.String(), "/auth/begin").Get("return_to"); got != "" { t.Errorf("%s: the door's form carries %q", start, got) } } // A page that is a page still lands there. f.member(t, "a-real-page@example.test") if got := f.emailLanding(t, "/signin?way=email&return_to=%2Fwork", "a-real-page@example.test", nil, nil); got != "/work" { t.Errorf("a real page: landed on %q", got) } if home { f.link(t) // handoff.SafePath refuses a dot segment before any exchange. if w := f.request("GET", "/auth/home?return_to=%2F.%2Fsigned-out", "", nil); w.Code == http.StatusSeeOther { t.Error("an Oficina ID exchange started for /./signed-out") } start := f.request("GET", "/auth/home?return_to=%2Fsigned-out", "", nil) u, _ := url.Parse(start.Header().Get("Location")) receipt := f.request("POST", "/auth/assertion", f.issuer, url.Values{"assertion": {f.signed(t, u.Query().Get("nonce"), person, time.Now())}}) done := f.request("GET", "/auth/finish", "", nil, cookie(t, start, "__Host-equipo-id-pending"), cookie(t, receipt, "__Host-equipo-id-inbox")) if done.Code != http.StatusSeeOther || done.Header().Get("Location") != "/" { t.Errorf("an Oficina ID sign-in landed on %q", done.Header().Get("Location")) } } }) } // Removed and added back: the old session predates the member's session // epoch, so the guard refuses it. The door ends it and says nothing of a // refusal, because this person can sign in; a remembered way is kept. func TestDoorStaleSessionIsSignedOutNotRefused(t *testing.T) { t.Parallel() modes(t, func(t *testing.T, f *fixture, home bool) { const who = "readded@example.test" f.member(t, who) s, record := f.emailRecord(t, who) sessions := map[string][2]*http.Cookie{"email": {s, record}} if home { f.link(t) done := f.oficinaSignin(t) sessions["oficina-id"] = [2]*http.Cookie{cookie(t, done, sessionCookie), cookie(t, done, jarCookie)} } epoch := time.Now().Add(5 * time.Second).Unix() if _, err := f.db.Writer().Exec("UPDATE members SET session_epoch=? WHERE address IN (?,?)", epoch, who, address); err != nil { t.Fatal(err) } for name, c := range sessions { if w := f.request("GET", "/work", "", nil, c[0]); w.Code < 400 || w.Code >= 500 { t.Fatalf("%s: the guard admits a stale session: %d", name, w.Code) } door := f.request("GET", "/signin", "", nil, c[0], c[1]) if door.Code != http.StatusOK || strings.Contains(door.Body.String(), refusal) || title(door.Body.String()) != "Sign in to Equipo" { t.Errorf("%s: %d %q, or told an active member they can't sign in", name, door.Code, title(door.Body.String())) } if _, clears := set(door, sessionCookie); !clears || f.sessionAlive(t, c[0]) { t.Errorf("%s: the stale session lived on", name) } if c, _ := set(door, jarCookie); c != nil { t.Errorf("%s: the door forgot how this person signs in", name) } } }) } // The Oficina ID settings page, when the session cannot be read, answers // 503 rather than sending the browser to a door that sends it back. func TestDoorOficinaSettingsUnreadableIsNoLoop(t *testing.T) { t.Parallel() f := newFixture(t, true) s := f.local(t, address) f.breakRoster(t) w := f.request("GET", "/conversation/settings/oficina-id", "", nil, s) if w.Code != http.StatusServiceUnavailable || w.Header().Get("Location") != "" { t.Fatalf("settings on a storage failure: %d %s", w.Code, w.Header().Get("Location")) } signedOut := f.request("GET", "/conversation/settings/oficina-id", "", nil) if signedOut.Code != http.StatusSeeOther || !strings.HasPrefix(signedOut.Header().Get("Location"), "/signin?") { t.Fatalf("settings signed out: %d %s", signedOut.Code, signedOut.Header().Get("Location")) } } // An invited person who has signed in to verify their address, and not yet // joined, is not refused at the door and keeps their session: the real // invitation, requireMember and MayVerifyInvitation. func TestDoorLeavesAnInviteeAlone(t *testing.T) { t.Parallel() modes(t, func(t *testing.T, f *fixture, home bool) { const invitee = "invitee@example.test" owner := f.local(t, address) created := f.request("POST", "/invites", f.origin, url.Values{"address": {invitee}, "role": {"member"}, "request_id": {"door-invite"}}, owner) if created.Code != http.StatusOK { t.Fatalf("invite %d %.300s", created.Code, created.Body.String()) } s := f.sessionAs(t, invitee, "magiclink") if w := f.request("GET", "/work", "", nil, s); w.Code != http.StatusForbidden { t.Fatalf("the guard admits an invitee: %d", w.Code) } door := f.request("GET", "/signin", "", nil, s) if door.Code != http.StatusOK || strings.Contains(door.Body.String(), refusal) { t.Fatalf("invitee door %d, or refused", door.Code) } if c, _ := set(door, sessionCookie); c != nil || !f.sessionAlive(t, s) { t.Fatal("the door ended an invitee's session") } // Without the invitation the same person is refused. stranger := f.sessionAs(t, "stranger@example.test", "magiclink") if !strings.Contains(f.request("GET", "/signin", "", nil, stranger).Body.String(), refusal) { t.Fatal("a stranger was not refused") } }) }