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

Function TestFilterUntrusted

coderd/httpmw/realip_test.go:421–474  ·  view source on GitHub ↗

TestFilterUntrusted tests that untrusted headers are removed from the request.

(t *testing.T)

Source from the content-addressed store, hash-verified

419
420// TestFilterUntrusted tests that untrusted headers are removed from the request.
421func TestFilterUntrusted(t *testing.T) {
422 t.Parallel()
423
424 tests := []struct {
425 Name string
426 Config *httpmw.RealIPConfig
427 Header http.Header
428 RemoteAddr string
429 ExpectedHeader http.Header
430 ExpectedRemoteAddr string
431 }{
432 {
433 Name: "untrusted-origin",
434 Config: &httpmw.RealIPConfig{
435 TrustedOrigins: nil,
436 TrustedHeaders: []string{
437 "Cf-Connecting-Ip",
438 "X-Forwarded-For",
439 "X-Real-Ip",
440 "True-Client-Ip",
441 },
442 },
443 Header: http.Header{
444 "X-Forwarded-For": []string{"1.2.3.4,123.45.67.89"},
445 "X-Forwarded-Proto": []string{"https"},
446 "X-Real-Ip": []string{"4.4.4.4"},
447 "True-Client-Ip": []string{"5.6.7.8"},
448 "Authorization": []string{"Bearer 123"},
449 "Accept-Encoding": []string{"gzip", "compress", "deflate", "identity"},
450 },
451 RemoteAddr: "1.2.3.4",
452 ExpectedHeader: http.Header{
453 "Authorization": []string{"Bearer 123"},
454 "Accept-Encoding": []string{"gzip", "compress", "deflate", "identity"},
455 "X-Forwarded-Proto": []string{"https"},
456 },
457 ExpectedRemoteAddr: "1.2.3.4",
458 },
459 }
460
461 for _, test := range tests {
462 t.Run(test.Name, func(t *testing.T) {
463 t.Parallel()
464
465 req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
466 req.Header = test.Header
467 req.RemoteAddr = test.RemoteAddr
468
469 httpmw.FilterUntrustedOriginHeaders(test.Config, req)
470 require.Equal(t, test.ExpectedRemoteAddr, req.RemoteAddr, "remote address should match")
471 require.Equal(t, test.ExpectedHeader, req.Header, "filtered headers should match")
472 })
473 }
474}
475
476func TestEffectiveHost(t *testing.T) {
477 t.Parallel()

Callers

nothing calls this directly

Calls 3

RunMethod · 0.65
EqualMethod · 0.45

Tested by

no test coverage detected