console: getting-started hide, show, skip, done
Four CSRF'd POSTs under /a/{sqid}/getting-started/, gated by s.account;
the person is the session address. Skip and done take step=secrets|domain
only. Claim census fixtures and probes added for the new routes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+284
−0
internal/console/claim_invariant_fixtures_test.go+4 −0internal/console/claim_invariant_probes_nonapp_test.go+33 −0internal/console/gettingstarted_handlers.go+90 −0internal/console/gettingstarted_handlers_test.go+151 −0internal/console/server.go+6 −0
diff --git a/internal/console/claim_invariant_fixtures_test.go b/internal/console/claim_invariant_fixtures_test.go| index b848a2e97..a2761aae0 100644 |
| --- a/internal/console/claim_invariant_fixtures_test.go |
| +++ b/internal/console/claim_invariant_fixtures_test.go |
| @@ -1057,6 +1057,10 @@ var claimFixtures = map[string]claimFixture{ |
| "route POST /a/{sqid}/apps/{app}/star": claimNonApp("the viewer's own bookmark in app_stars, keyed by account, person and app lifetime; reads the app row, writes nothing of the app's"), |
| "route POST /a/{sqid}/delete": claimNonApp("deletes an account only when it holds no app: AppCount is re-checked before the step-up and DeleteAccount refuses otherwise"), |
| "route POST /a/{sqid}/errors/level": claimNonApp("the account-level error log level (telemetry account record)"), |
| + "route POST /a/{sqid}/getting-started/done": claimNonApp("the viewer's own Getting started mark in getting_started_marks, keyed by account and person; touches no app"), |
| + "route POST /a/{sqid}/getting-started/hide": claimNonApp("the viewer's own Getting started mark in getting_started_marks, keyed by account and person; touches no app"), |
| + "route POST /a/{sqid}/getting-started/show": claimNonApp("clears the viewer's own Getting started marks in getting_started_marks; touches no app"), |
| + "route POST /a/{sqid}/getting-started/skip": claimNonApp("the viewer's own Getting started mark in getting_started_marks, keyed by account and person; touches no app"), |
| "route POST /a/{sqid}/fleets/{fleet}/detach": claimNonApp("detaches a fleet box in the registry; places no app"), |
| "route POST /a/{sqid}/fleets/{fleet}/place": {door: &claimDoor{ |
| apps: claimRefs("bab/titogo"), |
diff --git a/internal/console/claim_invariant_probes_nonapp_test.go b/internal/console/claim_invariant_probes_nonapp_test.go| index 2614ce716..09d9e3fe5 100644 |
| --- a/internal/console/claim_invariant_probes_nonapp_test.go |
| +++ b/internal/console/claim_invariant_probes_nonapp_test.go |
| @@ -668,6 +668,10 @@ var claimNonAppProbes = map[string]claimProbe{ |
| return "" |
| }, |
| }, |
| + "route POST /a/{sqid}/getting-started/hide": claimGettingStartedProbe("hide", nil, gsHidden, true), |
| + "route POST /a/{sqid}/getting-started/skip": claimGettingStartedProbe("skip", []string{"step", "secrets"}, gsSecretsSkip, true), |
| + "route POST /a/{sqid}/getting-started/done": claimGettingStartedProbe("done", []string{"step", "domain"}, gsDomainDone, true), |
| + "route POST /a/{sqid}/getting-started/show": claimGettingStartedProbe("show", nil, gsHidden, false), |
| "route POST /a/{sqid}/errors/level": { |
| do: claimFormReply("/a/bab/errors/level", "level", "warn"), |
| status: http.StatusSeeOther, loc: "/a/bab/settings", |
| @@ -1514,3 +1518,32 @@ func claimBillingProblemWorld(t *testing.T, cx *claimCase) { |
| t.Fatal(err) |
| } |
| } |
| + |
| +// claimGettingStartedProbe drives one Getting started preference POST as the |
| +// owner. want says whether mark must be present afterwards; Show (want |
| +// false) is staged with mark already set, so its effect is the deletion. |
| +func claimGettingStartedProbe(action string, kv []string, mark gsMark, want bool) claimProbe { |
| + form := append([]string{}, kv...) |
| + return claimProbe{ |
| + setup: func(t *testing.T, cx *claimCase) { |
| + if want { |
| + return |
| + } |
| + if err := cx.st.SetGettingStartedMark(OpsAccountID, claimFxOwner, mark, time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + }, |
| + do: claimFormReply("/a/bab/getting-started/"+action, form...), |
| + status: http.StatusSeeOther, loc: "/a/bab/", |
| + effect: func(t *testing.T, cx *claimCase, r claimReply) string { |
| + marks, err := cx.st.GettingStartedMarks(OpsAccountID, claimFxOwner) |
| + if err != nil { |
| + return err.Error() |
| + } |
| + if marks[mark] != want { |
| + return fmt.Sprintf("getting started %s: the owner's %s mark is %v, want %v", action, mark, marks[mark], want) |
| + } |
| + return "" |
| + }, |
| + } |
| +} |
diff --git a/internal/console/gettingstarted_handlers.go b/internal/console/gettingstarted_handlers.go| new file mode 100644 |
| index 000000000..5932cc4ed |
| --- /dev/null |
| +++ b/internal/console/gettingstarted_handlers.go |
| @@ -0,0 +1,90 @@ |
| +package console |
| + |
| +import ( |
| + "crypto/subtle" |
| + "log" |
| + "net/http" |
| + "time" |
| +) |
| + |
| +// The Getting started preference handlers are POST |
| +// /a/{sqid}/getting-started/{hide,show,skip,done}: any member, CSRF, the |
| +// person always the session's address (never a form field), so a member |
| +// can only change their own checklist. The marks are personal preferences |
| +// that grant nothing, so a demo account takes them like any other. |
| + |
| +// gsMarkFor is the mark Skip or Mark done records for step; only the |
| +// secrets and domain steps take one. |
| +var gsMarkFor = map[string]map[string]gsMark{ |
| + "skip": {"secrets": gsSecretsSkip, "domain": gsDomainSkip}, |
| + "done": {"secrets": gsSecretsDone, "domain": gsDomainDone}, |
| +} |
| + |
| +// gettingStartedRequest gates a preference POST: session, membership and |
| +// CSRF. It answers the refusal itself when ok is false. |
| +func (s *Server) gettingStartedRequest(w http.ResponseWriter, r *http.Request) (addr string, acct *Account, ok bool) { |
| + addr, raw, acct, _, ok := s.account(w, r) |
| + if !ok { |
| + return "", nil, false |
| + } |
| + if subtle.ConstantTimeCompare([]byte(r.FormValue("csrf")), []byte(raw)) != 1 { |
| + s.renderMessage(w, r, http.StatusForbidden, "Refused", "Cross-site request blocked.") |
| + return "", nil, false |
| + } |
| + return addr, acct, true |
| +} |
| + |
| +// gettingStartedSaved finishes a preference POST: back to the account home, |
| +// or the save-failed message. |
| +func (s *Server) gettingStartedSaved(w http.ResponseWriter, r *http.Request, acct *Account, what string, err error) { |
| + if err != nil { |
| + log.Printf("console: getting started %s %s: %v", what, acct.Sqid, err) |
| + s.renderMessage(w, r, http.StatusInternalServerError, "Error", approvedGettingStartedCopy.SaveFailed) |
| + return |
| + } |
| + http.Redirect(w, r, accountHomeURL(acct.Sqid, ""), http.StatusSeeOther) |
| +} |
| + |
| +// handleGettingStartedHide is POST /a/{sqid}/getting-started/hide. |
| +func (s *Server) handleGettingStartedHide(w http.ResponseWriter, r *http.Request) { |
| + addr, acct, ok := s.gettingStartedRequest(w, r) |
| + if !ok { |
| + return |
| + } |
| + s.gettingStartedSaved(w, r, acct, "hide", s.store.SetGettingStartedMark(acct.ID, addr, gsHidden, time.Now())) |
| +} |
| + |
| +// handleGettingStartedShow is POST /a/{sqid}/getting-started/show: it |
| +// clears every mark the person has in the account. |
| +func (s *Server) handleGettingStartedShow(w http.ResponseWriter, r *http.Request) { |
| + addr, acct, ok := s.gettingStartedRequest(w, r) |
| + if !ok { |
| + return |
| + } |
| + s.gettingStartedSaved(w, r, acct, "show", s.store.ResetGettingStarted(acct.ID, addr)) |
| +} |
| + |
| +// handleGettingStartedSkip is POST /a/{sqid}/getting-started/skip. |
| +func (s *Server) handleGettingStartedSkip(w http.ResponseWriter, r *http.Request) { |
| + s.gettingStartedStep(w, r, "skip") |
| +} |
| + |
| +// handleGettingStartedDone is POST /a/{sqid}/getting-started/done. |
| +func (s *Server) handleGettingStartedDone(w http.ResponseWriter, r *http.Request) { |
| + s.gettingStartedStep(w, r, "done") |
| +} |
| + |
| +// gettingStartedStep records Skip or Mark done for step=secrets|domain; |
| +// any other step is refused with nothing written. |
| +func (s *Server) gettingStartedStep(w http.ResponseWriter, r *http.Request, action string) { |
| + addr, acct, ok := s.gettingStartedRequest(w, r) |
| + if !ok { |
| + return |
| + } |
| + m, ok := gsMarkFor[action][r.FormValue("step")] |
| + if !ok { |
| + s.renderMessage(w, r, http.StatusBadRequest, "Refused", "That request could not be read.") |
| + return |
| + } |
| + s.gettingStartedSaved(w, r, acct, action, s.store.SetGettingStartedMark(acct.ID, addr, m, time.Now())) |
| +} |
diff --git a/internal/console/gettingstarted_handlers_test.go b/internal/console/gettingstarted_handlers_test.go| new file mode 100644 |
| index 000000000..1ce07addf |
| --- /dev/null |
| +++ b/internal/console/gettingstarted_handlers_test.go |
| @@ -0,0 +1,151 @@ |
| +package console |
| + |
| +import ( |
| + "net/http" |
| + "net/url" |
| + "testing" |
| + "time" |
| +) |
| + |
| +func gsMarksOf(t *testing.T, fx *consoleFixture, address string) map[gsMark]bool { |
| + t.Helper() |
| + got, err := fx.st.GettingStartedMarks(OpsAccountID, address) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return got |
| +} |
| + |
| +func gsPost(t *testing.T, fx *consoleFixture, c *http.Client, action string, form url.Values) (int, string) { |
| + t.Helper() |
| + status, loc, _ := postForm(t, c, fx.u("/getting-started/"+action), form) |
| + return status, loc |
| +} |
| + |
| +func TestGettingStartedHideShow(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + paul, csrf := signInAs(t, fx, "paul@keymail.dev", "code-gs-hide") |
| + |
| + if status, loc := gsPost(t, fx, paul, "hide", url.Values{"csrf": {csrf}}); status != http.StatusSeeOther || loc != fx.path("/") { |
| + t.Fatalf("hide = %d %q, want 303 to %q", status, loc, fx.path("/")) |
| + } |
| + if got := gsMarksOf(t, fx, "paul@keymail.dev"); !got[gsHidden] || len(got) != 1 { |
| + t.Fatalf("marks after hide = %v, want hidden", got) |
| + } |
| + if err := fx.st.SetGettingStartedMark(OpsAccountID, "paul@keymail.dev", gsSecretsSkip, time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + if status, loc := gsPost(t, fx, paul, "show", url.Values{"csrf": {csrf}}); status != http.StatusSeeOther || loc != fx.path("/") { |
| + t.Fatalf("show = %d %q, want 303 to %q", status, loc, fx.path("/")) |
| + } |
| + if got := gsMarksOf(t, fx, "paul@keymail.dev"); len(got) != 0 { |
| + t.Fatalf("marks after show = %v, want none", got) |
| + } |
| +} |
| + |
| +func TestGettingStartedSkipDone(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + paul, csrf := signInAs(t, fx, "paul@keymail.dev", "code-gs-skip") |
| + |
| + if status, loc := gsPost(t, fx, paul, "skip", url.Values{"csrf": {csrf}, "step": {"secrets"}}); status != http.StatusSeeOther || loc != fx.path("/") { |
| + t.Fatalf("skip secrets = %d %q", status, loc) |
| + } |
| + if status, loc := gsPost(t, fx, paul, "done", url.Values{"csrf": {csrf}, "step": {"domain"}}); status != http.StatusSeeOther || loc != fx.path("/") { |
| + t.Fatalf("done domain = %d %q", status, loc) |
| + } |
| + got := gsMarksOf(t, fx, "paul@keymail.dev") |
| + if !got[gsSecretsSkip] || !got[gsDomainDone] || len(got) != 2 { |
| + t.Fatalf("marks = %v, want secrets:skip and domain:done", got) |
| + } |
| + // Done after skip replaces it. |
| + if status, _ := gsPost(t, fx, paul, "done", url.Values{"csrf": {csrf}, "step": {"secrets"}}); status != http.StatusSeeOther { |
| + t.Fatalf("done secrets = %d", status) |
| + } |
| + got = gsMarksOf(t, fx, "paul@keymail.dev") |
| + if !got[gsSecretsDone] || got[gsSecretsSkip] || !got[gsDomainDone] || len(got) != 2 { |
| + t.Fatalf("marks = %v, want secrets:done and domain:done", got) |
| + } |
| +} |
| + |
| +func TestGettingStartedRefusesBadStep(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + paul, csrf := signInAs(t, fx, "paul@keymail.dev", "code-gs-step") |
| + for _, action := range []string{"skip", "done"} { |
| + for _, step := range []string{"connect", "deploy", "", "x"} { |
| + if status, _ := gsPost(t, fx, paul, action, url.Values{"csrf": {csrf}, "step": {step}}); status != http.StatusBadRequest { |
| + t.Errorf("%s step=%q = %d, want 400", action, step, status) |
| + } |
| + } |
| + } |
| + if got := gsMarksOf(t, fx, "paul@keymail.dev"); len(got) != 0 { |
| + t.Fatalf("refused steps wrote marks: %v", got) |
| + } |
| +} |
| + |
| +func TestGettingStartedRefusesCSRF(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + paul, _ := signInAs(t, fx, "paul@keymail.dev", "code-gs-csrf") |
| + for _, action := range []string{"hide", "show", "skip", "done"} { |
| + for name, form := range map[string]url.Values{ |
| + "missing": {"step": {"secrets"}}, |
| + "wrong": {"csrf": {"wrong"}, "step": {"secrets"}}, |
| + } { |
| + if status, _ := gsPost(t, fx, paul, action, form); status != http.StatusForbidden { |
| + t.Errorf("%s with %s csrf = %d, want 403", action, name, status) |
| + } |
| + } |
| + } |
| + if got := gsMarksOf(t, fx, "paul@keymail.dev"); len(got) != 0 { |
| + t.Fatalf("refused requests wrote marks: %v", got) |
| + } |
| +} |
| + |
| +func TestGettingStartedRefusesStranger(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + _, paulCSRF := signInAs(t, fx, "paul@keymail.dev", "code-gs-paul") |
| + eve, eveCSRF := signInAs(t, fx, "eve@keymail.dev", "code-gs-eve") |
| + anon := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} |
| + for _, action := range []string{"hide", "show", "skip", "done"} { |
| + form := url.Values{"csrf": {eveCSRF}, "step": {"secrets"}} |
| + if status, _ := gsPost(t, fx, eve, action, form); status != http.StatusNotFound { |
| + t.Errorf("non-member %s = %d, want 404", action, status) |
| + } |
| + form = url.Values{"csrf": {paulCSRF}, "step": {"secrets"}} |
| + if status, loc := gsPost(t, fx, anon, action, form); status != http.StatusFound || loc != "/" { |
| + t.Errorf("signed-out %s = %d %q, want 302 to /", action, status, loc) |
| + } |
| + } |
| + for _, addr := range []string{"eve@keymail.dev", "paul@keymail.dev"} { |
| + if got := gsMarksOf(t, fx, addr); len(got) != 0 { |
| + t.Fatalf("%s has marks %v after refused requests", addr, got) |
| + } |
| + } |
| +} |
| + |
| +func TestGettingStartedIgnoresFormAddress(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + if err := fx.st.AddMember(OpsAccountID, "kim@keymail.dev", RoleMember, time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + paul, csrf := signInAs(t, fx, "paul@keymail.dev", "code-gs-addr") |
| + if err := fx.st.SetGettingStartedMark(OpsAccountID, "kim@keymail.dev", gsHidden, time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, action := range []string{"skip", "show"} { |
| + form := url.Values{"csrf": {csrf}, "step": {"domain"}, "address": {"kim@keymail.dev"}} |
| + if status, _ := gsPost(t, fx, paul, action, form); status != http.StatusSeeOther { |
| + t.Fatalf("%s = %d", action, status) |
| + } |
| + if action == "skip" { |
| + if got := gsMarksOf(t, fx, "paul@keymail.dev"); !got[gsDomainSkip] || len(got) != 1 { |
| + t.Fatalf("paul's marks = %v, want domain:skip", got) |
| + } |
| + } |
| + } |
| + if got := gsMarksOf(t, fx, "kim@keymail.dev"); !got[gsHidden] || len(got) != 1 { |
| + t.Fatalf("kim's marks = %v, want only her own hidden mark", got) |
| + } |
| + if got := gsMarksOf(t, fx, "paul@keymail.dev"); len(got) != 0 { |
| + t.Fatalf("paul's marks after show = %v, want none", got) |
| + } |
| +} |
diff --git a/internal/console/server.go b/internal/console/server.go| index 42670a55c..06f41be0e 100644 |
| --- a/internal/console/server.go |
| +++ b/internal/console/server.go |
| @@ -2128,6 +2128,12 @@ func NewServer(cfg Config, st *Store) (*Server, error) { |
| // (owners) (applist.go). |
| s.registerRoute("POST /a/{sqid}/apps/{app}/star", s.handleAppStar) |
| s.registerRoute("POST /a/{sqid}/apps/{app}/extend", s.handleAppExtend) |
| + // The Getting started card's preferences (any member, the viewer's own) |
| + // (gettingstarted_handlers.go). |
| + s.registerRoute("POST /a/{sqid}/getting-started/hide", s.handleGettingStartedHide) |
| + s.registerRoute("POST /a/{sqid}/getting-started/show", s.handleGettingStartedShow) |
| + s.registerRoute("POST /a/{sqid}/getting-started/skip", s.handleGettingStartedSkip) |
| + s.registerRoute("POST /a/{sqid}/getting-started/done", s.handleGettingStartedDone) |
| s.registerRoute("POST /a/{sqid}/apps/{app}/delete", s.handleAppDelete) |
| s.registerRoute("POST /a/{sqid}/apps/{app}/move", s.handleAppMoveAccount) |
| // The control-plane flag's two directions, both browser-only and both |