oficina / sheets Public

Smart door: Fable's fixes on #71 (undecided guard, return memory edges)

- An admitted /signin probe writes nothing to the real response; a
  refused session's cookie is now cleared on the door's response.
- The guard's 503 branch is proved: /signin draws no refusal, keeps the
  remembered Oficina ID and the session; MintHomeSession remembers nothing.
- The return memory treats /signed-out as "/", so a local sign-in no
  longer lands on "You're signed out"; Forget and Home Sign out clear it.
- Negative tests: off-site return_to values land on /sheets; a tampered,
  expired or duplicated cookie reads as "/".
- AGENTS.md: doctor refuses while native.go needs the pre-move path, so
  tokens.css is re-copied from ui.TokensCSS() and pinned by
  TestSharedThemeAssetFingerprintAndCache; doctor's template checks are
  blind meanwhile.
- Makefile: the browser filter's four tests and Chromium discovery.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 3d9e3f22966ed0c5461ccfce91cd39ad122cad70 parent 7b33142
8 files changed, +234 −10
  • AGENTS.md +8 −0
  • Makefile +9 −4
  • internal/sheets/home_door.go +7 −2
  • internal/sheets/home_routes.go +11 −1
  • internal/sheets/signin.go +5 −1
  • internal/sheets/signin_door_edges_test.go +173 −0
  • internal/sheets/signin_door_helpers_test.go +6 −0
  • internal/sheets/signin_return.go +15 −2

CI passed — run details

CI log
amadan: .amadan/timeout: this job's budget is 15m0s (this runner allows up to 30m0s)
=== vet ===
CGO_ENABLED=0 go vet ./...
CGO_ENABLED=0 go vet -tags browser ./internal/sheets/
=== fmt ===
=== build ===
CGO_ENABLED=0 go build ./...
=== migrations ===
go tool rastrillo migration check
rastrillo migration check: models and migrations agree
=== go-test ===
CGO_ENABLED=0 go test -count=1 ./...
?   	amadan.net/oficina/sheets/cmd/sheets	[no test files]
?   	amadan.net/oficina/sheets/cmd/sheets-contract	[no test files]
ok  	amadan.net/oficina/sheets/internal/guardstest	0.170s
ok  	amadan.net/oficina/sheets/internal/sheets	15.806s
ok  	amadan.net/oficina/sheets/internal/sheets/calc	0.010s
ok  	amadan.net/oficina/sheets/internal/sheets/format	0.003s
?   	amadan.net/oficina/sheets/internal/sheets/keymailtest	[no test files]
ok  	amadan.net/oficina/sheets/internal/sheets/live	0.057s
ok  	amadan.net/oficina/sheets/internal/sheets/seal	0.002s
ok  	amadan.net/oficina/sheets/internal/sheets/sealtest	0.003s
ok  	amadan.net/oficina/sheets/internal/sheets/store	6.187s
ok  	amadan.net/oficina/sheets/internal/sheets/teamstate	0.122s
ok  	amadan.net/oficina/sheets/internal/sheets/xlsx	4.545s
?   	amadan.net/oficina/sheets/tools/gencorpus	[no test files]
=== race ===
go test -race -count=1 ./internal/sheets/store/... ./internal/sheets/live/...
ok  	amadan.net/oficina/sheets/internal/sheets/store	72.169s
ok  	amadan.net/oficina/sheets/internal/sheets/live	1.065s
diff --git a/AGENTS.md b/AGENTS.md
index a476ef8..bc68abf 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -243,6 +243,14 @@ relying on remembering one, make it a test instead.
`doctor` binary alongside the module** — a `doctor` one release behind
compares against its own library copy and calls the upgrade you just did
the drift.
+ - **Until `native.go` moves off the pre-move module path, `doctor` refuses
+ to run here.** `go.mod` still requires `github.com/carlosframework/rastrillo`
+ v0.23.0 for the native server's sessions, and `doctor` stops at that
+ ("No files were compared"). Meanwhile `static/tokens.css` is re-copied
+ by hand from the pinned module's `ui.TokensCSS()` on every rastrillo
+ upgrade, and `TestSharedThemeAssetFingerprintAndCache` pins it to
+ those bytes. `doctor`'s template checks are blind meanwhile: read the
+ changelog's markup notes for each release yourself.
- **Theme bytes come from the Estilo module pin.** There is no app-local
`static/theme.css` to copy. On framework upgrades, update the Estilo pin
to the same released `day` body and run the theme asset tests. Estilo's
diff --git a/Makefile b/Makefile
index 1466981..ae42cc4 100644
--- a/Makefile
+++ b/Makefile
@@ -127,11 +127,16 @@ test-race:
go test -race -count=1 ./internal/sheets/store/... ./internal/sheets/live/...
# The browser checks (internal/sheets/*_browser_test.go), in a real
-# Chromium: on PATH, via RASTRILLO_CHROME, or in a Playwright cache.
+# Chromium: RASTRILLO_CHROME, else chromium or google-chrome on PATH,
+# else a Playwright cache under ~/.cache/ms-playwright (the Linux build
+# only; on a Mac set RASTRILLO_CHROME to Playwright's or Chrome's binary).
# Deliberately NOT part of `test`: the gate CI runs on every push must
# not need a browser. Run it before shipping a change to the CSS or to
-# rastrillo's pin — TestNoZoomOnTap is the iOS zoom-on-tap check, and
-# TestDocumentStylesSurviveTheProductionCSP is the one browser check served
-# through rastrillo's production stack, so it sees the real CSP.
+# rastrillo's pin. The filter runs four tests: OnTap matches both
+# TestNoZoomOnTap (the iOS zoom-on-tap check) and TestFormatMenuFitsOnTap;
+# TestDocumentStylesSurviveTheProductionCSP and TestSmartDoorInABrowser
+# are served through rastrillo's production stack, so they see the real
+# CSP. TestSmartDoorInABrowser forces the light scheme itself; the others
+# measure whichever scheme the browser starts in.
test-browser:
CGO_ENABLED=0 go test -tags browser -count=1 -run 'OnTap|DocumentStyles|SmartDoorInABrowser' ./internal/sheets/
diff --git a/internal/sheets/home_door.go b/internal/sheets/home_door.go
index 5878ea5..f66a241 100644
--- a/internal/sheets/home_door.go
+++ b/internal/sheets/home_door.go
@@ -23,11 +23,16 @@ import (
// The door then says why (the refusal, in the screen's app-problem slot,
// with the email field), and a remembered Oficina ID is forgotten: offered
// again, it would sign the same refused person in and send them back here.
-// A remembered email way is kept: it is no loop. SigninLocal is wrapped
-// too, for the refusals that land there.
+// A remembered email way is kept: it is no loop. The refused session's
+// cookie is cleared on this response. SigninLocal is wrapped too, for the
+// refusals that land there.
func (a *App) admittedOnly(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if signedIn, admitted := a.admission(r, false); signedIn && !admitted {
+ // The probe's guard has already ended the session; clear its
+ // cookie on this response too, so a reload is plainly signed
+ // out rather than a dead cookie over the plain door.
+ a.homeSessions.SignOut(w, r)
r = withRefusal(withoutCookie(r, a.homeSessions.CookieName()))
jar := a.auth.RememberJar()
if rec, res := jar.Read(r); res == lastsignin.Valid && rec.Method == lastsignin.MethodFederated {
diff --git a/internal/sheets/home_routes.go b/internal/sheets/home_routes.go
index 29f2632..6c7714a 100644
--- a/internal/sheets/home_routes.go
+++ b/internal/sheets/home_routes.go
@@ -1,6 +1,8 @@
package sheets
import (
+ "net/http"
+
"amadan.net/oficina/home/connect"
"github.com/go-chi/chi/v5"
)
@@ -62,7 +64,7 @@ func (a *App) mountAuth(r chi.Router) {
r.Get("/auth/callback", a.homeRoutes.LocalGuard(a.auth.Callback))
r.Get("/auth/verify", a.homeRoutes.LocalGuard(a.auth.Verify))
r.Post("/auth/verify", a.homeRoutes.LocalGuard(a.auth.Verify))
- r.Post("/signout", a.homeRoutes.Signout)
+ r.Post("/signout", a.homeSignout)
} else {
r.Post("/signin", a.handleBegin)
r.Get("/auth/callback", a.auth.Callback)
@@ -72,3 +74,11 @@ func (a *App) mountAuth(r chi.Router) {
}
}
}
+
+// homeSignout is connect's Sign out, which also forgets a page a sign-in
+// in progress kept (signin_return.go): it was this person's, not the
+// next one's.
+func (a *App) homeSignout(w http.ResponseWriter, r *http.Request) {
+ forgetReturn(w)
+ a.homeRoutes.Signout(w, r)
+}
diff --git a/internal/sheets/signin.go b/internal/sheets/signin.go
index 8242cf1..eb81d82 100644
--- a/internal/sheets/signin.go
+++ b/internal/sheets/signin.go
@@ -228,8 +228,12 @@ func (a *App) handleSignedOut(w http.ResponseWriter, r *http.Request) {
// {issuer}/forget?return=<origin>/signed-out. id.oficina.works asks "Forget
// me on Oficina ID too?" and returns only to a registered consumer's exact
// /signed-out. Anything but Forget's own redirect (a cross-origin refusal,
-// a wrong method) is passed on as it is.
+// a wrong method) is passed on as it is. A page a sign-in in progress
+// kept (signin_return.go) is forgotten too.
func (a *App) forget(w http.ResponseWriter, r *http.Request) {
+ if a.returns != nil && r.Method == http.MethodPost {
+ forgetReturn(w)
+ }
if a.homeCreds == nil {
a.auth.Forget(w, r)
return
diff --git a/internal/sheets/signin_door_edges_test.go b/internal/sheets/signin_door_edges_test.go
new file mode 100644
index 0000000..64fd612
--- /dev/null
+++ b/internal/sheets/signin_door_edges_test.go
@@ -0,0 +1,173 @@
+package sheets
+
+// The door's edges (Fable's review of #71): an undecided guard, what the
+// admission probe leaves on the real response, and what the return memory
+// refuses to keep or read.
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "strings"
+ "testing"
+ "time"
+
+ "amadan.net/rastrillo/rastrillo/lastsignin"
+ "amadan.net/rastrillo/rastrillo/sessions"
+)
+
+// undecided makes team mode's guard answer 503 for a local session: the
+// table the member's Home identity is read from is gone, so teamIdentify
+// cannot decide (teamstate.ErrUnavailable).
+func undecided(t *testing.T) *teamFixture {
+ t.Helper()
+ f := newTeamFixture(t, signedInAddress)
+ if _, err := f.app.team.db.Exec(`DROP TABLE sheets_team_home_members`); err != nil {
+ t.Fatal(err)
+ }
+ return f
+}
+
+// A guard that cannot decide is no refusal: /signin draws no refusal,
+// keeps the remembered Oficina ID and keeps the session.
+func TestAnUndecidedGuardIsNoRefusalAtTheDoor(t *testing.T) {
+ f := undecided(t)
+ f.local(t, signedInAddress)
+ if rec := f.get(t, "/sheets"); rec.Code != http.StatusServiceUnavailable {
+ t.Fatalf("the guard answered %d, want its 503", rec.Code)
+ }
+ f.remember(t, lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina})
+ rec := f.get(t, "/signin")
+ if rec.Code == http.StatusOK {
+ t.Fatalf("an undecided session got the door:\n%s", rec.Body.String())
+ }
+ if got, res := f.remembered(t); res != lastsignin.Valid || got.Method != lastsignin.MethodFederated {
+ t.Fatalf("an undecided session lost the remembered Oficina ID: %+v (%v)", got, res)
+ }
+ if _, kept := f.jar[f.app.homeSessions.CookieName()]; !kept {
+ t.Fatal("an undecided session was signed out")
+ }
+}
+
+// And MintHomeSession remembers nothing on one, leaving the session alone.
+func TestAnUndecidedGuardRemembersNothing(t *testing.T) {
+ f := undecided(t)
+ if _, err := f.app.memberFor(signedInAddress); err != nil {
+ t.Fatal(err)
+ }
+ token, err := f.app.homeSessions.Mint(sessions.Session{Subject: signedInAddress, Method: "magiclink", AuthTime: time.Now()})
+ if err != nil {
+ t.Fatal(err)
+ }
+ cookie := &http.Cookie{Name: f.app.homeSessions.CookieName(), Value: token}
+ w := httptest.NewRecorder()
+ http.SetCookie(w, cookie)
+ f.app.rememberOficina(w, httptest.NewRequest(http.MethodGet, testOrigin+"/auth/finish", nil))
+ for _, c := range w.Result().Cookies() {
+ if c.Name == f.app.auth.RememberJar().CookieName() {
+ t.Fatal("Oficina ID was remembered on an undecided guard")
+ }
+ }
+ r := httptest.NewRequest(http.MethodGet, testOrigin+"/", nil)
+ r.AddCookie(cookie)
+ if _, ok := f.app.homeSessions.From(r); !ok {
+ t.Fatal("the probe ended a session it could not decide on")
+ }
+}
+
+// The probe writes nothing to the real response: an admitted session's
+// /signin goes straight to its page and sets no cookie.
+func TestAnAdmittedSigninSetsNoCookie(t *testing.T) {
+ f := newHomeFixture(t)
+ f.local(t, signedInAddress)
+ f.remember(t, lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina})
+ rec := f.get(t, "/signin?return_to=%2Fsheets%2F7")
+ redirectsTo(t, rec, "/sheets/7")
+ if cookies := rec.Result().Cookies(); len(cookies) != 0 {
+ t.Fatalf("an admitted /signin set cookies: %+v", cookies)
+ }
+}
+
+// A refused session's cookie is cleared on the door's own response, so a
+// reload is plainly signed out.
+func TestARefusedSessionsCookieIsCleared(t *testing.T) {
+ f := newHomeFixture(t)
+ f.local(t, "carol@example.com")
+ rec := f.get(t, "/signin")
+ if card := door(t, rec.Body.String()); rec.Code != http.StatusOK || !strings.Contains(card, appProblem) {
+ t.Fatalf("not the refusal: %d", rec.Code)
+ }
+ if _, kept := f.jar[f.app.homeSessions.CookieName()]; kept {
+ t.Fatal("the refused session's cookie was left on the browser")
+ }
+}
+
+// A hostile or self-defeating return_to lands a local sign-in on the list:
+// off-site paths, and the signed-out page, which would greet a fresh
+// sign-in with "You're signed out".
+func TestAReturnToThatIsNoPageLandsOnTheList(t *testing.T) {
+ f := newHomeFixture(t)
+ for _, to := range []string{"//evil.example/x", "https://evil.example/", `/\evil.example`, "/signed-out", "/signed-out?x=1"} {
+ b := f.browser()
+ b.get(t, "/signin?return_to="+url.QueryEscape(to))
+ if landed := b.emailSignin(t, url.Values{"address": {signedInAddress}, "return_to": {to}}); landed != "/sheets" {
+ t.Errorf("return_to %q landed on %q, want /sheets", to, landed)
+ }
+ }
+}
+
+// The return memory reads a tampered, an expired or a duplicated cookie
+// as "/", and a sound one as its page.
+func TestTheReturnMemoryReadsOnlyItsOwnCookie(t *testing.T) {
+ f := newHomeFixture(t)
+ mem := f.app.returns
+ w := httptest.NewRecorder()
+ if !mem.remember(w, "/sheets/7") {
+ t.Fatal("remember refused a sound page")
+ }
+ kept := w.Result().Cookies()[0]
+ read := func(m *returnMemory, cookies ...*http.Cookie) string {
+ r := httptest.NewRequest(http.MethodGet, testOrigin+"/", nil)
+ for _, c := range cookies {
+ r.AddCookie(c)
+ }
+ return m.path(r)
+ }
+ if got := read(mem, kept); got != "/sheets/7" {
+ t.Fatalf("a sound cookie read as %q", got)
+ }
+ tampered := *kept
+ b := []byte(tampered.Value)
+ b[len(b)/2] ^= 1
+ tampered.Value = string(b)
+ later := *mem
+ later.now = func() time.Time { return time.Now().Add(time.Duration(homeReturnLifetime+1) * time.Second) }
+ for name, got := range map[string]string{
+ "tampered": read(mem, &tampered),
+ "expired": read(&later, kept),
+ "duplicated": read(mem, kept, kept),
+ } {
+ if got != "/" {
+ t.Errorf("a %s cookie read as %q, want /", name, got)
+ }
+ }
+}
+
+// Forget and Sign out forget a page a sign-in in progress kept.
+func TestForgetAndSignOutClearTheReturnMemory(t *testing.T) {
+ f := newHomeFixture(t)
+ f.get(t, "/signin?return_to=%2Fsheets%2F7")
+ if f.jar[homeReturnCookie] == nil {
+ t.Fatal("the door did not remember the page")
+ }
+ f.request(t, "POST", signinForgetPath, url.Values{"way": {"email"}}, false)
+ if _, kept := f.jar[homeReturnCookie]; kept {
+ t.Fatal("Forget kept the remembered page")
+ }
+ f.get(t, "/signin?return_to=%2Fsheets%2F7")
+ f.local(t, signedInAddress)
+ f.request(t, "POST", "/signout", nil, false)
+ if _, kept := f.jar[homeReturnCookie]; kept {
+ t.Fatal("Sign out kept the remembered page")
+ }
+}
diff --git a/internal/sheets/signin_door_helpers_test.go b/internal/sheets/signin_door_helpers_test.go
index 612375e..c2701fc 100644
--- a/internal/sheets/signin_door_helpers_test.go
+++ b/internal/sheets/signin_door_helpers_test.go
@@ -21,6 +21,12 @@ func (f *homeFixture) browser() *homeFixture {
return &homeFixture{app: f.app, key: f.key, jar: map[string]*http.Cookie{}, creds: f.creds}
}
+// get is a GET from this browser.
+func (f *homeFixture) get(t *testing.T, path string) *httptest.ResponseRecorder {
+ t.Helper()
+ return f.request(t, "GET", path, nil, false)
+}
+
// remember gives the browser a remembered way in, sealed by Sheets' own
// jar, as rastrillo would have after a sign-in.
func (f *homeFixture) remember(t *testing.T, rec lastsignin.Record) {
diff --git a/internal/sheets/signin_return.go b/internal/sheets/signin_return.go
index 679184c..ad66f19 100644
--- a/internal/sheets/signin_return.go
+++ b/internal/sheets/signin_return.go
@@ -54,9 +54,11 @@ type homeReturnIntent struct {
func (n *returnMemory) purpose() string { return "sheets.home-return.v1:" + n.origin }
-// remember keeps path ("/" when it is not one safe local path).
+// remember keeps path ("/" when it is not one safe local path, or is the
+// signed-out page, which would land a fresh sign-in on "You're signed
+// out").
func (n *returnMemory) remember(w http.ResponseWriter, path string) bool {
- if !handoff.SafePath(path) {
+ if !handoff.SafePath(path) || signedOutPage(path) {
path = "/"
}
now := n.now().Unix()
@@ -116,6 +118,17 @@ func (n *returnMemory) path(r *http.Request) string {
return intent.Path
}
+// signedOutPage reports whether path is /signed-out, with or without a
+// query or fragment.
+func signedOutPage(path string) bool {
+ u, err := url.Parse(path)
+ return err != nil || u.Path == "/signed-out"
+}
+
+// forgetReturn clears the return memory: Forget and Sign out end what a
+// sign-in in progress had kept.
+func forgetReturn(w http.ResponseWriter) { setReturnCookie(w, "", -1) }
+
func setReturnCookie(w http.ResponseWriter, value string, age int) {
http.SetCookie(w, &http.Cookie{Name: homeReturnCookie, Value: value, Path: "/", Secure: true, HttpOnly: true, SameSite: http.SameSiteLaxMode, MaxAge: age})
}