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:
1 parent
3a0cc64160
commit
996201c4ed
11 files changed
+432
-127
No files matched your search
@@ -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), " "))
|
||||
}
|
||||
@@ -1,18 +1,22 @@
|
||||
package telegram
|
||||
|
||||
// 本文件钉住 Bot API 出站调用的两条出镜边界(M41 #12/#13 那一族):
|
||||
// 本文件钉住 Bot API 出站调用的三条出镜边界(M41 #12/#13 那一族,M50 补 description):
|
||||
//
|
||||
// - **非 200 的响应体原文不进错误**:旧实现把 512B 体原文当 description 拼进
|
||||
// error,而 `ReadMessages` 会以 `long poll failed` 落日志 —— 上游在体里回显什么,
|
||||
// 日志里就有什么。现在只解 error_code 与 description(清洗、截断后)。
|
||||
// 日志里就有什么。现在只解 error_code 与 HTTP status。
|
||||
// - **description 默认不带**:它是上游自由文本,Telegram 会回显我们发过去的参数
|
||||
// (里面就有她的话)。要带必须显式 opt-in;本适配器不开。
|
||||
// - **地址里的 bot token 不进错误**:Bot API 的地址是 /bot<token>/<method>,
|
||||
// 传输层失败时 net/http 的 *url.Error 会把整条地址拼进错误串。
|
||||
//
|
||||
// 两条都是「改回去就变红」的形状:断言的是错误文本 —— 也就是真正被写进日志的字符串。
|
||||
// 三条都是「改回去就变红」的形状:断言的是错误文本 —— 也就是真正被写进日志的字符串。
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -28,14 +32,15 @@ func (failingTransport) RoundTrip(*http.Request) (*http.Response, error) {
|
||||
|
||||
// TestDoJSON_StatusErrorDoesNotEchoBodySentinel 4xx 体里放哨兵 ⇒ 错误里搜不到。
|
||||
//
|
||||
// 哨兵刻意放在 description 之外的字段:description 按设计要保留(那是 Telegram 对
|
||||
// 这次请求的说明),而「体里其余字节一个字都不拼」才是要钉住的口径。
|
||||
// 哨兵同时放在 description 之外与 description 之内:description 是上游自由文本面,
|
||||
// **默认不带**(M50),因此两个位置的哨兵都必须搜不到;留下的诊断面是 status 与
|
||||
// error_code。
|
||||
func TestDoJSON_StatusErrorDoesNotEchoBodySentinel(t *testing.T) {
|
||||
t.Parallel()
|
||||
const sentinel = "SENTINEL-TG-BODY"
|
||||
a, _ := newTestAdapter(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
_, _ = w.Write([]byte(`{"ok":false,"error_code":400,"description":"Bad Request: chat not found",` +
|
||||
_, _ = w.Write([]byte(`{"ok":false,"error_code":400,"description":"Bad Request: can't parse entities: ` + sentinel + `",` +
|
||||
`"echoed_params":{"text":"` + sentinel + `"}}`))
|
||||
}))
|
||||
|
||||
@@ -46,20 +51,26 @@ func TestDoJSON_StatusErrorDoesNotEchoBodySentinel(t *testing.T) {
|
||||
if !errors.Is(err, ErrBadResponse) {
|
||||
t.Fatalf("err = %v, want ErrBadResponse", err)
|
||||
}
|
||||
if strings.Contains(err.Error(), sentinel) {
|
||||
t.Fatalf("响应体其余字段进了错误(会随 long poll failed 落日志):%v", err)
|
||||
var logBuf bytes.Buffer
|
||||
slog.New(slog.NewJSONHandler(&logBuf, nil)).Error("long poll failed", "err", err)
|
||||
for _, surface := range []string{err.Error(), logBuf.String()} {
|
||||
if strings.Contains(surface, sentinel) {
|
||||
t.Fatalf("响应体原文(含 description 回显)进了错误或日志:%s", surface)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(err.Error(), "error_code 400") || !strings.Contains(err.Error(), "chat not found") {
|
||||
t.Fatalf("error_code/description 丢了(诊断面不能一起丢):%v", err)
|
||||
// 变异:把 description 拼回 parts ⇒ 上面那条立刻红。
|
||||
if !strings.Contains(err.Error(), "error_code 400") {
|
||||
t.Fatalf("error_code 丢了(诊断面不能一起丢):%v", err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "status 400") {
|
||||
t.Fatalf("HTTP 状态码丢了:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDoJSON_StatusErrorRedactsDescriptionSecrets description 是上游自由文本,而
|
||||
// setWebhook 失败时 Telegram 会把我们给的地址回显在里面 —— 地址的 query 常带凭据。
|
||||
// 这类赋值形状必须被清洗。改回原样透传就变红。
|
||||
// TestDoJSON_StatusErrorRedactsDescriptionSecrets 上一族里的凭据形态:description 现在
|
||||
// 整段不带,因此 setWebhook 回显的地址(query 常带凭据)也一并不能出镜。这条与
|
||||
// TestDoJSON_StatusErrorDoesNotEchoBodySentinel 是同一处代码的两个变异方向 —— 一个钉
|
||||
// 「正文不进来」,一个钉「凭据不出去」。
|
||||
func TestDoJSON_StatusErrorRedactsDescriptionSecrets(t *testing.T) {
|
||||
t.Parallel()
|
||||
const secret = "WEBHOOK-SECRET-VALUE"
|
||||
@@ -76,6 +87,9 @@ func TestDoJSON_StatusErrorRedactsDescriptionSecrets(t *testing.T) {
|
||||
if strings.Contains(err.Error(), secret) {
|
||||
t.Fatalf("description 里的凭据原样进了错误:%v", err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "error_code 400") || !strings.Contains(err.Error(), "status 400") {
|
||||
t.Fatalf("剥掉 description 时把分类信息一起丢了:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDoJSON_TransportErrorOmitsBotToken 传输层失败时,错误里不得出现 bot token ——
|
||||
@@ -130,10 +144,10 @@ func TestDoJSON_BuildErrorOmitsBotToken(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestDoJSON_DescriptionIsBoundedByRedactText 钉住长度口径:description 只有一道
|
||||
// 清洗(trace.RedactText,80 rune + 省略号)。本片曾按 200 rune 自己再截一刀,那层
|
||||
// 永远命中不了 —— 这条用例让「到底哪一层在截」变成可断言的事实。
|
||||
func TestDoJSON_DescriptionIsBoundedByRedactText(t *testing.T) {
|
||||
// TestDoJSON_DescriptionIsDropped 钉住「description 默认不带」:无论它多长、写的是什么,
|
||||
// 错误里都只剩 status + error_code。变异:把 description 拼回 badResponseError 的
|
||||
// parts ⇒ 本用例立刻红(`aaaa…` 会出现)。
|
||||
func TestDoJSON_DescriptionIsDropped(t *testing.T) {
|
||||
t.Parallel()
|
||||
long := strings.Repeat("a", 300)
|
||||
a, _ := newTestAdapter(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -145,10 +159,10 @@ func TestDoJSON_DescriptionIsBoundedByRedactText(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatal("400 却报成功")
|
||||
}
|
||||
if !strings.Contains(err.Error(), strings.Repeat("a", 80)+"…") {
|
||||
t.Fatalf("description 没有按 80 rune 截断:%v", err)
|
||||
if strings.Contains(err.Error(), "aaaa") {
|
||||
t.Fatalf("description 进了错误(M50 口径是默认不带):%v", err)
|
||||
}
|
||||
if strings.Contains(err.Error(), strings.Repeat("a", 81)) {
|
||||
t.Fatalf("description 超过了 80 rune 上限:%v", err)
|
||||
if !strings.Contains(err.Error(), "status 400") || !strings.Contains(err.Error(), "error_code 400") {
|
||||
t.Fatalf("剥掉 description 时把分类信息一起丢了:%v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user