| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httptest" |
| 6 | "net/url" |
| 7 | "regexp" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/home/internal/home/sealtest" |
| 12 | ) |
| 13 | |
| 14 | // signedInPair walks a full sign-in through both roles and returns the |
| 15 | // credential-origin session and the directory session it produced. |
| 16 | func signedInPair(t *testing.T, p exchangePair) (credential, directory *http.Cookie) { |
| 17 | t.Helper() |
| 18 | credential = signInFully(t, p.credential, sealtest.CanaryAddress) |
| 19 | pending, intent := p.start(t) |
| 20 | token, _ := p.mint(t, intent, credential) |
| 21 | return credential, p.finish(t, token, pending) |
| 22 | } |
| 23 | |
| 24 | // resumeMints reports whether the credential origin would mint an |
| 25 | // assertion for this session without asking anything: the silent |
| 26 | // re-entry that made "Sign out" a lie (polish audit B2). |
| 27 | func resumeMints(t *testing.T, p exchangePair, credential *http.Cookie) bool { |
| 28 | t.Helper() |
| 29 | _, intent := p.start(t) |
| 30 | rec := exchangeRequest(p.credential.App, "POST", Origin+"/authorize/resume", Origin, nil, intent, credential) |
| 31 | switch { |
| 32 | case rec.Code == 200 && strings.Contains(rec.Body.String(), `name="assertion"`): |
| 33 | return true |
| 34 | case rec.Code == 403 || (rec.Code == 303 && strings.HasPrefix(rec.Header().Get("Location"), "/signin")): |
| 35 | return false |
| 36 | } |
| 37 | t.Fatalf("resume answered %d %q", rec.Code, rec.Header().Get("Location")) |
| 38 | return false |
| 39 | } |
| 40 | |
| 41 | // signoutHop posts to the credential sign-out with the given evidence |
| 42 | // headers; an empty value leaves that header out. |
| 43 | func signoutHop(p exchangePair, origin, fetchSite, referer string, cookies ...*http.Cookie) *httptest.ResponseRecorder { |
| 44 | r := httptest.NewRequest("POST", Origin+"/signout", nil) |
| 45 | for k, v := range map[string]string{"Origin": origin, "Sec-Fetch-Site": fetchSite, "Referer": referer} { |
| 46 | if v != "" { |
| 47 | r.Header.Set(k, v) |
| 48 | } |
| 49 | } |
| 50 | for _, c := range cookies { |
| 51 | r.AddCookie(c) |
| 52 | } |
| 53 | rec := httptest.NewRecorder() |
| 54 | p.credential.Router().ServeHTTP(rec, r) |
| 55 | return rec |
| 56 | } |
| 57 | |
| 58 | func clearsCookie(rec interface{ Result() *http.Response }, name string) bool { |
| 59 | for _, c := range rec.Result().Cookies() { |
| 60 | if c.Name == name && c.Value == "" && c.MaxAge < 0 { |
| 61 | return true |
| 62 | } |
| 63 | } |
| 64 | return false |
| 65 | } |
| 66 | |
| 67 | // TestSignOutEndsTheCredentialSession is B2: signing out of the |
| 68 | // directory must also end the id.* session, so the next Sign in asks for |
| 69 | // the email (or passkey) step instead of bouncing straight back in. |
| 70 | func TestSignOutEndsTheCredentialSession(t *testing.T) { |
| 71 | p := newExchangePair(t) |
| 72 | credential, directory := signedInPair(t, p) |
| 73 | if !resumeMints(t, p, credential) { |
| 74 | t.Fatal("precondition: a live credential session mints silently") |
| 75 | } |
| 76 | |
| 77 | out := exchangeRequest(p.directory, "POST", directoryOrigin+"/signout", directoryOrigin, url.Values{}, directory) |
| 78 | if out.Code != http.StatusSeeOther || out.Header().Get("Location") != "/signed-out" { |
| 79 | t.Fatal("local landing missing") |
| 80 | } |
| 81 | if !clearsCookie(out, directory.Name) { |
| 82 | t.Fatal("directory cookie survived") |
| 83 | } |
| 84 | landing := exchangeRequest(p.directory, "GET", directoryOrigin+out.Header().Get("Location"), "", nil, out.Result().Cookies()...) |
| 85 | action := signoutFormAction(t, landing.Body.String()) |
| 86 | if action != Origin+"/signout" { |
| 87 | t.Fatal("wrong issuer form action", action) |
| 88 | } |
| 89 | if !strings.Contains(landing.Body.String(), "/static/assertion.js") { |
| 90 | t.Fatal("automatic second step missing") |
| 91 | } |
| 92 | // Follow the page's actual form, preserving its method and browser origin. |
| 93 | end := exchangeRequest(p.credential.App, "POST", action, directoryOrigin, url.Values{}, credential) |
| 94 | if end.Code != http.StatusSeeOther || end.Header().Get("Location") != directoryOrigin+"/signed-out" { |
| 95 | t.Fatalf("credential sign-out: %d %q", end.Code, end.Header().Get("Location")) |
| 96 | } |
| 97 | if !clearsCookie(end, credential.Name) { |
| 98 | t.Fatal("credential sign-out left its cookie") |
| 99 | } |
| 100 | if end.Header().Get("Cache-Control") != "no-store" { |
| 101 | t.Fatal("sign-out response cacheable") |
| 102 | } |
| 103 | |
| 104 | returned := exchangeRequest(p.directory, "GET", end.Header().Get("Location"), "", nil, landing.Result().Cookies()...) |
| 105 | if returned.Code != 200 || strings.Contains(returned.Body.String(), "data-assertion-handoff") { |
| 106 | t.Fatal("signout return loops") |
| 107 | } |
| 108 | if page := exchangeRequest(p.directory, "GET", directoryOrigin+"/", "", nil, directory); strings.Contains(page.Body.String(), "You are signed in.") { |
| 109 | t.Fatal("directory session survived") |
| 110 | } |
| 111 | |
| 112 | // The cookie is gone from the browser, but a copy of it must not work |
| 113 | // either: the row is revoked, not merely forgotten. |
| 114 | if resumeMints(t, p, credential) { |
| 115 | t.Fatal("credential session survived sign-out: Sign in re-enters without a prompt") |
| 116 | } |
| 117 | var rows int |
| 118 | if err := p.credential.db.Writer().QueryRow(`SELECT count(*) FROM sessions`).Scan(&rows); err != nil || rows != 0 { |
| 119 | t.Fatal("credential session row not revoked", rows, err) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // TestCredentialSignOutRefusesOtherOrigins: the credential sign-out is |
| 124 | // outside csrf.Protect (the directory's hand-off arrives same-site, not |
| 125 | // same-origin), so it carries its own exact origin check. Anything other |
| 126 | // than a registered consumer (the directory is one) or the credential |
| 127 | // origin itself is refused and the session survives, and a GET does |
| 128 | // nothing at all. blue.docs is registered in newExchangePair; red.docs is |
| 129 | // the same site but is not. |
| 130 | func TestCredentialSignOutRefusesOtherOrigins(t *testing.T) { |
| 131 | p := newExchangePair(t) |
| 132 | credential, _ := signedInPair(t, p) |
| 133 | for _, c := range []struct{ origin, site, referer string }{ |
| 134 | {"", "", ""}, |
| 135 | {"null", "", ""}, |
| 136 | {"null", "same-site", ""}, |
| 137 | {"https://evil.invalid", "cross-site", directoryOrigin + "/"}, |
| 138 | {"https://red.docs.oficina.works", "same-site", ""}, |
| 139 | {"https://red.docs.oficina.works", "same-site", directoryOrigin + "/"}, |
| 140 | {"http://oficina.works", "cross-site", ""}, |
| 141 | {"https://oficina.works.evil.invalid", "cross-site", ""}, |
| 142 | {"null", "same-site", "https://red.docs.oficina.works/"}, |
| 143 | {"null", "cross-site", directoryOrigin + "/"}, |
| 144 | {"null", "same-site", "http://oficina.works/"}, |
| 145 | {"null", "same-site", "https://oficina.works.evil.invalid/"}, |
| 146 | {"null", "same-site", "https://user@oficina.works/"}, |
| 147 | {"", "same-site", "https://evil.invalid/"}, |
| 148 | } { |
| 149 | rec := signoutHop(p, c.origin, c.site, c.referer, credential) |
| 150 | if rec.Code != http.StatusForbidden || clearsCookie(rec, credential.Name) { |
| 151 | t.Fatalf("%+v: %d", c, rec.Code) |
| 152 | } |
| 153 | } |
| 154 | // Both shapes of the directory's own hand-off pass: Origin named (the |
| 155 | // Fetch spec's reading), or nulled with Referer (Chromium's). |
| 156 | for _, c := range []struct{ origin, site, referer string }{ |
| 157 | {directoryOrigin, "same-site", ""}, |
| 158 | {"null", "same-site", directoryOrigin + "/"}, |
| 159 | {"", "", directoryOrigin + "/"}, |
| 160 | } { |
| 161 | fresh, _ := signedInPair(t, p) |
| 162 | if rec := signoutHop(p, c.origin, c.site, c.referer, fresh); rec.Code != http.StatusSeeOther || resumeMints(t, p, fresh) { |
| 163 | t.Fatalf("directory hand-off %+v refused or left the session: %d", c, rec.Code) |
| 164 | } |
| 165 | } |
| 166 | if rec := exchangeRequest(p.credential.App, "GET", Origin+"/signout", "", nil, credential); rec.Code/100 == 3 || clearsCookie(rec, credential.Name) { |
| 167 | t.Fatal("GET signed out", rec.Code) |
| 168 | } |
| 169 | if !resumeMints(t, p, credential) { |
| 170 | t.Fatal("refused sign-out still ended the session") |
| 171 | } |
| 172 | // Its own origin may sign out too, for a future button on id.* pages. |
| 173 | if rec := exchangeRequest(p.credential.App, "POST", Origin+"/signout", Origin, url.Values{}, credential); rec.Code != http.StatusSeeOther { |
| 174 | t.Fatal("same-origin sign-out refused", rec.Code) |
| 175 | } |
| 176 | if resumeMints(t, p, credential) { |
| 177 | t.Fatal("same-origin sign-out left the session") |
| 178 | } |
| 179 | // Neither role answers the other's host: the credential sign-out is |
| 180 | // id.*-only, and the directory's is the directory's. |
| 181 | if rec := exchangeRequest(p.directory, "POST", Origin+"/signout", directoryOrigin, url.Values{}); rec.Code != 404 { |
| 182 | t.Fatal("directory served credential sign-out", rec.Code) |
| 183 | } |
| 184 | if rec := exchangeRequest(p.credential.App, "POST", directoryOrigin+"/signout", directoryOrigin, url.Values{}); rec.Code != 404 { |
| 185 | t.Fatal("credential served directory sign-out", rec.Code) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // TestDirectoryPolicyAdmitsTheSignOutHandOff: CSP form-action is enforced |
| 190 | // across a form's redirects, so without the credential origin in the |
| 191 | // directory's form-action the 307 is blocked in the browser and B2 comes |
| 192 | // back with nothing on screen but a console error. |
| 193 | func TestDirectoryPolicyAdmitsTheSignOutHandOff(t *testing.T) { |
| 194 | p := newExchangePair(t) |
| 195 | policy := p.directory.SecurityPolicy() |
| 196 | var formAction string |
| 197 | for _, d := range strings.Split(policy, ";") { |
| 198 | if d = strings.TrimSpace(d); strings.HasPrefix(d, "form-action ") { |
| 199 | formAction = d |
| 200 | } |
| 201 | } |
| 202 | found := false |
| 203 | for _, source := range strings.Fields(formAction) { |
| 204 | found = found || source == Origin |
| 205 | } |
| 206 | if !found { |
| 207 | t.Fatalf("directory form-action omits %s: %q", Origin, formAction) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | const ( |
| 212 | teamOrigin = "https://blue.docs.oficina.works" |
| 213 | previewOrigin = "https://sheets-preview.example.net" |
| 214 | ) |
| 215 | |
| 216 | // withPreviewConsumer registers a preview deployment beside the pair's |
| 217 | // directory and team consumers: an origin that is registered but is not |
| 218 | // on the suite's site, so the browser marks its hop cross-site. |
| 219 | func withPreviewConsumer(p exchangePair) exchangePair { |
| 220 | p.credential.runtime.consumers["preview_sheets"] = previewOrigin |
| 221 | return p |
| 222 | } |
| 223 | |
| 224 | // isFinishSigningOut reports whether rec is the branded refusal page: the |
| 225 | // way out for a hop that arrived without evidence, a same-origin form |
| 226 | // that csrf.SameOrigin admits without any Referer. |
| 227 | func isFinishSigningOut(rec *httptest.ResponseRecorder) bool { |
| 228 | body := rec.Body.String() |
| 229 | return strings.HasPrefix(rec.Header().Get("Content-Type"), "text/html") && |
| 230 | rec.Header().Get("Cache-Control") == "no-store" && |
| 231 | strings.Contains(body, "<title>Finish signing out · Oficina</title>") && |
| 232 | strings.Contains(body, "<h1>Finish signing out</h1>") && |
| 233 | strings.Contains(body, "Select Sign out to finish signing out of Oficina on this device.") && |
| 234 | strings.Contains(body, `<form method="post" action="/signout"`) && |
| 235 | strings.Contains(body, `<button type="submit" rst-btn="primary" estilo-signin-action>Sign out</button>`) |
| 236 | } |
| 237 | |
| 238 | // TestCredentialSignOutAdmitsRegisteredConsumers is round 2's widening |
| 239 | // (spec §9.5): an app's own Sign out 307s to id.*/signout, so every |
| 240 | // origin in HOME_CONSUMERS is admitted, by Origin or (Origin nulled by |
| 241 | // the redirect, as Chromium and WebKit do) by Referer, and lands on THAT |
| 242 | // origin's /signed-out. The destination comes from the matched |
| 243 | // registration and never from the request. |
| 244 | func TestCredentialSignOutAdmitsRegisteredConsumers(t *testing.T) { |
| 245 | for _, c := range []struct{ origin, site, referer, want string }{ |
| 246 | {teamOrigin, "same-site", "", teamOrigin}, |
| 247 | {"null", "same-site", teamOrigin + "/", teamOrigin}, |
| 248 | {"null", "same-site", teamOrigin + "/d/42?view=compact", teamOrigin}, |
| 249 | {"", "", teamOrigin + "/", teamOrigin}, |
| 250 | {teamOrigin, "", "", teamOrigin}, |
| 251 | {"null", "same-site", directoryOrigin + "/", directoryOrigin}, |
| 252 | {directoryOrigin, "same-site", directoryOrigin + "/", directoryOrigin}, |
| 253 | } { |
| 254 | // A pair per case: sign-in's rate limiter would refuse ten in a row. |
| 255 | p := withPreviewConsumer(newExchangePair(t)) |
| 256 | fresh, _ := signedInPair(t, p) |
| 257 | rec := signoutHop(p, c.origin, c.site, c.referer, fresh) |
| 258 | if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != c.want+"/signed-out" { |
| 259 | t.Fatalf("%+v: %d %q", c, rec.Code, rec.Header().Get("Location")) |
| 260 | } |
| 261 | if !clearsCookie(rec, fresh.Name) || rec.Header().Get("Cache-Control") != "no-store" { |
| 262 | t.Fatalf("%+v: cookie kept or response cacheable", c) |
| 263 | } |
| 264 | if resumeMints(t, p, fresh) { |
| 265 | t.Fatalf("%+v: session survived an admitted sign-out", c) |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // TestCredentialSignOutNeverTakesADestination: nothing a request carries |
| 271 | // chooses where it lands. A query or a form field naming somewhere else |
| 272 | // is ignored and the browser goes to the matched origin's /signed-out. |
| 273 | func TestCredentialSignOutNeverTakesADestination(t *testing.T) { |
| 274 | p := newExchangePair(t) |
| 275 | credential, _ := signedInPair(t, p) |
| 276 | r := httptest.NewRequest("POST", Origin+"/signout?return_to=https://evil.invalid/&next=https://evil.invalid/", strings.NewReader(url.Values{"return_to": {"https://evil.invalid/"}, "redirect": {"//evil.invalid"}}.Encode())) |
| 277 | r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 278 | r.Header.Set("Origin", "null") |
| 279 | r.Header.Set("Sec-Fetch-Site", "same-site") |
| 280 | r.Header.Set("Referer", teamOrigin+"/") |
| 281 | r.AddCookie(credential) |
| 282 | rec := httptest.NewRecorder() |
| 283 | p.credential.Router().ServeHTTP(rec, r) |
| 284 | if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != teamOrigin+"/signed-out" { |
| 285 | t.Fatalf("destination taken from the request: %d %q", rec.Code, rec.Header().Get("Location")) |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | // TestCredentialSignOutRefusalIsBrandedAndRecoverable: a refused POST is |
| 290 | // never bare error text (the B2 review's finding 1). It is the same |
| 291 | // branded page a plain GET shows, with status 403, and its Sign out |
| 292 | // button is a same-origin POST that then succeeds without any Referer: |
| 293 | // the recovery for a browser that strips it. |
| 294 | func TestCredentialSignOutRefusalIsBrandedAndRecoverable(t *testing.T) { |
| 295 | p := withPreviewConsumer(newExchangePair(t)) |
| 296 | credential, _ := signedInPair(t, p) |
| 297 | for _, c := range []struct{ origin, site, referer string }{ |
| 298 | // No evidence at all, or a nulled Origin with the Referer stripped. |
| 299 | {"", "", ""}, |
| 300 | {"null", "same-site", ""}, |
| 301 | {"null", "cross-site", ""}, |
| 302 | // Unregistered: another site, and a team host on the same site. |
| 303 | {"https://evil.invalid", "cross-site", ""}, |
| 304 | {"null", "cross-site", "https://evil.invalid/"}, |
| 305 | {"https://red.docs.oficina.works", "same-site", ""}, |
| 306 | {"null", "same-site", "https://red.docs.oficina.works/"}, |
| 307 | // A named Origin is the evidence; a Referer cannot overrule it. |
| 308 | {"https://evil.invalid", "cross-site", teamOrigin + "/"}, |
| 309 | {"https://red.docs.oficina.works", "same-site", teamOrigin + "/"}, |
| 310 | // Registered, but the browser's site marking contradicts it: a |
| 311 | // suite origin arriving cross-site went through another site on |
| 312 | // the way, and a preview is never same-site. |
| 313 | {teamOrigin, "cross-site", ""}, |
| 314 | {"null", "cross-site", teamOrigin + "/"}, |
| 315 | {"null", "cross-site", directoryOrigin + "/"}, |
| 316 | {previewOrigin, "same-site", ""}, |
| 317 | {"null", "same-site", previewOrigin + "/"}, |
| 318 | {teamOrigin, "bogus", ""}, |
| 319 | // Near misses on the Referer: scheme, userinfo, suffix, port. |
| 320 | {"null", "same-site", "http://blue.docs.oficina.works/"}, |
| 321 | {"null", "same-site", "https://user@blue.docs.oficina.works/"}, |
| 322 | {"null", "same-site", "https://blue.docs.oficina.works.evil.invalid/"}, |
| 323 | {"null", "same-site", "https://blue.docs.oficina.works:8443/"}, |
| 324 | {"null", "same-site", "https://BLUE.docs.oficina.works/"}, |
| 325 | {"null", "same-site", "not a url\x7f"}, |
| 326 | } { |
| 327 | rec := signoutHop(p, c.origin, c.site, c.referer, credential) |
| 328 | if rec.Code != http.StatusForbidden || !isFinishSigningOut(rec) || clearsCookie(rec, credential.Name) { |
| 329 | t.Fatalf("%+v: %d, branded=%v\n%s", c, rec.Code, isFinishSigningOut(rec), rec.Body.String()) |
| 330 | } |
| 331 | } |
| 332 | if !resumeMints(t, p, credential) { |
| 333 | t.Fatal("a refused sign-out ended the session") |
| 334 | } |
| 335 | |
| 336 | page := exchangeRequest(p.credential.App, "GET", Origin+"/signout", "", nil, credential) |
| 337 | if page.Code != http.StatusOK || !isFinishSigningOut(page) { |
| 338 | t.Fatalf("GET /signout: %d\n%s", page.Code, page.Body.String()) |
| 339 | } |
| 340 | if !resumeMints(t, p, credential) { |
| 341 | t.Fatal("GET /signout ended the session") |
| 342 | } |
| 343 | |
| 344 | // The button: a same-origin form POST, which is what the browser |
| 345 | // sends from that page, with no Referer at all. |
| 346 | r := httptest.NewRequest("POST", Origin+"/signout", nil) |
| 347 | r.Header.Set("Origin", Origin) |
| 348 | r.Header.Set("Sec-Fetch-Site", "same-origin") |
| 349 | r.AddCookie(credential) |
| 350 | rec := httptest.NewRecorder() |
| 351 | p.credential.Router().ServeHTTP(rec, r) |
| 352 | if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != Origin+"/signed-out" { |
| 353 | t.Fatalf("Finish signing out button: %d %q", rec.Code, rec.Header().Get("Location")) |
| 354 | } |
| 355 | if resumeMints(t, p, credential) { |
| 356 | t.Fatal("Finish signing out left the session") |
| 357 | } |
| 358 | if rec := exchangeRequest(p.directory, "GET", directoryOrigin+"/signout", "", nil); rec.Code != http.StatusNotFound && rec.Code != http.StatusMethodNotAllowed { |
| 359 | t.Fatal("directory served the credential's sign-out page", rec.Code) |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // TestCredentialSignOutFromAPreviewConfirmsFirst: a preview is registered |
| 364 | // but off the suite's site, so its hop is cross-site, and the id.* cookie |
| 365 | // (SameSite=Lax) does not ride a cross-site POST. Answering it with a |
| 366 | // sign-out would clear the cookie in the browser but leave the session |
| 367 | // row alive, so a copy of the cookie would still sign in. So the hop gets |
| 368 | // the "Finish signing out" page, the cookie is kept, and its same-origin |
| 369 | // button, which does carry the cookie, revokes the row. |
| 370 | func TestCredentialSignOutFromAPreviewConfirmsFirst(t *testing.T) { |
| 371 | for _, c := range []struct{ origin, site, referer string }{ |
| 372 | {previewOrigin, "cross-site", ""}, |
| 373 | {"null", "cross-site", previewOrigin + "/"}, |
| 374 | {"", "", previewOrigin + "/"}, |
| 375 | } { |
| 376 | p := withPreviewConsumer(newExchangePair(t)) |
| 377 | credential, _ := signedInPair(t, p) |
| 378 | // The browser sends no cookie on this hop; send none. |
| 379 | rec := signoutHop(p, c.origin, c.site, c.referer) |
| 380 | if rec.Code != http.StatusOK || !isFinishSigningOut(rec) || clearsCookie(rec, credential.Name) { |
| 381 | t.Fatalf("%+v: %d, branded=%v, cleared=%v", c, rec.Code, isFinishSigningOut(rec), clearsCookie(rec, credential.Name)) |
| 382 | } |
| 383 | if !resumeMints(t, p, credential) { |
| 384 | t.Fatalf("%+v: the session ended before it was confirmed", c) |
| 385 | } |
| 386 | r := httptest.NewRequest("POST", Origin+"/signout", nil) |
| 387 | r.Header.Set("Origin", Origin) |
| 388 | r.Header.Set("Sec-Fetch-Site", "same-origin") |
| 389 | r.AddCookie(credential) |
| 390 | confirm := httptest.NewRecorder() |
| 391 | p.credential.Router().ServeHTTP(confirm, r) |
| 392 | if confirm.Code != http.StatusSeeOther || confirm.Header().Get("Location") != Origin+"/signed-out" || !clearsCookie(confirm, credential.Name) { |
| 393 | t.Fatalf("%+v: confirm %d %q", c, confirm.Code, confirm.Header().Get("Location")) |
| 394 | } |
| 395 | // The row is revoked, so a copy of the old cookie is refused. |
| 396 | if resumeMints(t, p, credential) { |
| 397 | t.Fatalf("%+v: a copy of the cookie still signs in after confirming", c) |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | // TestCredentialSignOutKeepsASuiteOriginsPort: a registered suite origin |
| 403 | // on a non-default port is same-site. The site is judged by hostname, |
| 404 | // and the browser goes back to the full origin, port included. |
| 405 | func TestCredentialSignOutKeepsASuiteOriginsPort(t *testing.T) { |
| 406 | const ported = "https://blue.sheets.oficina.works:8443" |
| 407 | for _, c := range []struct{ origin, site, referer string }{ |
| 408 | {ported, "same-site", ""}, |
| 409 | {"null", "same-site", ported + "/"}, |
| 410 | } { |
| 411 | p := newExchangePair(t) |
| 412 | p.credential.runtime.consumers["team_sheets_ported"] = ported |
| 413 | credential, _ := signedInPair(t, p) |
| 414 | rec := signoutHop(p, c.origin, c.site, c.referer, credential) |
| 415 | if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != ported+"/signed-out" { |
| 416 | t.Fatalf("%+v: %d %q", c, rec.Code, rec.Header().Get("Location")) |
| 417 | } |
| 418 | if resumeMints(t, p, credential) { |
| 419 | t.Fatalf("%+v: session survived", c) |
| 420 | } |
| 421 | } |
| 422 | // Still the site rule: that ported suite origin arriving cross-site is refused. |
| 423 | p := newExchangePair(t) |
| 424 | p.credential.runtime.consumers["team_sheets_ported"] = ported |
| 425 | credential, _ := signedInPair(t, p) |
| 426 | if rec := signoutHop(p, ported, "cross-site", "", credential); rec.Code != http.StatusForbidden { |
| 427 | t.Fatal("ported suite origin admitted cross-site", rec.Code) |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | func signoutFormAction(t *testing.T, body string) string { |
| 432 | t.Helper() |
| 433 | m := regexp.MustCompile(`<form method="post" action="([^"]+)"[^>]*data-assertion-handoff`).FindStringSubmatch(body) |
| 434 | if len(m) != 2 || !strings.Contains(body, "Sign out of Oficina ID</button>") { |
| 435 | t.Fatal("signed-out page has no issuer POST and fallback button") |
| 436 | } |
| 437 | return m[1] |
| 438 | } |
| 439 | |
| 440 | func TestCredentialSignoutUsesIssuerSite(t *testing.T) { |
| 441 | p := newExchangePair(t) |
| 442 | a := p.credential.App |
| 443 | a.origins = Origins{"https://id.example.org", "https://directory.example.test"} |
| 444 | a.directoryHost = "directory.example.test" |
| 445 | a.runtime.consumers["directory"] = a.origins.Directory |
| 446 | for _, metadata := range []string{"", "cross-site"} { |
| 447 | r := httptest.NewRequest("POST", a.origins.Credential+signoutPath, nil) |
| 448 | r.Header.Set("Origin", a.origins.Directory) |
| 449 | r.Header.Set("Sec-Fetch-Site", metadata) |
| 450 | if _, verdict := a.signoutReturn(r); verdict != signoutConfirm { |
| 451 | t.Fatal("cross-site registered directory must confirm with its ID cookie", metadata, verdict) |
| 452 | } |
| 453 | } |
| 454 | for _, tc := range []struct { |
| 455 | issuer, consumer string |
| 456 | same bool |
| 457 | }{ |
| 458 | {"https://id.example.co.uk", "https://directory.example.co.uk", true}, |
| 459 | {"https://id.example.co.uk", "https://other.co.uk", false}, |
| 460 | {"https://id.oficina.works", "https://blue.docs.oficina.works:8443", true}, |
| 461 | {"https://id.example.org", "http://id.example.org", false}, |
| 462 | } { |
| 463 | a.origins.Credential = tc.issuer |
| 464 | if a.onSuiteSite(tc.consumer) != tc.same { |
| 465 | t.Fatal("incorrect issuer-site classification", tc.issuer, tc.consumer) |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |