oficina / docs Public

Docs: a native session never approves another device's sign-in

rastrillo/native's browser approval (/native/request, /native/authorize,
/native/exchange) ran h.requireCurrent, which admits any current
Home-derived session, including Oficina ID native sessions
(connect.NativeMethod) and native-web ones (connect.NativeWebMethod).
The exchange mints a copy of the approving session with the same method
and a fresh created_at, so a six-day-old native session could approve a
new request and get a session good for another seven days, and repeat,
with no Home grant behind it: connect.NativeLifetime's absolute limit,
a revoked grant and "Sign out all devices" all stopped holding.

Probed through the real Router before the fix, in Home mode and team
mode: authorize GET=200 POST=200, exchange=200, minted method
home-assertion-native created 0s ago; with the original past seven days
its list was 303 and the minted one's 200. Same for home-native-web.

nativeApprover wraps requireCurrent for the approval flow only and
refuses a native or native-web session with the same 401 requireCurrent
answers a signed-out approval. It also covers the exchange's re-check of
the approved cookie. Streams (nativeStreamAllowed) and the sign-in return
keep requireCurrent itself. A browser sign-in still approves
(TestBrowserSessionStillApprovesANativeSignIn).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 765e75b2355bb795fcd4c8e7880249086bd5925f parent 84d1008
4 files changed, +213 −1
  • internal/docs/app.go +1 −1
  • internal/docs/errors_test.go +1 −0
  • internal/docs/native.go +24 −0
  • internal/docs/native_approval_test.go +187 −0

CI passed — run details

diff --git a/internal/docs/app.go b/internal/docs/app.go
index 0812c70..3594fda 100644
--- a/internal/docs/app.go
+++ b/internal/docs/app.go
@@ -636,7 +636,7 @@ func New(cfg Config) (*App, error) {
d.Close()
return nil, err
}
- native := nativeauth.New(nativeSessions, cfg.Origin, "Docs", h.requireCurrent)
+ native := nativeauth.New(nativeSessions, cfg.Origin, "Docs", nativeApprover(h.requireCurrent))
var standalone *returnMemory
if home == nil {
if standalone, err = standaloneReturn(instanceKey, cfg.Origin); err != nil {
diff --git a/internal/docs/errors_test.go b/internal/docs/errors_test.go
index 9cce5a9..dc51cd2 100644
--- a/internal/docs/errors_test.go
+++ b/internal/docs/errors_test.go
@@ -253,6 +253,7 @@ func TestEveryPlainTextErrorIsClassified(t *testing.T) {
"home_render.go:renderer.homeLinkBack": {1, "the page itself could not render"},
"identity.go:identityResolver.middleware": {1, "shared guard in front of page and fetch routes (R3)"},
"identity.go:refuseIdentity": {1, "non-GET refusal; a GET redirects to sign-in"},
+ "native.go:nativeApprover": {1, "a native session refused native approval: the same 401 requireCurrent answers a signed-out approval"},
"native.go:nativeJSON": {1, "native API JSON"},
"native.go:handlers.nativeList": {2, "native API JSON"},
"search.go:handlers.searchJSON": {3, "jump-to palette JSON"},
diff --git a/internal/docs/native.go b/internal/docs/native.go
index dbbfe42..21fe2c8 100644
--- a/internal/docs/native.go
+++ b/internal/docs/native.go
@@ -8,7 +8,9 @@ import (
"strings"
"time"
+ "amadan.net/oficina/home/connect"
"amadan.net/rastrillo/rastrillo"
+ "amadan.net/rastrillo/rastrillo/sessions"
// The one import left on rastrillo's pre-move path, on purpose:
// rastrillo/native's server (2a5d07b, and its main) still takes a
@@ -27,6 +29,28 @@ func newNativeSessions(db *sql.DB, origin string, logger *slog.Logger) (*presess
return presessions.New(presessions.Config{DB: db, Origin: origin, SigninPath: "/signin", Logger: logger})
}
+// nativeApprover is the require rastrillo/native's browser approval runs
+// (/native/authorize, and /native/exchange's re-check of the approved
+// cookie): requireCurrent, with a native or native-web session refused as
+// signed out. The exchange mints a copy of the approving session, same
+// method and a fresh created_at, so a native session that could approve
+// would renew itself past connect.NativeLifetime with no Home grant behind
+// it, outliving a revoked grant or "Sign out all devices". Only a browser
+// sign-in approves another device (native_approval_test.go). Streams and
+// the sign-in return keep requireCurrent itself.
+func nativeApprover(require func(http.Handler) http.Handler) func(http.Handler) http.Handler {
+ return func(next http.Handler) http.Handler {
+ return require(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ s, ok := sessions.Current(r)
+ if !ok || s.Method == connect.NativeMethod || s.Method == connect.NativeWebMethod {
+ http.Error(w, rastrillo.T(r, "docs.error.generic"), http.StatusUnauthorized)
+ return
+ }
+ next.ServeHTTP(w, r)
+ }))
+ }
+}
+
func wantsNative(r *http.Request) bool {
return strings.Contains(r.Header.Get("Accept"), "application/json")
}
diff --git a/internal/docs/native_approval_test.go b/internal/docs/native_approval_test.go
new file mode 100644
index 0000000..5e46429
--- /dev/null
+++ b/internal/docs/native_approval_test.go
@@ -0,0 +1,187 @@
+package docs
+
+import (
+ "crypto/sha256"
+ "encoding/base64"
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+ "time"
+
+ "amadan.net/oficina/home/connect"
+ "amadan.net/rastrillo/rastrillo/sessions"
+)
+
+// An Oficina ID native session ends seven days after it was minted
+// (connect.NativeLifetime, Home spec 2026-11-14-oficina-id-native.md §7),
+// so a revoked device grant or "Sign out all devices" holds within a week.
+// rastrillo/native's browser approval (/native/request, /native/authorize,
+// /native/exchange) mints a copy of the approving session with the same
+// method and a fresh created_at. If a native session could approve, it
+// could renew itself every six days with no Home grant behind it, forever.
+// So a native or native-web session never approves another sign-in; a
+// browser session still does.
+
+// approvalRequest is one of the native approval flow's requests, sent
+// through the router with the given session cookie (if any).
+func approvalRequest(f *routerHomeFixture, cookie *http.Cookie, method, path, body string) *httptest.ResponseRecorder {
+ b := f.browser()
+ if cookie != nil {
+ b.cookies[cookie.Name] = cookie
+ }
+ r := b.request(method, path, body)
+ if strings.HasPrefix(body, "{") {
+ r.Header.Set("Content-Type", "application/json")
+ r.Header.Set("Accept", "application/json")
+ }
+ return b.serve(r)
+}
+
+// approvalBegin is POST /native/request; it returns the request's id and
+// the verifier only the app holds.
+func approvalBegin(t *testing.T, f *routerHomeFixture) (id, verifier string) {
+ t.Helper()
+ verifier = exchangeProof(t)
+ sum := sha256.Sum256([]byte(verifier))
+ rec := approvalRequest(f, nil, "POST", "/native/request", `{"challenge":"`+base64.RawURLEncoding.EncodeToString(sum[:])+`"}`)
+ var out struct{ ID string }
+ if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &out) != nil || out.ID == "" {
+ t.Fatalf("POST /native/request = %d %q", rec.Code, rec.Body.String())
+ }
+ return out.ID, verifier
+}
+
+// approvalExchange is POST /native/exchange; minted is the new session's
+// cookie value, "" when nothing was minted.
+func approvalExchange(t *testing.T, f *routerHomeFixture, id, verifier string) (code int, minted string) {
+ t.Helper()
+ rec := approvalRequest(f, nil, "POST", "/native/exchange", `{"id":"`+id+`","verifier":"`+verifier+`"}`)
+ var out struct{ Value string }
+ _ = json.Unmarshal(rec.Body.Bytes(), &out)
+ return rec.Code, out.Value
+}
+
+func ageSession(t *testing.T, f *routerHomeFixture, token string, age time.Duration) {
+ t.Helper()
+ at := time.Now().Add(-age).UTC().Format(time.RFC3339)
+ res, err := f.app.db.Writer().Exec("UPDATE sessions SET created_at=? WHERE token_hash=?", at, sessions.HashToken(token))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if n, _ := res.RowsAffected(); n != 1 {
+ t.Fatalf("age session: %d rows", n)
+ }
+}
+
+// probeNativeApproval has a six-day-old session (still inside its seven
+// days) try to approve a native sign-in, and fails with what that bought
+// if it could.
+func probeNativeApproval(t *testing.T, f *routerHomeFixture, session nativeAnswer) {
+ t.Helper()
+ ageSession(t, f, session.Value, 6*24*time.Hour)
+ if r := f.nativeList(t, session); r.Code != 200 {
+ t.Fatalf("a six-day-old native session was refused before the probe: %d", r.Code)
+ }
+ cookie := &http.Cookie{Name: session.Name, Value: session.Value}
+ id, verifier := approvalBegin(t, f)
+ page := approvalRequest(f, cookie, "GET", "/native/authorize?id="+id, "")
+ approve := approvalRequest(f, cookie, "POST", "/native/authorize?id="+id, "")
+ code, minted := approvalExchange(t, f, id, verifier)
+ if minted != "" {
+ s, _ := f.sessions.From(sessionRequest(nativeAnswer{Name: session.Name, Value: minted}))
+ ageSession(t, f, session.Value, connect.NativeLifetime+time.Hour)
+ original := f.nativeList(t, session).Code
+ renewed := f.nativeList(t, nativeAnswer{Name: session.Name, Value: minted}).Code
+ t.Fatalf("a %s session approved a native sign-in: authorize GET=%d POST=%d, exchange=%d minted method=%q created %s ago; with the original past seven days its list=%d, the minted one's=%d",
+ session.methodOf(t, f), page.Code, approve.Code, code, s.Method, time.Since(s.At).Round(time.Second), original, renewed)
+ }
+ if page.Code == 200 || approve.Code == 200 {
+ t.Fatalf("approval page served to a native session: GET=%d POST=%d", page.Code, approve.Code)
+ }
+ if page.Code != http.StatusUnauthorized || approve.Code != http.StatusUnauthorized {
+ t.Fatalf("refusal = GET %d POST %d, want the signed-out 401", page.Code, approve.Code)
+ }
+ if code != http.StatusAccepted {
+ t.Fatalf("exchange after a refused approval = %d, want 202 (still pending)", code)
+ }
+}
+
+func (a nativeAnswer) methodOf(t *testing.T, f *routerHomeFixture) string {
+ t.Helper()
+ s, ok := f.sessions.From(sessionRequest(a))
+ if !ok {
+ return "(no session)"
+ }
+ return s.Method
+}
+
+// homeNativeFixture is a Home-mode Docs with alice linked to pr_alice, and
+// the browser she linked from (which holds a browser session).
+func homeNativeFixture(t *testing.T) (*routerHomeFixture, *routerBrowser) {
+ t.Helper()
+ f := newRouterHomeFixture(t)
+ b := f.browser()
+ b.localSignin(t, "alice@example.com")
+ b.linkProof(t, "pr_alice", "alice@example.com")
+ routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL("/"))
+ return f, b
+}
+
+func TestNativeSessionCannotApproveANativeSignIn(t *testing.T) {
+ f, _ := homeNativeFixture(t)
+ rec, answer := f.nativeExchange(t, "pr_alice", nil)
+ if rec.Code != 200 || answer.Value == "" {
+ t.Fatalf("native exchange=%d %s", rec.Code, rec.Body.String())
+ }
+ if m := answer.methodOf(t, f); m != connect.NativeMethod {
+ t.Fatalf("native exchange minted %q", m)
+ }
+ probeNativeApproval(t, f, answer)
+}
+
+// "Open on the web" sessions age like native ones, so they cannot approve
+// either. connect does not mint them yet; the row is what it will mint.
+func TestNativeWebSessionCannotApproveANativeSignIn(t *testing.T) {
+ f, _ := homeNativeFixture(t)
+ value, err := f.sessions.Mint(sessions.Session{Subject: "pr_alice", Method: connect.NativeWebMethod, AuthTime: time.Now().Add(-time.Minute)})
+ if err != nil {
+ t.Fatal(err)
+ }
+ answer := nativeAnswer{Name: f.sessions.CookieName(), Value: value}
+ probeNativeApproval(t, f, answer)
+}
+
+func TestTeamNativeSessionCannotApproveANativeSignIn(t *testing.T) {
+ f := newTeamRouterFixture(t)
+ rec, answer := f.nativeExchange(t, teamSubject('b'), map[string]string{"choice": "new"})
+ if rec.Code != 200 || answer.Value == "" {
+ t.Fatalf("native exchange=%d %s", rec.Code, rec.Body.String())
+ }
+ probeNativeApproval(t, f.routerHomeFixture, answer)
+}
+
+// The control: the browser session alice signed in with still approves,
+// through the same requests, and the exchange mints her app a session.
+func TestBrowserSessionStillApprovesANativeSignIn(t *testing.T) {
+ f, b := homeNativeFixture(t)
+ cookie := b.cookies[f.sessions.CookieName()]
+ if cookie == nil {
+ t.Fatal("no browser session")
+ }
+ id, verifier := approvalBegin(t, f)
+ if r := approvalRequest(f, cookie, "GET", "/native/authorize?id="+id, ""); r.Code != 200 {
+ t.Fatalf("approval page = %d", r.Code)
+ }
+ if r := approvalRequest(f, cookie, "POST", "/native/authorize?id="+id, ""); r.Code != 200 {
+ t.Fatalf("approval = %d %q", r.Code, r.Body.String())
+ }
+ code, minted := approvalExchange(t, f, id, verifier)
+ if code != 200 || minted == "" {
+ t.Fatalf("exchange = %d", code)
+ }
+ if r := f.nativeList(t, nativeAnswer{Name: f.sessions.CookieName(), Value: minted}); r.Code != 200 {
+ t.Fatalf("minted session list = %d", r.Code)
+ }
+}