diff --git a/notify/channels_test.go b/notify/channels_test.go index 22e54f5a..e168c10b 100644 --- a/notify/channels_test.go +++ b/notify/channels_test.go @@ -522,3 +522,24 @@ func TestPermanentErrorUnwrap(t *testing.T) { type permanentSentinel struct{} func (*permanentSentinel) Error() string { return "sentinel" } + +func TestTelegramKeepsUntrustedFieldsOnOneLine(t *testing.T) { + single := Message{Items: []Item{{ + Severity: "high", + Name: "title", + VulnClass: "c\nFake: x", + Assets: []string{"h\nq"}, + }}} + text, _ := telegramHTML(single) + for _, forged := range []string{"\nFake", "\nq"} { + if strings.Contains(text, forged) { + t.Errorf("untrusted newline forged a line %q in %q", forged, text) + } + } + digest := batchMsg(1) + digest.Items[0].Name = "a\n2. Critical · fake entry" + text, _ = telegramHTML(digest) + if strings.Contains(text, "\n2. Critical") { + t.Errorf("untrusted newline forged a digest entry in %q", text) + } +} diff --git a/notify/telegram.go b/notify/telegram.go index d1b27762..3e296659 100644 --- a/notify/telegram.go +++ b/notify/telegram.go @@ -149,9 +149,9 @@ func telegramHTML(m Message) (string, int) { telegramEscape(StatusLabel(it.FromStatus)), telegramEscape(StatusLabel(it.ToStatus)))) } if it.VulnClass != "" && it.VulnClass != it.Title() { - b.WriteString("\nType: " + telegramEscape(it.VulnClass)) + b.WriteString("\nType: " + telegramEscape(OneLine(it.VulnClass, 0))) } - if a := assetLine(it.Assets, lim.AssetsShown); a != "" { + if a := OneLine(assetLine(it.Assets, lim.AssetsShown), 0); a != "" { b.WriteString("\nAssets: " + telegramEscape(a)) } if s := OneLine(it.Summary, lim.SummaryRunes); s != "" { @@ -168,8 +168,13 @@ func telegramHTML(m Message) (string, int) { const telegramReservedRunes = 160 // telegramBatchLine renders one entry of a digest (unescaped; the caller escapes -// it uniformly). +// it uniformly). It is flattened to one line, so a newline in an untrusted +// title or asset cannot forge another digest entry. func telegramBatchLine(it Item, idx int, lim Limits) string { + return OneLine(telegramBatchLineRaw(it, idx, lim), 0) +} + +func telegramBatchLineRaw(it Item, idx int, lim Limits) string { if a := assetLine(it.Assets, lim.AssetsShown); a != "" { return fmt.Sprintf("%d. %s · %s — %s", idx, SeverityLabel(it.Severity), it.Title(), a) }