fix(tgbot): escape template parameters once in I18nBot

Every bot message is sent HTML-parsed while the intended markup lives
in the translation templates, yet the values filled into them were
escaped only at some call sites. Remarks and e-mails, which a node
chooses for the inbounds it reports, reached the scheduled report and
the client info unescaped and turned into markup, and the reports that
escaped their whole text instead showed the templates' own tags.

I18nBot now escapes every parameter value, the call sites drop their
own escaping and the whole-report escapes, the two values concatenated
outside a template are escaped where they are written, and callback
answers, which are plain text, are unescaped again.
This commit is contained in:
MHSanaei
2026-11-01 08:58:43 +01:00
parent a9191cb468
commit fac9009398
10 changed files with 88 additions and 172 deletions
@@ -6,12 +6,14 @@ import (
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/logger"
"github.com/mymmrac/telego"
tu "github.com/mymmrac/telego/telegoutil"
"github.com/nicksnyder/go-i18n/v2/i18n"
)
func TestIsTelegramNotModifiedError(t *testing.T) {
@@ -136,3 +138,37 @@ func TestPageMessageSplitsLinkListWithoutBlankLines(t *testing.T) {
t.Errorf("pages contain %d links, want 50", links)
}
}
// Regression test: callback answers are shown as plain text, so the entities
// I18nBot escapes for HTML-parsed messages must not surface in them.
func TestCallbackAnswerIsPlainText(t *testing.T) {
var mu sync.Mutex
var texts []string
mock := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/bot"+testBotToken+"/answerCallbackQuery" {
w.WriteHeader(http.StatusNotFound)
return
}
var body struct {
Text string `json:"text"`
}
_ = json.NewDecoder(r.Body).Decode(&body)
mu.Lock()
texts = append(texts, body.Text)
mu.Unlock()
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{"ok": true, "result": true})
}))
defer mock.Close()
swapTestBot(t, mock.URL)
draftLocalizer(t, &i18n.Message{ID: "tgbot.answers.canceled", Other: "❌ {{ .Email }}: Operation canceled."})
tb := &Tgbot{}
tb.sendCallbackAnswerTgBot("q1", tb.I18nBot("tgbot.answers.canceled", "Email==a&b<c>"))
mu.Lock()
defer mu.Unlock()
if len(texts) != 1 || texts[0] != "❌ a&b<c>: Operation canceled." {
t.Errorf("answerCallbackQuery texts = %q, want the e-mail shown verbatim", texts)
}
}