Skip to content

Commit 42f591a

Browse files
committed
Remove all URL params
1 parent a8b6409 commit 42f591a

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

client.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -910,16 +910,16 @@ func (c *Client) StandardClient() *http.Client {
910910
}
911911
}
912912

913-
// Taken from url.URL#Redacted() which was introduced in go 1.15.
914-
// We can switch to using it directly if we'll bump the minimum required go version.
913+
// Should be aligned with wiz-sec/wiz/commonlib/anonymize/anonymize.go#RedactURL()
915914
func redactURL(u *url.URL) string {
916915
if u == nil {
917916
return ""
918917
}
919918

920919
ru := *u
921-
if _, has := ru.User.Password(); has {
922-
ru.User = url.UserPassword(ru.User.Username(), "xxxxx")
923-
}
924-
return ru.String()
920+
921+
// Remove query as it might contain secrets, e.g. presigned URLs
922+
ru.RawQuery = ""
923+
ru.ForceQuery = false
924+
return ru.Redacted()
925925
}

client_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"errors"
1010
"fmt"
1111
"io"
12+
"log"
1213
"net"
1314
"net/http"
1415
"net/http/httptest"
@@ -1283,3 +1284,37 @@ func TestClient_RedirectWithBody(t *testing.T) {
12831284
t.Fatalf("Expected the client to be redirected 2 times, got: %d", atomic.LoadInt32(&redirects))
12841285
}
12851286
}
1287+
1288+
func TestUrlQueryParamsAreRedactedFromLogs(t *testing.T) {
1289+
ts := serveFailOnceServer()
1290+
defer ts.Close()
1291+
1292+
logBuffer := &bytes.Buffer{}
1293+
logger := log.New(logBuffer, "", log.LstdFlags)
1294+
client := NewClient()
1295+
client.Logger = logger
1296+
1297+
resp, err := client.Get(ts.URL + "?X-Amz-Credential=SECRET")
1298+
if err != nil {
1299+
t.Fatalf("err: %v", err)
1300+
}
1301+
resp.Body.Close()
1302+
1303+
actualLogs := string(logBuffer.Bytes())
1304+
if strings.Contains(actualLogs, "SECRET") {
1305+
t.Fatalf("log contains SECRET that should have been redacted: %s", actualLogs)
1306+
}
1307+
}
1308+
1309+
func serveFailOnceServer() *httptest.Server {
1310+
var retries int32 = 0
1311+
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1312+
if atomic.LoadInt32(&retries) == 0 {
1313+
w.WriteHeader(500)
1314+
} else {
1315+
w.WriteHeader(200)
1316+
}
1317+
1318+
atomic.AddInt32(&retries, 1)
1319+
}))
1320+
}

0 commit comments

Comments
 (0)