fix(httpclient): 信封 message 默认不进错误,渠道上游自由文本一并收口

信封路径(非 2xx + 信封体、2xx + code!=0)此前把上游 message 经 RedactText
拼进 error,而上游会在 message 里回显被拒的请求正文 —— 她的原话因此进了错误
与调用方日志(M49 探针实测:code 1: invalid content: 她刚才说的那句话 ...)。

- envelope message 改为**默认不带**(新 opt-in WithEnvelopeMessage(),与
  trace.UpstreamError 的 message 同一口径);code 与上游 status 保留,
  errors.Is 的 ErrAPI / trace.ErrUpstream 链不变。
- opt-in 下统一走单一清洗 upstreamText:压平空白 + trace.RedactTextLimited,
  上限 40 rune(比 trace 的 128 短:错误里那句话是提示,不是摘要)。
- plugins/channels 同族出口一并收口(扫过全部,命中 4 处):
  telegram description、qq_obv11 msg(错误 + logEchoFailure 日志)、
  weixin_ilink CDN x-error-message;qq 的 status 字段改走 trace.ErrorToken
  令牌守卫。被剥掉的一律由 status / error_code / retcode 承担分类。

每条新增/改写用例都做过变异验证(把字段拼回去即变红)。
This commit is contained in:
AskaEth committed 2026-10-26 19:28:15 +08:00
1 parent 3a0cc64160
commit 996201c4ed
11 files changed
+432 -127

No files matched your search

+17 -14
View File
@@ -23,6 +23,7 @@ type botResponse struct {
}
// maxErrorBodyBytes caps how much of a non-200 reply is read before classifying it.
// Only error_code is taken from it (description is dropped — see badResponseError).
const maxErrorBodyBytes = 512
// doJSON posts a JSON payload to a Bot API method and decodes the envelope. The
@@ -63,6 +64,13 @@ func (a *Adapter) doJSON(ctx context.Context, method string, payload any) (*botR
return nil, fmt.Errorf("telegram: decode %s response: %w", method, err)
}
if !out.OK {
// 200 + `ok:false` 也是失败。这一支没有 status 可报(HTTP 恒 200),于是把
// error_code 带上 —— 它是**分类令牌**,不是正文通道,与上面非 200 分支同一口径。
// description 在这里同样**默认不带**:Telegram 会在里面回显我们发过去的参数
// (M50 口径,见 badResponseError)。
if out.ErrorCode != 0 {
return nil, fmt.Errorf("telegram: %s: error_code %d: %w", method, out.ErrorCode, ErrBadResponse)
}
return nil, fmt.Errorf("telegram: %s: %w", method, ErrBadResponse)
}
return &out, nil
@@ -210,12 +218,16 @@ func parseReplyToID(s string) int64 {
return n
}
// badResponseError 把一次非 200 的回复收敛成错误:**只**取 error_code 与
// description。响应体其余字段(以及 Telegram 回显的请求参数)一个字都不拼 ——
// 旧实现把 512B 体原文当 desc 拼进来,等于把上游报文写进调用方的日志。
// badResponseError 把一次非 200 的回复收敛成错误:**只**取 status 与 error_code。
// 响应体其余字段(以及 Telegram 回显的请求参数)一个字都不拼 —— 旧实现把 512B 体原文
// 当 desc 拼进来,等于把上游报文写进调用方的日志。
//
// description 是上游自由文本(例如 setWebhook 失败时会回显我们给的地址),因此过
// trace.RedactText 清洗凭据形状并按 rune 截断后才出镜。
// 🔴 description 默认**不带**(M50):它是上游自由文本,Telegram 会回显我们发过去的
// 参数,而那些参数里就有她的话(`Bad Request: can't parse entities: <正文>` 是真实
// 形状)。口径与 `trace.UpstreamError` 的 message 一致 —— 默认安全,要带必须显式
// opt-in;本适配器不开这个开关,因为 status + error_code 已足够分类(400/403/429)。
// 将来若真要带,请回到 httpclient 那条单一清洗口径(`trace` 的唯一打码 + 40 rune,
// 见 internal/channel/adapter/httpclient/upstream_text.go),不要在这里另写截断。
func badResponseError(method string, status int, body []byte) error {
var env botResponse
_ = json.Unmarshal(body, &env)
@@ -223,14 +235,5 @@ func badResponseError(method string, status int, body []byte) error {
if env.ErrorCode != 0 {
parts = append(parts, fmt.Sprintf("error_code %d", env.ErrorCode))
}
if desc := boundedDescription(env.Description); desc != "" {
parts = append(parts, desc)
}
return fmt.Errorf("%s: %w", strings.Join(parts, ": "), ErrBadResponse)
}
// boundedDescription 是 Bot API description 进入错误前的唯一处理:压平空白,再交给
// trace.RedactText —— 它既清洗凭据形状,也把长度压到 80 rune(超出的带省略号)。
func boundedDescription(s string) string {
return trace.RedactText(strings.Join(strings.Fields(s), " "))
}