Merge commit from fork

MaskSensitiveInfo trimmed trailing punctuation one byte at a time and
recounted the parentheses of the whole URL for each trailing ')'. Trim
the punctuation set, including ')', with strings.TrimRight; the trimmed
characters are still appended after the masked URL.
This commit is contained in:
RedwindA authored and GitHub committed 2026-10-30 00:55:16 +08:00
1 parent e449cc700e
commit a2defd2a82
2 files changed
+21 -8

No files matched your search

+3 -8
View File
@@ -123,14 +123,9 @@ func MaskSensitiveInfo(str string, upstreamURLs ...string) string {
}
// Mask URLs on an upstream host, and the query values of every other URL
str = maskURLPattern.ReplaceAllStringFunc(str, func(match string) string {
urlStr := match
for urlStr != "" {
last := urlStr[len(urlStr)-1]
if strings.IndexByte(".,;:!?'", last) < 0 && (last != ')' || strings.Count(urlStr, "(") >= strings.Count(urlStr, ")")) {
break
}
urlStr = urlStr[:len(urlStr)-1]
}
// Trailing punctuation belongs to the surrounding text and is kept
// outside the masked URL.
urlStr := strings.TrimRight(match, ".,;:!?')")
suffix := match[len(urlStr):]
u, err := url.Parse(urlStr)
if err != nil {
@@ -49,6 +49,24 @@ func TestMaskSensitiveInfoMasksOnlyTheUpstreamAddress(t *testing.T) {
input: `failed to download https://cdn.example/image(1).png?token=secret (status 403)`,
want: `failed to download https://cdn.example/image(1).png?token=*** (status 403)`,
},
{
name: "trailing parenthesis stays outside the masked URL",
upstreamURLs: nil,
input: `(see https://cdn.example/a?sig)`,
want: `(see https://cdn.example/a?sig=***)`,
},
{
name: "mixed trailing punctuation and parentheses are trimmed together",
upstreamURLs: nil,
input: `(https://cdn.example/a?token=1&sig). ('https://cdn.example/a?sig') (https://cdn.example/a?sig.)`,
want: `(https://cdn.example/a?sig=***&token=***). ('https://cdn.example/a?sig=***') (https://cdn.example/a?sig=***.)`,
},
{
name: "trailing parentheses on an upstream URL",
upstreamURLs: []string{"https://wiki.upstream.example.com"},
input: `(see https://wiki.upstream.example.com/Go_(lang)?k=v)).`,
want: `(see https://***.com/***?k=***)).`,
},
{
name: "apostrophes inside a URL keep its query and userinfo masked",
upstreamURLs: nil,