Skip to content

Commit e158f04

Browse files
authored
fix: incorrect https warning on credential flow (#972)
1 parent 51c16e1 commit e158f04

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

client_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ func TestClientLogCallbacks(t *testing.T) {
933933
Get(ts.URL + "/profile")
934934
assertNil(t, err)
935935
assertNotNil(t, resp)
936-
assertEqual(t, int64(50), resp.Size())
936+
assertEqual(t, int64(66), resp.Size())
937937
assertEqual(t, true, strings.Contains(lb.String(), "Overwriting an existing on-debug-log callback from=resty.dev/v3.TestClientLogCallbacks.func1 to=resty.dev/v3.TestClientLogCallbacks.func2"))
938938
}
939939

middleware.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func addCredentials(c *Client, r *Request) error {
286286
}
287287

288288
if !c.IsDisableWarn() && credentialsAdded {
289-
if strings.HasPrefix(r.URL, "http") {
289+
if r.RawRequest.URL.Scheme == "http" {
290290
r.log.Warnf("Using sensitive credentials in HTTP mode is not secure. Use HTTPS")
291291
}
292292
}

request_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -2381,6 +2381,43 @@ func TestRequestFuncs(t *testing.T) {
23812381
assertEqual(t, "TestGet: text response", resp.String())
23822382
}
23832383

2384+
func TestHTTPWarnGH970(t *testing.T) {
2385+
lookupText := "Using sensitive credentials in HTTP mode is not secure. Use HTTPS"
2386+
2387+
t.Run("SSL used", func(t *testing.T) {
2388+
ts := createAuthServerTLSOptional(t, true)
2389+
defer ts.Close()
2390+
2391+
c, lb := dcldb()
2392+
c.SetBaseURL(ts.URL).
2393+
SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
2394+
2395+
res, err := c.R().
2396+
SetAuthToken("004DDB79-6801-4587-B976-F093E6AC44FF").
2397+
Get("/profile")
2398+
2399+
assertNil(t, err)
2400+
assertEqual(t, true, strings.Contains(res.String(), "profile fetch successful"))
2401+
assertEqual(t, false, strings.Contains(lb.String(), lookupText))
2402+
})
2403+
2404+
t.Run("non-SSL used", func(t *testing.T) {
2405+
ts := createAuthServerTLSOptional(t, false)
2406+
defer ts.Close()
2407+
2408+
c, lb := dcldb()
2409+
c.SetBaseURL(ts.URL)
2410+
2411+
res, err := c.R().
2412+
SetAuthToken("004DDB79-6801-4587-B976-F093E6AC44FF").
2413+
Get("/profile")
2414+
2415+
assertNil(t, err)
2416+
assertEqual(t, true, strings.Contains(res.String(), "profile fetch successful"))
2417+
assertEqual(t, true, strings.Contains(lb.String(), lookupText))
2418+
})
2419+
}
2420+
23842421
// This test methods exist for test coverage purpose
23852422
// to validate the getter and setter
23862423
func TestRequestSettingsCoverage(t *testing.T) {

resty_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ func createAuthServerTLSOptional(t *testing.T, useTLS bool) *httptest.Server {
546546
}
547547

548548
if strings.Contains(auth, "004DDB79-6801-4587-B976-F093E6AC44FF") {
549-
_, _ = w.Write([]byte(`{ "id": "success", "message": "login successful" }`))
549+
_, _ = w.Write([]byte(`{ "username": "auth_test", "message": "profile fetch successful" }`))
550550
}
551551
}
552552

0 commit comments

Comments
 (0)