Sheets Oficina ID native: Fable's review of b942939
1 (MEDIUM, security) rastrillo/native's browser approval now runs nativeApprover, which refuses a native or native-web session before the app's own guard: /native/authorize would otherwise approve a device with one, and /native/exchange mint a copy keeping the native method with a fresh creation time, so a native session could renew itself past connect's seven days. TestNativeSessionCannotApproveTheBrowserSignIn (native routes mounted as main mounts them) fails with the old guard and passes now; a browser's own session still approves. 2 members.go actor() uses connect.IsHomeDerived (the seventh site). 3 Tests: the cross-origin choose POST carries the page's real form token and proof cookie; "I'm new here" over a live app session must be 403 with no member made; the approval test also pins POST /native/oficina-id routing ahead of the /native/* wildcard. 4 An expired native session's 303 to /signin stays as it is: rastrillo's NativeHTTP never follows redirects and reads 303 as unauthorized, and the native transport re-mints on 401 or 303, as Docs' DocsSessionWatch does. No server change. 5 With no live choice, the choose page renders "failed" with the way on to /account/home, as Docs' home_render.go does. The approval test lives in home_native_approval_test.go (the 300-line cap); the new refusal is classified in TestEveryPlainTextErrorIsClassified. make test passes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+126
−6
internal/sheets/errors_test.go+1 −0internal/sheets/home.go+4 −0internal/sheets/home_native_approval_test.go+80 −0internal/sheets/home_native_test.go+16 −4internal/sheets/members.go+2 −1internal/sheets/native.go+23 −1
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.164s ok amadan.net/oficina/sheets/internal/sheets 15.416s ok amadan.net/oficina/sheets/internal/sheets/calc 0.014s 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.054s 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.275s ok amadan.net/oficina/sheets/internal/sheets/teamstate 0.099s ok amadan.net/oficina/sheets/internal/sheets/xlsx 4.635s ? 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.089s ok amadan.net/oficina/sheets/internal/sheets/live 1.066s
diff --git a/internal/sheets/errors_test.go b/internal/sheets/errors_test.go| index 24dcd8c..122b26f 100644 |
| --- a/internal/sheets/errors_test.go |
| +++ b/internal/sheets/errors_test.go |
| @@ -172,6 +172,7 @@ func TestEveryPlainTextErrorIsClassified(t *testing.T) { |
| "home.go:App.homeLocalComplete": {2, "Home link completion refusals (R3: moves with home/connect)"}, |
| "home.go:App.rejectDisabledHome": {1, "shared session guard for pages and fetches (R3)"}, |
| "jobpages.go:App.renderJobFragment": {2, "the job page's polled fragment, not a page"}, |
| + "native.go:App.nativeApprover": {1, "refuses a native session at rastrillo/native's device approval; no browser holds one"}, |
| "team_access.go:App.teamRequire": {2, "team mode's session guard, homeRequire's twin: a page GET redirects to /signin"}, |
| } |
| got := httpErrorSites(t) |
diff --git a/internal/sheets/home.go b/internal/sheets/home.go| index 1724167..c3c676a 100644 |
| --- a/internal/sheets/home.go |
| +++ b/internal/sheets/home.go |
| @@ -71,6 +71,10 @@ func (a *App) ConfigureHome(raw *connect.Credentials) error { |
| view.Choice = &homeChoice{Action: form.Action, Token: form.Token, |
| ConnectLabel: homeCopy["choose_connect_action"], NewLabel: homeCopy["choose_new_action"]} |
| } else { |
| + // No live choice to offer: "failed", with the way on to |
| + // connecting an account, as Docs' home_render.go does. |
| + view.PageData = connect.PageFor(r, "failed", label, func(k string) string { return homeCopy[k] }) |
| + view.Action, view.ActionLabel = "/account/home", homeCopy["menu_action"] |
| status = http.StatusForbidden |
| } |
| } |
diff --git a/internal/sheets/home_native_approval_test.go b/internal/sheets/home_native_approval_test.go| new file mode 100644 |
| index 0000000..28c753d |
| --- /dev/null |
| +++ b/internal/sheets/home_native_approval_test.go |
| @@ -0,0 +1,80 @@ |
| +package sheets |
| + |
| +import ( |
| + "crypto/sha256" |
| + "encoding/base64" |
| + "encoding/json" |
| + "net/http" |
| + "net/http/httptest" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +// The native exchange with rastrillo/native's browser approval mounted, as |
| +// main mounts both: POST /native/oficina-id still reaches connect ahead of |
| +// the /native/* wildcard, and a native session can never approve another |
| +// device. The approval mints a copy with a fresh creation time, so a |
| +// native session approving itself would outlive its seven days (Fable, |
| +// sheets b942939). |
| +func TestNativeSessionCannotApproveTheBrowserSignIn(t *testing.T) { |
| + f := newHomeFixture(t) |
| + if err := f.app.ConfigureNative(); err != nil { |
| + t.Fatal(err) |
| + } |
| + f.local(t, signedInAddress) |
| + f.link(t, "pr_linked") |
| + local := f.jar[f.app.homeSessions.CookieName()] |
| + f.jar = map[string]*http.Cookie{} |
| + |
| + rec, answer := f.nativeExchange(t, "pr_linked", nil) |
| + if rec.Code != http.StatusOK || answer.State != "ok" { |
| + t.Fatalf("Oficina ID exchange with native routes mounted = %d %s", rec.Code, rec.Body.String()) |
| + } |
| + native := &http.Cookie{Name: answer.Name, Value: answer.Value} |
| + |
| + // rastrillo/native's browser approval, at this instance's origin. |
| + post := func(path string, body any, cookie *http.Cookie) *httptest.ResponseRecorder { |
| + var reader *strings.Reader |
| + r := httptest.NewRequest("POST", testOrigin+path, nil) |
| + if body != nil { |
| + data, _ := json.Marshal(body) |
| + reader = strings.NewReader(string(data)) |
| + r = httptest.NewRequest("POST", testOrigin+path, reader) |
| + r.Header.Set("Content-Type", "application/json") |
| + } |
| + r.Header.Set("Origin", testOrigin) |
| + if cookie != nil { |
| + r.AddCookie(cookie) |
| + } |
| + w := httptest.NewRecorder() |
| + f.app.Router().ServeHTTP(w, r) |
| + return w |
| + } |
| + request := func() (string, string) { |
| + verifier := randomProof(t) |
| + sum := sha256.Sum256([]byte(verifier)) |
| + rec := post("/native/request", map[string]string{"challenge": base64.RawURLEncoding.EncodeToString(sum[:])}, nil) |
| + var out struct{ ID string } |
| + if rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &out) != nil || out.ID == "" { |
| + t.Fatalf("POST /native/request = %d %s", rec.Code, rec.Body.String()) |
| + } |
| + return out.ID, verifier |
| + } |
| + |
| + id, verifier := request() |
| + if approve := post("/native/authorize?id="+id, nil, native); approve.Code == http.StatusOK || strings.Contains(approve.Body.String(), "is signed in") { |
| + t.Fatalf("a native session approved a device: %d %s", approve.Code, approve.Body.String()) |
| + } |
| + if ex := post("/native/exchange", map[string]string{"id": id, "verifier": verifier}, nil); ex.Code == http.StatusOK { |
| + t.Fatalf("exchange after a native session's approval = %d %s", ex.Code, ex.Body.String()) |
| + } |
| + |
| + // A browser's own session still approves, as before. |
| + id, verifier = request() |
| + if approve := post("/native/authorize?id="+id, nil, local); approve.Code != http.StatusOK { |
| + t.Fatalf("a browser session's approval = %d %s", approve.Code, approve.Body.String()) |
| + } |
| + if ex := post("/native/exchange", map[string]string{"id": id, "verifier": verifier}, nil); ex.Code != http.StatusOK { |
| + t.Fatalf("exchange after a browser approval = %d %s", ex.Code, ex.Body.String()) |
| + } |
| +} |
diff --git a/internal/sheets/home_native_test.go b/internal/sheets/home_native_test.go| index 226bc9c..4ce3831 100644 |
| --- a/internal/sheets/home_native_test.go |
| +++ b/internal/sheets/home_native_test.go |
| @@ -5,9 +5,11 @@ import ( |
| "crypto/rand" |
| "encoding/base64" |
| "encoding/json" |
| + "html" |
| "net/http" |
| "net/http/httptest" |
| "net/url" |
| + "regexp" |
| "strings" |
| "testing" |
| "time" |
| @@ -238,14 +240,24 @@ func TestTeamMemberChoicePage(t *testing.T) { |
| t.Fatalf("member sign-in again = %q", fin.Header().Get("Location")) |
| } |
| f.local(t, "local@example.com") |
| - if rec := f.choose(t, "new"); rec.Code == http.StatusSeeOther && rec.Header().Get("Location") == "/" { |
| - t.Fatal("I'm new here was allowed over a live app session") |
| + if rec := f.choose(t, "new"); rec.Code != http.StatusForbidden { |
| + t.Fatalf("I'm new here over a live app session = %d %q, want 403", rec.Code, rec.Header().Get("Location")) |
| + } |
| + var made int64 |
| + f.app.db.Model(&Member{}).Where("address IS NULL").Count(&made) |
| + if made != 0 { |
| + t.Fatalf("Home-only members = %d after a refused choice, want 0", made) |
| } |
| - // A POST from another origin never reaches the choice. |
| + // A POST from another origin never reaches the choice, even carrying |
| + // the page's real form token and the proof cookie. |
| f.jar = map[string]*http.Cookie{} |
| f.signInHomeChoosing(t, a) |
| - r := httptest.NewRequest("POST", testOrigin+connect.ChoosePath, strings.NewReader(url.Values{"choice": {"new"}, "token": {"x"}}.Encode())) |
| + token := regexp.MustCompile(`name="token" value="([^"]+)"`).FindStringSubmatch(f.get(t, connect.ChoosePath).Body.String()) |
| + if token == nil { |
| + t.Fatal("no form token on the choose page") |
| + } |
| + r := httptest.NewRequest("POST", testOrigin+connect.ChoosePath, strings.NewReader(url.Values{"choice": {"new"}, "token": {html.UnescapeString(token[1])}}.Encode())) |
| r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| r.Header.Set("Origin", "https://evil.example") |
| for _, c := range f.jar { |
diff --git a/internal/sheets/members.go b/internal/sheets/members.go| index 71eb082..42248d9 100644 |
| --- a/internal/sheets/members.go |
| +++ b/internal/sheets/members.go |
| @@ -5,6 +5,7 @@ import ( |
| "net/http" |
| "strings" |
| + "amadan.net/oficina/home/connect" |
| "gorm.io/gorm" |
| "gorm.io/gorm/clause" |
| ) |
| @@ -100,7 +101,7 @@ func (a *App) actor(r *http.Request) (Member, bool) { |
| } |
| if a.home != nil { |
| s, ok := a.homeSessions.From(r) |
| - if ok && s.Method == "home-assertion" { |
| + if ok && connect.IsHomeDerived(s.Method) { |
| p, err := a.home.ResolveSession(r) |
| if err != nil { |
| return Member{}, false |
diff --git a/internal/sheets/native.go b/internal/sheets/native.go| index 1947650..6c76931 100644 |
| --- a/internal/sheets/native.go |
| +++ b/internal/sheets/native.go |
| @@ -5,6 +5,7 @@ import ( |
| "net/http" |
| "strings" |
| + "amadan.net/oficina/home/connect" |
| nativeauth "amadan.net/rastrillo/native/server" |
| "amadan.net/rastrillo/rastrillo/sessions" |
| @@ -28,9 +29,30 @@ func (a *App) ConfigureNative() error { |
| if err != nil { |
| return err |
| } |
| - a.native = nativeauth.New(s, a.origin, "Sheets", a.requireSession) |
| + a.native = nativeauth.New(s, a.origin, "Sheets", a.nativeApprover) |
| return nil |
| } |
| + |
| +// nativeApprover is the guard rastrillo/native's browser approval runs |
| +// before approving a device (/native/authorize) and again on the approved |
| +// cookie before minting its session (/native/exchange): the app's own |
| +// guard, but never for a native or native-web session. The exchange mints a |
| +// copy that keeps the method with a fresh creation time, and connect's |
| +// seven-day limit counts from that, so a native session approving another |
| +// would renew itself for ever (Fable, sheets b942939). A native session |
| +// signs in with Oficina ID again instead. |
| +func (a *App) nativeApprover(next http.Handler) http.Handler { |
| + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + if a.homeSessions != nil { |
| + if s, ok := a.homeSessions.From(r); ok && (s.Method == connect.NativeMethod || s.Method == connect.NativeWebMethod) { |
| + w.Header().Set("Cache-Control", "no-store") |
| + http.Error(w, "Sign in first.", http.StatusForbidden) |
| + return |
| + } |
| + } |
| + a.requireSession(next).ServeHTTP(w, r) |
| + }) |
| +} |
| func wantsNative(r *http.Request) bool { |
| return strings.Contains(r.Header.Get("Accept"), "application/json") |
| } |