| 1 | package connect |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "html/template" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "net/url" |
| 9 | "os/exec" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/oficina/home/handoff" |
| 15 | "amadan.net/rastrillo/rastrillo/csrf" |
| 16 | ) |
| 17 | |
| 18 | // doorFixture is Routes over navigationFixture's sealer, with a session |
| 19 | // reader that knows "verified" (a live Home sign-in), "guest" (a Calls |
| 20 | // guest, a session but not a sign-in) and "revoked" (no session). The |
| 21 | // app's door (LocalSignin) answers 299 and records what connect told it. |
| 22 | type doorFixture struct { |
| 23 | h *Routes |
| 24 | calls int |
| 25 | href string |
| 26 | path string |
| 27 | } |
| 28 | |
| 29 | func newDoorFixture(t *testing.T) *doorFixture { |
| 30 | t.Helper() |
| 31 | _, n := navigationFixture(t) |
| 32 | f := &doorFixture{} |
| 33 | f.h = &Routes{bridge: &Bridge{now: time.Now, render: func(w http.ResponseWriter, r *http.Request, state, label string, status int) { w.WriteHeader(status) }}, navigation: n, cfg: RoutesConfig{Origin: navigationOrigin, SessionCookie: "__Host-rastrillo_session", ReadSession: func(r *http.Request) (*Session, error) { |
| 34 | c, err := r.Cookie("__Host-rastrillo_session") |
| 35 | if err != nil || (c.Value != "verified" && c.Value != "guest") { |
| 36 | return nil, nil |
| 37 | } |
| 38 | method := HomeMethod |
| 39 | if c.Value == "guest" { |
| 40 | method = "guest" |
| 41 | } |
| 42 | return &Session{Subject: "pr_live", Method: method, At: time.Now()}, nil |
| 43 | }}} |
| 44 | f.h.cfg.LocalSignin = func(w http.ResponseWriter, r *http.Request) { |
| 45 | f.calls++ |
| 46 | f.href, f.path = OficinaHref(r), r.URL.Path |
| 47 | w.WriteHeader(299) |
| 48 | } |
| 49 | return f |
| 50 | } |
| 51 | |
| 52 | func (f *doorFixture) get(handler http.HandlerFunc, target string, cookies ...*http.Cookie) *httptest.ResponseRecorder { |
| 53 | f.href, f.path = "unset", "" |
| 54 | r := httptest.NewRequest(http.MethodGet, navigationOrigin+target, nil) |
| 55 | for _, c := range cookies { |
| 56 | r.AddCookie(c) |
| 57 | } |
| 58 | w := httptest.NewRecorder() |
| 59 | handler(w, r) |
| 60 | return w |
| 61 | } |
| 62 | |
| 63 | var ( |
| 64 | liveSession = &http.Cookie{Name: "__Host-rastrillo_session", Value: "verified"} |
| 65 | staleSession = &http.Cookie{Name: "__Host-rastrillo_session", Value: "revoked"} |
| 66 | guestSession = &http.Cookie{Name: "__Host-rastrillo_session", Value: "guest"} |
| 67 | ) |
| 68 | |
| 69 | // TestBareSigninRendersTheDoor is the smart door's first route: GET /signin |
| 70 | // always renders the door, and never starts a Home exchange. Before, a bare |
| 71 | // /signin went straight to id.oficina.works (connect's query dispatch), so |
| 72 | // nobody could pick email from it. Nothing is remembered and no cookie is |
| 73 | // set: the exchange, and its return cookie, belong to /signin/oficina. |
| 74 | func TestBareSigninRendersTheDoor(t *testing.T) { |
| 75 | f := newDoorFixture(t) |
| 76 | for _, tc := range []struct { |
| 77 | target string |
| 78 | cookies []*http.Cookie |
| 79 | href string |
| 80 | }{ |
| 81 | {"/signin", nil, SigninOficinaPath}, |
| 82 | {"/signin?return_to=%2Fd%2F42%3Fview%3Dcompact", nil, "/signin/oficina?return_to=%2Fd%2F42%3Fview%3Dcompact"}, |
| 83 | // A stale cookie, an ambiguous pair or a Calls guest is not signed |
| 84 | // in: the door, as for nobody. |
| 85 | {"/signin?return_to=%2Fd%2F42", []*http.Cookie{staleSession}, "/signin/oficina?return_to=%2Fd%2F42"}, |
| 86 | {"/signin?return_to=%2Fd%2F42", []*http.Cookie{liveSession, liveSession}, "/signin/oficina?return_to=%2Fd%2F42"}, |
| 87 | {"/signin?return_to=%2Fd%2F42", []*http.Cookie{guestSession}, "/signin/oficina?return_to=%2Fd%2F42"}, |
| 88 | // The chooser and a picked way are the door's own pages. |
| 89 | {"/signin?ways=1&way=oficina", nil, SigninOficinaPath}, |
| 90 | {"/signin?way=email&return_to=%2Fd%2F1", nil, "/signin/oficina?return_to=%2Fd%2F1"}, |
| 91 | // A return_to connect would not follow is left off, as the exchange |
| 92 | // would land on "/" anyway. |
| 93 | {"/signin?return_to=https%3A%2F%2Fevil.example%2F", nil, SigninOficinaPath}, |
| 94 | {"/signin?return_to=%2F%2Fevil.example", nil, SigninOficinaPath}, |
| 95 | {"/signin?return_to=%2Fsignin", nil, SigninOficinaPath}, |
| 96 | {"/signin?return_to=%2Fa&return_to=%2Fb", nil, SigninOficinaPath}, |
| 97 | } { |
| 98 | calls := f.calls |
| 99 | w := f.get(f.h.SigninDoor, tc.target, tc.cookies...) |
| 100 | if w.Code != 299 || f.calls != calls+1 { |
| 101 | t.Fatalf("%s: %d %q, want the door", tc.target, w.Code, w.Header().Get("Location")) |
| 102 | } |
| 103 | if f.href != tc.href { |
| 104 | t.Fatalf("%s: Oficina href %q, want %q", tc.target, f.href, tc.href) |
| 105 | } |
| 106 | if len(w.Result().Cookies()) != 0 { |
| 107 | t.Fatalf("%s: the door set a cookie", tc.target) |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // TestSigninWhenAlreadySignedIn is spec shared-chrome §9.4, kept on the |
| 113 | // door: a live session goes straight to the validated return_to, or /, |
| 114 | // with no door and no exchange. A query naming a state of the door (a |
| 115 | // sent link, a problem, Keymail's force, a step-up, a continuation) is |
| 116 | // drawn even then. |
| 117 | func TestSigninWhenAlreadySignedIn(t *testing.T) { |
| 118 | f := newDoorFixture(t) |
| 119 | for _, tc := range []struct{ query, want string }{ |
| 120 | {"?return_to=%2Fd%2F42%3Fview%3Dcompact", "/d/42?view=compact"}, |
| 121 | {"", "/"}, |
| 122 | {"?return_to=https%3A%2F%2Fevil.example%2F", "/"}, |
| 123 | {"?return_to=%2F%2Fevil.example", "/"}, |
| 124 | {"?return_to=%2Fsignin", "/"}, |
| 125 | {"?return_to=%2Fa&return_to=%2Fb", "/"}, |
| 126 | } { |
| 127 | for _, route := range []struct { |
| 128 | path string |
| 129 | handler http.HandlerFunc |
| 130 | }{{SigninPath, f.h.SigninDoor}, {SigninOficinaPath, f.h.SigninOficina}} { |
| 131 | w := f.get(route.handler, route.path+tc.query, liveSession) |
| 132 | if w.Code != 303 || w.Header().Get("Location") != tc.want { |
| 133 | t.Fatalf("%s%s: %d %q", route.path, tc.query, w.Code, w.Header().Get("Location")) |
| 134 | } |
| 135 | if w.Header().Get("Cache-Control") != "no-store" || len(w.Result().Cookies()) != 0 { |
| 136 | t.Fatalf("%s%s: cacheable, or set a cookie", route.path, tc.query) |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | for _, query := range []string{"?force=1", "?err=removed", "?err=rate", "?sent=1&attempt=x", "?reauth=1", "?continue=abc"} { |
| 141 | calls := f.calls |
| 142 | if w := f.get(f.h.SigninDoor, SigninPath+query, liveSession); w.Code != 299 || f.calls != calls+1 { |
| 143 | t.Fatalf("%s with a live session: %d, want the door", query, w.Code) |
| 144 | } |
| 145 | } |
| 146 | if w := f.get(f.h.SigninDoor, "/signin?%zz", nil...); w.Code != 400 { |
| 147 | t.Fatal("a malformed query reached the door", w.Code) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // TestSigninLocalIsEmailOnly: /signin/local is where connect sends people |
| 152 | // Oficina ID could not help (unknown, failed, a link to resume, "Connect my |
| 153 | // existing account", a step-up to link). Its door must never offer Oficina |
| 154 | // ID, or those people would loop through Home and back. It has no |
| 155 | // signed-in shortcut either: a signed-in person too stale to link is sent |
| 156 | // here precisely to sign in again. |
| 157 | func TestSigninLocalIsEmailOnly(t *testing.T) { |
| 158 | f := newDoorFixture(t) |
| 159 | for _, target := range []string{"/signin/local", "/signin/local?return_to=%2Fd%2F42", "/signin/local?way=oficina", "/signin/local?ways=1"} { |
| 160 | for _, cookies := range [][]*http.Cookie{nil, {liveSession}} { |
| 161 | calls := f.calls |
| 162 | w := f.get(f.h.SigninLocal, target, cookies...) |
| 163 | if w.Code != 299 || f.calls != calls+1 || f.path != SigninLocalPath { |
| 164 | t.Fatalf("%s (%d cookies): %d %q, want the door", target, len(cookies), w.Code, w.Header().Get("Location")) |
| 165 | } |
| 166 | if f.href != "" { |
| 167 | t.Fatalf("%s offered Oficina ID: %q", target, f.href) |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | // Every flow that ends locally ends here. |
| 172 | for state, want := range map[string]string{"unknown": SigninLocalPath, "failed": SigninLocalPath, "anything": SigninLocalPath, "signedout": SigninPath} { |
| 173 | if got := Page(state, "").Action; got != want { |
| 174 | t.Errorf("%s leads to %q, want %q", state, got, want) |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // TestOficinaHrefOnlyWhereTheDoorMayOfferIt: the href exists only on a |
| 180 | // request connect's /signin handed to the app, and never on a step-up. |
| 181 | func TestOficinaHrefOnlyWhereTheDoorMayOfferIt(t *testing.T) { |
| 182 | if got := OficinaHref(httptest.NewRequest(http.MethodGet, navigationOrigin+"/signin", nil)); got != "" { |
| 183 | t.Fatalf("a door connect did not hand over offers Oficina ID: %q", got) |
| 184 | } |
| 185 | f := newDoorFixture(t) |
| 186 | for _, query := range []string{"?reauth=1", "?reauth=1&return_to=%2Fd%2F42", "?reauth=0", "?reauth=1&way=oficina", "?ways=1&reauth=1"} { |
| 187 | f.get(f.h.SigninDoor, SigninPath+query) |
| 188 | if f.href != "" { |
| 189 | t.Fatalf("%s: a step-up offered Oficina ID: %q", query, f.href) |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // TestSigninOficinaStartsTheExchange is "Continue with Oficina ID": the |
| 195 | // sealed return cookie remembers the page, and the browser goes to |
| 196 | // handoff.StartPath with it. A return_to connect would not follow is "/". |
| 197 | func TestSigninOficinaStartsTheExchange(t *testing.T) { |
| 198 | f := newDoorFixture(t) |
| 199 | for _, tc := range []struct { |
| 200 | query string |
| 201 | cookies []*http.Cookie |
| 202 | want string |
| 203 | }{ |
| 204 | {"?return_to=%2Fd%2F42%3Fview%3Dcompact", nil, "/d/42?view=compact"}, |
| 205 | {"", nil, "/"}, |
| 206 | {"?return_to=https%3A%2F%2Fevil.example%2F", nil, "/"}, |
| 207 | {"?return_to=%2F%2Fevil.example", nil, "/"}, |
| 208 | {"?return_to=%2Fsignin", nil, "/"}, |
| 209 | {"?return_to=%2Fauth%2Fstart", nil, "/"}, |
| 210 | {"?return_to=%2Fa&return_to=%2Fb", nil, "/"}, |
| 211 | {"?return_to=javascript%3Aalert(1)", nil, "/"}, |
| 212 | {"?return_to=%2Fd%2F42", []*http.Cookie{staleSession}, "/d/42"}, |
| 213 | {"?return_to=%2Fd%2F42", []*http.Cookie{liveSession, liveSession}, "/d/42"}, |
| 214 | {"?return_to=%2Fd%2F42", []*http.Cookie{guestSession}, "/d/42"}, |
| 215 | } { |
| 216 | w := f.get(f.h.SigninOficina, SigninOficinaPath+tc.query, tc.cookies...) |
| 217 | start := handoff.StartPath + "?" + url.Values{"return_to": {tc.want}}.Encode() |
| 218 | if w.Code != 303 || w.Header().Get("Location") != start { |
| 219 | t.Fatalf("%s: %d %q", tc.query, w.Code, w.Header().Get("Location")) |
| 220 | } |
| 221 | if w.Header().Get("Cache-Control") != "no-store" { |
| 222 | t.Fatalf("%s: cacheable", tc.query) |
| 223 | } |
| 224 | var remembered *http.Cookie |
| 225 | for _, c := range w.Result().Cookies() { |
| 226 | if c.Name == homeReturnCookie { |
| 227 | remembered = c |
| 228 | } |
| 229 | } |
| 230 | if remembered == nil || remembered.MaxAge <= 0 || !remembered.Secure || !remembered.HttpOnly { |
| 231 | t.Fatalf("%s: the page was not remembered", tc.query) |
| 232 | } |
| 233 | back := httptest.NewRequest(http.MethodGet, navigationOrigin+"/auth/finish", nil) |
| 234 | back.AddCookie(remembered) |
| 235 | if got := f.h.navigation.returnPath(back); got != tc.want { |
| 236 | t.Fatalf("%s: remembered %q, want %q", tc.query, got, tc.want) |
| 237 | } |
| 238 | if f.calls != 0 { |
| 239 | t.Fatal("Continue with Oficina ID drew the door") |
| 240 | } |
| 241 | } |
| 242 | if w := f.get(f.h.SigninOficina, SigninOficinaPath+"?%zz"); w.Code != 400 || w.Header().Get("Location") != "" { |
| 243 | t.Fatal("a malformed query started an exchange", w.Code) |
| 244 | } |
| 245 | // Failing to remember is a 503, never an exchange that forgets the page. |
| 246 | f.h.navigation.now = func() time.Time { return time.Unix(0, 0) } |
| 247 | if w := f.get(f.h.SigninOficina, SigninOficinaPath+"?return_to=%2Fd%2F42"); w.Code != 503 || w.Header().Get("Location") != "" { |
| 248 | t.Fatal("an unremembered return started the exchange", w.Code) |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // TestStepUpNeverReachesHome is the spec's reauth rule: a step-up offers |
| 253 | // local ways only. Linking needs FreshLocal, which no Home sign-in gives, |
| 254 | // and a live id.* session would hand back an old auth_time. So a reauth |
| 255 | // that reaches /signin/oficina anyway goes to the local door, keeping it. |
| 256 | func TestStepUpNeverReachesHome(t *testing.T) { |
| 257 | f := newDoorFixture(t) |
| 258 | for _, tc := range []struct{ query, want string }{ |
| 259 | {"?reauth=1&return_to=%2Fd%2F42", "/signin/local?reauth=1&return_to=%2Fd%2F42"}, |
| 260 | {"?reauth=1", "/signin/local?reauth=1"}, |
| 261 | {"?reauth=yes&return_to=%2F%2Fevil.example", "/signin/local?reauth=1"}, |
| 262 | } { |
| 263 | for _, cookies := range [][]*http.Cookie{nil, {liveSession}} { |
| 264 | w := f.get(f.h.SigninOficina, SigninOficinaPath+tc.query, cookies...) |
| 265 | if w.Code != 303 || w.Header().Get("Location") != tc.want || len(w.Result().Cookies()) != 0 { |
| 266 | t.Fatalf("%s: %d %q", tc.query, w.Code, w.Header().Get("Location")) |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | // The link flow's own step-up: a session too old to link goes to the |
| 271 | // local door, which offers no Oficina ID. |
| 272 | _, n := navigationFixture(t) |
| 273 | f.h.navigation = n |
| 274 | w := httptest.NewRecorder() |
| 275 | n.Begin(w, httptest.NewRequest(http.MethodGet, navigationOrigin+"/account/home", nil)) |
| 276 | if w.Code != 303 || w.Header().Get("Location") != linkStepUpPath { |
| 277 | t.Fatalf("link step-up: %d %q", w.Code, w.Header().Get("Location")) |
| 278 | } |
| 279 | f.get(f.h.SigninLocal, w.Header().Get("Location"), liveSession) |
| 280 | if f.href != "" { |
| 281 | t.Fatal("the link step-up offered Oficina ID") |
| 282 | } |
| 283 | if _, fresh := FreshLocal(Session{Subject: "pr_x", Method: HomeMethod, AuthTime: time.Now()}, time.Now()); fresh { |
| 284 | t.Fatal("a Home sign-in counted as a fresh local one") |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | // TestLinkStepUpStaysLocalPastTheFirstPage is Fable's finding 1 on |
| 289 | // f763fbc. Linking sends a signed-in person to sign in again, and the |
| 290 | // first page is /signin/local. Every page after it is rastrillo's: an |
| 291 | // outcome, a chooser link, Forget and Begin all go to auth.Config.SigninPath, |
| 292 | // which is /signin on the smart door. Marked reauth=1, they carry the |
| 293 | // step-up, so /signin still draws the door (no shortcut to "/" before the |
| 294 | // person has signed in again) and still offers no Oficina ID. |
| 295 | func TestLinkStepUpStaysLocalPastTheFirstPage(t *testing.T) { |
| 296 | f := newDoorFixture(t) |
| 297 | _, n := navigationFixture(t) |
| 298 | w := httptest.NewRecorder() |
| 299 | n.Begin(w, httptest.NewRequest(http.MethodGet, navigationOrigin+"/account/home", nil)) |
| 300 | first, err := url.Parse(w.Header().Get("Location")) |
| 301 | if err != nil || first.Path != SigninLocalPath || first.Query().Get("reauth") != "1" { |
| 302 | t.Fatalf("link step-up is not marked: %q", w.Header().Get("Location")) |
| 303 | } |
| 304 | // What rastrillo v0.38.0's one-way screen sends next, with SigninPath |
| 305 | // /signin (auth/oneway.go: signinHref sets reauth=1 on every chooser |
| 306 | // link and Back; afterForget and keepReauth keep it on Forget's and |
| 307 | // Begin's redirects). connect cannot import auth here without pulling |
| 308 | // rastrillo's whole module graph into its go.sum, so the shapes are |
| 309 | // written out. |
| 310 | for _, next := range []string{ |
| 311 | SigninPath + "?reauth=1&way=email", // EmailHref, Use a different email |
| 312 | SigninPath + "?reauth=1", // Back |
| 313 | SigninPath + "?err=rate&reauth=1", // Begin's rate limit, kept by keepReauth |
| 314 | SigninPath + "?reauth=1&return_to=%2Fd%2F42&ways=1", // the chooser |
| 315 | } { |
| 316 | calls := f.calls |
| 317 | w := f.get(f.h.SigninDoor, next, liveSession) |
| 318 | if w.Code != 299 || f.calls != calls+1 { |
| 319 | t.Fatalf("%s bounced a person stepping up: %d %q", next, w.Code, w.Header().Get("Location")) |
| 320 | } |
| 321 | if f.href != "" { |
| 322 | t.Fatalf("%s offered Oficina ID to a step-up", next) |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | // TestSignedOutOffersForget: with ForgetPath, the signed-out page carries a |
| 328 | // second form, "Forget me on this device", posting to the app's Forget. |
| 329 | // Without it there is none, as before. It is a plain same-origin form, so |
| 330 | // the app's csrf.Protect admits it from this origin, refuses it from any |
| 331 | // other, and Middleware exempts nothing for it. |
| 332 | func TestSignedOutOffersForget(t *testing.T) { |
| 333 | f := newBridgeFixture(t) |
| 334 | for _, forget := range []string{"", "/signin/forget", "/auth/forget"} { |
| 335 | var got PageData |
| 336 | cfg := RoutesConfig{Namespace: "oficina/docs", Origin: bridgeOrigin, Issuer: bridgeIssuer, SessionCookie: f.sessions.cookie, DEK: bytes.Repeat([]byte{41}, 32), ForgetPath: forget, |
| 337 | ReadSession: func(*http.Request) (*Session, error) { return nil, nil }, |
| 338 | SignOut: func(http.ResponseWriter, *http.Request) {}, LocalSignin: func(http.ResponseWriter, *http.Request) {}, |
| 339 | Protect: csrf.Protect(bridgeOrigin), |
| 340 | Render: func(w http.ResponseWriter, r *http.Request, state, label string, status int) { |
| 341 | got = PageFor(r, state, label) |
| 342 | for _, other := range []string{"failed", "unknown", "success", "start"} { |
| 343 | if PageFor(r, other, label).ForgetAction != "" { |
| 344 | t.Fatalf("%s offered Forget", other) |
| 345 | } |
| 346 | } |
| 347 | w.WriteHeader(status) |
| 348 | }} |
| 349 | h, err := NewRoutes(f.b, cfg) |
| 350 | if err != nil { |
| 351 | t.Fatal(forget, err) |
| 352 | } |
| 353 | w := httptest.NewRecorder() |
| 354 | h.SignedOut(w, bridgeRequest(http.MethodGet, "/signed-out", nil)) |
| 355 | if w.Code != 200 || got.Title != "You’re signed out" || got.Action != SigninPath || got.ForgetAction != forget { |
| 356 | t.Fatalf("ForgetPath %q: %d %+v", forget, w.Code, got) |
| 357 | } |
| 358 | if forget == "" { |
| 359 | continue |
| 360 | } |
| 361 | reached := 0 |
| 362 | handler := h.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { reached++; w.WriteHeader(303) })) |
| 363 | for _, tc := range []struct { |
| 364 | header, value string |
| 365 | want int |
| 366 | }{{"Origin", bridgeOrigin, 303}, {"Sec-Fetch-Site", "same-origin", 303}, {"Origin", "https://evil.example", 403}, {"Sec-Fetch-Site", "cross-site", 403}, {"", "", 403}} { |
| 367 | r := httptest.NewRequest(http.MethodPost, bridgeOrigin+forget, strings.NewReader("")) |
| 368 | r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 369 | if tc.header != "" { |
| 370 | r.Header.Set(tc.header, tc.value) |
| 371 | } |
| 372 | w := httptest.NewRecorder() |
| 373 | handler.ServeHTTP(w, r) |
| 374 | if w.Code != tc.want { |
| 375 | t.Fatalf("Forget POST with %s %q: %d, want %d", tc.header, tc.value, w.Code, tc.want) |
| 376 | } |
| 377 | } |
| 378 | if reached != 2 { |
| 379 | t.Fatal("Forget reached past the same-origin check", reached) |
| 380 | } |
| 381 | } |
| 382 | // Only the page Routes.SignedOut draws carries it. |
| 383 | r := bridgeRequest(http.MethodGet, "/signed-out", nil) |
| 384 | if Page("signedout", "").ForgetAction != "" || PageFor(r, "signedout", "").ForgetAction != "" { |
| 385 | t.Fatal("Forget offered without the signed-out route") |
| 386 | } |
| 387 | // The Home-derived sign-out still chains to id.* (TestSignoutChainsToTheIssuer). |
| 388 | } |
| 389 | |
| 390 | // TestForgetPathMustBeLocal: a form action that could leave the origin, or |
| 391 | // carry a query, is refused at boot rather than drawn. |
| 392 | func TestForgetPathMustBeLocal(t *testing.T) { |
| 393 | f := newBridgeFixture(t) |
| 394 | for _, bad := range []string{"signin/forget", "//evil.example/forget", "https://evil.example/forget", "/forget?x=1", "/forget#x", "/a/../forget", "/a\\forget", "/a%2Fforget", "/forget/", "/a//forget", "javascript:alert(1)"} { |
| 395 | _, err := NewRoutes(f.b, RoutesConfig{Namespace: "oficina/docs", Origin: bridgeOrigin, Issuer: bridgeIssuer, SessionCookie: f.sessions.cookie, DEK: bytes.Repeat([]byte{41}, 32), ForgetPath: bad, |
| 396 | ReadSession: func(*http.Request) (*Session, error) { return nil, nil }, SignOut: func(http.ResponseWriter, *http.Request) {}, LocalSignin: func(http.ResponseWriter, *http.Request) {}, |
| 397 | Protect: func(next http.Handler) http.Handler { return next }, Render: f.b.render}) |
| 398 | if err == nil { |
| 399 | t.Errorf("ForgetPath %q accepted", bad) |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | // TestPageDataStillDrawsTheSharedTemplate: PageData stopped being an alias |
| 405 | // of estilo's homeconnect.PageData when it gained ForgetAction. Its shared |
| 406 | // fields keep their names, so estilo's home-connect template draws it. |
| 407 | func TestPageDataStillDrawsTheSharedTemplate(t *testing.T) { |
| 408 | view := template.Must(template.ParseFS(Templates(), "templates/*.html")) |
| 409 | var out bytes.Buffer |
| 410 | if err := view.ExecuteTemplate(&out, "home-connect", Page("confirm", "person@example.test")); err != nil { |
| 411 | t.Fatal(err) |
| 412 | } |
| 413 | for _, want := range []string{English["confirm_title"], `action="/account/home/confirm"`, "<bdi>person@example.test</bdi>", English["cancel_action"]} { |
| 414 | if !strings.Contains(out.String(), want) { |
| 415 | t.Fatalf("missing %q in %s", want, out.String()) |
| 416 | } |
| 417 | } |
| 418 | // And a struct embedding it, as Slides and Sheets do. |
| 419 | type appView struct{ PageData } |
| 420 | out.Reset() |
| 421 | if err := view.ExecuteTemplate(&out, "home-connect", appView{Page("start", "")}); err != nil || !strings.Contains(out.String(), English["start_title"]) { |
| 422 | t.Fatal("an embedding view no longer draws", err) |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | // TestMintedPersonIsWhoJustSignedIn: MintHomeSession is where an app |
| 427 | // remembers "Continue with Oficina ID" (the smart door's federated record), |
| 428 | // with the address of the person who just signed in, or none. connect hands |
| 429 | // it that person on the minting request only: the linked local account, or |
| 430 | // a Home-only person with no address. Never on link, choose or unknown. |
| 431 | func TestMintedPersonIsWhoJustSignedIn(t *testing.T) { |
| 432 | type mint struct { |
| 433 | person Person |
| 434 | ok bool |
| 435 | } |
| 436 | watch := func(f *bridgeFixture) *[]mint { |
| 437 | var seen []mint |
| 438 | inner := f.b.mintSession |
| 439 | f.b.mintSession = func(w http.ResponseWriter, r *http.Request, s Session) error { |
| 440 | p, ok := MintedPerson(r) |
| 441 | seen = append(seen, mint{p, ok}) |
| 442 | return inner(w, r, s) |
| 443 | } |
| 444 | return &seen |
| 445 | } |
| 446 | |
| 447 | f := newBridgeFixture(t) |
| 448 | seen := watch(f) |
| 449 | local := f.local(t, "one@example.com", "magiclink", time.Time{}) |
| 450 | f.link(t, local, "opaque-subject-one") |
| 451 | if len(*seen) != 0 { |
| 452 | t.Fatal("linking minted a Home session") |
| 453 | } |
| 454 | unknown := f.finishAs(t, "opaque-subject-nobody", nil) |
| 455 | if unknown.Code != 403 || len(*seen) != 0 { |
| 456 | t.Fatal("an unknown subject minted", unknown.Code) |
| 457 | } |
| 458 | if w := f.finishAs(t, "opaque-subject-one", nil); w.Code != 303 { |
| 459 | t.Fatalf("ordinary finish=%d", w.Code) |
| 460 | } |
| 461 | var id int64 |
| 462 | if err := f.db.QueryRow("SELECT id FROM people WHERE email='one@example.com'").Scan(&id); err != nil { |
| 463 | t.Fatal(err) |
| 464 | } |
| 465 | if len(*seen) != 1 || !(*seen)[0].ok || (*seen)[0].person != (Person{ID: id, Label: "one@example.com"}) { |
| 466 | t.Fatalf("minted with %+v", *seen) |
| 467 | } |
| 468 | if _, ok := MintedPerson(bridgeRequest(http.MethodGet, "/", nil)); ok { |
| 469 | t.Fatal("a person on a request that minted nothing") |
| 470 | } |
| 471 | |
| 472 | team, _ := newHomeOnlyFixture(t) |
| 473 | seen = watch(team) |
| 474 | finish := team.finishAs(t, "pr_member", nil) |
| 475 | if len(*seen) != 0 { |
| 476 | t.Fatal("the choose page minted") |
| 477 | } |
| 478 | if _, w := team.choose(t, finish, "new", nil); w.Code != 303 { |
| 479 | t.Fatalf("choose new=%d", w.Code) |
| 480 | } |
| 481 | if len(*seen) != 1 || !(*seen)[0].ok || (*seen)[0].person.ID <= 1000 || (*seen)[0].person.Label != "" { |
| 482 | t.Fatalf("a new member minted with %+v", *seen) |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | // TestConnectImportsNoLastsignin: remembering the way in is each app's job |
| 487 | // (MintHomeSession), so connect never forces a rastrillo minor on its |
| 488 | // consumers by importing lastsignin, directly or through auth. |
| 489 | func TestConnectImportsNoLastsignin(t *testing.T) { |
| 490 | out, err := exec.Command("go", "list", "-deps", "./...").Output() |
| 491 | if err != nil { |
| 492 | t.Fatal("go list -deps failed, so nothing was checked:", err) |
| 493 | } |
| 494 | if !strings.Contains(string(out), "amadan.net/oficina/home/connect\n") { |
| 495 | t.Fatal("go list -deps did not list connect itself") |
| 496 | } |
| 497 | for _, dep := range strings.Fields(string(out)) { |
| 498 | switch dep { |
| 499 | case "amadan.net/rastrillo/rastrillo/lastsignin", "amadan.net/rastrillo/rastrillo/auth": |
| 500 | t.Errorf("connect depends on %s", dep) |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | |