package coordinatormail import "testing" func scope() Scope { return Scope{"team", "person", "person@example.com", "coordinator", "project", "task", "thread"} } func TestOutbound(t *testing.T) { good := Outbound{ Scope: scope(), Recipient: "person@example.com", Sender: "coordinator@example.com", SenderLabel: "Equipo coordinator", Configured: true, Authorized: true, Verified: true, Active: true, OptedIn: true, } if err := CheckOutbound(good); err != nil { t.Fatal(err) } for name, mutate := range map[string]func(*Outbound){ "unconfigured": func(o *Outbound) { o.Configured = false }, "unauthorized": func(o *Outbound) { o.Authorized = false }, "unverified": func(o *Outbound) { o.Verified = false }, "revoked": func(o *Outbound) { o.Active = false }, "opt out": func(o *Outbound) { o.OptedIn = false }, "bounce": func(o *Outbound) { o.Suppressed = true }, "other recipient": func(o *Outbound) { o.Recipient = "other@example.com" }, "multiple recipients": func(o *Outbound) { o.Recipient += ",other@example.com" }, "impersonation": func(o *Outbound) { o.Sender = o.Address }, "undisclosed agent": func(o *Outbound) { o.SenderLabel = "Paul" }, "header injection": func(o *Outbound) { o.Sender += "\r\nBcc: other@example.com" }, "missing thread": func(o *Outbound) { o.Thread = "" }, } { t.Run(name, func(t *testing.T) { o := good mutate(&o) if CheckOutbound(o) != ErrPolicy { t.Fatal("accepted unsafe outbound") } }) } if CheckOutbound(Outbound{}) != ErrPolicy { t.Fatal("zero value must fail closed") } } func TestReply(t *testing.T) { good := Reply{Expected: scope(), Bound: scope(), EventID: "event", TransportVerified: true, SenderBound: true, RouteVerified: true, Active: true} if err := CheckReply(good); err != nil { t.Fatal(err) } for name, mutate := range map[string]func(*Reply){ "forged webhook": func(r *Reply) { r.TransportVerified = false }, "From alone": func(r *Reply) { r.SenderBound = false }, "subject alone": func(r *Reply) { r.RouteVerified = false }, "revoked member": func(r *Reply) { r.Active = false }, "auto reply": func(r *Reply) { r.Automated = true }, "self loop": func(r *Reply) { r.FromCoordinator = true }, "missing event": func(r *Reply) { r.EventID = "" }, "other team": func(r *Reply) { r.Bound.Team = "other" }, "other person": func(r *Reply) { r.Bound.Person = "other" }, "other address": func(r *Reply) { r.Bound.Address = "other@example.com" }, "other coordinator": func(r *Reply) { r.Bound.Coordinator = "other" }, "other project": func(r *Reply) { r.Bound.Project = "other" }, "other task": func(r *Reply) { r.Bound.Task = "other" }, "other thread": func(r *Reply) { r.Bound.Thread = "other" }, } { t.Run(name, func(t *testing.T) { r := good mutate(&r) if CheckReply(r) != ErrPolicy { t.Fatal("accepted unsafe reply") } }) } if CheckReply(Reply{}) != ErrPolicy { t.Fatal("zero value must fail closed") } }