hub v0.135.0: CSRF on the Basic-auth path (R-135), console passwords sealed at rest (R-133), boxes left behind listed and alarmed (R-604, R-530), no-e-mail banner (R-508)
gates / gates (push) Successful in 29s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
This commit is contained in:
adminandClaude Opus 5.5 committed 2026-10-05 11:12:56 +02:00
1 parent 9bb45eaaa2
commit 3d7a2761fc
24 files changed
+1148 -17

No files matched your search

+59 -4
View File
@@ -3,6 +3,8 @@ package store
import (
"database/sql"
"encoding/json"
"fmt"
"strings"
"time"
)
@@ -16,17 +18,24 @@ type HostRecoveryCredential struct {
}
// SaveHostRecoveryCredential upserts a host's break-glass credential (last-write-wins: day-0 sets it,
// --rotate re-sets). The secret is stored as-is at rest; the hub NEVER logs it and only ever returns
// it over the operator-authenticated retrieval path.
// --rotate re-sets). R-133 (hub v0.135.0): the secret is SEALED at rest with the same key and the same
// helpers as the off-site sub-account passwords (offsite_seal.go, OFFSITE_SECRET_KEY) — a copy of hub.db
// alone no longer holds any box's console password. No key → the save is REFUSED (ErrNoSealKey): a hub that
// cannot seal must not fall back to plaintext. The hub NEVER logs the secret and only ever returns it over
// the operator-authenticated retrieval paths. Pinned by r133_recovery_seal_test.go.
func (s *Store) SaveHostRecoveryCredential(hostID, username, secret string) error {
_, err := s.db.Exec(`
sealed, err := s.sealSecret(secret)
if err != nil {
return err
}
_, err = s.db.Exec(`
INSERT INTO host_recovery (host_id, username, secret, set_at, updated_at)
VALUES (?, ?, ?, datetime('now'), datetime('now'))
ON CONFLICT(host_id) DO UPDATE SET
username = excluded.username,
secret = excluded.secret,
updated_at = datetime('now')`,
hostID, username, secret)
hostID, username, sealed)
return err
}
@@ -43,6 +52,13 @@ func (s *Store) GetHostRecoveryCredential(hostID string) (*HostRecoveryCredentia
if err != nil {
return nil, err
}
// R-133: open the sealed value. A wrong or missing key, or a row still in plaintext (the start-up
// sealing has not run), is an ERROR — never a fallback that hands out what the column holds.
plain, err := s.openSecret(c.Secret)
if err != nil {
return nil, fmt.Errorf("host_recovery %s: %w", hostID, err)
}
c.Secret = plain
c.SetAt = parseSQLiteTime(setAt)
return &c, nil
}
@@ -136,3 +152,42 @@ func (s *Store) GetHostMgmtPlaneStates() ([]HostMgmtPlaneRow, error) {
}
return out, rows.Err()
}
// SealLegacyRecoverySecrets seals, in place, every host_recovery row still holding a plaintext console
// password (written before hub v0.135.0, R-133). Idempotent; returns how many rows it sealed. Values are
// never logged. Called at start-up right after the key is installed (cmd/hub/main.go), beside
// SealLegacyOffsiteSecrets.
func (s *Store) SealLegacyRecoverySecrets() (int, error) {
if s.sealer == nil {
return 0, ErrNoSealKey
}
rows, err := s.db.Query(`SELECT host_id, secret FROM host_recovery`)
if err != nil {
return 0, err
}
type row struct{ id, v string }
var todo []row
for rows.Next() {
var r row
if err := rows.Scan(&r.id, &r.v); err != nil {
rows.Close()
return 0, err
}
if !strings.HasPrefix(r.v, sealPrefix) {
todo = append(todo, r)
}
}
rows.Close()
n := 0
for _, r := range todo {
sealed, err := s.sealSecret(r.v)
if err != nil {
return n, err
}
if _, err := s.db.Exec(`UPDATE host_recovery SET secret = ? WHERE host_id = ? AND secret = ?`, sealed, r.id, r.v); err != nil {
return n, err
}
n++
}
return n, nil
}
@@ -0,0 +1,96 @@
package store
import (
"strings"
"testing"
)
// R-133 (hub v0.135.0): the break-glass console password is sealed at rest with the off-site seal
// (offsite_seal.go). RED-PROOF (audits/hub-safety-2026-10-05/partB/red-proof.txt): make
// SaveHostRecoveryCredential store `secret` instead of `sealed` → TestR133_RawRowHoldsNoPassword fails.
func rawRecovery(t *testing.T, st *Store, hostID string) string {
t.Helper()
var v string
if err := st.db.QueryRow(`SELECT secret FROM host_recovery WHERE host_id = ?`, hostID).Scan(&v); err != nil {
t.Fatal(err)
}
return v
}
// A copy of hub.db alone holds no console password: asserted on the raw column, and the value still opens.
func TestR133_RawRowHoldsNoPassword(t *testing.T) {
st := sealTestStore(t)
if err := st.SaveHostRecoveryCredential("h1", "root@pam", "Console-Pw-7741"); err != nil {
t.Fatal(err)
}
raw := rawRecovery(t, st, "h1")
if strings.Contains(raw, "Console-Pw-7741") || !strings.HasPrefix(raw, sealPrefix) {
t.Fatalf("raw host_recovery.secret is not sealed: %q", raw)
}
c, err := st.GetHostRecoveryCredential("h1")
if err != nil || c == nil || c.Secret != "Console-Pw-7741" || c.Username != "root@pam" {
t.Fatalf("GetHostRecoveryCredential = %+v, %v", c, err)
}
// The page path never carried the secret, and still does not.
m, err := st.GetHostRecoveryMeta("h1")
if err != nil || m == nil || m.Username != "root@pam" {
t.Fatalf("meta = %+v, %v", m, err)
}
}
// The migration: a row written in plaintext before v0.135.0 is sealed in place, once; the value survives.
func TestR133_SealLegacyRecoverySecrets(t *testing.T) {
st := sealTestStore(t)
if _, err := st.db.Exec(`INSERT INTO host_recovery (host_id, username, secret) VALUES ('old', 'root@pam', 'Legacy-Plain-1')`); err != nil {
t.Fatal(err)
}
if err := st.SaveHostRecoveryCredential("new", "root@pam", "Fresh-2"); err != nil {
t.Fatal(err)
}
if _, err := st.GetHostRecoveryCredential("old"); err == nil {
t.Fatal("a plaintext row was handed out before the migration sealed it")
}
n, err := st.SealLegacyRecoverySecrets()
if err != nil || n != 1 {
t.Fatalf("SealLegacyRecoverySecrets = %d, %v — want exactly the one plaintext row", n, err)
}
if raw := rawRecovery(t, st, "old"); strings.Contains(raw, "Legacy-Plain-1") || !strings.HasPrefix(raw, sealPrefix) {
t.Fatalf("legacy row not sealed: %q", raw)
}
if c, err := st.GetHostRecoveryCredential("old"); err != nil || c.Secret != "Legacy-Plain-1" {
t.Fatalf("after migration = %+v, %v", c, err)
}
if n, err := st.SealLegacyRecoverySecrets(); err != nil || n != 0 {
t.Fatalf("second run sealed %d (%v) — want 0 (idempotent)", n, err)
}
}
// A wrong key fails CLOSED: an error, never the column's bytes.
func TestR133_WrongKeyFailsClosed(t *testing.T) {
st := sealTestStore(t)
if err := st.SaveHostRecoveryCredential("h1", "root@pam", "Console-Pw-7741"); err != nil {
t.Fatal(err)
}
if err := st.SetOffsiteSecretKey([]byte("another-key-of-exactly-32-bytes!")); err != nil {
t.Fatal(err)
}
c, err := st.GetHostRecoveryCredential("h1")
if err == nil || c != nil {
t.Fatalf("a wrong key returned %+v (err %v) — want an error and nothing", c, err)
}
}
// No key → the save is refused; nothing is written in the clear.
func TestR133_NoKeyRefusesToSave(t *testing.T) {
st := sealTestStore(t)
st.sealer = nil
if err := st.SaveHostRecoveryCredential("h1", "root@pam", "Console-Pw-7741"); err != ErrNoSealKey {
t.Fatalf("save without a key = %v, want ErrNoSealKey", err)
}
var n int
_ = st.db.QueryRow(`SELECT COUNT(*) FROM host_recovery`).Scan(&n)
if n != 0 {
t.Fatalf("%d row(s) written without a key", n)
}
}
+57 -2
View File
@@ -184,6 +184,8 @@ func (s *Store) migrate() error {
// per-customer override → the effective floor falls back to the global default (hub_settings /
// config). Idempotent.
s.db.Exec("ALTER TABLE customer_configs ADD COLUMN min_controller_version TEXT NOT NULL DEFAULT ''")
// R-604 (v0.135.0): when the per-customer floor override was set (its age, on the System page).
s.db.Exec("ALTER TABLE customer_configs ADD COLUMN min_controller_set_at TEXT NOT NULL DEFAULT ''")
// v0.112.0 (R-472): the MinAgent DECLARED with a per-customer floor, stored as "FLOOR=MINAGENT" so
// it only ever applies to the exact floor it was declared for (see floor_declared.go). Idempotent.
@@ -1986,14 +1988,47 @@ func (s *Store) SetCustomerConfigStatus(customerID, status string) error {
// SetMinControllerVersion sets (or clears, with "") the per-customer controller-version floor
// override. The customer config must already exist.
func (s *Store) SetMinControllerVersion(customerID, version string) error {
// R-604 (v0.135.0): the override's SET TIME travels with it, so the System page can show its age. Cleared
// with the override; an override written before v0.135.0 has none ("age unknown").
_, err := s.db.Exec(`
UPDATE customer_configs SET min_controller_version = ?, updated_at = datetime('now')
UPDATE customer_configs SET min_controller_version = ?,
min_controller_set_at = CASE WHEN ? = '' THEN '' ELSE datetime('now') END,
updated_at = datetime('now')
WHERE customer_id = ?`,
version, customerID,
version, version, customerID,
)
return err
}
// CustomerFloorOverride is one per-customer controller-floor override (R-604).
type CustomerFloorOverride struct {
CustomerID, CustomerName, Version string
SetAt time.Time // zero = set before v0.135.0 (age unknown)
}
// CustomerFloorOverrides lists every customer whose config carries its own controller floor, by customer id.
func (s *Store) CustomerFloorOverrides() ([]CustomerFloorOverride, error) {
rows, err := s.db.Query(`SELECT customer_id, customer_name, min_controller_version, min_controller_set_at
FROM customer_configs WHERE min_controller_version != '' ORDER BY customer_id`)
if err != nil {
return nil, err
}
defer rows.Close()
var out []CustomerFloorOverride
for rows.Next() {
var o CustomerFloorOverride
var at string
if err := rows.Scan(&o.CustomerID, &o.CustomerName, &o.Version, &at); err != nil {
return nil, err
}
if at != "" {
o.SetAt = parseSQLiteTime(at)
}
out = append(out, o)
}
return out, rows.Err()
}
// GetGlobalMinControllerVersion returns the operator-set global floor from hub_settings if present,
// else the config/env-supplied default. Empty string = no global floor.
func (s *Store) GetGlobalMinControllerVersion() string {
@@ -2231,6 +2266,26 @@ func (s *Store) SetBundleBehindSince(hostID string, t time.Time) error {
return s.setSetting("bundle_behind_since:"+hostID, v)
}
// AgentBehindSince returns since when the hub has seen a box run an agent OLDER than the vouched one (zero = it
// is current, or was never seen behind). R-530's 7-day operator alarm counts from here (hub v0.135.0).
func (s *Store) AgentBehindSince(hostID string) time.Time {
v := s.getSetting("agent_behind_since:" + hostID)
if v == "" {
return time.Time{}
}
t, _ := time.Parse(time.RFC3339, v)
return t
}
// SetAgentBehindSince records (or, with a zero time, clears) the first moment a box was seen behind the vouched agent.
func (s *Store) SetAgentBehindSince(hostID string, t time.Time) error {
v := ""
if !t.IsZero() {
v = t.UTC().Format(time.RFC3339)
}
return s.setSetting("agent_behind_since:"+hostID, v)
}
// EffectiveMinControllerVersion resolves the floor that actually applies to a customer: the
// per-customer override when set (non-empty), otherwise the global floor (hub_settings → config/env
// default). Returns "" when no floor applies at all (Phase 2 inert for that customer).