Skip to content

Commit 11551c3

Browse files
authored
feat(enhancement)!: do not strip the port from the request URL host for redirect host comparison (#1159)
1 parent db80558 commit 11551c3

3 files changed

Lines changed: 8 additions & 18 deletions

File tree

client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func TestClientRedirectPolicy(t *testing.T) {
115115
ts := createRedirectServer(t)
116116
defer ts.Close()
117117

118-
c := dcnl().SetRedirectPolicy(RedirectFlexiblePolicy(20), RedirectDomainCheckPolicy("127.0.0.1"))
118+
c := dcnl().SetRedirectPolicy(RedirectFlexiblePolicy(20), RedirectDomainCheckPolicy(ts.URL[len("http://"):]))
119119
res, err := c.R().
120120
SetHeader("Name1", "Value1").
121121
SetHeader("Name2", "Value2").

redirect.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package resty
88
import (
99
"errors"
1010
"fmt"
11-
"net"
11+
"maps"
1212
"net/http"
1313
"strings"
1414
)
@@ -76,8 +76,8 @@ func RedirectDomainCheckPolicy(hostnames ...string) RedirectPolicy {
7676
}
7777

7878
return RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error {
79-
if ok := hosts[getHostname(req.URL.Host)]; !ok {
80-
return errors.New("redirect is not allowed as per DomainCheckRedirectPolicy")
79+
if ok := hosts[strings.ToLower(req.URL.Host)]; !ok {
80+
return errors.New("resty: redirect is not allowed as per DomainCheckRedirectPolicy")
8181
}
8282
checkHostAndAddHeaders(req, via[0])
8383
return nil
@@ -123,14 +123,6 @@ func RedirectHeaderStripSensitivePolicy(applyDefault bool, headers ...string) Re
123123
})
124124
}
125125

126-
func getHostname(host string) (hostname string) {
127-
if strings.Index(host, ":") > 0 {
128-
host, _, _ = net.SplitHostPort(host)
129-
}
130-
hostname = strings.ToLower(host)
131-
return
132-
}
133-
134126
// By default, Golang will not redirect request headers.
135127
// After reading through the various discussion comments from the thread -
136128
// https://github.com/golang/go/issues/4800
@@ -143,12 +135,10 @@ func getHostname(host string) (hostname string) {
143135
// (e.g. those set via [Client.SetHeaderAuthorizationKey]) are forwarded
144136
// verbatim unless explicitly removed. See https://github.com/go-resty/resty/issues/1128.
145137
func checkHostAndAddHeaders(cur *http.Request, pre *http.Request) {
146-
curHostname := getHostname(cur.URL.Host)
147-
preHostname := getHostname(pre.URL.Host)
138+
curHostname := strings.ToLower(cur.URL.Host)
139+
preHostname := strings.ToLower(pre.URL.Host)
148140
if strings.EqualFold(curHostname, preHostname) {
149-
for key, val := range pre.Header {
150-
cur.Header[key] = val
151-
}
141+
maps.Copy(cur.Header, pre.Header)
152142
} else {
153143
// Cross-domain redirect: strip sensitive headers that Go's
154144
// net/http does not know about (custom auth, token, api-key, etc.).

request_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ func TestHostCheckRedirectPolicy(t *testing.T) {
975975
defer ts.Close()
976976

977977
c := dcnl().
978-
SetRedirectPolicy(RedirectDomainCheckPolicy("127.0.0.1"))
978+
SetRedirectPolicy(RedirectDomainCheckPolicy(ts.URL[len("http://"):]))
979979

980980
_, err := c.R().Get(ts.URL + "/redirect-host-check-1")
981981

0 commit comments

Comments
 (0)