Skip to content

Commit a2defd2

Browse files
authored
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.
1 parent e449cc7 commit a2defd2

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

‎relaykit/relayconvert/kitutil/mask.go‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,9 @@ func MaskSensitiveInfo(str string, upstreamURLs ...string) string {
123123
}
124124
// Mask URLs on an upstream host, and the query values of every other URL
125125
str = maskURLPattern.ReplaceAllStringFunc(str, func(match string) string {
126-
urlStr := match
127-
for urlStr != "" {
128-
last := urlStr[len(urlStr)-1]
129-
if strings.IndexByte(".,;:!?'", last) < 0 && (last != ')' || strings.Count(urlStr, "(") >= strings.Count(urlStr, ")")) {
130-
break
131-
}
132-
urlStr = urlStr[:len(urlStr)-1]
133-
}
126+
// Trailing punctuation belongs to the surrounding text and is kept
127+
// outside the masked URL.
128+
urlStr := strings.TrimRight(match, ".,;:!?')")
134129
suffix := match[len(urlStr):]
135130
u, err := url.Parse(urlStr)
136131
if err != nil {

‎relaykit/relayconvert/kitutil/mask_test.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ func TestMaskSensitiveInfoMasksOnlyTheUpstreamAddress(t *testing.T) {
4949
input: `failed to download https://cdn.example/image(1).png?token=secret (status 403)`,
5050
want: `failed to download https://cdn.example/image(1).png?token=*** (status 403)`,
5151
},
52+
{
53+
name: "trailing parenthesis stays outside the masked URL",
54+
upstreamURLs: nil,
55+
input: `(see https://cdn.example/a?sig)`,
56+
want: `(see https://cdn.example/a?sig=***)`,
57+
},
58+
{
59+
name: "mixed trailing punctuation and parentheses are trimmed together",
60+
upstreamURLs: nil,
61+
input: `(https://cdn.example/a?token=1&sig). ('https://cdn.example/a?sig') (https://cdn.example/a?sig.)`,
62+
want: `(https://cdn.example/a?sig=***&token=***). ('https://cdn.example/a?sig=***') (https://cdn.example/a?sig=***.)`,
63+
},
64+
{
65+
name: "trailing parentheses on an upstream URL",
66+
upstreamURLs: []string{"https://wiki.upstream.example.com"},
67+
input: `(see https://wiki.upstream.example.com/Go_(lang)?k=v)).`,
68+
want: `(see https://***.com/***?k=***)).`,
69+
},
5270
{
5371
name: "apostrophes inside a URL keep its query and userinfo masked",
5472
upstreamURLs: nil,

0 commit comments

Comments
 (0)