mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-28 02:40:53 +08:00
Merge pull request #561 from chaitanya0305/fix/telegram-digest-pack
fix(notify): pack Telegram digests by their escaped text
This commit is contained in:
@@ -102,6 +102,35 @@ func TestTelegramPackingUsesRuneBudget(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestTelegramPackingCountsEscapedText covers digest lines that carry HTML
|
||||
// metacharacters (an XSS title, a query string with &). They grow when escaped,
|
||||
// so packing must measure the escaped text: every item reported as kept has to
|
||||
// appear whole in the message, and the footer must survive.
|
||||
func TestTelegramPackingCountsEscapedText(t *testing.T) {
|
||||
m := batchMsg(200)
|
||||
m.WindowMinutes = 60
|
||||
m.HomeURL = "https://artex.example.com/"
|
||||
for i := range m.Items {
|
||||
m.Items[i].Name = "Reflected XSS <script> in q param of /search, vuln" + itoa(i+1) + " end"
|
||||
m.Items[i].Assets = []string{"https://shop.example.com/search?q=1&lang=en"}
|
||||
}
|
||||
text, kept := telegramHTML(m)
|
||||
if n := utf8.RuneCountInString(text); n > telegramTextLimit {
|
||||
t.Fatalf("body of %d characters exceeds the limit of %d", n, telegramTextLimit)
|
||||
}
|
||||
if kept <= 0 || kept >= len(m.Items) {
|
||||
t.Fatalf("only a part should fit, got %d", kept)
|
||||
}
|
||||
for i := 0; i < kept; i++ {
|
||||
if !strings.Contains(text, "vuln"+itoa(i+1)+" end") {
|
||||
t.Fatalf("item %d of %d reported as kept is not whole in the message:\n...%s", i+1, kept, text[max(0, len(text)-300):])
|
||||
}
|
||||
}
|
||||
if !strings.Contains(text, "View all on the platform</a>") {
|
||||
t.Fatalf("the footer was cut off:\n...%s", text[max(0, len(text)-300):])
|
||||
}
|
||||
}
|
||||
|
||||
func TestFeishuPackingReportsKept(t *testing.T) {
|
||||
m := batchMsg(2000)
|
||||
_, kept := feishuCard(m)
|
||||
|
||||
+4
-1
@@ -125,8 +125,11 @@ func telegramHTML(m Message) (string, int) {
|
||||
if m.HomeURL != "" {
|
||||
footer = fmt.Sprintf("\n\n<a href=\"%s\">View all on the platform</a>", telegramEscapeAttr(m.HomeURL))
|
||||
}
|
||||
// Each line is measured as it is written below: escaped, with its
|
||||
// newline. Measuring the raw line let an escaped digest outgrow the
|
||||
// budget, and TruncateHTML then cut items that were counted as sent.
|
||||
kept := packItemCount(m.Items, telegramTextLimit, telegramReservedRunes, footer, runeSize, func(it Item, idx int) string {
|
||||
return telegramBatchLine(it, idx+1, lim)
|
||||
return "\n" + telegramEscape(telegramBatchLine(it, idx+1, lim))
|
||||
})
|
||||
items := m.Items[:kept]
|
||||
b.Reset()
|
||||
|
||||
Reference in New Issue
Block a user