hub: the first sight of a large request is the hub's own record; no box text in the ledger (security review)

The wait of a large removed-app request was timed from a search of the
windows' refusal text, which also held names a box had claimed. A box could
plant the marker in a claimed name a week ahead and skip the wait and the mail.

- offsite_large_requests (household, key of the app set, first_seen): written
  only by the hub; at most 50 rows per household; it goes with the household.
  No text is searched any more.
- A claimed name that has not the shape of an app name is written nowhere: the
  ledger, the answer and the operator's message carry a fixed placeholder.

Tests seen red; 6 mutations seen red (redproof-review-s1.txt).

Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
This commit is contained in:
admin committed 2026-11-15 14:50:55 +01:00
1 parent be272b7cf4
commit 1fac9c16f1
10 files changed
+261 -32

No files matched your search

+4 -1
View File
@@ -53,6 +53,8 @@ type CustomerResidue struct {
OffsiteAbandonRequests int
// D72: the hub's own record of which apps the household's box runs (offsite_ended.go).
OffsiteAppPresence int
// The hub's own first sight of a large ended-app request (offsite_ended.go).
OffsiteLargeRequests int
}
// Total is the single number the "is there anything left at all?" decision hangs on.
@@ -62,7 +64,7 @@ func (r *CustomerResidue) Total() int {
}
return r.Reports + r.AppTelemetry + r.AppLogTails + r.LogTailRequests +
r.NotificationPrefs + r.SelfBindTokens + r.ApplianceRegistrations + r.AppLogIssueRefs +
r.OffsiteKeys + r.OffsiteWindows + r.OffsiteAbandonRequests + r.OffsiteAppPresence
r.OffsiteKeys + r.OffsiteWindows + r.OffsiteAbandonRequests + r.OffsiteAppPresence + r.OffsiteLargeRequests
}
// residueQueries is the ONE list both the counter and the purge walk, so a table can never be
@@ -82,6 +84,7 @@ var residueQueries = []struct {
{"offsite_windows", func(r *CustomerResidue) *int { return &r.OffsiteWindows }},
{"offsite_abandon_requests", func(r *CustomerResidue) *int { return &r.OffsiteAbandonRequests }},
{"offsite_app_presence", func(r *CustomerResidue) *int { return &r.OffsiteAppPresence }},
{"offsite_large_requests", func(r *CustomerResidue) *int { return &r.OffsiteLargeRequests }},
}
// CustomerResidue counts the customer's report-derived + binding residue. Read-only.
+76 -16
View File
@@ -38,6 +38,15 @@ func (s *Store) migrateOffsiteEnded() error {
s.db.Exec("ALTER TABLE offsite_windows ADD COLUMN ended_removed TEXT")
// 1 on a row that exists ONLY because the box named the app in an end record (the hub never saw it installed).
s.db.Exec("ALTER TABLE offsite_app_presence ADD COLUMN claim_born INTEGER NOT NULL DEFAULT 0")
if _, err := s.db.Exec(`
CREATE TABLE IF NOT EXISTS offsite_large_requests (
customer_id TEXT NOT NULL,
set_key TEXT NOT NULL,
first_seen TEXT NOT NULL,
PRIMARY KEY (customer_id, set_key)
);`); err != nil {
return fmt.Errorf("offsite_large_requests: %w", err)
}
s.db.Exec("ALTER TABLE offsite_windows ADD COLUMN ended_apps TEXT") // agreed app → its agreed ids (the per-app record)
return nil
}
@@ -382,28 +391,79 @@ func jsonStrings(v sql.NullString) []string {
return out
}
// LargeEndedRefusalMark is the text a window's refusal carries when the request was for more than half of the store
// (offsitekeys). FirstLargeEndedRefusal finds it on the window rows — the ledger is the memory, so it goes with the
// household and needs no second store.
const LargeEndedRefusalMark = "more than half of the store"
// THE HUB'S OWN FIRST SIGHT OF A LARGE REQUEST (security review of 2026-11-15). A request to remove more than half of
// a store for removed apps waits a week after the hub first saw it (offsitekeys). That first sight is kept HERE, in a
// table only the hub writes: (household, a key of the app set, first_seen). It was first found by searching the
// windows' refusal text — text that also held names a box had claimed, so a box could plant its own „first sight”.
// No box-supplied text is searched any more.
// FirstLargeEndedRefusal returns when the household's EARLIEST window since `since` refused a request for more than
// half of the store FOR THE SAME APPS (`needle` is the refusal's own text for them; zero time: none). A store that
// cannot answer reads as none — and then the request waits again.
func (s *Store) FirstLargeEndedRefusal(customerID, needle string, since time.Time) time.Time {
// maxLargeRequestRows bounds one household's rows: a box can ask for many app sets.
const maxLargeRequestRows = 50
// LargeEndedRequestFirstSeen returns when the hub first saw this household ask for this app set in a large request,
// if that was after `since` (zero time: never, or too long ago — the request then waits again).
func (s *Store) LargeEndedRequestFirstSeen(customerID, key string, since time.Time) time.Time {
var v sql.NullString
err := s.db.QueryRow(`SELECT MIN(opened_at) FROM offsite_windows WHERE customer_id = ? AND opened_at >= ? AND instr(COALESCE(ended_refused, ''), ?) > 0`,
customerID, since.UTC().Format("2006-01-02 15:04:05"), needle).Scan(&v)
if err != nil || !v.Valid || v.String == "" {
if err := s.db.QueryRow(`SELECT first_seen FROM offsite_large_requests WHERE customer_id = ? AND set_key = ?`, customerID, key).Scan(&v); err != nil || !v.Valid {
return time.Time{}
}
return parseSQLiteTime(v.String)
t, err := time.Parse(time.RFC3339, v.String)
if err != nil || t.Before(since) {
return time.Time{}
}
return t
}
// BackdateOffsiteWindowForTest moves a window's opened_at into the past. TEST-ONLY.
func (s *Store) BackdateOffsiteWindowForTest(id int64, by time.Duration) error {
_, err := s.db.Exec(`UPDATE offsite_windows SET opened_at = ? WHERE id = ?`, time.Now().UTC().Add(-by).Format("2006-01-02 15:04:05"), id)
return err
// NoteLargeEndedRequest records the hub's first sight (now) of this app set — replacing a sight that is older than
// the caller's memory is the caller's choice: it calls this only when LargeEndedRequestFirstSeen answered „never”.
// A household holds at most maxLargeRequestRows rows; the oldest go first.
func (s *Store) NoteLargeEndedRequest(customerID, key string, now time.Time) error {
if customerID == "" || key == "" {
return fmt.Errorf("NoteLargeEndedRequest: empty household or key")
}
tx, err := s.db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
if _, err := tx.Exec(`INSERT INTO offsite_large_requests (customer_id, set_key, first_seen) VALUES (?, ?, ?)
ON CONFLICT(customer_id, set_key) DO UPDATE SET first_seen = excluded.first_seen`, customerID, key, now.UTC().Format(time.RFC3339)); err != nil {
return err
}
if _, err := tx.Exec(`DELETE FROM offsite_large_requests WHERE customer_id = ? AND set_key NOT IN (
SELECT set_key FROM offsite_large_requests WHERE customer_id = ? ORDER BY first_seen DESC, set_key LIMIT ?)`, customerID, customerID, maxLargeRequestRows); err != nil {
return err
}
return tx.Commit()
}
// BackdateLargeEndedRequestsForTest moves a household's first sights into the past. TEST-ONLY.
func (s *Store) BackdateLargeEndedRequestsForTest(customerID string, by time.Duration) error {
rows, err := s.db.Query(`SELECT set_key, first_seen FROM offsite_large_requests WHERE customer_id = ?`, customerID)
if err != nil {
return err
}
type row struct{ key, at string }
var all []row
for rows.Next() {
var r row
if err := rows.Scan(&r.key, &r.at); err != nil {
rows.Close()
return err
}
all = append(all, r)
}
rows.Close()
for _, r := range all {
t, perr := time.Parse(time.RFC3339, r.at)
if perr != nil {
continue
}
if _, err := s.db.Exec(`UPDATE offsite_large_requests SET first_seen = ? WHERE customer_id = ? AND set_key = ?`, t.Add(-by).Format(time.RFC3339), customerID, r.key); err != nil {
return err
}
}
return nil
}
// CountCustomerSentSince counts the mails of one type the hub's own log says were SENT to a household since `since`
+1
View File
@@ -28,6 +28,7 @@ var orphanTables = []struct{ table, where string }{
{"offsite_windows", `customer_id NOT IN (SELECT customer_id FROM customer_configs) AND customer_id NOT IN (SELECT customer_id FROM hosts)`},
{"offsite_abandon_requests", `customer_id NOT IN (SELECT customer_id FROM customer_configs) AND customer_id NOT IN (SELECT customer_id FROM hosts)`},
{"offsite_app_presence", `customer_id NOT IN (SELECT customer_id FROM customer_configs) AND customer_id NOT IN (SELECT customer_id FROM hosts)`},
{"offsite_large_requests", `customer_id NOT IN (SELECT customer_id FROM customer_configs) AND customer_id NOT IN (SELECT customer_id FROM hosts)`},
}
// PurgeOrphanRows removes, in ONE transaction, the rows of boxes and households that no longer exist from the six