Skip to content

Commit

Permalink
introduce structure BadIPError and BadHostError
Browse files Browse the repository at this point in the history
  • Loading branch information
astj committed Apr 12, 2018
1 parent eda4d4a commit c82c87c
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@ import (
// DefaultClient is the default Client whose setting is the same as http.DefaultClient.
var DefaultClient *http.Client

// BadIPError is returned when requested address is not permitted by paranoidhttp.
type BadIPError struct {
IP net.IP
}

func (e *BadIPError) Error() string {
return fmt.Sprintf("bad ip is detected: %v", e.IP)
}

// BadHostError is returned when requested hostname is not permitted by paranoidhttp.
type BadHostError struct {
hostname string
}

func (e *BadHostError) Error() string {
return fmt.Sprintf("bad host is detected: %v", e.hostname)
}

func newBadIPError(ip net.IP) error {
return &BadIPError{ip}
}

func newBadHostError(hostname string) error {
return &BadHostError{hostname}
}

func mustParseCIDR(addr string) *net.IPNet {
_, ipnet, err := net.ParseCIDR(addr)
if err != nil {
Expand Down Expand Up @@ -43,13 +69,13 @@ func safeAddr(ctx context.Context, resolver *net.Resolver, hostport string) (str
ip := net.ParseIP(host)
if ip != nil {
if ip.To4() != nil && isBadIPv4(ip) {
return "", fmt.Errorf("bad ip is detected: %v", ip)
return "", newBadIPError(ip)
}
return net.JoinHostPort(ip.String(), port), nil
}

if isBadHost(host) {
return "", fmt.Errorf("bad host is detected: %v", host)
return "", newBadHostError(host)
}

r := resolver
Expand All @@ -62,7 +88,7 @@ func safeAddr(ctx context.Context, resolver *net.Resolver, hostport string) (str
}
for _, addr := range addrs {
if addr.IP.To4() != nil && isBadIPv4(addr.IP) {
return "", fmt.Errorf("bad ip is detected: %v", addr.IP)
return "", newBadIPError(addr.IP)
}
}
return net.JoinHostPort(addrs[0].IP.String(), port), nil
Expand Down

0 comments on commit c82c87c

Please sign in to comment.