oficina / home Public

handoff: ConsumerConfig.StartPath for a fresh-auth consumer

Adds an optional ConsumerConfig.StartPath (empty = the existing /auth/start constant, so every current consumer is unchanged), validated at construction (under /auth/, clean, [a-z0-9/_-], <=128 bytes, not the callback or finish path), so the directory can mount a second RequireFreshAuth consumer at its own start path for the Team page and operator confirm pages (implementation/team-members.md s2, team-provisioning-amendment.md s4). Freshness stays bound to the sealed pending row, enforced in Store.Finish regardless of which consumer's Finish runs. Reviewed by Claude Fable 5.1 (claude-fable-5-1), discussion 93, APPROVED; CI green at 88e76c6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Squashed from handoff-start-path into main.
claude agent, operated by paul@keymail.dev 69ea999ccbf78badbd4015ef606478c663490e50 parent 23b1ef2
2 files changed, +237 −2
  • handoff/http.go +44 −2
  • handoff/start_path_test.go +193 −0

CI passed — run details

diff --git a/handoff/http.go b/handoff/http.go
index fd0fe64..eb19fbf 100644
--- a/handoff/http.go
+++ b/handoff/http.go
@@ -5,6 +5,7 @@ import (
"mime"
"net/http"
"net/url"
+ "path"
"strings"
"time"
@@ -24,6 +25,25 @@ type ConsumerConfig struct {
ConsumerID string
// RequireFreshAuth requests and enforces actual authentication within five minutes.
RequireFreshAuth bool
+ // StartPath is the one path Start serves. Empty means the package
+ // StartPath, so a consumer that leaves it unset behaves exactly as before.
+ // It lets a second consumer on the same Store and ConsumerID, typically
+ // RequireFreshAuth, have its own entry (for example /auth/fresh/start).
+ // NewConsumer refuses a path that is not clean, lowercase [a-z0-9/_-]
+ // under /auth/ (SafePath refuses that prefix, so no return_to can point
+ // back into a start), or that is CallbackPath or FinishPath, which every
+ // consumer of a Store shares. Two consumers at one path are the router's
+ // business: an app may dispatch between them itself, as connect does.
+ //
+ // Callback and Finish do not read this field or RequireFreshAuth. The
+ // freshness class is sealed into the pending row by Start (Store.StartFresh's
+ // path prefix), and Store.Finish enforces it whichever consumer's Finish
+ // handles the browser, so one shared Finish serves both start paths and
+ // no Finish can change a row's class: a fresh row refuses (and burns) a
+ // proof older than five minutes or from the future, an ordinary row does
+ // not check age. Complete is not told which path started the flow; code
+ // that needs freshness after Finish reads the claims' AuthTime.
+ StartPath string
// BindStart binds the public nonce to an app initiation after pending storage
// commits, before any cookie or redirect. It must not make network calls or
// create grants. Rejection cancels this new pending request.
@@ -39,7 +59,10 @@ type ConsumerConfig struct {
type Consumer struct{ cfg ConsumerConfig }
func NewConsumer(cfg ConsumerConfig) (*Consumer, error) {
- if cfg.Store == nil || cfg.Store.db == nil || cfg.Complete == nil || !consumerID(cfg.ConsumerID) {
+ if cfg.StartPath == "" {
+ cfg.StartPath = StartPath
+ }
+ if cfg.Store == nil || cfg.Store.db == nil || cfg.Complete == nil || !consumerID(cfg.ConsumerID) || !startPath(cfg.StartPath) {
return nil, ErrConfig
}
if cfg.Now == nil {
@@ -67,6 +90,25 @@ func consumerID(s string) bool {
return true
}
+// startPath accepts the package StartPath and any clean path in the exchange's
+// own /auth/ namespace other than the two shared routes. Every byte is checked,
+// so the value is a literal r.URL.Path with no query, escape or dot segment.
+func startPath(s string) bool {
+ if len(s) > 128 || !strings.HasPrefix(s, "/auth/") || path.Clean(s) != s || s == CallbackPath || s == FinishPath || SafePath(s) {
+ return false
+ }
+ for _, b := range []byte(s) {
+ if !(b >= 'a' && b <= 'z' || b >= '0' && b <= '9' || b == '/' || b == '_' || b == '-') {
+ return false
+ }
+ }
+ return true
+}
+
+// StartPath reports the path this consumer's Start serves, for apps that
+// mount it or link to it.
+func (c *Consumer) StartPath() string { return c.cfg.StartPath }
+
func (c *Consumer) guard(w http.ResponseWriter, r *http.Request, method, path string) bool {
w.Header().Set("Cache-Control", "no-store")
if r.Host != strings.TrimPrefix(c.cfg.Store.audience, "https://") || r.URL.Path != path {
@@ -104,7 +146,7 @@ func (c *Consumer) fail(w http.ResponseWriter, r *http.Request, e error) {
// Start accepts a bounded local return_to query and writes only sealed intent.
func (c *Consumer) Start(w http.ResponseWriter, r *http.Request) {
- if !c.guard(w, r, http.MethodGet, StartPath) {
+ if !c.guard(w, r, http.MethodGet, c.cfg.StartPath) {
return
}
q, e := url.ParseQuery(r.URL.RawQuery)
diff --git a/handoff/start_path_test.go b/handoff/start_path_test.go
new file mode 100644
index 0000000..59de1d2
--- /dev/null
+++ b/handoff/start_path_test.go
@@ -0,0 +1,193 @@
+package handoff
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "strings"
+ "testing"
+ "time"
+
+ "amadan.net/rastrillo/rastrillo/assertion"
+)
+
+const testFreshStart = "/auth/fresh/start"
+
+func pathConsumer(t *testing.T, f fixture, start string, freshAuth bool, done func(http.ResponseWriter, *http.Request, assertion.Claims) error) *Consumer {
+ t.Helper()
+ c, e := NewConsumer(ConsumerConfig{Store: f.s, ConsumerID: "directory", StartPath: start, RequireFreshAuth: freshAuth, Now: func() time.Time { return f.now }, Complete: done})
+ if e != nil {
+ t.Fatal(e)
+ }
+ return c
+}
+
+func TestStartPathDefaultsToPackageConstant(t *testing.T) {
+ f := fresh(t)
+ done := func(http.ResponseWriter, *http.Request, assertion.Claims) error { return nil }
+ for _, configured := range []string{"", StartPath} {
+ if c := pathConsumer(t, f, configured, false, done); c.StartPath() != StartPath || c.cfg.StartPath != StartPath {
+ t.Fatalf("%q: start path %q", configured, c.StartPath())
+ }
+ }
+ if c := pathConsumer(t, f, testFreshStart, true, done); c.StartPath() != testFreshStart {
+ t.Fatal("custom start path lost")
+ }
+}
+
+func TestStartPathRefusedAtConstruction(t *testing.T) {
+ f := fresh(t)
+ for _, bad := range []string{
+ CallbackPath, FinishPath, "/auth", "/auth/", "/", "/team/fresh/start", "/signin/fresh", "/authorize/fresh",
+ "auth/fresh/start", "/auth/fresh/start/", "/auth//fresh/start", "/auth/./start", "/auth/fresh/../start",
+ "/auth/fresh/start?return_to=%2F", "/auth/fresh/start?", "/auth/fresh/start#x", "/auth/Fresh/start",
+ "/auth/fresh%2Fstart", "/auth/fresh start", "/auth/fresh\x00start", "/auth/fresh\\start", "/auth/fresh.start",
+ "https://app.example.com/auth/fresh/start", "//app.example.com/auth/fresh/start", "/auth/" + strings.Repeat("a", 123),
+ } {
+ for _, freshAuth := range []bool{false, true} {
+ if c, e := NewConsumer(ConsumerConfig{Store: f.s, ConsumerID: "directory", StartPath: bad, RequireFreshAuth: freshAuth, Complete: func(http.ResponseWriter, *http.Request, assertion.Claims) error { return nil }}); c != nil || e != ErrConfig {
+ t.Fatalf("start path %q accepted", bad)
+ }
+ }
+ }
+ if _, e := NewConsumer(ConsumerConfig{Store: f.s, ConsumerID: "directory", StartPath: "/auth/" + strings.Repeat("a", 122), Complete: func(http.ResponseWriter, *http.Request, assertion.Claims) error { return nil }}); e != nil {
+ t.Fatal("128-byte start path refused")
+ }
+}
+
+func TestCustomStartPathIsTheOnlyStart(t *testing.T) {
+ f := fresh(t)
+ done := func(http.ResponseWriter, *http.Request, assertion.Claims) error { return nil }
+ ordinary := pathConsumer(t, f, "", false, done)
+ freshC := pathConsumer(t, f, testFreshStart, true, done)
+ for _, tc := range []struct {
+ c *Consumer
+ path string
+ }{{freshC, StartPath}, {ordinary, testFreshStart}, {freshC, testFreshStart + "/"}, {freshC, "/auth/fresh"}} {
+ rec := httptest.NewRecorder()
+ tc.c.Start(rec, request("GET", tc.path+"?return_to=%2Fteam", ""))
+ if rec.Code != 404 || len(rec.Result().Cookies()) != 0 || rec.Header().Get("Location") != "" || rec.Header().Get("Cache-Control") != "no-store" || count(t, f.db, "oficina_handoff_pending") != 0 {
+ t.Fatalf("%s served on the wrong consumer: %d", tc.path, rec.Code)
+ }
+ }
+ rec := httptest.NewRecorder()
+ freshC.Start(rec, request("POST", testFreshStart, ""))
+ if rec.Code != 405 || rec.Header().Get("Allow") != "GET" {
+ t.Fatal("custom start accepted POST")
+ }
+ // Neither start path is a return target, so a finish never re-enters a start.
+ for _, c := range []*Consumer{ordinary, freshC} {
+ for _, back := range []string{StartPath, testFreshStart} {
+ rec := httptest.NewRecorder()
+ c.Start(rec, request("GET", c.StartPath()+"?"+url.Values{"return_to": {back}}.Encode(), ""))
+ if rec.Code != 400 || len(rec.Result().Cookies()) != 0 {
+ t.Fatalf("return_to %s accepted at %s", back, c.StartPath())
+ }
+ }
+ }
+ rec = httptest.NewRecorder()
+ freshC.Start(rec, request("GET", testFreshStart+"?return_to=%2Fteam%3Fteam%3Dabc", ""))
+ target, e := url.Parse(rec.Header().Get("Location"))
+ if rec.Code != 303 || e != nil || target.Host != "id.example.com" || target.Path != "/authorize" || target.Query().Get("consumer") != "directory" || target.Query().Get("max_age") != "300" {
+ t.Fatal("custom start not served")
+ }
+}
+
+// Two consumers share one Store and ConsumerID as the directory will mount
+// them: ordinary at StartPath, fresh at its own path, one Callback, and either
+// consumer's Finish. The pending row alone decides the freshness class.
+func TestTwoStartPathsOneStoreKeepTheirFreshness(t *testing.T) {
+ setup := func(t *testing.T) (fixture, *Consumer, *Consumer, *int) {
+ f := fresh(t)
+ completed := new(int)
+ done := func(w http.ResponseWriter, r *http.Request, a assertion.Claims) error { *completed++; return nil }
+ return f, pathConsumer(t, f, "", false, done), pathConsumer(t, f, testFreshStart, true, done), completed
+ }
+ begin := func(t *testing.T, f fixture, c *Consumer, back string) (*httptest.ResponseRecorder, string) {
+ t.Helper()
+ rec := httptest.NewRecorder()
+ c.Start(rec, request("GET", c.StartPath()+"?"+url.Values{"return_to": {back}}.Encode(), ""))
+ target, e := url.Parse(rec.Header().Get("Location"))
+ if rec.Code != 303 || e != nil || target.Query().Get("consumer") != "directory" {
+ t.Fatal("start failed", rec.Code)
+ }
+ if got, want := target.Query().Get("max_age"), map[bool]string{true: "300"}[c.cfg.RequireFreshAuth]; got != want || len(target.Query()) != map[bool]int{false: 2, true: 3}[c.cfg.RequireFreshAuth] {
+ t.Fatalf("max_age %q forwarded by start at %s", got, c.StartPath())
+ }
+ return rec, target.Query().Get("nonce")
+ }
+ receive := func(t *testing.T, f fixture, c *Consumer, token string) *httptest.ResponseRecorder {
+ t.Helper()
+ rec := httptest.NewRecorder()
+ c.Callback(rec, request("POST", CallbackPath, url.Values{"assertion": {token}}.Encode()))
+ if rec.Code != 303 {
+ t.Fatal("callback refused", rec.Code)
+ }
+ return rec
+ }
+ finish := func(c *Consumer, secret, handle *http.Cookie) *httptest.ResponseRecorder {
+ r := request("GET", FinishPath, "")
+ r.AddCookie(secret)
+ r.AddCookie(handle)
+ rec := httptest.NewRecorder()
+ c.Finish(rec, r)
+ return rec
+ }
+ for _, tc := range []struct {
+ name string
+ freshStart bool
+ age int64
+ finishOnFresh bool
+ accept bool
+ wantLocation string
+ }{
+ {"fresh recent via ordinary finish", true, 10, false, true, "/team?team=abc"},
+ {"fresh recent via fresh finish", true, 10, true, true, "/team?team=abc"},
+ {"fresh boundary", true, 300, false, true, "/team?team=abc"},
+ {"fresh stale via ordinary finish", true, 301, false, false, ""},
+ {"fresh stale via fresh finish", true, 301, true, false, ""},
+ {"fresh future", true, -1, false, false, ""},
+ {"ordinary stale via ordinary finish", false, 900, false, true, "/team?team=abc"},
+ {"ordinary stale via fresh finish", false, 900, true, true, "/team?team=abc"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ f, ordinary, freshC, completed := setup(t)
+ starter := map[bool]*Consumer{false: ordinary, true: freshC}[tc.freshStart]
+ start, nonce := begin(t, f, starter, "/team?team=abc")
+ callback := receive(t, f, ordinary, f.timedToken(t, nonce, f.now.Unix()-tc.age))
+ rec := finish(map[bool]*Consumer{false: ordinary, true: freshC}[tc.finishOnFresh], matchingCookie(t, start, pendingCookie), matchingCookie(t, callback, inboxCookie))
+ if tc.accept {
+ if rec.Code != 303 || rec.Header().Get("Location") != tc.wantLocation || *completed != 1 {
+ t.Fatalf("refused: %d", rec.Code)
+ }
+ } else if rec.Code != 400 || *completed != 0 || rec.Header().Get("Location") != "" {
+ t.Fatalf("stale proof finished a fresh row: %d", rec.Code)
+ }
+ if count(t, f.db, "oficina_handoff_pending") != 0 || count(t, f.db, "oficina_handoff_inbox") != 0 || count(t, f.db, "oficina_handoff_replay") != 1 {
+ t.Fatal("proof not consumed exactly once")
+ }
+ })
+ }
+ // No cross-talk: a proof minted for one start path's nonce cannot be
+ // paired with the other path's pending row, in either direction, and the
+ // refusal leaves both rows and the proof unconsumed.
+ t.Run("cross pairing", func(t *testing.T) {
+ f, ordinary, freshC, completed := setup(t)
+ freshStart, freshNonce := begin(t, f, freshC, "/team")
+ plainStart, plainNonce := begin(t, f, ordinary, "/documents")
+ for _, pair := range []struct {
+ secret *httptest.ResponseRecorder
+ nonce string
+ }{{freshStart, plainNonce}, {plainStart, freshNonce}} {
+ callback := receive(t, f, ordinary, f.timedToken(t, pair.nonce, f.now.Unix()-900))
+ for _, c := range []*Consumer{ordinary, freshC} {
+ if rec := finish(c, matchingCookie(t, pair.secret, pendingCookie), matchingCookie(t, callback, inboxCookie)); rec.Code != 400 || *completed != 0 {
+ t.Fatal("proof finished another start path's row")
+ }
+ }
+ }
+ if count(t, f.db, "oficina_handoff_pending") != 2 || count(t, f.db, "oficina_handoff_replay") != 0 {
+ t.Fatal("cross pairing consumed state")
+ }
+ })
+}