@@ -50,7 +50,7 @@ func TestListenAndServe(t *testing.T) {
5050 verifyStatus (t , s , StatusClosed )
5151}
5252
53- func TestListenAndServeRejectsPlainConnectionsWhenProxyProtocolEnabled (t * testing.T ) {
53+ func TestListenAndServe_proxyProtocolEnabled (t * testing.T ) {
5454 target , err := net .ResolveTCPAddr ("tcp" , serverUrl )
5555 require .NoError (t , err )
5656
@@ -70,10 +70,11 @@ func TestListenAndServeRejectsPlainConnectionsWhenProxyProtocolEnabled(t *testin
7070 }()
7171
7272 testCases := []struct {
73- desc string
74- proxyPolicy string
75- header * proxyproto.Header
76- isRejected bool
73+ desc string
74+ proxyPolicy string
75+ proxyAllowed []string
76+ header * proxyproto.Header
77+ isRejected bool
7778 }{
7879 {
7980 desc : "USE (default) without a header" ,
@@ -123,11 +124,65 @@ func TestListenAndServeRejectsPlainConnectionsWhenProxyProtocolEnabled(t *testin
123124 header : header ,
124125 isRejected : false ,
125126 },
127+ {
128+ desc : "Allow-listed IP with a header" ,
129+ proxyAllowed : []string {"127.0.0.1" },
130+ header : header ,
131+ isRejected : false ,
132+ },
133+ {
134+ desc : "Allow-listed IP without a header" ,
135+ proxyAllowed : []string {"127.0.0.1" },
136+ header : nil ,
137+ isRejected : false ,
138+ },
139+ {
140+ desc : "Allow-listed range with a header" ,
141+ proxyAllowed : []string {"127.0.0.0/24" },
142+ header : header ,
143+ isRejected : false ,
144+ },
145+ {
146+ desc : "Allow-listed range without a header" ,
147+ proxyAllowed : []string {"127.0.0.0/24" },
148+ header : nil ,
149+ isRejected : false ,
150+ },
151+ {
152+ desc : "Not allow-listed IP with a header" ,
153+ proxyAllowed : []string {"192.168.1.1" },
154+ header : header ,
155+ isRejected : true ,
156+ },
157+ {
158+ desc : "Not allow-listed IP without a header" ,
159+ proxyAllowed : []string {"192.168.1.1" },
160+ header : nil ,
161+ isRejected : false ,
162+ },
163+ {
164+ desc : "Not allow-listed range with a header" ,
165+ proxyAllowed : []string {"192.168.1.0/24" },
166+ header : header ,
167+ isRejected : true ,
168+ },
169+ {
170+ desc : "Not allow-listed range without a header" ,
171+ proxyAllowed : []string {"192.168.1.0/24" },
172+ header : nil ,
173+ isRejected : false ,
174+ },
126175 }
127176
128177 for _ , tc := range testCases {
129178 t .Run (tc .desc , func (t * testing.T ) {
130- setupServerWithConfig (t , & config.Config {Server : config.ServerConfig {ProxyProtocol : true , ProxyPolicy : tc .proxyPolicy }})
179+ setupServerWithConfig (t , & config.Config {
180+ Server : config.ServerConfig {
181+ ProxyProtocol : true ,
182+ ProxyPolicy : tc .proxyPolicy ,
183+ ProxyAllowed : tc .proxyAllowed ,
184+ },
185+ })
131186
132187 conn , err := net .DialTCP ("tcp" , nil , target )
133188 require .NoError (t , err )
0 commit comments