| 1 | package sheets |
| 2 | |
| 3 | // The door's edges (Fable's review of #71): an undecided guard, what the |
| 4 | // admission probe leaves on the real response, and what the return memory |
| 5 | // refuses to keep or read. |
| 6 | |
| 7 | import ( |
| 8 | "net/http" |
| 9 | "net/http/httptest" |
| 10 | "net/url" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 16 | "amadan.net/rastrillo/rastrillo/sessions" |
| 17 | ) |
| 18 | |
| 19 | // undecided makes team mode's guard answer 503 for a local session: the |
| 20 | // table the member's Home identity is read from is gone, so teamIdentify |
| 21 | // cannot decide (teamstate.ErrUnavailable). |
| 22 | func undecided(t *testing.T) *teamFixture { |
| 23 | t.Helper() |
| 24 | f := newTeamFixture(t, signedInAddress) |
| 25 | if _, err := f.app.team.db.Exec(`DROP TABLE sheets_team_home_members`); err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | return f |
| 29 | } |
| 30 | |
| 31 | // A guard that cannot decide is no refusal: /signin draws no refusal, |
| 32 | // keeps the remembered Oficina ID and keeps the session. |
| 33 | func TestAnUndecidedGuardIsNoRefusalAtTheDoor(t *testing.T) { |
| 34 | f := undecided(t) |
| 35 | f.local(t, signedInAddress) |
| 36 | if rec := f.get(t, "/sheets"); rec.Code != http.StatusServiceUnavailable { |
| 37 | t.Fatalf("the guard answered %d, want its 503", rec.Code) |
| 38 | } |
| 39 | f.remember(t, lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina}) |
| 40 | rec := f.get(t, "/signin") |
| 41 | if rec.Code == http.StatusOK { |
| 42 | t.Fatalf("an undecided session got the door:\n%s", rec.Body.String()) |
| 43 | } |
| 44 | if got, res := f.remembered(t); res != lastsignin.Valid || got.Method != lastsignin.MethodFederated { |
| 45 | t.Fatalf("an undecided session lost the remembered Oficina ID: %+v (%v)", got, res) |
| 46 | } |
| 47 | if _, kept := f.jar[f.app.homeSessions.CookieName()]; !kept { |
| 48 | t.Fatal("an undecided session was signed out") |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // And MintHomeSession remembers nothing on one, leaving the session alone. |
| 53 | func TestAnUndecidedGuardRemembersNothing(t *testing.T) { |
| 54 | f := undecided(t) |
| 55 | if _, err := f.app.memberFor(signedInAddress); err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | token, err := f.app.homeSessions.Mint(sessions.Session{Subject: signedInAddress, Method: "magiclink", AuthTime: time.Now()}) |
| 59 | if err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | cookie := &http.Cookie{Name: f.app.homeSessions.CookieName(), Value: token} |
| 63 | w := httptest.NewRecorder() |
| 64 | http.SetCookie(w, cookie) |
| 65 | f.app.rememberOficina(w, httptest.NewRequest(http.MethodGet, testOrigin+"/auth/finish", nil)) |
| 66 | for _, c := range w.Result().Cookies() { |
| 67 | if c.Name == f.app.auth.RememberJar().CookieName() { |
| 68 | t.Fatal("Oficina ID was remembered on an undecided guard") |
| 69 | } |
| 70 | } |
| 71 | r := httptest.NewRequest(http.MethodGet, testOrigin+"/", nil) |
| 72 | r.AddCookie(cookie) |
| 73 | if _, ok := f.app.homeSessions.From(r); !ok { |
| 74 | t.Fatal("the probe ended a session it could not decide on") |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // The probe writes nothing to the real response: an admitted session's |
| 79 | // /signin goes straight to its page and sets no cookie. |
| 80 | func TestAnAdmittedSigninSetsNoCookie(t *testing.T) { |
| 81 | f := newHomeFixture(t) |
| 82 | f.local(t, signedInAddress) |
| 83 | f.remember(t, lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina}) |
| 84 | rec := f.get(t, "/signin?return_to=%2Fsheets%2F7") |
| 85 | redirectsTo(t, rec, "/sheets/7") |
| 86 | if cookies := rec.Result().Cookies(); len(cookies) != 0 { |
| 87 | t.Fatalf("an admitted /signin set cookies: %+v", cookies) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // A refused session's cookie is cleared on the door's own response, so a |
| 92 | // reload is plainly signed out. |
| 93 | func TestARefusedSessionsCookieIsCleared(t *testing.T) { |
| 94 | f := newHomeFixture(t) |
| 95 | f.local(t, "carol@example.com") |
| 96 | rec := f.get(t, "/signin") |
| 97 | if card := door(t, rec.Body.String()); rec.Code != http.StatusOK || !strings.Contains(card, appProblem) { |
| 98 | t.Fatalf("not the refusal: %d", rec.Code) |
| 99 | } |
| 100 | if _, kept := f.jar[f.app.homeSessions.CookieName()]; kept { |
| 101 | t.Fatal("the refused session's cookie was left on the browser") |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // A hostile or self-defeating return_to lands a local sign-in on the list: |
| 106 | // off-site paths, and the signed-out page, which would greet a fresh |
| 107 | // sign-in with "You're signed out". |
| 108 | func TestAReturnToThatIsNoPageLandsOnTheList(t *testing.T) { |
| 109 | f := newHomeFixture(t) |
| 110 | for _, to := range []string{"//evil.example/x", "https://evil.example/", `/\evil.example`, "/signed-out", "/signed-out?x=1"} { |
| 111 | b := f.browser() |
| 112 | b.get(t, "/signin?return_to="+url.QueryEscape(to)) |
| 113 | if landed := b.emailSignin(t, url.Values{"address": {signedInAddress}, "return_to": {to}}); landed != "/sheets" { |
| 114 | t.Errorf("return_to %q landed on %q, want /sheets", to, landed) |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // The return memory reads a tampered, an expired or a duplicated cookie |
| 120 | // as "/", and a sound one as its page. |
| 121 | func TestTheReturnMemoryReadsOnlyItsOwnCookie(t *testing.T) { |
| 122 | f := newHomeFixture(t) |
| 123 | mem := f.app.returns |
| 124 | w := httptest.NewRecorder() |
| 125 | if !mem.remember(w, "/sheets/7") { |
| 126 | t.Fatal("remember refused a sound page") |
| 127 | } |
| 128 | kept := w.Result().Cookies()[0] |
| 129 | read := func(m *returnMemory, cookies ...*http.Cookie) string { |
| 130 | r := httptest.NewRequest(http.MethodGet, testOrigin+"/", nil) |
| 131 | for _, c := range cookies { |
| 132 | r.AddCookie(c) |
| 133 | } |
| 134 | return m.path(r) |
| 135 | } |
| 136 | if got := read(mem, kept); got != "/sheets/7" { |
| 137 | t.Fatalf("a sound cookie read as %q", got) |
| 138 | } |
| 139 | tampered := *kept |
| 140 | b := []byte(tampered.Value) |
| 141 | b[len(b)/2] ^= 1 |
| 142 | tampered.Value = string(b) |
| 143 | later := *mem |
| 144 | later.now = func() time.Time { return time.Now().Add(time.Duration(homeReturnLifetime+1) * time.Second) } |
| 145 | for name, got := range map[string]string{ |
| 146 | "tampered": read(mem, &tampered), |
| 147 | "expired": read(&later, kept), |
| 148 | "duplicated": read(mem, kept, kept), |
| 149 | } { |
| 150 | if got != "/" { |
| 151 | t.Errorf("a %s cookie read as %q, want /", name, got) |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // Forget and Sign out forget a page a sign-in in progress kept. |
| 157 | func TestForgetAndSignOutClearTheReturnMemory(t *testing.T) { |
| 158 | f := newHomeFixture(t) |
| 159 | f.get(t, "/signin?return_to=%2Fsheets%2F7") |
| 160 | if f.jar[homeReturnCookie] == nil { |
| 161 | t.Fatal("the door did not remember the page") |
| 162 | } |
| 163 | f.request(t, "POST", signinForgetPath, url.Values{"way": {"email"}}, false) |
| 164 | if _, kept := f.jar[homeReturnCookie]; kept { |
| 165 | t.Fatal("Forget kept the remembered page") |
| 166 | } |
| 167 | f.get(t, "/signin?return_to=%2Fsheets%2F7") |
| 168 | f.local(t, signedInAddress) |
| 169 | f.request(t, "POST", "/signout", nil, false) |
| 170 | if _, kept := f.jar[homeReturnCookie]; kept { |
| 171 | t.Fatal("Sign out kept the remembered page") |
| 172 | } |
| 173 | } |
| 174 | |