[PR #6492] [MERGED] fix(tgbot): render the add-client draft as HTML and escape its values #6305

Closed
opened 2026-11-02 04:07:49 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6492
Author: @BlindMaster24
Created: 9/12/2026
Status: ✅ Merged
Merged: 9/13/2026
Merged by: @MHSanaei

Base: main ← Head: fix/tgbot-draft-html-render


📝 Commits (1)

  • 5668ab4 fix(tgbot): render the add-client draft as HTML and escape its values

📊 Changes

3 files changed (+157 additions, -8 deletions)

View changed files

📝 internal/web/service/tgbot/tgbot_client.go (+6 -5)
➕ internal/web/service/tgbot/tgbot_draft_render_test.go (+148 -0)
📝 internal/web/service/tgbot/tgbot_router.go (+3 -3)

📄 Description

Summary

The wizard's other two text steps — the email and comment prompts, which echo the
same draft values and delete the card before they are sent — are escaped too, so
the flow cannot end on an empty screen.

BuildClientDraftMessage — the card the bot shows while an admin fills in a new
client — was written in Markdown but is sent with ParseMode: "HTML", and its
admin-supplied values went into the message unescaped. The card now uses the
same markup as every other message and escapes the values it interpolates.

Why

addClient renders the draft through SendMsgToTgbot / editMessageTgBot, and
both set ParseMode: "HTML" (tgbot_send.go:151, tgbot_send.go:238). The
draft's title and email were written for Markdown instead:

b.WriteString("📝 *New client draft*\r\n")
fmt.Fprintf(&b, "📧 Email: `%s`\r\n", client_Email)

What the operator sees

On every card, always. The title renders as *New client draft* — literal
asterisks — and the email as `a@b.c` with literal backticks, while the rest
of the bot (the client info card, the subscription links, every paged report)
renders its emphasis and its monospace values correctly through <b>/<code>.
The add-client flow is the one screen written for the other parser.

On some cards, nothing at all. The values interpolated into the draft come
straight from what the admin typed: the comment, the TG id, the email, and — on
the Attached: line — the remarks of the inbounds the client is being attached
to. Telegram's HTML parser refuses the whole message when it meets an unescaped
reserved character, and an ampersand is enough:

  • Admin picks Add client, opens Change comment, and types Ali & Co (or
    paid <10 GB>, or attaches an inbound remarked A&B).
  • The draft is re-rendered with that text raw, Telegram answers
    400 Bad Request: can't parse entities, and SendMsgToTgbot logs one
    warning and gives up (tgbot_send.go:154).
  • The admin sees no new card and no error — the wizard looks frozen, and every
    later step that re-renders the same text fails the same way. The only way out
    is to change the comment to something without &, < or >, which nothing
    tells them.

The same mistake sat in the TG-id prompt of the same wizard
(tgbot_router.go:1081), which is plain text with Markdown backticks around
-, none and the current value; it is rendered through the same HTML parse
mode.

The sibling prompts

Two steps of the same wizard echo a draft value and delete the card before the
prompt goes out: add_client_ch_default_email and
add_client_ch_default_comment (tgbot_router.go:1057, :1067). Both
interpolated that value raw into an HTML-parsed prompt, so the symptom was the
same as the card's: an admin who kept a comment of Ali & Co, or typed an email
with a <, lost the prompt after the card was gone, with no error shown. Both
now escape the value they echo, exactly like the TG-id prompt in the same
wizard.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

How was this tested?

internal/web/service/tgbot/tgbot_draft_render_test.go sets the draft's own
fields to values that expose both halves — a comment of
<b>promo</b> & <10 GB> — and asserts on the emitted string: the title is
<b>New client draft</b>, no * or backtick marker survives anywhere in the
message, the raw comment markup does not reach it, and the escaped form
does. Both assertions fail on the old code.

TestAddClientPromptsEscapeDraftValues covers the two sibling prompts the same
way: it drives both wizard callbacks through answerCallback against a mocked
API with the draft set to long<name>@example.com / promo <b>tag</b>, and
asserts the prompt that reached Telegram carries the escaped form and not the raw
one. That assertion fails on the old code.

The remaining draft fields (traffic, expiry, IP limit) are generated by the bot
itself and are left unescaped on purpose; only the values an admin or an inbound
remark can carry are escaped.

I could not build or run the test locally: this machine has no C compiler, and
the CGo SQLite driver makes every package importing internal/database — this
one included — fail to compile without one (CONTRIBUTING.md documents the
MinGW-w64 install). gofmt is clean; CI is the build here.

Screenshots / recordings

N/A — bot messages, no UI. The change is textual: *New client draft* becomes
New client draft, `email` becomes email in monospace, and a comment
of Ali & Co now delivers the card instead of being refused.

Breaking changes

None. Same cards, same fields, same buttons; only the markup they are written in
changes, and a comment that used to break the message now renders.

Checklist

  • I tested the change locally and confirmed the described behavior. — no C compiler on this machine, see above; CI runs the new test.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally. — same reason as above.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass. — no frontend files touched.
  • I updated the Wiki / README / API docs if user-facing behavior changed. — no endpoint or wire-format change.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6492 **Author:** [@BlindMaster24](https://github.com/BlindMaster24) **Created:** 9/12/2026 **Status:** ✅ Merged **Merged:** 9/13/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/tgbot-draft-html-render` --- ### 📝 Commits (1) - [`5668ab4`](https://github.com/MHSanaei/3x-ui/commit/5668ab42562cc37fa5677e37b3bea20e6c00f8c6) fix(tgbot): render the add-client draft as HTML and escape its values ### 📊 Changes **3 files changed** (+157 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/tgbot/tgbot_client.go` (+6 -5) ➕ `internal/web/service/tgbot/tgbot_draft_render_test.go` (+148 -0) 📝 `internal/web/service/tgbot/tgbot_router.go` (+3 -3) </details> ### 📄 Description ## Summary The wizard's other two text steps — the email and comment prompts, which echo the same draft values and delete the card before they are sent — are escaped too, so the flow cannot end on an empty screen. `BuildClientDraftMessage` — the card the bot shows while an admin fills in a new client — was written in Markdown but is sent with `ParseMode: "HTML"`, and its admin-supplied values went into the message unescaped. The card now uses the same markup as every other message and escapes the values it interpolates. ## Why `addClient` renders the draft through `SendMsgToTgbot` / `editMessageTgBot`, and both set `ParseMode: "HTML"` (`tgbot_send.go:151`, `tgbot_send.go:238`). The draft's title and email were written for Markdown instead: ```go b.WriteString("📝 *New client draft*\r\n") fmt.Fprintf(&b, "📧 Email: `%s`\r\n", client_Email) ``` ### What the operator sees **On every card, always.** The title renders as `*New client draft*` — literal asterisks — and the email as `` `a@b.c` `` with literal backticks, while the rest of the bot (the client info card, the subscription links, every paged report) renders its emphasis and its monospace values correctly through `<b>`/`<code>`. The add-client flow is the one screen written for the other parser. **On some cards, nothing at all.** The values interpolated into the draft come straight from what the admin typed: the comment, the TG id, the email, and — on the `Attached:` line — the remarks of the inbounds the client is being attached to. Telegram's HTML parser refuses the whole message when it meets an unescaped reserved character, and an ampersand is enough: - Admin picks *Add client*, opens *Change comment*, and types `Ali & Co` (or `paid <10 GB>`, or attaches an inbound remarked `A&B`). - The draft is re-rendered with that text raw, Telegram answers `400 Bad Request: can't parse entities`, and `SendMsgToTgbot` logs one warning and gives up (`tgbot_send.go:154`). - The admin sees no new card and no error — the wizard looks frozen, and every later step that re-renders the same text fails the same way. The only way out is to change the comment to something without `&`, `<` or `>`, which nothing tells them. The same mistake sat in the TG-id prompt of the same wizard (`tgbot_router.go:1081`), which is plain text with Markdown backticks around `-`, `none` and the current value; it is rendered through the same HTML parse mode. ### The sibling prompts Two steps of the same wizard echo a draft value and delete the card before the prompt goes out: `add_client_ch_default_email` and `add_client_ch_default_comment` (`tgbot_router.go:1057`, `:1067`). Both interpolated that value raw into an HTML-parsed prompt, so the symptom was the same as the card's: an admin who kept a comment of `Ali & Co`, or typed an email with a `<`, lost the prompt after the card was gone, with no error shown. Both now escape the value they echo, exactly like the TG-id prompt in the same wizard. ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [ ] Frontend (UI / panel pages) - [ ] Backend (API endpoints, login, settings) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [x] Telegram bot ## How was this tested? `internal/web/service/tgbot/tgbot_draft_render_test.go` sets the draft's own fields to values that expose both halves — a comment of `<b>promo</b> & <10 GB>` — and asserts on the emitted string: the title is `<b>New client draft</b>`, no `*` or backtick marker survives anywhere in the message, the raw comment markup does **not** reach it, and the escaped form does. Both assertions fail on the old code. `TestAddClientPromptsEscapeDraftValues` covers the two sibling prompts the same way: it drives both wizard callbacks through `answerCallback` against a mocked API with the draft set to `long<name>@example.com` / `promo <b>tag</b>`, and asserts the prompt that reached Telegram carries the escaped form and not the raw one. That assertion fails on the old code. The remaining draft fields (traffic, expiry, IP limit) are generated by the bot itself and are left unescaped on purpose; only the values an admin or an inbound remark can carry are escaped. I could not build or run the test locally: this machine has no C compiler, and the CGo SQLite driver makes every package importing `internal/database` — this one included — fail to compile without one (`CONTRIBUTING.md` documents the MinGW-w64 install). `gofmt` is clean; CI is the build here. ## Screenshots / recordings N/A — bot messages, no UI. The change is textual: `*New client draft*` becomes **New client draft**, `` `email` `` becomes `email` in monospace, and a comment of `Ali & Co` now delivers the card instead of being refused. ## Breaking changes None. Same cards, same fields, same buttons; only the markup they are written in changes, and a comment that used to break the message now renders. ## Checklist - [ ] I tested the change locally and confirmed the described behavior. — no C compiler on this machine, see above; CI runs the new test. - [x] I added or updated tests for the new behavior (when applicable). - [ ] `go build ./...` and the test suite pass locally. — same reason as above. - [ ] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. — no frontend files touched. - [ ] I updated the Wiki / README / API docs if user-facing behavior changed. — no endpoint or wire-format change. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:07:49 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6305
No description provided.