diff --git a/internal/browserrot/browserrot.go b/internal/browserrot/browserrot.go index 59c147a..3d903ec 100644 --- a/internal/browserrot/browserrot.go +++ b/internal/browserrot/browserrot.go @@ -110,7 +110,13 @@ type Site struct { ChangeURL string Form Form Login Login - Proof rotate.ProofLevel + // LoginBeforeChange makes Rotate authenticate through the Login flow (with the + // OLD secret) BEFORE navigating to the change page. Real account settings pages + // (Gitea /user/settings/account, most dashboards) are reachable only inside an + // authenticated session; a directly-served change form (the lab target, or a + // public reset link) does not need it. Requires a non-empty Login. + LoginBeforeChange bool + Proof rotate.ProofLevel } // Rotator drives one Site. Unlike the stateless API drivers, a browser Rotator is @@ -163,6 +169,25 @@ func (r *Rotator) Rotate(ctx context.Context, c discover.Credential, v *vault.Va } defer sess.Close() + // Sites whose change page lives behind auth (Gitea /user/settings/account, most + // dashboards) must be logged in first, using the OLD secret. The same login flow + // Verify uses; here the session's cookies then carry into the change-page fetch. + if r.site.LoginBeforeChange { + oldBuf, err := v.Open(c.Secret) + if err != nil { + return nil, fmt.Errorf("browserrot: open old secret (pre-change login): %w", err) + } + txt, err := r.login(ctx, sess, oldBuf.Bytes()) + if err != nil { + return nil, fmt.Errorf("browserrot: pre-change login: %w", err) + } + if !strings.Contains(txt, r.site.Login.SuccessText) { + // A login wall we could not clear with the old secret is also where MFA + // would appear: the marker won't match. Keep the old secret, flag the human. + return nil, fmt.Errorf("browserrot: pre-change login did not succeed (needs human / MFA?): got %q", strings.TrimSpace(txt)) + } + } + if _, err := sess.Goto(ctx, changeURL); err != nil { return nil, fmt.Errorf("browserrot: goto change page: %w", err) } @@ -304,6 +329,32 @@ func (r *Rotator) Healed() bool { return r.healed } // HealedSite returns the Site with any healed selectors folded in — the recipe to persist. func (r *Rotator) HealedSite() Site { return r.site } +// login runs the Site's Login flow with the given password on an already-open session +// and returns the post-login status text. It is the shared core of both Rotate's +// optional pre-change auth (with the OLD secret) and Verify's re-login (with the NEW +// secret). The password crosses as []byte and is never logged; each field self-heals. +func (r *Rotator) login(ctx context.Context, sess Session, password []byte) (string, error) { + if _, err := sess.Goto(ctx, r.site.Login.URL); err != nil { + return "", fmt.Errorf("browserrot: goto login: %w", err) + } + if r.site.Login.UserSel != "" { + if err := r.fill(ctx, sess, &r.site.Login.UserSel, "the username or email input field", []byte(r.username)); err != nil { + return "", err + } + } + if err := r.fill(ctx, sess, &r.site.Login.PassSel, "the password input field", password); err != nil { + return "", err + } + if err := r.click(ctx, sess, &r.site.Login.SubmitSel, "the log-in button"); err != nil { + return "", err + } + txt, err := r.text(ctx, sess, &r.site.Login.SuccessSel, "the post-login status message") + if err != nil { + return "", fmt.Errorf("browserrot: read login result: %w", err) + } + return txt, nil +} + // Verify proves the new secret authenticates by re-logging-in through the Site's // Login flow (hard rule 2: verify-new-before-revoke-old). If the Site has no Login // flow, Verify REFUSES — the spine then keeps the old secret and flags the human, @@ -318,27 +369,13 @@ func (r *Rotator) Verify(ctx context.Context, newSecret *vault.Handle, v *vault. } defer sess.Close() - if _, err := sess.Goto(ctx, r.site.Login.URL); err != nil { - return fmt.Errorf("browserrot: goto login: %w", err) - } - if r.site.Login.UserSel != "" { - if err := r.fill(ctx, sess, &r.site.Login.UserSel, "the username or email input field", []byte(r.username)); err != nil { - return err - } - } buf, err := v.Open(newSecret) if err != nil { return fmt.Errorf("browserrot: open new secret (verify): %w", err) } - if err := r.fill(ctx, sess, &r.site.Login.PassSel, "the password input field", buf.Bytes()); err != nil { - return err - } - if err := r.click(ctx, sess, &r.site.Login.SubmitSel, "the log-in button"); err != nil { - return err - } - txt, err := r.text(ctx, sess, &r.site.Login.SuccessSel, "the post-login status message") + txt, err := r.login(ctx, sess, buf.Bytes()) if err != nil { - return fmt.Errorf("browserrot: read login result: %w", err) + return err } if !strings.Contains(txt, r.site.Login.SuccessText) { return fmt.Errorf("browserrot: new password did NOT authenticate: got %q", strings.TrimSpace(txt)) diff --git a/internal/browserrot/browserrot_test.go b/internal/browserrot/browserrot_test.go index 7961865..2a81ac1 100644 --- a/internal/browserrot/browserrot_test.go +++ b/internal/browserrot/browserrot_test.go @@ -337,3 +337,185 @@ func runLiveRotationProof(t *testing.T, drv Driver, mutators ...func(*Site)) *Ro } func hostForDebug(c discover.Credential) string { return links.HostFor(c) } + +func TestGiteaSite_recipeShape(t *testing.T) { + s := GiteaSite("https://git.churchofmalware.org/", "trilltechnician") + if s.Host != "git.churchofmalware.org" { + t.Fatalf("host: got %q", s.Host) + } + if s.ChangeURL != "https://git.churchofmalware.org/user/settings/account" { + t.Fatalf("changeURL: got %q", s.ChangeURL) + } + if !s.LoginBeforeChange { + t.Fatal("Gitea account page is auth-gated; LoginBeforeChange must be true") + } + if s.Login.URL == "" || s.Form.CurrentSel == "" || s.Form.NewSel == "" || s.Form.SubmitSel == "" { + t.Fatalf("recipe has empty required selectors: %+v", s) + } + // A seed recipe is UNPROVEN until actually run against the real instance (proof-as-data). + if s.Proof != rotate.ProofUnproven { + t.Fatalf("a fresh curated recipe must be ProofUnproven, got %v", s.Proof) + } +} + +// TestIntegration_loginBeforeChangeRotation proves the Site.LoginBeforeChange path against +// a real Chromium: the change page is reachable ONLY inside an authenticated session (a +// Gitea-style cookie gate — 302 → /login when unauthenticated). Rotate must log in with the +// OLD secret first, carry the cookie into the change page, rotate, and then Verify re-logs-in +// with the NEW secret. This is the engine change M-B4 needs for real account-settings pages. +func TestIntegration_loginBeforeChangeRotation(t *testing.T) { + bin := os.Getenv("CHROME_BIN") + if bin == "" { + t.Skip("set CHROME_BIN to a Chromium executable to run the LIVE-VM login-gated proof") + } + + const oldPW = "OLD-corr3ct-horse-battery" + const user = "trilltechnician" + var mu sync.Mutex + stored := oldPW + const cookie = "sess=authok" + + authed := func(r *http.Request) bool { + c, err := r.Cookie("sess") + return err == nil && c.Value == "authok" + } + + mux := http.NewServeMux() + // /login: GET renders the form; POST checks the CURRENT stored password and, on + // success, sets the session cookie (this is what gates the settings page). + mux.HandleFunc("/user/login", func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodPost { + r.ParseForm() + mu.Lock() + ok := r.FormValue("user_name") == user && r.FormValue("password") == stored + mu.Unlock() + if ok { + http.SetCookie(w, &http.Cookie{Name: "sess", Value: "authok", Path: "/"}) + fmt.Fprint(w, `