Files
felhom.eu/hub/internal/offsitekeys/ended_large_marker_test.go
T
admin 6d2d4bd09d hub: the large request's wait is keyed on the whole app set, not its shortened display list (security review)
The key was the display string cut after ten names, so two different app sets
with the same first ten names shared one wait and one operator mail.
largeSetKey is a SHA-256 of the full sorted set; the display string is only
shown. Test seen red; 2 mutations seen red (redproof-review-s2.txt).

Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
2026-11-15 14:52:02 +01:00

127 lines
6.5 KiB
Go

package offsitekeys
import (
"context"
"strings"
"testing"
"time"
"gitea.dooplex.hu/admin/felhom-hub/internal/store"
)
// Security review of the review-fix-1 commit (2026-11-15). The hub remembered its first sight of a large request by
// SEARCHING the windows' refusal text — text that also held the box's own claimed names. A box could plant the marker
// in a claimed name a week ahead and so skip the wait, and the operator's mail with it. The first sight now lives in
// the hub's own table, written only by the hub, and a claimed name that is not an app name never reaches the ledger.
// RED-PROOF: documentation/audits/build-release-4-2026-11-15/A/redproof-review-s1.txt.
func TestD84_ABoxCannotPlantTheLargeRequestMarker(t *testing.T) {
s, events, all := ghostFixture(t)
ctx := context.Background()
// A week ahead: a window in which the box claims „apps” whose NAMES are the texts the hub writes for a large
// request — every form of it the box can guess.
planted := []EndedClaim{
{App: "more than half of the store (apps: ghost)", SnapshotIDs: []string{eid(0)}},
{App: "more than half of the store", SnapshotIDs: []string{eid(1)}},
{App: "the request is for 20 of 20 snapshots — more than half of the store (apps: ghost): it waits one week, and the operator is told", SnapshotIDs: []string{eid(2)}},
{App: "large:ghost", SnapshotIDs: []string{eid(3)}},
}
g0 := openGranted(t, s, 20, planted...)
if len(g0.EndedAllowed) != 0 {
t.Fatalf("fixture: the planted names were agreed: %v", shortIDs(g0.EndedAllowed))
}
_ = s.CloseWindowFor(ctx, "c1", WindowResult{WindowID: g0.WindowID, CountBefore: 20, CountAfter: 20, Outcome: "nothing"})
// the box's words are not in the hub's ledger at all
w, _ := s.Store.GetOffsiteWindow(g0.WindowID)
for _, r := range w.EndedRefused {
if strings.Contains(r.App, "more than half") || strings.Contains(r.App, "large:") || strings.Contains(r.App, " ") {
t.Errorf("a claimed name that is not an app name was written into the window's row: %q", r.App)
}
}
aWeekLater(t, s, g0.WindowID)
*events = nil
// Now the real request for the whole store: it must wait and the operator must be mailed, as if nothing was planted.
g := openGranted(t, s, 20, EndedClaim{App: "ghost", SnapshotIDs: all})
if len(g.EndedAllowed) != 0 {
t.Fatalf("the box planted the marker a week ahead and the hub agreed to %d ids at once — no wait, no warning", len(g.EndedAllowed))
}
if n := len(ofType(*events, EventEndedLargeAgreement)); n != 1 {
t.Fatalf("the operator was told %d time(s) about the large request, want once", n)
}
// and the hub's own first sight is what later ends the wait
if at := s.Store.LargeEndedRequestFirstSeen("c1", largeSetKey(map[string][]string{"ghost": nil}), time.Now().Add(-time.Hour)); at.IsZero() {
t.Fatal("the hub did not record its own first sight of the large request")
}
// a first sight older than the caller's memory is no first sight: the request waits again
if at := s.Store.LargeEndedRequestFirstSeen("c1", largeSetKey(map[string][]string{"ghost": nil}), time.Now().Add(time.Hour)); !at.IsZero() {
t.Fatal("a first sight older than `since` was returned")
}
if at := s.Store.LargeEndedRequestFirstSeen("other-household", largeSetKey(map[string][]string{"ghost": nil}), time.Now().Add(-time.Hour)); !at.IsZero() {
t.Fatal("another household reads this household's first sight")
}
}
// The first sight goes with the household, and one household cannot fill the table.
func TestD84_TheLargeRequestRecordIsBoundedAndGoesWithTheHousehold(t *testing.T) {
s, _, _ := ghostFixture(t)
now := time.Now().UTC()
for i := 0; i < 500; i++ {
if err := s.Store.NoteLargeEndedRequest("c1", strings.Repeat("k", 10)+string(rune('a'+i%26))+strings.Repeat("x", i%7)+strings.Repeat("y", i/26), now); err != nil {
t.Fatal(err)
}
}
r, err := s.Store.CustomerResidue("c1")
if err != nil || r.OffsiteLargeRequests == 0 || r.OffsiteLargeRequests > 50 {
t.Fatalf("500 different requests left %d rows (limit 50): %v", r.OffsiteLargeRequests, err)
}
if err := s.Store.PurgeCustomerResidue("c1"); err != nil {
t.Fatal(err)
}
if r2, _ := s.Store.CustomerResidue("c1"); r2.OffsiteLargeRequests != 0 {
t.Fatalf("the record outlived the household: %d", r2.OffsiteLargeRequests)
}
var _ = store.EndedRefusal{}
}
// Security review, second finding: the wait's key was the SHORTENED display list of the apps (ten names „and N
// more”), so two different app sets with the same first ten names shared one wait. The key is the whole sorted set.
// RED-PROOF: documentation/audits/build-release-4-2026-11-15/A/redproof-review-s2.txt.
func TestD84_TwoAppSetsThatDifferAfterTheTenthNameDoNotShareAWait(t *testing.T) {
s, events, all := ghostFixture(t)
now := time.Now().UTC()
var names []string
for i := 0; i < 10; i++ {
names = append(names, "app-0"+string(rune('0'+i)))
}
names = append(names, "zz-first", "zz-second")
if err := s.Store.NoteAppList("c1", []string{"immich"}, names, now.Add(-31*24*time.Hour)); err != nil {
t.Fatal(err)
}
set := func(last string) []EndedClaim {
var cl []EndedClaim
for i := 0; i < 10; i++ {
cl = append(cl, EndedClaim{App: names[i], SnapshotIDs: []string{all[i]}})
}
return append(cl, EndedClaim{App: last, SnapshotIDs: []string{all[10], all[11]}}) // 12 of 20: a large request
}
g0 := openGranted(t, s, 20, set("zz-first")...)
if len(g0.EndedAllowed) != 0 || !hasReason(g0.EndedRefused, "waits one week") {
t.Fatalf("fixture: the first large request must wait: %d agreed, %+v", len(g0.EndedAllowed), g0.EndedRefused)
}
_ = s.CloseWindowFor(context.Background(), "c1", WindowResult{WindowID: g0.WindowID, CountBefore: 20, CountAfter: 20, Outcome: "nothing"})
aWeekLater(t, s)
// a week later ANOTHER set — the same first ten names, another eleventh
g1 := openGranted(t, s, 20, set("zz-second")...)
if len(g1.EndedAllowed) != 0 || !hasReason(g1.EndedRefused, "waits one week") {
t.Fatalf("a different app set rode the first set's week (the two differ only after the tenth name): %d agreed, %+v", len(g1.EndedAllowed), g1.EndedRefused)
}
if n := len(ofType(*events, EventEndedLargeAgreement)); n != 2 {
t.Fatalf("two different large requests gave %d operator warning(s), want 2", n)
}
if a, b := largeSetKey(map[string][]string{"x": nil, "y": nil}), largeSetKey(map[string][]string{"y": nil, "x": nil}); a != b || len(a) != 64 {
t.Fatalf("the key must be one fixed-size value for one set, whatever its order: %q %q", a, b)
}
if largeSetKey(map[string][]string{"a,b": nil}) == largeSetKey(map[string][]string{"a": nil, "b": nil}) {
t.Fatal("two different sets have one key")
}
}