MCPcopy Index your code
hub / github.com/coder/coder / TestTrustedOrigins

Function TestTrustedOrigins

coderd/httpmw/realip_test.go:226–303  ·  view source on GitHub ↗

TestTrustedOrigins tests different settings for TrustedOrigins.

(t *testing.T)

Source from the content-addressed store, hash-verified

224
225// TestTrustedOrigins tests different settings for TrustedOrigins.
226func TestTrustedOrigins(t *testing.T) {
227 t.Parallel()
228
229 // Remote client protocol: HTTP or HTTPS
230 for _, proto := range []string{"http", "https"} {
231 // Trusted origin
232 // all: default behavior, trust all origins
233 // none: use an empty set (nothing will be accepted in this case)
234 // ipv4: trust an IPv6 network
235 // ipv6: trust an IPv4 network
236 for _, trusted := range []string{"none", "ipv4", "ipv6"} {
237 for _, header := range []string{"Cf-Connecting-Ip", "True-Client-Ip", "X-Real-Ip", "X-Forwarded-For"} {
238 name := fmt.Sprintf("%s-%s-%s", trusted, proto, strings.ToLower(header))
239
240 t.Run(name, func(t *testing.T) {
241 t.Parallel()
242
243 remoteAddr := "10.10.10.10"
244 actualAddr := "12.34.56.78"
245
246 config := &httpmw.RealIPConfig{
247 TrustedHeaders: []string{
248 "Cf-Connecting-Ip",
249 "X-Forwarded-For",
250 "X-Real-Ip",
251 "True-Client-Ip",
252 },
253 }
254 switch trusted {
255 case "none":
256 config.TrustedOrigins = []*net.IPNet{}
257 case "ipv4":
258 config.TrustedOrigins = []*net.IPNet{
259 {
260 IP: net.ParseIP("10.0.0.0"),
261 Mask: net.CIDRMask(24, 32),
262 },
263 }
264 remoteAddr = "10.0.0.1"
265 case "ipv6":
266 config.TrustedOrigins = []*net.IPNet{
267 {
268 IP: net.ParseIP("2606:4700::0"),
269 Mask: net.CIDRMask(32, 128),
270 },
271 }
272 remoteAddr = "2606:4700:4700::1111"
273 }
274
275 middleware := httpmw.ExtractRealIP(config)
276 req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
277 req.Header.Set(header, actualAddr)
278 req.RemoteAddr = remoteAddr
279 if proto == "https" {
280 req.TLS = &tls.ConnectionState{}
281 }
282
283 handlerCalled := false

Callers

nothing calls this directly

Calls 5

ExtractRealIPFunction · 0.92
RunMethod · 0.65
SetMethod · 0.65
EqualMethod · 0.45
ServeHTTPMethod · 0.45

Tested by

no test coverage detected