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

@@ -0,0 +1,36 @@
# Review finding 1 — „told” survives a change of the household's notification address. The tests on the code BEFORE the fix:
--- FAIL: TestReview1_ToldDoesNotSurviveAnAddressChange (0.05s)
end_notice_review_test.go:204: after the address changed the row still says told: {App:vikunja Press:2026-11-15T10:00:00Z FirstNamed:2026-10-06 16:19:04 +0000 UTC ToldAt:2026-10-06 16:19:04 +0000 UTC ToldTo:household Tries:1 LastTry:2026-11-15 16:19:04 +0000 UTC LastReason: StuckTold:0001-01-01 00:00:00 +0000 UTC} — the hu
--- FAIL: TestReview1_BackAndForth_Removed_AndDuringTheSend (0.05s)
end_notice_review_test.go:252: round 1: the record says told, and the newest notice went to "customer@example.com" — the household's address is "intruder@example.net"
FAIL
FAIL gitea.dooplex.hu/admin/felhom-hub/internal/api 0.177s
FAIL
# After the fix, by mutation:
== MUTATION: a save that changes the address un-tells nothing (the writer)
RED on the mutant, GREEN on the real file
--- FAIL: TestReview1_ToldDoesNotSurviveAnAddressChange (0.05s)
end_notice_review_test.go:208: 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: {App:vikunja Press:2026-11-15T10:00:00Z FirstNamed:2026-10-06 16:21
--- FAIL: TestReview1_BackAndForth_Removed_AndDuringTheSend (0.05s)
end_notice_review_test.go:259: round 1: not told, and the reason is "the household's notification address changed after it was told — it is told again at the address it has now, and the 30 days start again"
1 mutations, 0 not a red-proof
== MUTATION: a row told at another address reads as told (the reader)
RED on the mutant, GREEN on the real file
--- FAIL: TestReview1_BackAndForth_Removed_AndDuringTheSend (0.16s)
end_notice_review_test.go:299: the mail went to the old address and the household counts as told: {App:vikunja Press:2026-11-15T10:00:00Z FirstNamed:2026-11-15 16:21:59 +0000 UTC ToldAt:2026-11-15 16:21:59 +0000 UTC ToldTo:househo
--- FAIL: TestReview1_ToldCountsOnlyAtTheAddressOnRecord (0.04s)
offsite_end_notice_test.go:222: the address on record is "intruder@example.net" and the row still reads told: {App:vikunja Press: FirstNamed:2026-10-01 10:00:00 +0000 UTC ToldAt:2026-10-01 10:00:00 +0000 UTC ToldTo:household Tries
== MUTATION: an address in other capitals or with spaces is another address
RED on the mutant, GREEN on the real file
--- FAIL: TestReview1_ASaveThatChangesNothingKeepsTheNotice (0.05s)
end_notice_review_test.go:232: a save that did not change the address ({"customer_id":"c1","email":" Customer@Example.COM ","enabled_events":[],"cooldown_hours":6,"source":"household"}) voided the notice or mailed again: {App:vik
--- FAIL: TestReview1_ToldCountsOnlyAtTheAddressOnRecord (0.04s)
offsite_end_notice_test.go:214: the same address in other capitals must be the same address: {App:vikunja Press: FirstNamed:2026-10-01 10:00:00 +0000 UTC ToldAt:0001-01-01 00:00:00 +0000 UTC ToldTo: Tries:1 LastTry:2026-10-01 10:0
== MUTATION: a household notice may be marked told with no address key
RED on the mutant, GREEN on the real file
--- FAIL: TestReview1_ToldCountsOnlyAtTheAddressOnRecord (0.04s)
offsite_end_notice_test.go:211: a household notice was marked told with no address key
3 mutations, 0 not a red-proof
+16
View File
@@ -1,5 +1,21 @@
## Unreleased (after v0.182.0)
- **Review of the notice lock, finding 1 — the real hole (2026-11-15 night): „told” does not survive a change of the
household's notification address.** Somebody with the household's login could set the address to his own, press
„delete backup data”, get the notice, and put the real address back: the row stayed told, the real address got
nothing, and the hub agreed 30 days later — the case D84 exists for. Now **„told to the household” is told AT AN
ADDRESS**: the row keeps a key of the address the mail was accepted for (`told_addr`, a hash — the address is
not stored twice; `store.NoticeAddressKey`; `SendEndNotice` returns it), and (1) `store.EndNotices` reads a row
as told only while that is the address on record NOW; (2) `SaveNotificationPrefsAs` un-tells the household's rows
in the same transaction when a save changes the address, removes it or adds a first one (the 3-day line then
counts from the change). The next report mails the current address and **the 30 days start again**. The same
address in other capitals or with spaces is the same address; a push with an empty address (the hub keeps the
stored one) and a push of the switches only change nothing. Changing back and forth: at most 5 mails a day, and
the record says told only when the newest notice went to the address the household has. An address that changes
while the mail is on its way: not told. A removed address: the operator is told in the household's place. *The
rule „a changed address is told again and waits again” is CC's reading of D84 — the operator may reverse it
(`07` §8.2f).* **NOT closed:** a person who holds the login AND keeps the changed address (or the mailbox).
Tests `TestReview1_*` (api 3, store 1, offsitekeys 1); seen red before the fix, 4 mutations red.
- **Review of the notice lock, finding 7 (2026-11-15 night): a test for a guard that had none.** `endNoticeStuck`
skips a row that was told (`n.Told() ||`); removing the guard left the suite green. New test
`TestReview7_ARowToldToTheOperatorNeverGetsTheStuckLine`: a row told to the operator whose later household mail
+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)))
}
}
+17 -13
View File
@@ -84,49 +84,53 @@ func (d *Dispatcher) MailHeld() bool { return d.mailHold.Held() }
// SendEndNotice tries ONCE to mail the household that the off-site copies of `app` are deleted from the day `ends`
// (the hub's own day: the caller passes its clock + 30 days). It returns what the try came to and, when nobody was
// told, the hub's own words for why. It keeps no state: the caller's record decides whether there is a next try.
// told, the hub's own words for why; for a sent mail, the key of the address it went to (store.NoticeAddressKey).
// It keeps no state: the caller's record decides whether there is a next try.
// app must have passed store.ValidNoticeApp (a plain app slug); a name that has not is refused here again.
func (d *Dispatcher) SendEndNotice(customerID, app string, ends time.Time) (EndNoticeOutcome, string) {
func (d *Dispatcher) SendEndNotice(customerID, app string, ends time.Time) (outcome EndNoticeOutcome, why string, addrKey string) {
if !store.ValidNoticeApp(app) {
return EndNoticeFailed, "not an app name"
return EndNoticeFailed, "not an app name", ""
}
if d.resendAPIKey == "" {
return EndNoticeFailed, "the hub has no mail service configured"
return EndNoticeFailed, "the hub has no mail service configured", ""
}
if d.store.IsCustomerBlocked(customerID) {
return EndNoticeNoAddress, "the household is blocked"
return EndNoticeNoAddress, "the household is blocked", ""
}
prefs, err := d.store.GetNotificationPrefs(customerID)
if err != nil {
return EndNoticeFailed, "the household's notification address could not be read"
return EndNoticeFailed, "the household's notification address could not be read", ""
}
if prefs == nil || strings.TrimSpace(prefs.Email) == "" {
return EndNoticeNoAddress, "the household has no notification address"
return EndNoticeNoAddress, "the household has no notification address", ""
}
now := time.Now()
sentToday, cerr := d.store.CountCustomerTriesSince(customerID, EndNoticeEvent, now.Add(-24*time.Hour))
if cerr != nil {
// A log that cannot be read is not „nothing sent”: no receipt goes, and the notice stays to be tried again.
return EndNoticeLimit, "the hub's sent log could not be read"
return EndNoticeLimit, "the hub's sent log could not be read", ""
}
if sentToday >= receiptsPerDay {
return EndNoticeLimit, fmt.Sprintf("the household already got %d of these notices in the last 24 hours (the daily limit) — this one goes later", receiptsPerDay)
return EndNoticeLimit, fmt.Sprintf("the household already got %d of these notices in the last 24 hours (the daily limit) — this one goes later", receiptsPerDay), ""
}
// The address the mail goes to, read ONCE: its key is returned so that the caller's record says which address
// was told (the address may change while the mail is on its way — review finding 1).
to := strings.TrimSpace(prefs.Email)
subject, body := FormatReceiptEmail(d.store.CustomerLanguage(customerID), customerID, EndNoticeEvent, "info", app, ends)
logMsg := EndNoticeEvent + " " + app
if err := d.deliver("customer receipt "+EndNoticeEvent, customerID, strings.TrimSpace(prefs.Email), subject, body, nil); isHeld(err) {
if err := d.deliver("customer receipt "+EndNoticeEvent, customerID, to, subject, body, nil); isHeld(err) {
d.store.LogNotification(customerID, EndNoticeEvent, "info", logMsg, "held", "MAIL-HOLD marker present — not sent; the notice stays to be sent", "customer")
return EndNoticeHeld, "the hub is not sending mail (the MAIL-HOLD marker is present)"
return EndNoticeHeld, "the hub is not sending mail (the MAIL-HOLD marker is present)", ""
} else if err != nil {
d.logger.Printf("[ERROR] Notice %s failed for %s/%s: %v — it stays to be sent", EndNoticeEvent, customerID, app, err)
d.store.LogNotification(customerID, EndNoticeEvent, "info", logMsg, "failed", err.Error(), "customer")
return EndNoticeFailed, "the mail service did not accept the mail"
return EndNoticeFailed, "the mail service did not accept the mail", ""
}
d.logger.Printf("[INFO] Notice %s sent for %s/%s", EndNoticeEvent, customerID, app) // never the address (R-922)
if lerr := d.store.LogNotification(customerID, EndNoticeEvent, "info", logMsg, "sent", "", "customer"); lerr != nil {
d.logger.Printf("[WARN] Notice %s for %s/%s was sent, but the sent log could not be written: %v", EndNoticeEvent, customerID, app, lerr)
}
return EndNoticeSent, ""
return EndNoticeSent, "", store.NoticeAddressKey(to)
}
// SendOperatorNow mails the operator ONE hub-made event and returns only when the mail service has answered: nil
+13 -13
View File
@@ -46,7 +46,7 @@ const receiptDetails = `{"stack_name":"vikunja","removed_at":"2026-11-15T10:00:0
// for this. The operator is not mailed, and no raw details are in it.
func TestNotice_ReachesTheHouseholdWhateverItsSwitches(t *testing.T) {
d, mails := receiptDispatcher(t, "customer@example.com", []string{})
if out, why := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeSent {
if out, why, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeSent {
t.Fatalf("outcome = %s (%s), want sent", out, why)
}
if len(*mails) != 1 {
@@ -80,7 +80,7 @@ func TestNotice_FollowsTheHouseholdsLanguage(t *testing.T) {
if err := d.store.SaveReport("c1", []byte(`{"customer_id":"c1","language":"en"}`)); err != nil {
t.Fatal(err)
}
if out, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeSent || len(*mails) != 1 {
if out, _, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeSent || len(*mails) != 1 {
t.Fatalf("outcome %s, mails %d", out, len(*mails))
}
m := (*mails)[0]
@@ -126,20 +126,20 @@ func TestNotice_TheBoxesEventMailsNobody(t *testing.T) {
// the caller tells the operator in its place.
func TestNotice_NoAddressOrBlocked_SaysSoAndMailsNobody(t *testing.T) {
d, mails := receiptDispatcher(t, "", nil)
if out, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeNoAddress || len(*mails) != 0 {
if out, _, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeNoAddress || len(*mails) != 0 {
t.Fatalf("no address: outcome %s, %d mail(s)", out, len(*mails))
}
d, mails = receiptDispatcher(t, "customer@example.com", nil)
if err := d.store.SetCustomerConfigStatus("c1", "blocked"); err != nil {
t.Fatal(err)
}
if out, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeNoAddress || len(*mails) != 0 {
if out, _, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeNoAddress || len(*mails) != 0 {
t.Fatalf("blocked: outcome %s, %d mail(s)", out, len(*mails))
}
if err := d.store.SetCustomerConfigStatus("c1", "active"); err != nil {
t.Fatal(err)
}
if out, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeSent || len(*mails) != 1 {
if out, _, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeSent || len(*mails) != 1 {
t.Fatalf("positive control: an unblocked household must get the notice: outcome %s, %d mail(s)", out, len(*mails))
}
}
@@ -149,17 +149,17 @@ func TestNotice_NoAddressOrBlocked_SaysSoAndMailsNobody(t *testing.T) {
func TestNotice_TheDailyLimitDelaysAndSurvivesARestart(t *testing.T) {
d, mails := receiptDispatcher(t, "customer@example.com", nil)
for i := 0; i < receiptsPerDay; i++ {
if out, why := d.SendEndNotice("c1", fmt.Sprintf("app-%d", i), noticeDay); out != EndNoticeSent {
if out, why, _ := d.SendEndNotice("c1", fmt.Sprintf("app-%d", i), noticeDay); out != EndNoticeSent {
t.Fatalf("notice %d: %s (%s)", i, out, why)
}
}
if out, why := d.SendEndNotice("c1", "one-more", noticeDay); out != EndNoticeLimit || !strings.Contains(why, "later") {
if out, why, _ := d.SendEndNotice("c1", "one-more", noticeDay); out != EndNoticeLimit || !strings.Contains(why, "later") {
t.Fatalf("the sixth notice: outcome %s (%q), want limit", out, why)
}
d2 := opOnlyDispatcher(t, d.store, &sentTo{}) // the restart: nothing in memory
n2 := 0
d2.sendEmailFn = func(string, string, string, map[string]string) error { n2++; return nil }
if out, _ := d2.SendEndNotice("c1", "one-more", noticeDay); out != EndNoticeLimit || n2 != 0 {
if out, _, _ := d2.SendEndNotice("c1", "one-more", noticeDay); out != EndNoticeLimit || n2 != 0 {
t.Fatalf("after a restart: outcome %s, %d mail(s) — the limit must be the hub's sent log", out, n2)
}
if len(*mails) != receiptsPerDay {
@@ -169,7 +169,7 @@ func TestNotice_TheDailyLimitDelaysAndSurvivesARestart(t *testing.T) {
if err := d.store.BackdateNotificationLogForTest("c1", 25*time.Hour); err != nil {
t.Fatal(err)
}
if out, why := d.SendEndNotice("c1", "one-more", noticeDay); out != EndNoticeSent {
if out, why, _ := d.SendEndNotice("c1", "one-more", noticeDay); out != EndNoticeSent {
t.Fatalf("a day later: %s (%s), want sent — the limit delays, it does not drop", out, why)
}
}
@@ -182,7 +182,7 @@ func TestNotice_HeldOrFailed_IsNotSent(t *testing.T) {
t.Fatal(err)
}
d.SetMailHold(mailhold.New(dir, nil))
if out, why := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeHeld || !strings.Contains(why, "MAIL-HOLD") || len(*mails) != 0 {
if out, why, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeHeld || !strings.Contains(why, "MAIL-HOLD") || len(*mails) != 0 {
t.Fatalf("held: outcome %s (%q), %d mail(s)", out, why, len(*mails))
}
if !d.MailHeld() {
@@ -190,7 +190,7 @@ func TestNotice_HeldOrFailed_IsNotSent(t *testing.T) {
}
d.SetMailHold(nil)
d.sendEmailFn = func(string, string, string, map[string]string) error { return errors.New("resend: 503") }
if out, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeFailed {
if out, _, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeFailed {
t.Fatalf("failed send: outcome %s", out)
}
// neither left a „sent” row: the limit must not count a mail that did not go
@@ -199,7 +199,7 @@ func TestNotice_HeldOrFailed_IsNotSent(t *testing.T) {
}
// no mail service at all
d.resendAPIKey = ""
if out, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeFailed {
if out, _, _ := d.SendEndNotice("c1", "vikunja", noticeDay); out != EndNoticeFailed {
t.Fatalf("no mail service: outcome %s", out)
}
}
@@ -208,7 +208,7 @@ func TestNotice_HeldOrFailed_IsNotSent(t *testing.T) {
func TestNotice_OnlyAnAppNameIsMailed(t *testing.T) {
d, mails := receiptDispatcher(t, "customer@example.com", nil)
for _, app := range []string{"", "pay now at evil.example", "<b>x</b>", "traefik", "_shares", strings.Repeat("a", 65)} {
if out, _ := d.SendEndNotice("c1", app, noticeDay); out == EndNoticeSent || len(*mails) != 0 {
if out, _, _ := d.SendEndNotice("c1", app, noticeDay); out == EndNoticeSent || len(*mails) != 0 {
t.Fatalf("%q: outcome %s, %d mail(s)", app, out, len(*mails))
}
}
@@ -164,3 +164,17 @@ func TestReview3_ANameThatCannotBeToldIsNeverAgreed(t *testing.T) {
}
}
}
// Review finding 1, through the window: told 31 days ago at one address, and the household's address is another one
// today — nothing is agreed, and the answer says why.
func TestReview1_Window_ToldAtAnotherAddressIsNotTold(t *testing.T) {
s, _ := endedFixture(t) // vikunja: absent 31 days, told 31 days ago at the fixture's address
claim := EndedClaim{App: "vikunja", SnapshotIDs: []string{eid(0)}}
if err := s.Store.SaveNotificationPrefs("c1", "someone-else@example.net", nil, 6); err != nil {
t.Fatal(err)
}
g := openGranted(t, s, 20, claim)
if len(g.EndedAllowed) != 0 || !hasReason(g.EndedRefused, "address changed") {
t.Fatalf("agreed %v, refused %+v", shortIDs(g.EndedAllowed), g.EndedRefused)
}
}
+91 -9
View File
@@ -1,9 +1,12 @@
package store
import (
"crypto/sha256"
"database/sql"
"encoding/hex"
"fmt"
"sort"
"strings"
"sync"
"time"
)
@@ -56,9 +59,53 @@ func (s *Store) migrateOffsiteEndNotices() error {
);`); err != nil {
return fmt.Errorf("offsite_end_notices: %w", err)
}
// told_addr: a key of the household's notification address the notice was accepted for (NoticeAddressKey) —
// never the address itself. „Told to the household” counts only while it is the address the household has NOW.
s.db.Exec("ALTER TABLE offsite_end_notices ADD COLUMN told_addr TEXT NOT NULL DEFAULT ''")
return nil
}
// „TOLD” IS TOLD AT AN ADDRESS (review of 2026-11-15, finding 1). Somebody who holds the household's login could
// set the notification address to his own, press „delete backup data”, get the notice and put the real address
// back: the row said told, the real address had got nothing, and 30 days later the hub agreed. So a row that is
// told to the household carries a key of the address the mail was accepted for, and it counts as told only while
// that is the household's CURRENT address. Two mechanisms, on purpose:
//
// 1. THE READER (EndNotices) compares the row's key with the address on record at every read: a row told at another
// address reads as NOT told. This is the rule; it holds whoever changed the address and however.
// 2. THE WRITER of the address (SaveNotificationPrefsAs) un-tells the household's rows in the same transaction when
// the address changes, is removed or is added — so the row is clean, its „first named” starts again (the
// operator's 3-day line counts from the change) and the log says what happened.
//
// The same address in other capitals or with spaces around it is the same address. An operator-told row is not
// touched: the operator was told, and a household that gets an address is mailed by the walk anyway.
// reasonAddressChanged is the hub's own reason on a row whose household must be told again.
const reasonAddressChanged = "the household's notification address changed after it was told — it is told again at the address it has now, and the 30 days start again"
// NoticeAddressKey is the key of a notification address for the notice record: a hash of the address in lower case
// without the spaces around it; "" for no address. The address itself is not stored a second time.
func NoticeAddressKey(email string) string {
norm := strings.ToLower(strings.TrimSpace(email))
if norm == "" {
return ""
}
sum := sha256.Sum256([]byte("felhom notice address\n" + norm))
return hex.EncodeToString(sum[:])
}
// untellHouseholdNotices clears „told to the household” on every row of a household, inside the caller's
// transaction. Returns how many rows it changed.
func untellHouseholdNotices(tx *sql.Tx, customerID string, now time.Time) (int64, error) {
res, err := tx.Exec(`UPDATE offsite_end_notices SET told_at = NULL, told_to = '', told_addr = '', tries = 0, last_try = NULL,
last_reason = ?, first_named = ?, stuck_told = NULL WHERE customer_id = ? AND told_to = ?`,
reasonAddressChanged, now.UTC().Format(time.RFC3339), customerID, EndNoticeHousehold)
if err != nil {
return 0, err
}
return res.RowsAffected()
}
// EndClaim is one pending end record of a report, as far as the notice needs it.
type EndClaim struct {
App string
@@ -211,7 +258,13 @@ func (s *Store) noteEndNoticesFull(customerID string, notStored int) {
// EndNotices returns the hub's record for one household, by app. A date that cannot be read is the zero time — and
// a zero ToldAt is „not told”.
func (s *Store) EndNotices(customerID string) (map[string]EndNotice, error) {
rows, err := s.db.Query(`SELECT app, press, first_named, told_at, told_to, tries, last_try, last_reason, stuck_told
// The address the household has NOW (no row, or an empty one: no address).
var email sql.NullString
if err := s.db.QueryRow(`SELECT email FROM customer_notifications WHERE customer_id = ?`, customerID).Scan(&email); err != nil && err != sql.ErrNoRows {
return nil, err
}
current := NoticeAddressKey(email.String)
rows, err := s.db.Query(`SELECT app, press, first_named, told_at, told_to, tries, last_try, last_reason, stuck_told, told_addr
FROM offsite_end_notices WHERE customer_id = ?`, customerID)
if err != nil {
return nil, err
@@ -220,11 +273,16 @@ func (s *Store) EndNotices(customerID string) (map[string]EndNotice, error) {
out := map[string]EndNotice{}
for rows.Next() {
var n EndNotice
var first string
var first, toldAddr string
var told, lastTry, stuck sql.NullString
if err := rows.Scan(&n.App, &n.Press, &first, &told, &n.ToldTo, &n.Tries, &lastTry, &n.LastReason, &stuck); err != nil {
if err := rows.Scan(&n.App, &n.Press, &first, &told, &n.ToldTo, &n.Tries, &lastTry, &n.LastReason, &stuck, &toldAddr); err != nil {
return nil, err
}
if n.ToldTo == EndNoticeHousehold && (toldAddr == "" || toldAddr != current) {
// told at an address the household does not have now (or at none the hub can show): not told
n.ToldTo, told = "", sql.NullString{}
n.LastReason = reasonAddressChanged
}
n.FirstNamed = parseNoticeTime(first)
n.ToldAt = parseNoticeTime(told.String)
n.LastTry = parseNoticeTime(lastTry.String)
@@ -251,13 +309,23 @@ func parseNoticeTime(v string) time.Time {
// MarkEndNoticeTold writes that the notice of THIS press was accepted by the mail service, at the hub's time `now`,
// to `to` (EndNoticeHousehold or EndNoticeOperator). It matches the press: a row that was voided or reset while the
// mail was being sent is not marked (ok=false) — that press was never told.
func (s *Store) MarkEndNoticeTold(customerID, app, press, to string, now time.Time) (ok bool, err error) {
//
// addrKey is NoticeAddressKey of the address the household's mail was accepted for (the address at the SEND, which
// the sender returns) — required for EndNoticeHousehold, ignored for EndNoticeOperator. If the household's address
// has changed since the send, the row is written and reads as not told (EndNotices).
func (s *Store) MarkEndNoticeTold(customerID, app, press, to, addrKey string, now time.Time) (ok bool, err error) {
if to != EndNoticeHousehold && to != EndNoticeOperator {
return false, fmt.Errorf("MarkEndNoticeTold: unknown recipient %q", to)
}
if to == EndNoticeHousehold && addrKey == "" {
return false, fmt.Errorf("MarkEndNoticeTold: a household notice needs the key of the address it went to")
}
if to == EndNoticeOperator {
addrKey = ""
}
stamp := now.UTC().Format(time.RFC3339)
res, err := s.db.Exec(`UPDATE offsite_end_notices SET told_at = ?, told_to = ?, tries = tries + 1, last_try = ?, last_reason = ''
WHERE customer_id = ? AND app = ? AND press = ?`, stamp, to, stamp, customerID, app, press)
res, err := s.db.Exec(`UPDATE offsite_end_notices SET told_at = ?, told_to = ?, told_addr = ?, tries = tries + 1, last_try = ?, last_reason = ''
WHERE customer_id = ? AND app = ? AND press = ?`, stamp, to, addrKey, stamp, customerID, app, press)
if err != nil {
return false, err
}
@@ -286,9 +354,23 @@ func (s *Store) SetEndNoticeForTest(customerID, app, press string, firstNamed, t
if !toldAt.IsZero() {
told = sql.NullString{String: toldAt.UTC().Format(time.RFC3339), Valid: true}
}
_, err := s.db.Exec(`INSERT INTO offsite_end_notices (customer_id, app, press, first_named, told_at, told_to) VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT(customer_id, app) DO UPDATE SET press = excluded.press, first_named = excluded.first_named, told_at = excluded.told_at, told_to = excluded.told_to`,
customerID, app, press, firstNamed.UTC().Format(time.RFC3339), told, to)
// A household-told row is told at the address the household has now; a fixture household with none gets one.
addr := ""
if to == EndNoticeHousehold {
var email sql.NullString
_ = s.db.QueryRow(`SELECT email FROM customer_notifications WHERE customer_id = ?`, customerID).Scan(&email)
if strings.TrimSpace(email.String) == "" {
email.String = "fixture@example.com"
if _, err := s.db.Exec(`INSERT INTO customer_notifications (customer_id, email, enabled_events, cooldown_hours) VALUES (?, ?, '[]', 6)
ON CONFLICT(customer_id) DO UPDATE SET email = excluded.email`, customerID, email.String); err != nil {
return err
}
}
addr = NoticeAddressKey(email.String)
}
_, err := s.db.Exec(`INSERT INTO offsite_end_notices (customer_id, app, press, first_named, told_at, told_to, told_addr) VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(customer_id, app) DO UPDATE SET press = excluded.press, first_named = excluded.first_named, told_at = excluded.told_at, told_to = excluded.told_to, told_addr = excluded.told_addr`,
customerID, app, press, firstNamed.UTC().Format(time.RFC3339), told, to, addr)
return err
}
+62 -3
View File
@@ -26,6 +26,10 @@ func TestNotice_Record_OnePressOneRow(t *testing.T) {
s := newTestStore(t)
t0 := time.Date(2026, 11, 15, 10, 0, 0, 0, time.UTC)
press := "2026-11-15T09:59:00Z"
if err := s.SaveNotificationPrefs("c1", "customer@example.com", nil, 6); err != nil {
t.Fatal(err)
}
addr := NoticeAddressKey("customer@example.com")
note := func(at time.Time, deployed []string, tierOn bool, claims ...EndClaim) {
t.Helper()
if err := s.NoteEndNotices("c1", deployed, claims, tierOn, at); err != nil {
@@ -43,17 +47,17 @@ func TestNotice_Record_OnePressOneRow(t *testing.T) {
t.Fatalf("a later report of the same press moved the row: %+v", n)
}
// told: only with the press the hub mailed about
if ok, err := s.MarkEndNoticeTold("c1", "vikunja", "2026-11-01T00:00:00Z", EndNoticeHousehold, t0); err != nil || ok {
if ok, err := s.MarkEndNoticeTold("c1", "vikunja", "2026-11-01T00:00:00Z", EndNoticeHousehold, addr, t0); err != nil || ok {
t.Fatalf("a told mark for ANOTHER press was written (ok=%v err=%v)", ok, err)
}
if n, _ := endNotice(t, s, "c1", "vikunja"); n.Told() {
t.Fatalf("told by a mark for another press: %+v", n)
}
if _, err := s.MarkEndNoticeTold("c1", "vikunja", press, "the box", t0); err == nil {
if _, err := s.MarkEndNoticeTold("c1", "vikunja", press, "the box", addr, t0); err == nil {
t.Fatal("an unknown recipient was accepted")
}
told := t0.Add(time.Hour)
if ok, err := s.MarkEndNoticeTold("c1", "vikunja", press, EndNoticeHousehold, told); err != nil || !ok {
if ok, err := s.MarkEndNoticeTold("c1", "vikunja", press, EndNoticeHousehold, addr, told); err != nil || !ok {
t.Fatalf("MarkEndNoticeTold: ok=%v err=%v", ok, err)
}
n, _ = endNotice(t, s, "c1", "vikunja")
@@ -184,3 +188,58 @@ func TestNotice_Record_GoesWithTheHousehold(t *testing.T) {
t.Fatalf("orphan sweep: %+v %v", n, err)
}
}
// Review finding 1 — „told” is told AT AN ADDRESS. The reader's rule alone: a row told to the household counts only
// while the household's address on record is the one the mail was accepted for — however the address came to differ
// (here: changed behind the writer's back). An operator-told row does not depend on the address.
func TestReview1_ToldCountsOnlyAtTheAddressOnRecord(t *testing.T) {
s := newTestStore(t)
at := time.Date(2026, 10, 1, 10, 0, 0, 0, time.UTC)
if err := s.SaveNotificationPrefs("c1", "Real@Example.com", nil, 6); err != nil {
t.Fatal(err)
}
if err := s.NoteEndNotices("c1", nil, []EndClaim{{App: "vikunja"}, {App: "kimai"}}, true, at); err != nil {
t.Fatal(err)
}
if ok, err := s.MarkEndNoticeTold("c1", "vikunja", "", EndNoticeHousehold, NoticeAddressKey(" real@example.COM "), at); err != nil || !ok {
t.Fatalf("mark: %v %v", ok, err)
}
if ok, err := s.MarkEndNoticeTold("c1", "kimai", "", EndNoticeOperator, "", at); err != nil || !ok {
t.Fatalf("mark: %v %v", ok, err)
}
if _, err := s.MarkEndNoticeTold("c1", "vikunja", "", EndNoticeHousehold, "", at); err == nil {
t.Fatal("a household notice was marked told with no address key")
}
if n, _ := endNotice(t, s, "c1", "vikunja"); !n.Told() {
t.Fatalf("the same address in other capitals must be the same address: %+v", n)
}
for _, other := range []string{"intruder@example.net", ""} {
if _, err := s.db.Exec(`UPDATE customer_notifications SET email = ? WHERE customer_id = 'c1'`, other); err != nil {
t.Fatal(err)
}
n, _ := endNotice(t, s, "c1", "vikunja")
if n.Told() || !n.ToldAt.IsZero() || !strings.Contains(n.LastReason, "address changed") {
t.Fatalf("the address on record is %q and the row still reads told: %+v", other, n)
}
if k, _ := endNotice(t, s, "c1", "kimai"); !k.Told() || k.ToldTo != EndNoticeOperator {
t.Fatalf("an operator-told row must not depend on the household's address: %+v", k)
}
}
// the address is put back by hand: the row reads told again — it WAS told at this address
if _, err := s.db.Exec(`UPDATE customer_notifications SET email = 'real@example.com' WHERE customer_id = 'c1'`); err != nil {
t.Fatal(err)
}
if n, _ := endNotice(t, s, "c1", "vikunja"); !n.Told() {
t.Fatalf("told at the address the household has: %+v", n)
}
// … but through the WRITER a change is final: the row is un-told, and changing back does not bring it back
if err := s.SaveNotificationPrefs("c1", "intruder@example.net", nil, 6); err != nil {
t.Fatal(err)
}
if err := s.SaveNotificationPrefs("c1", "real@example.com", nil, 6); err != nil {
t.Fatal(err)
}
if n, _ := endNotice(t, s, "c1", "vikunja"); n.Told() {
t.Fatalf("away and back through the save: the old „told” came back: %+v", n)
}
}
+25 -2
View File
@@ -1049,7 +1049,27 @@ func (s *Store) SaveNotificationPrefsAs(customerID, email string, enabledEvents
if householdSaved {
hs = 1
}
_, err := s.db.Exec(`
// The notice lock (review of 2026-11-15, finding 1): a save that CHANGES the address — another one, none, or a
// first one — un-tells the household's off-site end notices in the same transaction (offsite_end_notice.go).
tx, err := s.db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
var old sql.NullString
if qerr := tx.QueryRow(`SELECT email FROM customer_notifications WHERE customer_id = ?`, customerID).Scan(&old); qerr != nil && qerr != sql.ErrNoRows {
return qerr
}
if NoticeAddressKey(old.String) != NoticeAddressKey(email) {
n, uerr := untellHouseholdNotices(tx, customerID, time.Now())
if uerr != nil {
return uerr
}
if n > 0 && s.logger != nil {
s.logger.Printf("[WARN] notification address of %s changed: %d off-site end notice(s) that were told at the old address are NOT told any more — the household is mailed again at its new address and the 30 days start again", customerID, n)
}
}
_, err = tx.Exec(`
INSERT INTO customer_notifications (customer_id, email, enabled_events, cooldown_hours, household_saved, updated_at)
VALUES (?, ?, ?, ?, ?, datetime('now'))
ON CONFLICT(customer_id) DO UPDATE SET
@@ -1060,7 +1080,10 @@ func (s *Store) SaveNotificationPrefsAs(customerID, email string, enabledEvents
updated_at = datetime('now')`,
customerID, email, string(eventsJSON), cooldownHours, hs,
)
return err
if err != nil {
return err
}
return tx.Commit()
}
// LogNotification records a notification attempt.