From fb37b278d2042be1808eae7b4c80d873f4d52acd Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 2 Dec 2024 17:24:58 -0600 Subject: [PATCH] escape MD symbols in user-name for reported bans #174 --- app/events/admin.go | 4 +++- app/events/admin_test.go | 37 +++++++++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/app/events/admin.go b/app/events/admin.go index c04c678..7939b50 100644 --- a/app/events/admin.go +++ b/app/events/admin.go @@ -45,7 +45,9 @@ func (a *admin) ReportBan(banUserStr string, msg *bot.Message) { if a.dry { would = "would have " } - forwardMsg := fmt.Sprintf("**%spermanently banned [%s](tg://user?id=%d)**\n\n%s\n\n", would, banUserStr, msg.From.ID, text) + + forwardMsg := fmt.Sprintf("**%spermanently banned [%s](tg://user?id=%d)**\n\n%s\n\n", + would, escapeMarkDownV1Text(banUserStr), msg.From.ID, text) if err := a.sendWithUnbanMarkup(forwardMsg, "change ban", msg.From, msg.ID, a.adminChatID); err != nil { log.Printf("[WARN] failed to send admin message, %v", err) } diff --git a/app/events/admin_test.go b/app/events/admin_test.go index 2ab3b6d..c0a752e 100644 --- a/app/events/admin_test.go +++ b/app/events/admin_test.go @@ -30,16 +30,33 @@ func TestAdmin_reportBan(t *testing.T) { Text: "Test\n\n_message_", } - adm.ReportBan("testUser", msg) - - require.Equal(t, 1, len(mockAPI.SendCalls())) - t.Logf("sent text: %+v", mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).Text) - assert.Equal(t, int64(123), mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).ChatID) - assert.Contains(t, mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).Text, "permanently banned [testUser](tg://user?id=456)") - assert.Contains(t, mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).Text, "Test \\_message\\_") - assert.NotNil(t, mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).ReplyMarkup) - assert.Equal(t, "⛔︎ change ban", - mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).ReplyMarkup.(tbapi.InlineKeyboardMarkup).InlineKeyboard[0][0].Text) + t.Run("normal user name", func(t *testing.T) { + mockAPI.ResetCalls() + adm.ReportBan("testUser", msg) + + require.Equal(t, 1, len(mockAPI.SendCalls())) + t.Logf("sent text: %+v", mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).Text) + assert.Equal(t, int64(123), mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).ChatID) + assert.Contains(t, mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).Text, "permanently banned [testUser](tg://user?id=456)") + assert.Contains(t, mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).Text, "Test \\_message\\_") + assert.NotNil(t, mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).ReplyMarkup) + assert.Equal(t, "⛔︎ change ban", + mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).ReplyMarkup.(tbapi.InlineKeyboardMarkup).InlineKeyboard[0][0].Text) + }) + + t.Run("name with md chars", func(t *testing.T) { + mockAPI.ResetCalls() + adm.ReportBan("test_User", msg) + + require.Equal(t, 1, len(mockAPI.SendCalls())) + t.Logf("sent text: %+v", mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).Text) + assert.Equal(t, int64(123), mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).ChatID) + assert.Contains(t, mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).Text, "permanently banned [test\\_User](tg://user?id=456)") + assert.Contains(t, mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).Text, "Test \\_message\\_") + assert.NotNil(t, mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).ReplyMarkup) + assert.Equal(t, "⛔︎ change ban", + mockAPI.SendCalls()[0].C.(tbapi.MessageConfig).ReplyMarkup.(tbapi.InlineKeyboardMarkup).InlineKeyboard[0][0].Text) + }) } func TestAdmin_getCleanMessage(t *testing.T) {