offsite_end_scheduled bypasses the household's switches and went through the ordinary customer leg: subject and body were the box's own sentence, and 50 invented subjects gave 50 mails. - processReceipt: the details must name a checked app (app-name shape, not reserved) and two parsed dates; the mail is rendered only from the hub's bundle with that app and the parsed end date — never the box's message. - The cooldown key is the checked app + the parsed time of the press. - At most 5 receipts per household in 24 hours, counted in the hub's sent log; drops are said once a day; old receipt keys leave the cooldown map. - Pinned: a blocked household and a household with no address get none. Tests seen red before the fix; 12 mutations seen red, 3 after a redo (documentation/audits/build-release-4-2026-11-15/A/redproof-review-4.txt). Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
171 lines
8.1 KiB
Go
171 lines
8.1 KiB
Go
package notify
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Review of 2026-11-15, finding 4: `offsite_end_scheduled` let a box mail its household without limit and with its
|
|
// own words — 50 subjects gave 50 mails, and the subject and body were the box's sentence. A receipt is now built by
|
|
// the HUB alone, from a checked app name and parsed dates; a household gets at most receiptsPerDay of them a day.
|
|
// RED-PROOF: documentation/audits/build-release-4-2026-11-15/A/redproof-review-4.txt.
|
|
|
|
func receiptDet(app string, removed time.Time) string {
|
|
return fmt.Sprintf(`{"stack_name":%q,"removed_at":%q,"ends_at":%q,"subject":"whatever the box says"}`,
|
|
app, removed.UTC().Format(time.RFC3339), removed.Add(30*24*time.Hour).UTC().Format(time.RFC3339))
|
|
}
|
|
|
|
// A box that posts free words under many subjects: no mail carries its words, and the household gets at most the
|
|
// daily limit — whatever the subjects, the app names or the times it invents.
|
|
func TestD84_Receipt_ABoxCannotFloodOrWordTheHouseholdsMail(t *testing.T) {
|
|
d, mails := receiptDispatcher(t, "customer@example.com", []string{})
|
|
evil := "ANY WORDS THE BOX CHOOSES: pay at http://evil.example"
|
|
now := time.Now().UTC()
|
|
// 50 subjects with no app at all (the review's reproduction): not one mail
|
|
for i := 0; i < 50; i++ {
|
|
d.ProcessBoxEvent("c1", "offsite_end_scheduled", "info", evil, evil, fmt.Sprintf(`{"subject":"x%d"}`, i), "controller")
|
|
}
|
|
if len(*mails) != 0 {
|
|
t.Fatalf("%d mail(s) for receipts that name no app; subject=%q", len(*mails), (*mails)[0].subject)
|
|
}
|
|
// 50 valid-looking presses, each another app and another time, each with the box's own words
|
|
for i := 0; i < 50; i++ {
|
|
d.ProcessBoxEvent("c1", "offsite_end_scheduled", "info", evil, evil, receiptDet(fmt.Sprintf("app-%d", i), now.Add(-time.Duration(i)*time.Minute)), "controller")
|
|
}
|
|
if len(*mails) != receiptsPerDay {
|
|
t.Fatalf("50 presses in one burst gave %d mails, want the daily limit %d", len(*mails), receiptsPerDay)
|
|
}
|
|
for _, m := range *mails {
|
|
if strings.Contains(m.subject, "evil") || strings.Contains(m.body, "evil") || strings.Contains(m.body, "ANY WORDS") {
|
|
t.Fatalf("the box's own words reached the household:\n%s\n%s", m.subject, m.body)
|
|
}
|
|
if !strings.Contains(m.subject, "a távoli másolatok törlésre kijelölve") || !strings.Contains(m.body, "Mégis megtartom") {
|
|
t.Fatalf("not the hub's own receipt:\n%s\n%s", m.subject, m.body)
|
|
}
|
|
}
|
|
// the cooldown map did not grow with the box's inventions
|
|
d.mu.Lock()
|
|
n := 0
|
|
for k := range d.custCooldowns {
|
|
if strings.HasPrefix(k, "c1:offsite_end_scheduled") {
|
|
n++
|
|
}
|
|
}
|
|
d.mu.Unlock()
|
|
if n > receiptsPerDay {
|
|
t.Fatalf("the cooldown map holds %d receipt keys for one household, want at most %d", n, receiptsPerDay)
|
|
}
|
|
}
|
|
|
|
// What the mail says comes from the details the hub CHECKED: the app name's shape, the dates as times.
|
|
func TestD84_Receipt_IsBuiltFromCheckedDetailsOnly(t *testing.T) {
|
|
now := time.Now().UTC()
|
|
good := func(app string) string { return receiptDet(app, now) }
|
|
for name, det := range map[string]string{
|
|
"no stack_name": `{"removed_at":"2026-11-15T10:00:00Z","ends_at":"2026-12-15T10:00:00Z"}`,
|
|
"a sentence as the app name": strings.Replace(good("x"), `"x"`, `"pay now at evil.example"`, 1),
|
|
"markup as the app name": strings.Replace(good("x"), `"x"`, `"<b>x</b>"`, 1),
|
|
"a reserved name": good("traefik"),
|
|
"the shares": good("_shares"),
|
|
"removed_at is not a time": `{"stack_name":"vikunja","removed_at":"yesterday","ends_at":"2026-12-15T10:00:00Z"}`,
|
|
"ends_at is not a time": `{"stack_name":"vikunja","removed_at":"2026-11-15T10:00:00Z","ends_at":"soon; click evil.example"}`,
|
|
"ends before it was removed": `{"stack_name":"vikunja","removed_at":"2026-11-15T10:00:00Z","ends_at":"2026-11-01T10:00:00Z"}`,
|
|
"details are not JSON": `not json`,
|
|
"no details": ``,
|
|
} {
|
|
d, mails := receiptDispatcher(t, "customer@example.com", nil)
|
|
d.ProcessBoxEvent("c1", "offsite_end_scheduled", "info", "box words", "box words", det, "controller")
|
|
if len(*mails) != 0 {
|
|
t.Errorf("%s: a mail went out: %q", name, (*mails)[0].subject)
|
|
}
|
|
}
|
|
// the date in the mail is the PARSED ends_at, in the household's date form — not text from the box
|
|
d, mails := receiptDispatcher(t, "customer@example.com", nil)
|
|
d.ProcessBoxEvent("c1", "offsite_end_scheduled", "info", "Törlésük napja: 1999. 01. 01.", "",
|
|
`{"stack_name":"vikunja","removed_at":"2026-11-15T10:00:00Z","ends_at":"2026-12-15T10:00:00Z"}`, "controller")
|
|
if len(*mails) != 1 || !strings.Contains((*mails)[0].body, "2026. 12. 15.") || strings.Contains((*mails)[0].body, "1999") {
|
|
t.Fatalf("the mail must carry the parsed end date and none of the box's text: %+v", *mails)
|
|
}
|
|
// the press's key is the checked app + the parsed time: the same press again, with another `subject` and the time
|
|
// written in another zone, is NOT a second mail
|
|
d.ProcessBoxEvent("c1", "offsite_end_scheduled", "info", "x", "",
|
|
`{"stack_name":"vikunja","removed_at":"2026-11-15T11:00:00+01:00","ends_at":"2026-12-15T10:00:00Z","subject":"another"}`, "controller")
|
|
if len(*mails) != 1 {
|
|
t.Fatalf("one press sent twice with a changed subject gave %d mails", len(*mails))
|
|
}
|
|
}
|
|
|
|
// A blocked household gets no receipt.
|
|
func TestD84_Receipt_ABlockedHouseholdGetsNone(t *testing.T) {
|
|
d, mails := receiptDispatcher(t, "customer@example.com", nil)
|
|
if err := d.store.SetCustomerConfigStatus("c1", "blocked"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
d.ProcessBoxEvent("c1", "offsite_end_scheduled", "info", "x", "", receiptDet("vikunja", time.Now()), "controller")
|
|
if len(*mails) != 0 {
|
|
t.Fatalf("a blocked household was mailed: %+v", *mails)
|
|
}
|
|
if err := d.store.SetCustomerConfigStatus("c1", "active"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
d.ProcessBoxEvent("c1", "offsite_end_scheduled", "info", "x", "", receiptDet("vikunja", time.Now()), "controller")
|
|
if len(*mails) != 1 {
|
|
t.Fatalf("positive control: an unblocked household must get the receipt, got %d", len(*mails))
|
|
}
|
|
}
|
|
|
|
// The map of mailed presses does not grow for ever: a household's receipt keys older than a day go at its next
|
|
// receipt; another household's keys and other event types' keys are not touched.
|
|
func TestD84_Receipt_OldPressesLeaveTheCooldownMap(t *testing.T) {
|
|
d, mails := receiptDispatcher(t, "customer@example.com", nil)
|
|
old := time.Now().Add(-25 * time.Hour)
|
|
d.mu.Lock()
|
|
for i := 0; i < 30; i++ {
|
|
d.custCooldowns[fmt.Sprintf("c1:offsite_end_scheduled:old-%d|x", i)] = old
|
|
}
|
|
d.custCooldowns["c2:offsite_end_scheduled:other|x"] = old
|
|
d.custCooldowns["c1:app_update_undone:vikunja"] = old
|
|
d.mu.Unlock()
|
|
d.ProcessBoxEvent("c1", "offsite_end_scheduled", "info", "x", "", receiptDet("vikunja", time.Now()), "controller")
|
|
if len(*mails) != 1 {
|
|
t.Fatalf("30 presses of yesterday must not count against today's limit: %d mail(s)", len(*mails))
|
|
}
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
n := 0
|
|
for k := range d.custCooldowns {
|
|
if strings.HasPrefix(k, "c1:offsite_end_scheduled:") {
|
|
n++
|
|
}
|
|
}
|
|
if n != 1 {
|
|
t.Fatalf("the household's receipt keys: %d, want only today's one", n)
|
|
}
|
|
if _, ok := d.custCooldowns["c2:offsite_end_scheduled:other|x"]; !ok {
|
|
t.Fatal("another household's key was removed")
|
|
}
|
|
if _, ok := d.custCooldowns["c1:app_update_undone:vikunja"]; !ok {
|
|
t.Fatal("another event type's key was removed")
|
|
}
|
|
}
|
|
|
|
// The daily limit is the hub's own SENT LOG, so a hub restart (an empty map) does not give a box five more.
|
|
func TestD84_Receipt_TheDailyLimitSurvivesARestart(t *testing.T) {
|
|
d, mails := receiptDispatcher(t, "customer@example.com", nil)
|
|
for i := 0; i < receiptsPerDay; i++ {
|
|
d.ProcessBoxEvent("c1", "offsite_end_scheduled", "info", "x", "", receiptDet(fmt.Sprintf("app-%d", i), time.Now()), "controller")
|
|
}
|
|
if len(*mails) != receiptsPerDay {
|
|
t.Fatalf("fixture: %d mails", len(*mails))
|
|
}
|
|
d.mu.Lock()
|
|
d.custCooldowns = map[string]time.Time{} // the restart
|
|
d.mu.Unlock()
|
|
d.ProcessBoxEvent("c1", "offsite_end_scheduled", "info", "x", "", receiptDet("one-more", time.Now()), "controller")
|
|
if len(*mails) != receiptsPerDay {
|
|
t.Fatalf("after a restart the household got mail number %d of the day (limit %d)", len(*mails), receiptsPerDay)
|
|
}
|
|
}
|