Skip to content

Commit

Permalink
httputil: fix ParseIP usage
Browse files Browse the repository at this point in the history
The string fed into the ParseIP function needs to not have a port.
This does that and adds a test to check the desired behavior.

Closes: #1689
Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Feb 14, 2023
1 parent 12f38e4 commit b18f989
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
13 changes: 11 additions & 2 deletions internal/httputil/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,23 @@ func ctlLocalOnly(network, address string, _ syscall.RawConn) error {
Err: "disallowed by policy",
}
}
addr := net.ParseIP(address)
host, _, err := net.SplitHostPort(address)
if err != nil {
return &net.AddrError{
Addr: network + "!" + address,
Err: "martian address",
}
}
addr := net.ParseIP(host)
if addr == nil {
return &net.AddrError{
Addr: network + "!" + address,
Err: "martian address",
}
}
if !addr.IsPrivate() {
if !addr.IsPrivate() &&
!addr.IsLoopback() &&
!addr.IsLinkLocalUnicast() {
return &net.AddrError{
Addr: network + "!" + address,
Err: "disallowed by policy",
Expand Down
57 changes: 57 additions & 0 deletions internal/httputil/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package httputil

import (
"errors"
"net"
"testing"
)

func TestLocalOnly(t *testing.T) {
tt := []struct {
Network string
Addr string
Err *net.AddrError
}{
{
Network: "tcp4",
Addr: "192.168.0.1:443",
Err: nil,
},
{
Network: "tcp4",
Addr: "127.0.0.1:443",
Err: nil,
},
{
Network: "tcp6",
Addr: "[fe80::]:443",
Err: nil,
},
{
Network: "tcp4",
Addr: "8.8.8.8:443",
Err: &net.AddrError{
Addr: "tcp4!8.8.8.8:443",
Err: "disallowed by policy",
},
},
{
Network: "tcp6",
Addr: "[2000::]:443",
Err: &net.AddrError{
Addr: "tcp6![2000::]:443",
Err: "disallowed by policy",
},
},
}
for _, tc := range tt {
t.Logf("%s!%s", tc.Network, tc.Addr)
var nErr *net.AddrError
got := ctlLocalOnly(tc.Network, tc.Addr, nil)
if errors.As(got, &nErr) {
if tc.Err.Err != nErr.Err || tc.Err.Addr != nErr.Addr {
t.Errorf("got: %v, want: %v", got, tc.Err)
}
}
}
}

0 comments on commit b18f989

Please sign in to comment.