From cda87d8dfca0aacf36eb3d4b02af963bb64900ba Mon Sep 17 00:00:00 2001
From: Marco Azarmaneche
<306425250+thecorporatetractor@users.noreply.github.com>
Date: Thu, 3 Dec 2026 03:07:51 +0200
Subject: [PATCH] fix(notify): keep untrusted Telegram fields on one line
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Slack flattens titles, types and assets with OneLine so a newline in an
untrusted field cannot forge another line (TestSlackKeepsUntrustedFieldsOnOneLine).
Telegram did not: a finding named "a\n2. Critical · fake entry" added a
forged entry to a digest, and a newline in the vulnerability class or an
asset added a forged line to a single notification. Flatten the digest
line and the Type and Assets fields the same way.
---
notify/channels_test.go | 21 +++++++++++++++++++++
notify/telegram.go | 11 ++++++++---
2 files changed, 29 insertions(+), 3 deletions(-)
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)
}