Credential consumer registry: HOME_CONSUMERS ∪ table, per-response form-action
team-provisioning-amendment.md §7. - consumerRegistry: the union of HOME_CONSUMERS and the consumers table's active rows, one immutable map behind an atomic.Pointer, loaded at boot and swapped whole after each committed registry write. Every reader uses it: /authorize, the assertion handoff, sign-out's consumerOrigin, the dashboard source check (re-run in NewRuntime against the union). - Boot refuses an ID or origin present in both (retired rows included). - The credential origin's standing CSP names only the directory in form-action; the assertion handoff and the preview 'Finish signing out' page add their one consumer per response, through setConsumerPolicy in security.go (the one file TestNoHandlerSetsItsOwnCSP now exempts; TestConsumerPolicyOnlyWidensFormAction holds it to form-action). - Runtime.ServingSchema = Schema + the operator HTTP migration sets; boot and the schema inventory use it. Schema stays the selected-release schema the frozen compatibility, admission and legacy-diagnostic profiles describe. Tests: 17th+ consumer admitted at /authorize, retired refused at /authorize and sign-out, collision refuses boot, concurrent reads during swaps (race), handoff CSP names exactly its consumer.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
12 files changed,
+364
−35
cmd/home/main.go+1 −1internal/home/consumer_registry.go+96 −0internal/home/consumer_registry_test.go+122 −0internal/home/dashboard_config.go+21 −10internal/home/exchange.go+5 −2internal/home/exchange_test.go+29 −2internal/home/noscript_test.go+4 −1internal/home/operator_schema_linux_test.go+16 −0internal/home/runtime.go+32 −1internal/home/schema_inventory.go+2 −2internal/home/security.go+31 −13internal/home/signout.go+5 −3
diff --git a/cmd/home/main.go b/cmd/home/main.go| index ed7948e..5f32a85 100644 |
| --- a/cmd/home/main.go |
| +++ b/cmd/home/main.go |
| @@ -96,7 +96,7 @@ func main() { |
| // Ledgered, once at boot, forward-only. A boot that cannot reconcile |
| // its schema refuses rather than guessing, same as a failed open. |
| - if _, err := migrate.Apply(context.Background(), d, runtime.Schema()); err != nil { |
| + if _, err := migrate.Apply(context.Background(), d, runtime.ServingSchema()); err != nil { |
| logger.Error("migrate", "err", err) |
| os.Exit(1) |
| } |
diff --git a/internal/home/consumer_registry.go b/internal/home/consumer_registry.go| new file mode 100644 |
| index 0000000..f013ec1 |
| --- /dev/null |
| +++ b/internal/home/consumer_registry.go |
| @@ -0,0 +1,96 @@ |
| +package home |
| + |
| +import ( |
| + "context" |
| + "database/sql" |
| + "maps" |
| + "sync" |
| + "sync/atomic" |
| + "time" |
| + |
| + "amadan.net/oficina/home/internal/operator" |
| + "amadan.net/rastrillo/rastrillo/assertion" |
| +) |
| + |
| +// consumerRegistry is the credential origin's effective consumer list |
| +// (team-provisioning-amendment.md §7): HOME_CONSUMERS ∪ the active rows of |
| +// the consumers table, held as one immutable map behind an atomic pointer |
| +// and swapped whole after each committed registry write. Every reader — |
| +// /authorize, the assertion handoff, sign-out, the dashboard check, the |
| +// per-response CSP — reads the union through it. |
| +type consumerRegistry struct { |
| + env map[string]string |
| + current atomic.Pointer[map[string]string] |
| + reload sync.Mutex |
| +} |
| + |
| +// errConsumerCollision refuses boot: an ID or origin in both HOME_CONSUMERS |
| +// and the table. That takes suite sign-in down, deliberately; the runbook is |
| +// to remove the env entry and redeploy. |
| +var errConsumerCollision = errExchangeConfig |
| + |
| +func newConsumerRegistry(env map[string]string) *consumerRegistry { |
| + r := &consumerRegistry{env: maps.Clone(env)} |
| + union := maps.Clone(env) |
| + r.current.Store(&union) |
| + return r |
| +} |
| + |
| +// load reads the table and swaps in the union. Any row (retired included) |
| +// sharing an ID or origin with the env manifest is a collision. |
| +func (r *consumerRegistry) load(ctx context.Context, db *sql.DB, credential string, keys map[string][]byte) error { |
| + r.reload.Lock() |
| + defer r.reload.Unlock() |
| + ctx, cancel := context.WithTimeout(ctx, 5*time.Second) |
| + defer cancel() |
| + envOrigins := map[string]bool{} |
| + for _, origin := range r.env { |
| + envOrigins[origin] = true |
| + } |
| + rows, err := db.QueryContext(ctx, "SELECT consumer_id,origin FROM consumers LIMIT 4097") |
| + if err != nil { |
| + return errExchangeConfig |
| + } |
| + defer rows.Close() |
| + for rows.Next() { |
| + var id, origin string |
| + if rows.Scan(&id, &origin) != nil { |
| + return errExchangeConfig |
| + } |
| + if _, clash := r.env[id]; clash || envOrigins[origin] { |
| + return errConsumerCollision |
| + } |
| + } |
| + if rows.Err() != nil { |
| + return errExchangeConfig |
| + } |
| + active, err := operator.ActiveConsumers(ctx, db) |
| + if err != nil { |
| + return errExchangeConfig |
| + } |
| + union := maps.Clone(r.env) |
| + for id, origin := range active { |
| + if !consumerName(id) { |
| + return errExchangeConfig |
| + } |
| + if _, err := assertion.NewVerifier(credential, origin, keys); err != nil { |
| + return errExchangeConfig |
| + } |
| + union[id] = origin |
| + } |
| + r.current.Store(&union) |
| + return nil |
| +} |
| + |
| +func (r *consumerRegistry) snapshot() map[string]string { return *r.current.Load() } |
| + |
| +// consumers is the one reader every credential-origin path uses. |
| +func (a *App) consumers() map[string]string { |
| + if a.runtime == nil { |
| + return nil |
| + } |
| + if a.runtime.registry != nil { |
| + return a.runtime.registry.snapshot() |
| + } |
| + return a.runtime.consumers |
| +} |
diff --git a/internal/home/consumer_registry_test.go b/internal/home/consumer_registry_test.go| new file mode 100644 |
| index 0000000..5daa4d4 |
| --- /dev/null |
| +++ b/internal/home/consumer_registry_test.go |
| @@ -0,0 +1,122 @@ |
| +package home |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "crypto/sha256" |
| + "encoding/base64" |
| + "encoding/hex" |
| + "path/filepath" |
| + "strings" |
| + "sync" |
| + "testing" |
| + |
| + "amadan.net/rastrillo/rastrillo/assertion" |
| + family "amadan.net/rastrillo/rastrillo/crypto" |
| + "amadan.net/rastrillo/rastrillo/db" |
| + "amadan.net/rastrillo/rastrillo/migrate" |
| +) |
| + |
| +func fixtureToken(b byte) string { |
| + return base64.RawURLEncoding.EncodeToString(bytes.Repeat([]byte{b}, 32)) |
| +} |
| +func hexID(k *family.Keypair) string { |
| + sum := sha256.Sum256(k.SignPub()) |
| + return hex.EncodeToString(sum[:]) |
| +} |
| + |
| +// newRegistryCredential boots a credential role through NewRuntime on a |
| +// serving-schema database, with rows written to the consumers table first. |
| +func newRegistryCredential(t *testing.T, rows ...[3]string) (*App, Runtime, *db.DB, error) { |
| + t.Helper() |
| + RegisterSecureDelete() |
| + d, err := db.Open(filepath.Join(t.TempDir(), "credential.db"), quietLogger()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { d.Close() }) |
| + if _, err = migrate.Apply(context.Background(), d, (Runtime{role: "credential"}).ServingSchema()); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, row := range rows { |
| + if _, err = d.Writer().Exec("INSERT INTO consumers VALUES(?,?,'docs',?,'2026-10-27',?)", row[0], row[1], row[2], fixtureToken(1)); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + secrets, _ := testSecrets(t) |
| + key, _ := family.Generate() |
| + kid := hexID(key) |
| + cfg := Runtime{role: "credential", origins: Origins{Origin, directoryOrigin}, secrets: secrets, signer: key, keyID: kid, keys: map[string][]byte{kid: key.SignPub()}, consumers: map[string]string{"directory": directoryOrigin, "team_docs": "https://blue.docs.oficina.works"}} |
| + a, err := NewRuntime(cfg, d, quietLogger()) |
| + return a, cfg, d, err |
| +} |
| + |
| +func TestConsumerRegistryUnionPastSixteen(t *testing.T) { |
| + var rows [][3]string |
| + for i := 0; i < 20; i++ { |
| + id := "t" + strings.Repeat("x", i%3) + string(rune('a'+i)) |
| + rows = append(rows, [3]string{id, "https://" + id + ".docs.oficina.works", "active"}) |
| + } |
| + rows = append(rows, [3]string{"gone", "https://gone.docs.oficina.works", "retired"}) |
| + a, _, d, err := newRegistryCredential(t, rows...) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if n := len(a.consumers()); n != 22 { |
| + t.Fatal("union size", n) |
| + } |
| + nonce, _ := assertion.RandomID() |
| + // The twentieth table consumer, past HOME_CONSUMERS' 16-entry cap. |
| + if r := exchangeRequest(a, "GET", Origin+"/authorize?consumer=txt&nonce="+nonce, "", nil); r.Code != 303 { |
| + t.Fatal("table consumer past sixteen refused", r.Code) |
| + } |
| + // Retired: refused at /authorize and at sign-out. |
| + if r := exchangeRequest(a, "GET", Origin+"/authorize?consumer=gone&nonce="+nonce, "", nil); r.Code != 400 { |
| + t.Fatal("retired consumer admitted at /authorize", r.Code) |
| + } |
| + if r := exchangeRequest(a, "POST", Origin+signoutPath, "https://gone.docs.oficina.works", nil); r.Code != 403 { |
| + t.Fatal("retired consumer admitted at sign-out", r.Code) |
| + } |
| + if r := exchangeRequest(a, "POST", Origin+signoutPath, "https://txt.docs.oficina.works", nil); r.Code != 303 { |
| + t.Fatal("table consumer refused at sign-out", r.Code) |
| + } |
| + // The standing CSP names no consumer, however many there are. |
| + if strings.Contains(a.SecurityPolicy(), "docs.oficina.works") { |
| + t.Fatal("standing CSP lists consumers") |
| + } |
| + // Concurrent reads while the union is swapped (make test-race). |
| + var wg sync.WaitGroup |
| + stop := make(chan struct{}) |
| + for i := 0; i < 4; i++ { |
| + wg.Add(1) |
| + go func() { |
| + defer wg.Done() |
| + for { |
| + select { |
| + case <-stop: |
| + return |
| + default: |
| + _ = a.consumers()["txt"] |
| + _, _ = a.consumerOrigin("https://txt.docs.oficina.works") |
| + } |
| + } |
| + }() |
| + } |
| + for i := 0; i < 20; i++ { |
| + if err := a.runtime.registry.load(context.Background(), d.Writer(), Origin, a.runtime.keys); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + close(stop) |
| + wg.Wait() |
| +} |
| + |
| +// An ID or origin in both HOME_CONSUMERS and the table — retired rows |
| +// included — refuses boot. |
| +func TestConsumerRegistryCollisionRefusesBoot(t *testing.T) { |
| + for _, row := range [][3]string{{"team_docs", "https://other.docs.oficina.works", "active"}, {"other", "https://blue.docs.oficina.works", "retired"}} { |
| + if _, _, _, err := newRegistryCredential(t, row); err != errExchangeConfig { |
| + t.Fatal("collision booted", row, err) |
| + } |
| + } |
| +} |
diff --git a/internal/home/dashboard_config.go b/internal/home/dashboard_config.go| index 4b8a220..24b7826 100644 |
| --- a/internal/home/dashboard_config.go |
| +++ b/internal/home/dashboard_config.go |
| @@ -67,16 +67,8 @@ func loadDashboardConfig(cfg *Runtime) error { |
| } |
| origins[s.Origin] = t.ID |
| apps[s.App] = true |
| - if cfg.role == "credential" { |
| - found := false |
| - for _, v := range cfg.consumers { |
| - if v == s.Origin { |
| - found = true |
| - } |
| - } |
| - if !found { |
| - return errExchangeConfig |
| - } |
| + if cfg.role == "credential" && !registeredOrigin(cfg.consumerUnion(), s.Origin) { |
| + return errExchangeConfig |
| } |
| } |
| launchers := map[string]string{} |
| @@ -107,3 +99,22 @@ func (a *App) dashboardTeam(id string) (dashboardTeam, bool) { |
| } |
| return dashboardTeam{}, false |
| } |
| + |
| +// registeredOrigin reports whether origin is a registered consumer's. |
| +func registeredOrigin(consumers map[string]string, origin string) bool { |
| + for _, v := range consumers { |
| + if v == origin { |
| + return true |
| + } |
| + } |
| + return false |
| +} |
| + |
| +// consumerUnion is the effective registry once it is loaded (NewRuntime |
| +// re-checks the dashboard against it), and HOME_CONSUMERS before that. |
| +func (cfg *Runtime) consumerUnion() map[string]string { |
| + if cfg.registry != nil { |
| + return cfg.registry.snapshot() |
| + } |
| + return cfg.consumers |
| +} |
diff --git a/internal/home/exchange.go b/internal/home/exchange.go| index 32b23a3..26e604e 100644 |
| --- a/internal/home/exchange.go |
| +++ b/internal/home/exchange.go |
| @@ -38,7 +38,7 @@ func (a *App) handleAuthorize(w http.ResponseWriter, r *http.Request) { |
| if maxAge != 0 { |
| wantFields++ |
| } |
| - if err != nil || len(q) != wantFields || len(q["consumer"]) != 1 || len(q["nonce"]) != 1 || !handoff.ValidNonce(q.Get("nonce")) || a.runtime.consumers[q.Get("consumer")] == "" { |
| + if err != nil || len(q) != wantFields || len(q["consumer"]) != 1 || len(q["nonce"]) != 1 || !handoff.ValidNonce(q.Get("nonce")) || a.consumers()[q.Get("consumer")] == "" { |
| a.failAssertion(w, r, authorizeRequest) |
| return |
| } |
| @@ -75,7 +75,7 @@ func (a *App) handleAssertion(w http.ResponseWriter, r *http.Request) { |
| a.failAssertion(w, r, assertionIntentInvalid) |
| return |
| } |
| - target, ok := a.runtime.consumers[intent.Consumer] |
| + target, ok := a.consumers()[intent.Consumer] |
| if !ok { |
| a.failAssertion(w, r, assertionConsumer) |
| return |
| @@ -108,6 +108,9 @@ func (a *App) handleAssertion(w http.ResponseWriter, r *http.Request) { |
| return |
| } |
| http.SetCookie(w, &http.Cookie{Name: assertionIntentCookie, Value: "", Path: "/", MaxAge: -1, Secure: true, HttpOnly: true, SameSite: http.SameSiteLaxMode}) |
| + // This page's form posts to one consumer; its form-action names that one |
| + // origin and no other (team-provisioning-amendment.md §7). |
| + a.setConsumerPolicy(w, target) |
| a.render(w, r, http.StatusOK, "assertion.html", map[string]any{"Action": target + handoff.CallbackPath, "Token": token}) |
| } |
diff --git a/internal/home/exchange_test.go b/internal/home/exchange_test.go| index 46119f8..7569860 100644 |
| --- a/internal/home/exchange_test.go |
| +++ b/internal/home/exchange_test.go |
| @@ -268,12 +268,39 @@ func TestHomeRoleAndMutationIsolation(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// The credential origin's standing policy names no consumer: the one page |
| +// whose form goes to a consumer names that one, per response |
| +// (team-provisioning-amendment.md §7), so the list can outgrow a header. |
| func TestHomePolicyMatchesRegisteredConsumers(t *testing.T) { |
| p := newExchangePair(t) |
| baseline := (&App{origins: p.credential.origins}).SecurityPolicy() |
| + if p.credential.SecurityPolicy() != baseline || strings.Contains(baseline, "blue.docs") { |
| + t.Fatal("standing CSP names consumers") |
| + } |
| + session := signInFully(t, p.credential, sealtest.CanaryAddress) |
| + nonce, _ := assertion.RandomID() |
| + authorize := exchangeRequest(p.credential.App, "GET", Origin+"/authorize?consumer=team_docs&nonce="+nonce, "", nil) |
| + rec := exchangeRequest(p.credential.App, "GET", Origin+"/authorize/resume", "", nil, namedCookie(t, authorize, assertionIntentCookie), session) |
| want := strings.Replace(baseline, "form-action 'self' "+directoryOrigin, "form-action 'self' https://blue.docs.oficina.works "+directoryOrigin, 1) |
| - if p.credential.SecurityPolicy() != want { |
| - t.Fatal("CSP differs outside exact manifest") |
| + if rec.Code != 200 || rec.Header().Get("Content-Security-Policy") != want { |
| + t.Fatalf("handoff CSP %d %q, want %q", rec.Code, rec.Header().Get("Content-Security-Policy"), want) |
| + } |
| +} |
| + |
| +func TestConsumerPolicyOnlyWidensFormAction(t *testing.T) { |
| + p := newExchangePair(t) |
| + base := p.credential.SecurityPolicy() |
| + rec := httptest.NewRecorder() |
| + p.credential.setConsumerPolicy(rec, "https://blue.docs.oficina.works") |
| + got := rec.Header().Get("Content-Security-Policy") |
| + if got != strings.Replace(base, "form-action 'self' "+directoryOrigin, "form-action 'self' https://blue.docs.oficina.works "+directoryOrigin, 1) || got == base { |
| + t.Fatalf("consumer policy changed more than form-action: %q", got) |
| + } |
| + rec = httptest.NewRecorder() |
| + p.credential.setConsumerPolicy(rec, "https://evil.oficina.works") |
| + if rec.Header().Get("Content-Security-Policy") != "" { |
| + t.Fatal("unregistered origin reached form-action") |
| } |
| } |
diff --git a/internal/home/noscript_test.go b/internal/home/noscript_test.go| index 932ec12..98795a8 100644 |
| --- a/internal/home/noscript_test.go |
| +++ b/internal/home/noscript_test.go |
| @@ -476,7 +476,10 @@ func TestNoHandlerSetsItsOwnCSP(t *testing.T) { |
| } |
| var checked int |
| for _, f := range files { |
| - if strings.HasSuffix(f, "_test.go") { |
| + // security.go's setConsumerPolicy is the one exception: it may only |
| + // add one registered consumer to form-action, and |
| + // TestConsumerPolicyOnlyWidensFormAction holds it to that. |
| + if strings.HasSuffix(f, "_test.go") || f == "security.go" { |
| continue |
| } |
| checked++ |
diff --git a/internal/home/operator_schema_linux_test.go b/internal/home/operator_schema_linux_test.go| index a9a39a7..80f261d 100644 |
| --- a/internal/home/operator_schema_linux_test.go |
| +++ b/internal/home/operator_schema_linux_test.go |
| @@ -95,6 +95,22 @@ func TestRuntimeOperatorSchemasAreAdditiveAndRoleSpecific(t *testing.T) { |
| if tables["credential_operator_results"] != (role == "credential") || tables["directory_operator_results"] != (role == "directory") || tables["people"] == (role == "directory") || tables["teams"] != (role == "directory") { |
| t.Fatal("role schema crossed boundary") |
| } |
| + // The operator endpoint's tables stay out of the selected-release |
| + // schema and on their own role in the serving one. |
| + if tables["consumers"] || tables["operator_plans"] || tables["operator_http_teams"] { |
| + t.Fatal("HTTP operator tables entered the frozen role schema") |
| + } |
| + serving, err := migrate.Replay(t.Context(), cfg.ServingSchema().All()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer serving.Close() |
| + for name, want := range map[string]bool{"consumers": role == "credential", "operator_http_teams": role == "directory", "operator_plans": role != ""} { |
| + var n int |
| + if serving.QueryRow("SELECT count(*) FROM sqlite_schema WHERE type='table' AND name=?", name).Scan(&n) != nil || (n == 1) != want { |
| + t.Fatal("serving schema table", name, "for role", role) |
| + } |
| + } |
| }) |
| } |
| } |
diff --git a/internal/home/runtime.go b/internal/home/runtime.go| index ecd6f39..842cf83 100644 |
| --- a/internal/home/runtime.go |
| +++ b/internal/home/runtime.go |
| @@ -1,6 +1,7 @@ |
| package home |
| import ( |
| + "context" |
| "crypto/elliptic" |
| "crypto/sha256" |
| "encoding/base64" |
| @@ -34,7 +35,11 @@ type Runtime struct { |
| signer *assertioncrypto.Keypair |
| keyID string |
| keys map[string][]byte |
| - consumers map[string]string |
| + // consumers is HOME_CONSUMERS as configured; registry, once NewRuntime |
| + // has loaded the credential consumers table, is the union every reader |
| + // uses (consumer_registry.go). |
| + consumers map[string]string |
| + registry *consumerRegistry |
| } |
| // LoadRuntime keeps existing deployments on the legacy path until a role is |
| @@ -174,6 +179,22 @@ func (cfg Runtime) Schema() *migrate.Set { |
| } |
| return BootSchema |
| } |
| + |
| +// ServingSchema is what boot applies: Schema plus the operator endpoint's |
| +// additive tables (team-provisioning-amendment.md §7). Schema stays the |
| +// role's selected-release schema, which the frozen compatibility, admission |
| +// and legacy-diagnostic profiles describe; a database carrying the HTTP |
| +// tables is refused by the native command's exact ledger check, as any |
| +// additive foreign ledger is, until those profiles are reviewed afresh. |
| +func (cfg Runtime) ServingSchema() *migrate.Set { |
| + switch cfg.role { |
| + case "directory": |
| + return migrate.Merge(cfg.Schema(), operator.DirectoryHTTPSchema) |
| + case "credential": |
| + return migrate.Merge(cfg.Schema(), operator.CredentialHTTPSchema) |
| + } |
| + return cfg.Schema() |
| +} |
| func NewRuntime(cfg Runtime, d *db.DB, logger *slog.Logger) (*App, error) { |
| if !cfg.roster.valid(cfg.role, cfg.keys) || !cfg.validAppPreviews() { |
| return nil, errExchangeConfig |
| @@ -192,6 +213,16 @@ func NewRuntime(cfg Runtime, d *db.DB, logger *slog.Logger) (*App, error) { |
| return nil, errExchangeConfig |
| } |
| a.runtime = &cfg |
| + if cfg.role == "credential" { |
| + // HOME_CONSUMERS ∪ the consumers table; a collision refuses boot. |
| + a.runtime.registry = newConsumerRegistry(cfg.consumers) |
| + if err := a.runtime.registry.load(context.Background(), d.Writer(), cfg.origins.Credential, cfg.keys); err != nil { |
| + return nil, err |
| + } |
| + if err := loadDashboardConfig(a.runtime); err != nil { |
| + return nil, err |
| + } |
| + } |
| return a, nil |
| } |
diff --git a/internal/home/schema_inventory.go b/internal/home/schema_inventory.go| index 8d0dada..cc7b69b 100644 |
| --- a/internal/home/schema_inventory.go |
| +++ b/internal/home/schema_inventory.go |
| @@ -21,11 +21,11 @@ func LogSchemaInventory(parent context.Context, d *db.DB, cfg Runtime, logger *s |
| defer logLegacySchemaDiagnostic(ctx, d.Writer(), cfg.role, logger) |
| expected := map[string]bool{} |
| known := map[string]bool{} |
| - for _, m := range cfg.Schema().All() { |
| + for _, m := range cfg.ServingSchema().All() { |
| expected[m.ID] = true |
| } |
| for _, role := range []string{"credential", "directory"} { |
| - for _, m := range (Runtime{role: role}).Schema().All() { |
| + for _, m := range (Runtime{role: role}).ServingSchema().All() { |
| known[m.ID] = true |
| } |
| } |
diff --git a/internal/home/security.go b/internal/home/security.go| index 9165dce..20eec76 100644 |
| --- a/internal/home/security.go |
| +++ b/internal/home/security.go |
| @@ -1,24 +1,42 @@ |
| package home |
| import ( |
| + "net/http" |
| "sort" |
| "strings" |
| ) |
| -// SecurityPolicy preserves framework defaults and names only registered form |
| -// destinations. The credential origin renders no account-authored markup. |
| -func (a *App) SecurityPolicy() string { |
| +// SecurityPolicy is the policy every response starts with. It preserves |
| +// framework defaults and names, in form-action, the directory and nothing |
| +// else on the credential origin; the two pages whose form goes to a consumer |
| +// (the assertion handoff and the "Finish signing out" page for a preview) |
| +// name that one consumer per response, through securityPolicy. That is |
| +// what lets the consumer list outgrow the header (team-provisioning- |
| +// amendment.md §7). The credential origin renders no account-authored |
| +// markup. |
| +func (a *App) SecurityPolicy() string { return a.securityPolicy("") } |
| + |
| +// setConsumerPolicy is the one place a handler replaces the policy, and it |
| +// can only add one registered consumer to form-action: the rest is |
| +// SecurityPolicy's, byte for byte (TestConsumerPolicyOnlyWidensFormAction). |
| +// The assertion handoff and the preview "Finish signing out" page call it. |
| +func (a *App) setConsumerPolicy(w http.ResponseWriter, consumer string) { |
| + if !registeredOrigin(a.consumers(), consumer) { |
| + return |
| + } |
| + w.Header().Set("Content-Security-Policy", a.securityPolicy(consumer)) |
| +} |
| + |
| +func (a *App) securityPolicy(consumer string) string { |
| origins := map[string]bool{a.origins.Directory: true} |
| - if a.runtime != nil { |
| - for _, origin := range a.runtime.consumers { |
| - origins[origin] = true |
| - } |
| - // form-action governs a form's redirects too, so the directory's |
| - // sign-out, which 307s its POST on to the credential origin, is |
| - // blocked in the browser without this. See signoutPath. |
| - if a.runtime.role == "directory" { |
| - origins[a.origins.Credential] = true |
| - } |
| + if consumer != "" { |
| + origins[consumer] = true |
| + } |
| + // form-action governs a form's redirects too, so the directory's |
| + // sign-out, which 307s its POST on to the credential origin, is |
| + // blocked in the browser without this. See signoutPath. |
| + if a.runtime != nil && a.runtime.role == "directory" { |
| + origins[a.origins.Credential] = true |
| } |
| list := make([]string, 0, len(origins)) |
| for origin := range origins { |
diff --git a/internal/home/signout.go b/internal/home/signout.go| index c7da8e2..5a884fa 100644 |
| --- a/internal/home/signout.go |
| +++ b/internal/home/signout.go |
| @@ -57,6 +57,7 @@ func (a *App) handleCredentialSignout(w http.ResponseWriter, r *http.Request) { |
| a.render(w, r, http.StatusForbidden, "signout_confirm.html", nil) |
| return |
| case signoutConfirm: |
| + a.setConsumerPolicy(w, back) |
| // Admitted, but the id.* cookie did not ride this hop, so there is |
| // no row to revoke yet. Clearing the cookie now would leave the |
| // row alive behind it; keep it, and let the same-origin button |
| @@ -164,13 +165,14 @@ func (a *App) signoutReturn(r *http.Request) (string, signoutVerdict) { |
| } |
| // consumerOrigin returns the registered consumer origin exactly equal to |
| -// origin. Registrations are validated at boot (assertion.NewVerifier), so |
| -// an exact string match is a match on scheme, host and port. |
| +// origin. Registrations are validated at boot and on every registry reload |
| +// (assertion.NewVerifier), so an exact string match is a match on scheme, |
| +// host and port. A retired table consumer is not in the union. |
| func (a *App) consumerOrigin(origin string) (string, bool) { |
| if a.runtime == nil { |
| return "", false |
| } |
| - for _, registered := range a.runtime.consumers { |
| + for _, registered := range a.consumers() { |
| if registered == origin { |
| return registered, true |
| } |