Skip to content

Commit

Permalink
feat: helper to extract ip address that isn't on an exclusion list (#593
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kelkarajay authored Sep 8, 2022
1 parent 3d0b2e4 commit 5819a6c
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 9 deletions.
34 changes: 34 additions & 0 deletions httpx/client_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package httpx

import (
"net"
"strings"
)

func GetClientIPAddress(ipAddresses []string, exclude []string) (string, error) {
var res string

for i := len(ipAddresses) - 1; i >= 0; i-- {
var isExcluded bool
ip := strings.TrimSpace(ipAddresses[i])

for _, j := range exclude {
_, cidr, err := net.ParseCIDR(j)
if err != nil {
return "", err
}

if cidr.Contains(net.ParseIP(ip)) {
isExcluded = true
break
}
}

if !isExcluded {
res = ip
break
}
}

return res, nil
}
22 changes: 22 additions & 0 deletions httpx/client_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package httpx

import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"strings"
"testing"
)

func TestIgnoresInternalIPs(t *testing.T) {
input := "54.155.246.232,10.145.1.10"

res, err := GetClientIPAddress(strings.Split(input, ","), InternalIPSet)
require.NoError(t, err)
assert.Equal(t, "54.155.246.232", res)
}

func TestEmptyInputArray(t *testing.T) {
res, err := GetClientIPAddress([]string{}, InternalIPSet)
require.NoError(t, err)
assert.Equal(t, "", res)
}
20 changes: 11 additions & 9 deletions httpx/private_ip_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ import (
"github.com/pkg/errors"
)

var InternalIPSet = []string{
"127.0.0.0/8",
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16",
"fd47:1ed0:805d:59f0::/64",
"fc00::/7",
"::1/128",
}

// ErrPrivateIPAddressDisallowed is returned when a private IP address is disallowed.
type ErrPrivateIPAddressDisallowed error

Expand Down Expand Up @@ -64,15 +74,7 @@ func DisallowIPPrivateAddresses(ipOrHostnameOrURL string) error {
ips = append(ips, ip)
}

for _, disabled := range []string{
"127.0.0.0/8",
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16",
"fd47:1ed0:805d:59f0::/64",
"fc00::/7",
"::1/128",
} {
for _, disabled := range InternalIPSet {
_, cidr, err := net.ParseCIDR(disabled)
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions stringsx/ptr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package stringsx

func GetPointer(s string) *string {
return &s
}
11 changes: 11 additions & 0 deletions stringsx/ptr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package stringsx

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestGetPointer(t *testing.T) {
s := "TestString"
assert.Equal(t, &s, GetPointer(s))
}

0 comments on commit 5819a6c

Please sign in to comment.