Debian Codegraph @

modules/caddyhttp/reverseproxy/headers_test.go

raw symbols SCIP
1 package reverseproxy
2
3 import (
4 "context"
5 "net/http/httptest"
6 "testing"
7
8 "github.com/caddyserver/caddy/v2/modules/caddyhttp"
9 )
10
11 func TestAddForwardedHeadersNonIP(t *testing.T) {
12 h := Handler{}
13
14 // Simulate a request with a non-IP remote address (e.g. SCION, abstract socket, or hostname)
15 req := httptest.NewRequest("GET", "/", nil)
16 req.RemoteAddr = "my-weird-network:12345"
17
18 // Mock the context variables required by Caddy.
19 // We need to inject the variable map manually since we aren't running the full server.
20 vars := map[string]any{
21 caddyhttp.TrustedProxyVarKey: false,
22 }
23 ctx := context.WithValue(req.Context(), caddyhttp.VarsCtxKey, vars)
24 req = req.WithContext(ctx)
25
26 // Execute the unexported function
27 err := h.addForwardedHeaders(req)
28
29 // Expectation: No error should be returned for non-IP addresses.
30 // The function should simply skip the trusted proxy check.
31 if err != nil {
32 t.Errorf("expected no error for non-IP address, got: %v", err)
33 }
34 }
35
36 func TestAddForwardedHeaders_UnixSocketTrusted(t *testing.T) {
37 h := Handler{}
38
39 req := httptest.NewRequest("GET", "http://example.com/", nil)
40 req.RemoteAddr = "@"
41 req.Header.Set("X-Forwarded-For", "1.2.3.4, 10.0.0.1")
42 req.Header.Set("X-Forwarded-Proto", "https")
43 req.Header.Set("X-Forwarded-Host", "original.example.com")
44
45 vars := map[string]any{
46 caddyhttp.TrustedProxyVarKey: true,
47 caddyhttp.ClientIPVarKey: "1.2.3.4",
48 }
49 ctx := context.WithValue(req.Context(), caddyhttp.VarsCtxKey, vars)
50 req = req.WithContext(ctx)
51
52 err := h.addForwardedHeaders(req)
53 if err != nil {
54 t.Fatalf("expected no error, got: %v", err)
55 }
56
57 if got := req.Header.Get("X-Forwarded-For"); got != "1.2.3.4, 10.0.0.1" {
58 t.Errorf("X-Forwarded-For = %q, want %q", got, "1.2.3.4, 10.0.0.1")
59 }
60 if got := req.Header.Get("X-Forwarded-Proto"); got != "https" {
61 t.Errorf("X-Forwarded-Proto = %q, want %q", got, "https")
62 }
63 if got := req.Header.Get("X-Forwarded-Host"); got != "original.example.com" {
64 t.Errorf("X-Forwarded-Host = %q, want %q", got, "original.example.com")
65 }
66 }
67
68 func TestAddForwardedHeaders_UnixSocketUntrusted(t *testing.T) {
69 h := Handler{}
70
71 req := httptest.NewRequest("GET", "http://example.com/", nil)
72 req.RemoteAddr = "@"
73 req.Header.Set("X-Forwarded-For", "1.2.3.4")
74 req.Header.Set("X-Forwarded-Proto", "https")
75 req.Header.Set("X-Forwarded-Host", "spoofed.example.com")
76
77 vars := map[string]any{
78 caddyhttp.TrustedProxyVarKey: false,
79 caddyhttp.ClientIPVarKey: "",
80 }
81 ctx := context.WithValue(req.Context(), caddyhttp.VarsCtxKey, vars)
82 req = req.WithContext(ctx)
83
84 err := h.addForwardedHeaders(req)
85 if err != nil {
86 t.Fatalf("expected no error, got: %v", err)
87 }
88
89 if got := req.Header.Get("X-Forwarded-For"); got != "" {
90 t.Errorf("X-Forwarded-For should be deleted, got %q", got)
91 }
92 if got := req.Header.Get("X-Forwarded-Proto"); got != "" {
93 t.Errorf("X-Forwarded-Proto should be deleted, got %q", got)
94 }
95 if got := req.Header.Get("X-Forwarded-Host"); got != "" {
96 t.Errorf("X-Forwarded-Host should be deleted, got %q", got)
97 }
98 }
99
100 func TestAddForwardedHeaders_UnixSocketTrustedNoExistingHeaders(t *testing.T) {
101 h := Handler{}
102
103 req := httptest.NewRequest("GET", "http://example.com/", nil)
104 req.RemoteAddr = "@"
105
106 vars := map[string]any{
107 caddyhttp.TrustedProxyVarKey: true,
108 caddyhttp.ClientIPVarKey: "5.6.7.8",
109 }
110 ctx := context.WithValue(req.Context(), caddyhttp.VarsCtxKey, vars)
111 req = req.WithContext(ctx)
112
113 err := h.addForwardedHeaders(req)
114 if err != nil {
115 t.Fatalf("expected no error, got: %v", err)
116 }
117
118 if got := req.Header.Get("X-Forwarded-For"); got != "" {
119 t.Errorf("X-Forwarded-For should be empty when no prior XFF exists, got %q", got)
120 }
121 if got := req.Header.Get("X-Forwarded-Proto"); got != "http" {
122 t.Errorf("X-Forwarded-Proto = %q, want %q", got, "http")
123 }
124 if got := req.Header.Get("X-Forwarded-Host"); got != "example.com" {
125 t.Errorf("X-Forwarded-Host = %q, want %q", got, "example.com")
126 }
127 }