-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
private_ip_validator_test.go
125 lines (111 loc) · 3.07 KB
/
private_ip_validator_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright © 2022 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package httpx
import (
"net"
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIsAssociatedIPAllowed(t *testing.T) {
for _, disallowed := range []string{
"localhost",
"https://localhost/foo?bar=baz#zab",
"127.0.0.0",
"127.255.255.255",
"172.16.0.0",
"172.31.255.255",
"192.168.0.0",
"192.168.255.255",
"10.0.0.0",
"0.0.0.0",
"10.255.255.255",
"::1",
} {
t.Run("case="+disallowed, func(t *testing.T) {
require.Error(t, DisallowIPPrivateAddresses(disallowed))
})
}
}
func TestDisallowLocalIPAddressesWhenSet(t *testing.T) {
require.NoError(t, DisallowIPPrivateAddresses(""))
require.Error(t, DisallowIPPrivateAddresses("127.0.0.1"))
require.ErrorAs(t, DisallowIPPrivateAddresses("127.0.0.1"), new(ErrPrivateIPAddressDisallowed))
}
type noOpRoundTripper struct{}
func (n noOpRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
return &http.Response{}, nil
}
var _ http.RoundTripper = new(noOpRoundTripper)
func TestAllowExceptions(t *testing.T) {
rt := &NoInternalIPRoundTripper{internalIPExceptions: []string{"http://localhost/asdf"}}
_, err := rt.RoundTrip(&http.Request{
Host: "localhost",
URL: &url.URL{Scheme: "http", Path: "/asdf", Host: "localhost"},
Header: http.Header{
"Host": []string{"localhost"},
},
})
// assert that the error is eiher nil or a dial error.
if err != nil {
opErr := new(net.OpError)
require.ErrorAs(t, err, &opErr)
require.Equal(t, "dial", opErr.Op)
}
_, err = rt.RoundTrip(&http.Request{
Host: "localhost",
URL: &url.URL{Scheme: "http", Path: "/not-asdf", Host: "localhost"},
Header: http.Header{
"Host": []string{"localhost"},
},
})
require.Error(t, err)
}
func assertErrorContains(msg string) assert.ErrorAssertionFunc {
return func(t assert.TestingT, err error, i ...interface{}) bool {
if !assert.Error(t, err, i...) {
return false
}
return assert.Contains(t, err.Error(), msg)
}
}
func TestNoInternalDialer(t *testing.T) {
for _, tt := range []struct {
name string
network string
address string
assertErr assert.ErrorAssertionFunc
}{{
name: "TCP public is allowed",
network: "tcp",
address: "www.google.de:443",
assertErr: assert.NoError,
}, {
name: "TCP private is denied",
network: "tcp",
address: "localhost:443",
assertErr: assertErrorContains("is not a public IP address"),
}, {
name: "UDP public is denied",
network: "udp",
address: "www.google.de:443",
assertErr: assertErrorContains("not a safe network type"),
}, {
name: "UDP public is denied",
network: "udp",
address: "www.google.de:443",
assertErr: assertErrorContains("not a safe network type"),
}, {
name: "UNIX sockets are denied",
network: "unix",
address: "/etc/passwd",
assertErr: assertErrorContains("not a safe network type"),
}} {
t.Run("case="+tt.name, func(t *testing.T) {
_, err := NoInternalDialer.Dial(tt.network, tt.address)
tt.assertErr(t, err)
})
}
}