From a2defd2a8256abe039e5295bd583aeb2d02f111f Mon Sep 17 00:00:00 2001 From: RedwindA <128586631+RedwindA@users.noreply.github.com> Date: Fri, 30 Oct 2026 00:55:16 +0800 Subject: [PATCH] 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. --- relaykit/relayconvert/kitutil/mask.go | 11 +++-------- relaykit/relayconvert/kitutil/mask_test.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 8 deletions(-) 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,