hub (unreleased): review fix 1 — „told” is told at an address: a changed notification address is told again and waits again (the hole D84 exists for)

Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
This commit is contained in:
admin committed 2026-11-15 17:26:21 +01:00
1 parent cbfeeb626e
commit a6f4aa0b64
10 files changed
+412 -44

No files matched your search

+4 -4
View File
@@ -116,15 +116,15 @@ func (h *Handler) endNotices(customerID string, start time.Time) {
h.endNoticeStuck(customerID, n, now)
continue
}
outcome, why := notify.EndNoticeFailed, "the hub has no mail service configured"
outcome, why, addrKey := notify.EndNoticeFailed, "the hub has no mail service configured", ""
if h.dispatcher != nil {
// The day in the mail is the hub's own: its clock now + 30 days. Never the box's date.
outcome, why = h.dispatcher.SendEndNotice(customerID, app, now.Add(offsitekeys.EndedAppAfter))
outcome, why, addrKey = h.dispatcher.SendEndNotice(customerID, app, now.Add(offsitekeys.EndedAppAfter))
}
now = clock() // after the send returned
switch outcome {
case notify.EndNoticeSent:
ok, merr := h.store.MarkEndNoticeTold(customerID, app, n.Press, store.EndNoticeHousehold, now)
ok, merr := h.store.MarkEndNoticeTold(customerID, app, n.Press, store.EndNoticeHousehold, addrKey, now)
if merr != nil || !ok {
// The mail went and the record does not say so: the household is mailed again at a later report and
// the 30 days start then. The safe direction (no deletion on a notice the hub cannot show).
@@ -197,7 +197,7 @@ func (h *Handler) endNoticeToOperator(customerID string, n store.EndNotice, why
if _, err := h.store.SaveEvent(customerID, offsitekeys.EventEndNoticeOperator, "warning", msg, string(details), "hub"); err != nil {
h.logger.Printf("[WARN] [end-notice] %s event save FAILED for %s (the mail went): %v", offsitekeys.EventEndNoticeOperator, customerID, err)
}
ok, err := h.store.MarkEndNoticeTold(customerID, n.App, n.Press, store.EndNoticeOperator, toldAt)
ok, err := h.store.MarkEndNoticeTold(customerID, n.App, n.Press, store.EndNoticeOperator, "", toldAt)
if err != nil || !ok {
return time.Time{}, fmt.Errorf("the operator's mail went, but the hub's record was not written (matched=%v, err=%v)", ok, err)
}
+134
View File
@@ -169,3 +169,137 @@ func TestReview7_ARowToldToTheOperatorNeverGetsTheStuckLine(t *testing.T) {
t.Fatalf("a row that was told to the operator got the „told nobody” line: %+v", e)
}
}
// Finding 1 — THE REAL HOLE. „Told” survived a change of the household's notification address. Somebody with the
// household's login sets the address to his own, presses „delete backup data”, gets the notice, and puts the real
// address back: the row stayed told, the real address got nothing, and the hub agreed 30 days later. This is the
// case D84 exists for. Now „told to the household” counts only for the address the household has NOW: a save that
// changes, removes or adds the address un-tells the household's rows, the next report mails the current address,
// and the 30 days start again.
func (r *noticeRig) savePrefs(body string) {
r.t.Helper()
if rr := do(r.h, "POST", "/preferences", "ckey", body); rr.Code != 200 {
r.t.Fatalf("preferences → %d (%s)", rr.Code, rr.Body.String())
}
}
const intruder = "intruder@example.net"
func TestReview1_ToldDoesNotSurviveAnAddressChange(t *testing.T) {
r := newNoticeRig(t, intruder) // the address was changed to the intruder's before the press
r.report(repPressed)
n, ok := r.notice("vikunja")
if !ok || n.ToldTo != store.EndNoticeHousehold || len(r.to(intruder)) != 1 {
t.Fatalf("fixture: the notice did not go to the address on record: %+v", n)
}
// the hub's record is 40 days old — old enough to agree
old := time.Now().UTC().Add(-40 * 24 * time.Hour)
if err := r.st.SetEndNoticeForTest("c1", "vikunja", "2026-11-15T10:00:00Z", old, old, store.EndNoticeHousehold); err != nil {
t.Fatal(err)
}
// the real address is put back, through the endpoint the box uses for the household's own save
r.savePrefs(`{"customer_id":"c1","email":"` + household + `","enabled_events":[],"cooldown_hours":6,"source":"household"}`)
n, _ = r.notice("vikunja")
if n.Told() {
t.Fatalf("after the address changed the row still says told: %+v — the hub would agree with nobody at the real address told", n)
}
if time.Since(n.FirstNamed) > time.Minute || !strings.Contains(n.LastReason, "address changed") {
t.Fatalf("the save must un-tell the row itself: first named now (the operator's 3-day line counts from the change) and the reason on the row: %+v", n)
}
r.report(repPressed)
n, _ = r.notice("vikunja")
if len(r.to(household)) != 1 || n.ToldTo != store.EndNoticeHousehold || time.Since(n.ToldAt) > time.Minute {
t.Fatalf("the CURRENT address must be mailed and the 30 days start again: mails to it = %d, record %+v", len(r.to(household)), n)
}
}
// A save that does not change the address un-tells nothing: the same address in other capitals or with spaces, a
// push with an empty address (the hub keeps the stored one), and a push that only changes the switches.
func TestReview1_ASaveThatChangesNothingKeepsTheNotice(t *testing.T) {
r := newNoticeRig(t, household)
r.report(repPressed)
before, _ := r.notice("vikunja")
for _, body := range []string{
`{"customer_id":"c1","email":" Customer@Example.COM ","enabled_events":[],"cooldown_hours":6,"source":"household"}`,
`{"customer_id":"c1","email":"","enabled_events":["node_down"],"cooldown_hours":6}`,
`{"customer_id":"c1","email":"` + household + `","enabled_events":["node_down"],"cooldown_hours":12,"source":"household"}`,
} {
r.savePrefs(body)
r.report(repPressed)
n, _ := r.notice("vikunja")
if !n.Told() || !n.ToldAt.Equal(before.ToldAt) || len(r.to(household)) != 1 {
t.Fatalf("a save that did not change the address (%s) voided the notice or mailed again: %+v, %d mail(s)", body, n, len(r.to(household)))
}
}
}
// The attacker's other moves. (a) The address is changed back and forth many times in a day: whenever the record
// says told, the newest notice went to the address the household has at that moment; the daily limit delays and
// never counts as told. (b) The address is REMOVED after the notice: the household's row is un-told and the
// operator is told in its place. (c) The address changes WHILE the mail is being sent: the mail went to the old
// address, so the household is not told.
func TestReview1_BackAndForth_Removed_AndDuringTheSend(t *testing.T) {
r := newNoticeRig(t, household)
cur := household
for i := 0; i < 8; i++ {
r.report(repPressed)
n, _ := r.notice("vikunja")
mails := r.all()
var last string
for _, m := range mails {
if strings.Contains(m.subject, "vikunja: ") && !strings.Contains(m.subject, "offsite_end") {
last = m.to
}
}
if n.Told() && last != cur {
t.Fatalf("round %d: the record says told, and the newest notice went to %q — the household's address is %q", i, last, cur)
}
if !n.Told() && !strings.Contains(n.LastReason, "daily limit") {
t.Fatalf("round %d: not told, and the reason is %q", i, n.LastReason)
}
if cur == household {
cur = intruder
} else {
cur = household
}
r.savePrefs(`{"customer_id":"c1","email":"` + cur + `","enabled_events":[],"cooldown_hours":6,"source":"household"}`)
}
if n := len(r.to(household)) + len(r.to(intruder)); n != 5 {
t.Fatalf("eight address changes in a day: %d notices, want the daily limit 5", n)
}
// (b) removed
r2 := newNoticeRig(t, household)
r2.report(repPressed)
r2.savePrefs(`{"customer_id":"c1","email":"","email_cleared":true,"enabled_events":[],"cooldown_hours":6,"source":"household"}`)
if n, _ := r2.notice("vikunja"); n.Told() {
t.Fatalf("the address was removed and the row still says the household was told: %+v", n)
}
r2.report(repPressed)
if n, _ := r2.notice("vikunja"); n.ToldTo != store.EndNoticeOperator || len(r2.opNotice(offsitekeys.EventEndNoticeOperator)) != 1 {
t.Fatalf("with the address removed the operator must be told in the household's place: %+v", n)
}
// (c) during the send
r3 := newNoticeRig(t, intruder)
r3.d.SetSendFnForTest(func(to, subject, body string, _ map[string]string) error {
if to == intruder { // while the intruder's mail is on its way, the real address is put back
if err := r3.st.SaveNotificationPrefsAs("c1", household, []string{}, 6, true); err != nil {
t.Error(err)
}
}
r3.mu.Lock()
defer r3.mu.Unlock()
r3.mails = append(r3.mails, noticeMail{to, subject, body})
return nil
})
r3.report(repPressed)
if n, _ := r3.notice("vikunja"); n.Told() {
t.Fatalf("the mail went to the old address and the household counts as told: %+v", n)
}
r3.h.endNotices("c1", time.Now().Add(2*time.Hour))
if n, _ := r3.notice("vikunja"); !n.Told() || len(r3.to(household)) != 1 {
t.Fatalf("the next walk must tell the address the household has now: %+v, %d mail(s)", n, len(r3.to(household)))
}
}