diff --git a/alertmanager.go b/alertmanager.go index 84569a4..62fe70f 100644 --- a/alertmanager.go +++ b/alertmanager.go @@ -4,7 +4,9 @@ import ( "crypto/sha256" "encoding/hex" "fmt" + "regexp" "sort" + "strconv" "strings" "time" ) @@ -36,8 +38,13 @@ type Alert struct { } // maxBodyLen stays under GitHub's 65536-character limit on an issue body, -// with room for the truncation note. -const maxBodyLen = 60000 +// with room for the truncation note. maxFieldLen caps any one fenced value, +// so a single huge label or annotation cannot use up the whole body before +// the first alert, or push it past the limit so the issue is never created. +const ( + maxBodyLen = 60000 + maxFieldLen = 8000 +) // groupID identifies an alert group across notifications. Alertmanager's // groupKey is stable for a group on one route, so its hash is what an open @@ -137,6 +144,12 @@ func body(m *Message, id string) string { return b.String() } +// With every fenced field bounded by fence(), the header (group labels, +// common annotations, URL) is at most about 3 x maxFieldLen, and the +// per-alert check above stops adding alerts at maxBodyLen, so the body stays +// under GitHub's 65,536-character limit. TestBodyIsCapped and +// TestHugeHeaderStillUnderLimit hold it to that. + func kv(m map[string]string) string { var b strings.Builder for _, k := range sortedKeys(m) { @@ -146,8 +159,15 @@ func kv(m map[string]string) string { } // fence wraps s in a code fence longer than any backtick run inside it, so -// the content cannot close the fence early and escape into Markdown. +// the content cannot close the fence early and escape into Markdown. Both +// the content and the fence are bounded: s is cut to maxFieldLen first, and +// any backtick run longer than maxTicks is shortened to maxTicks, so a field +// of nothing but backticks cannot make the fence itself enormous. func fence(s string) string { + if len(s) > maxFieldLen { + s = strings.ToValidUTF8(s[:maxFieldLen], "") + "\n[truncated]\n" + } + s = longTicks.ReplaceAllString(s, strings.Repeat("`", maxTicks)) longest, run := 0, 0 for _, r := range s { if r == '`' { @@ -166,6 +186,10 @@ func fence(s string) string { return f + "text\n" + s + f + "\n" } +const maxTicks = 10 + +var longTicks = regexp.MustCompile("`{" + strconv.Itoa(maxTicks+1) + ",}") + func sortedKeys(m map[string]string) []string { keys := make([]string, 0, len(m)) for k := range m { diff --git a/receiver.go b/receiver.go index 4057a93..034bb06 100644 --- a/receiver.go +++ b/receiver.go @@ -139,9 +139,12 @@ func (r *Receiver) find(ctx context.Context, id string) (*Issue, error) { if err != nil { return nil, err } - mk := marker(id) + // The marker must OPEN the body, as body() writes it. Anywhere else it + // could be alert text quoting another group's marker, which would + // redirect this group's updates and closure to that group's issue. + mk := marker(id) + "\n" for i := range open { - if strings.Contains(open[i].Body, mk) { + if strings.HasPrefix(open[i].Body, mk) { return &open[i], nil } } diff --git a/receiver_test.go b/receiver_test.go index 40e8ee5..796cf9e 100644 --- a/receiver_test.go +++ b/receiver_test.go @@ -348,3 +348,52 @@ func TestGroupIDStable(t *testing.T) { t.Fatal("groupID not stable or not receiver-scoped") } } + +func TestHugeHeaderStillUnderLimit(t *testing.T) { + big := strings.Repeat("y", 100000) + m := Message{ + Status: "firing", + GroupLabels: map[string]string{"alertname": "X", "a": big}, + CommonAnnotations: map[string]string{"summary": big}, + ExternalURL: big, + Alerts: []Alert{alert("firing", "db-0")}, + } + if b := body(&m, "id"); len(b) > 65536 { + t.Fatalf("body %d chars, over GitHub's limit", len(b)) + } +} + +func TestQuotedMarkerDoesNotCaptureAnotherGroup(t *testing.T) { + f, _, r := setup(t) + // Another group's issue whose alert text quotes OUR group's marker. + var m Message + json.Unmarshal(msg("firing", alert("firing", "db-0")), &m) + f.issues = append(f.issues, &fakeIssue{Number: 1, State: "open", Labels: []string{"alert"}, + Body: marker("other") + "\n\n```text\n" + marker(groupID(&m)) + "\n```\n"}) + post(t, r, msg("firing", alert("firing", "db-0"))) + if len(f.issues) != 2 { + t.Fatalf("quoted marker matched: want a new issue, have %d", len(f.issues)) + } + post(t, r, msg("resolved", alert("resolved", "db-0"))) + if f.issues[0].State != "open" || f.issues[1].State != "closed" { + t.Fatalf("closed the wrong issue: %s %s", f.issues[0].State, f.issues[1].State) + } +} + +func TestBacktickFloodStaysBounded(t *testing.T) { + ticks := strings.Repeat("`", 200000) + m := Message{ + Status: "firing", + GroupLabels: map[string]string{"alertname": "X", "a": ticks}, + CommonAnnotations: map[string]string{"summary": ticks, "description": ticks}, + ExternalURL: ticks, + Alerts: []Alert{alert("firing", "db-0")}, + } + b := body(&m, "id") + if len(b) > 65536 { + t.Fatalf("body %d chars, over GitHub's limit", len(b)) + } + if !strings.Contains(b, "db-0") { + t.Fatal("the alert itself was crowded out") + } +}