diff --git a/relaykit/relayconvert/kitutil/mask.go b/relaykit/relayconvert/kitutil/mask.go index 6e9b49d535..bb84b2bf36 100644 --- a/relaykit/relayconvert/kitutil/mask.go +++ b/relaykit/relayconvert/kitutil/mask.go @@ -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 { diff --git a/relaykit/relayconvert/kitutil/mask_test.go b/relaykit/relayconvert/kitutil/mask_test.go index 84d3421cca..8e12770f2a 100644 --- a/relaykit/relayconvert/kitutil/mask_test.go +++ b/relaykit/relayconvert/kitutil/mask_test.go @@ -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,