mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-12-06 00:03:11 +00:00
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:
1 parent
e449cc700e
commit
a2defd2a82
2 files changed
+21
-8
No files matched your search
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user