fix(voice/tts): strip query from scheme-less URLs in redactURL

redactURL's fallback returned the raw string when url.Parse found no host.
A scheme-less BaseURL (127.0.0.1:9880/v1?token=...) fails to parse, so the
signed query reached the WARN log and the error chain verbatim. Strip at
? and # in that path, keeping the '@' full-redaction rule first.

Test pins the scheme-less query/fragment cases and goes red if the fix
is reverted.
This commit is contained in:
AskaEth committed 2026-10-26 21:54:21 +08:00
1 parent a5b3d40a74
commit 28a6cd7e53
2 files changed
+58 -10

No files matched your search

+36 -1
View File
@@ -6,7 +6,8 @@ package tts
// 里的 `input` 整段回显,所以「把错误体前 512 字节打进日志」= 把她的原话写进
// 日志。这里用一台**回显正文**的假上游 + 一个接日志缓冲区的 logger 来抓它。
// 2. `redactURL` 的兜底路径:地址里含 `@`(userinfo)时必须整体打码,哪怕 URL
// 本身不合法 —— 否则 `user:pass@` 会把凭据原样写进日志。
// 本身不合法 —— 否则 `user:pass@` 会把凭据原样写进日志;而**漏了 scheme** 的
// 地址(`127.0.0.1:9880/v1?token=…`)走的是同一条兜底,query 必须被切掉。
import (
"bytes"
@@ -121,3 +122,37 @@ func TestRedactURLCredentials(t *testing.T) {
})
}
}
// TestRedactURLNoSchemeDropsQuery 钉住 M55 的缺口:**漏了 scheme** 的地址
// (`127.0.0.1:9880/v1?token=…`)在 `url.Parse` 里是错误 —— 首段带 `:` ——
// 于是 host 为空、走兜底。旧兜底把原串连 query 一起返回,签名/token 就这么进了
// 日志与工单。现在兜底只按 `?`/`#` 切掉查询串与片段。
//
// 变异:把兜底的 `stripURLQuery(raw)` 改回 `raw` ⇒ 前两条用例必红(secret 与
// `token=` 都会出现);把 stripURLQuery 改成只切 `?` ⇒ 第二条用例留下 `#frag`。
func TestRedactURLNoSchemeDropsQuery(t *testing.T) {
const secret = "TTS-SECRET-7b3e"
cases := []struct {
name string
in string
want string
}{
{"无 scheme 带 query", "127.0.0.1:9880/v1/audio/speech?token=" + secret, "127.0.0.1:9880/v1/audio/speech"},
{"无 scheme 带 query 与片段", "tts.internal:9880/v1?token=" + secret + "#frag", "tts.internal:9880/v1"},
{"无 scheme 无 query 原样", "127.0.0.1:9880", "127.0.0.1:9880"},
{"无 scheme 带 userinfo ⇒ 整体打码", "user:pass@host/v1?token=" + secret, "<redacted>"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := redactURL(c.in)
if got != c.want {
t.Fatalf("redactURL(%q) = %q, want %q", c.in, got, c.want)
}
for _, leak := range []string{secret, "token=", "pass"} {
if strings.Contains(got, leak) {
t.Fatalf("可打日志的地址里出现了 %q: %q", leak, got)
}
}
})
}
}