Skip to content

Commit ebeaa71

Browse files
author
Yue Yin
committed
fix: Add IPv6 support for signing HTTP request
Add in a new function isIPv6() for IPv6 host check.
1 parent 18f21c3 commit ebeaa71

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ require (
1414
github.com/vmware/vmw-guestinfo v0.0.0-20170707015358-25eff159a728
1515
)
1616

17-
require github.com/kr/text v0.1.0 // indirect
17+
require github.com/kr/text v0.1.0 // indirect

sts/signer.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"io"
3131
"io/ioutil"
3232
mrand "math/rand"
33+
"net"
3334
"net/http"
3435
"net/url"
3536
"strings"
@@ -274,11 +275,18 @@ func (s *Signer) SignRequest(req *http.Request) error {
274275
}
275276

276277
var buf bytes.Buffer
278+
host := req.URL.Hostname()
279+
280+
// Check if the host IP is in IPv6 format. If yes, add the opening and closing square brackets.
281+
if isIPv6(host) {
282+
host = fmt.Sprintf("%s%s%s", "[", host, "]")
283+
}
284+
277285
msg := []string{
278286
nonce,
279287
req.Method,
280288
req.URL.Path,
281-
strings.ToLower(req.URL.Hostname()),
289+
strings.ToLower(host),
282290
port,
283291
}
284292
for i := range msg {
@@ -329,3 +337,11 @@ func (s *Signer) NewRequest() TokenRequest {
329337
KeyID: s.keyID,
330338
}
331339
}
340+
341+
func isIPv6(s string) bool {
342+
ip := net.ParseIP(s)
343+
if ip == nil {
344+
return false
345+
}
346+
return len(ip) == net.IPv6len
347+
}

sts/signer_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,10 @@ func TestSigner(t *testing.T) {
133133
if err != nil {
134134
t.Fatal(err)
135135
}
136+
137+
req, _ = http.NewRequest(http.MethodPost, "https://[0:0:0:0:0:0:0:1]", nil)
138+
err = s.SignRequest(req)
139+
if err != nil {
140+
t.Fatal(err)
141+
}
136142
}

0 commit comments

Comments
 (0)