From b91fc85b2a60a0a8a9575e7976c5c211e5639d58 Mon Sep 17 00:00:00 2001 From: Andrew Landsverk Date: Thu, 12 Nov 2026 21:01:59 -0600 Subject: [PATCH 1/2] Match the group marker only at the start of the body; cap each field Alert text is written inside fences, but a fence does not stop a substring match: an alert quoting another group's marker made that group's updates and closure land on this issue. body() always writes the marker first, so require it there. Header fields (group labels, common annotations, the URL) were not counted against the size cap, so one huge annotation could push the body past GitHub's limit and the issue would never be created. Each fenced value is now capped at 8000 characters. Co-Authored-By: Claude Opus 5.5 --- alertmanager.go | 16 ++++++++++++++-- receiver.go | 7 +++++-- receiver_test.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/alertmanager.go b/alertmanager.go index 84569a4..42b6172 100644 --- a/alertmanager.go +++ b/alertmanager.go @@ -36,8 +36,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 +142,10 @@ func body(m *Message, id string) string { return b.String() } +// The header (group labels, common annotations, URL) is at most three +// fields of maxFieldLen plus a little text, so with the per-alert check above +// the body stays under GitHub's limit; TestBodyIsCapped holds it to that. + func kv(m map[string]string) string { var b strings.Builder for _, k := range sortedKeys(m) { @@ -159,6 +168,9 @@ func fence(s string) string { run = 0 } } + if len(s) > maxFieldLen { + s = strings.ToValidUTF8(s[:maxFieldLen], "") + "\n[truncated]\n" + } f := strings.Repeat("`", max(3, longest+1)) if !strings.HasSuffix(s, "\n") { s += "\n" 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..e1f7293 100644 --- a/receiver_test.go +++ b/receiver_test.go @@ -348,3 +348,34 @@ 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) + } +} From 6931ea513144f25082538d9ea91fe8c66b1da715 Mon Sep 17 00:00:00 2001 From: Andrew Landsverk Date: Thu, 12 Nov 2026 21:04:25 -0600 Subject: [PATCH 2/2] Bound backtick runs so a fence cannot grow with its content The fence was sized from the untruncated content, so a field of 200,000 backticks produced a 200,001-backtick fence and a body far over GitHub's limit. Truncate first, and shorten any run over 10 backticks to 10, so a fence is at most 11 characters. Co-Authored-By: Claude Opus 5.5 --- alertmanager.go | 26 +++++++++++++++++++------- receiver_test.go | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/alertmanager.go b/alertmanager.go index 42b6172..62fe70f 100644 --- a/alertmanager.go +++ b/alertmanager.go @@ -4,7 +4,9 @@ import ( "crypto/sha256" "encoding/hex" "fmt" + "regexp" "sort" + "strconv" "strings" "time" ) @@ -142,9 +144,11 @@ func body(m *Message, id string) string { return b.String() } -// The header (group labels, common annotations, URL) is at most three -// fields of maxFieldLen plus a little text, so with the per-alert check above -// the body stays under GitHub's limit; TestBodyIsCapped holds it to that. +// 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 @@ -155,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 == '`' { @@ -168,9 +179,6 @@ func fence(s string) string { run = 0 } } - if len(s) > maxFieldLen { - s = strings.ToValidUTF8(s[:maxFieldLen], "") + "\n[truncated]\n" - } f := strings.Repeat("`", max(3, longest+1)) if !strings.HasSuffix(s, "\n") { s += "\n" @@ -178,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_test.go b/receiver_test.go index e1f7293..796cf9e 100644 --- a/receiver_test.go +++ b/receiver_test.go @@ -379,3 +379,21 @@ func TestQuotedMarkerDoesNotCaptureAnotherGroup(t *testing.T) { 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") + } +}