| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "regexp" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // TestHandoffInterstitialStaysHiddenWhileItAutoSubmits is spec |
| 13 | // shared-chrome §9.3: the assertion hand-off page submits itself, so its |
| 14 | // fallback copy is hidden for 1.5 s and appears only if the submit |
| 15 | // stalls. Without it every sign-in, and every app switch that needs an |
| 16 | // exchange, flashes "Continue to Oficina" mid-hop. With scripts off |
| 17 | // nothing submits, so a stylesheet linked in <noscript> shows the |
| 18 | // fallback at once. |
| 19 | func TestHandoffInterstitialStaysHiddenWhileItAutoSubmits(t *testing.T) { |
| 20 | app := newTestApp(t) |
| 21 | var buf bytes.Buffer |
| 22 | if err := app.renderer.pages["assertion.html"].ExecuteTemplate(&buf, "layout", page{Data: map[string]any{"Action": "https://blue.docs.oficina.works/auth/assertion", "Token": "sample-token"}}); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | page := buf.String() |
| 26 | head, body, ok := strings.Cut(page, "</head>") |
| 27 | if !ok { |
| 28 | t.Fatal("no head") |
| 29 | } |
| 30 | // The rules are files, linked from the head, since rastrillo's CSP |
| 31 | // (v0.27.0) allows no inline style; the script-off override is linked |
| 32 | // inside <noscript>, so only a page with scripts off loads it. |
| 33 | compact := regexp.MustCompile(`\s+`).ReplaceAllString(head, "") |
| 34 | for _, want := range []string{ |
| 35 | `<linkrel="stylesheet"href="` + versioned("/static/handoff.css") + `">`, |
| 36 | `<noscript><linkrel="stylesheet"href="` + versioned("/static/handoff-noscript.css") + `"></noscript>`, |
| 37 | } { |
| 38 | if !strings.Contains(compact, want) { |
| 39 | t.Fatalf("head lacks %q:\n%s", want, head) |
| 40 | } |
| 41 | } |
| 42 | if strings.Contains(head, "<style") { |
| 43 | t.Fatalf("head carries an inline <style>, which the CSP drops:\n%s", head) |
| 44 | } |
| 45 | for path, wants := range map[string][]string{ |
| 46 | "/static/handoff.css": { |
| 47 | "[estilo-door]>[estilo-signin]{animation:estilo-reveal0s1.5sboth;}", |
| 48 | "@keyframesestilo-reveal{from{visibility:hidden}to{visibility:visible}}", |
| 49 | }, |
| 50 | "/static/handoff-noscript.css": {"[estilo-door]>[estilo-signin]{animation:none;}"}, |
| 51 | } { |
| 52 | for _, host := range []string{credentialHost, directoryHost} { |
| 53 | req := httptest.NewRequest(http.MethodGet, path, nil) |
| 54 | req.Host = host |
| 55 | rec := httptest.NewRecorder() |
| 56 | app.Router().ServeHTTP(rec, req) |
| 57 | css := regexp.MustCompile(`/\*[\s\S]*?\*/|\s+`).ReplaceAllString(rec.Body.String(), "") |
| 58 | if rec.Code != http.StatusOK || rec.Header().Get("Content-Type") != "text/css; charset=utf-8" { |
| 59 | t.Fatalf("%s %s: status %d, type %q", host, path, rec.Code, rec.Header().Get("Content-Type")) |
| 60 | } |
| 61 | for _, want := range wants { |
| 62 | if !strings.Contains(css, want) { |
| 63 | t.Fatalf("%s %s lacks %q", host, path, want) |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | // Everything the person could read or press is inside the hidden |
| 69 | // card, and the form the script submits is still the one there. The |
| 70 | // bar and the sky's placeholder sit outside it, so the hop is never a |
| 71 | // blank page (r3 C2). |
| 72 | main := body[strings.Index(body, "<main"):strings.Index(body, "</main>")] |
| 73 | // The card is the door's, with the brand at its head (Paul, |
| 74 | // 2026-11-20: every id.* page in the door's card); handoff.css hides |
| 75 | // it, and only the sky's inline placeholder is behind it. |
| 76 | start := strings.Index(main, "<section estilo-signin>") |
| 77 | if start < 0 || !strings.Contains(main[:start], `<div estilo-door estilo-sky-host>`) || !strings.Contains(main[:start], `estilo-sky-layer="placeholder"`) || strings.Count(main[:start], "estilo-sky-layer=") != strings.Count(main[:start], `estilo-sky-layer="placeholder"`) { |
| 78 | t.Fatalf("content not wrapped in the hidden card on the sky:\n%s", main) |
| 79 | } |
| 80 | if !strings.Contains(main[start:], `<span>Oficina</span></p>`) || !strings.Contains(main[start:], `<p estilo-brand><img estilo-brand-icon="listing"`) { |
| 81 | t.Fatalf("the hidden card has no brand at its head:\n%s", main) |
| 82 | } |
| 83 | inner := main[start:strings.Index(main, "</section>")] |
| 84 | if !strings.Contains(inner, `data-assertion-handoff`) || !strings.Contains(inner, `<button type="submit" rst-btn="primary" estilo-signin-action>Continue</button>`) { |
| 85 | t.Fatalf("hand-off form changed:\n%s", inner) |
| 86 | } |
| 87 | } |
| 88 | |