| 1 | package reverseproxy |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "net" |
| 8 | "net/url" |
| 9 | "reflect" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/caddyserver/caddy/v2" |
| 13 | "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" |
| 14 | "github.com/caddyserver/caddy/v2/modules/caddyhttp" |
| 15 | ) |
| 16 | |
| 17 | func TestHTTPTransportUnmarshalCaddyFileWithCaPools(t *testing.T) { |
| 18 | const test_der_1 = `MIIDSzCCAjOgAwIBAgIUfIRObjWNUA4jxQ/0x8BOCvE2Vw4wDQYJKoZIhvcNAQELBQAwFjEUMBIGA1UEAwwLRWFzeS1SU0EgQ0EwHhcNMTkwODI4MTYyNTU5WhcNMjkwODI1MTYyNTU5WjAWMRQwEgYDVQQDDAtFYXN5LVJTQSBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK5m5elxhQfMp/3aVJ4JnpN9PUSz6LlP6LePAPFU7gqohVVFVtDkChJAG3FNkNQNlieVTja/bgH9IcC6oKbROwdY1h0MvNV8AHHigvl03WuJD8g2ReVFXXwsnrPmKXCFzQyMI6TYk3m2gYrXsZOU1GLnfMRC3KAMRgE2F45twOs9hqG169YJ6mM2eQjzjCHWI6S2/iUYvYxRkCOlYUbLsMD/AhgAf1plzg6LPqNxtdlwxZnA0ytgkmhK67HtzJu0+ovUCsMv0RwcMhsEo9T8nyFAGt9XLZ63X5WpBCTUApaAUhnG0XnerjmUWb6eUWw4zev54sEfY5F3x002iQaW6cECAwEAAaOBkDCBjTAdBgNVHQ4EFgQU4CBUbZsS2GaNIkGRz/cBsD5ivjswUQYDVR0jBEowSIAU4CBUbZsS2GaNIkGRz/cBsD5ivjuhGqQYMBYxFDASBgNVBAMMC0Vhc3ktUlNBIENBghR8hE5uNY1QDiPFD/THwE4K8TZXDjAMBgNVHRMEBTADAQH/MAsGA1UdDwQEAwIBBjANBgkqhkiG9w0BAQsFAAOCAQEAKB3V4HIzoiO/Ch6WMj9bLJ2FGbpkMrcb/Eq01hT5zcfKD66lVS1MlK+cRL446Z2b2KDP1oFyVs+qmrmtdwrWgD+nfe2sBmmIHo9m9KygMkEOfG3MghGTEcS+0cTKEcoHYWYyOqQh6jnedXY8Cdm4GM1hAc9MiL3/sqV8YCVSLNnkoNysmr06/rZ0MCUZPGUtRmfd0heWhrfzAKw2HLgX+RAmpOE2MZqWcjvqKGyaRiaZks4nJkP6521aC2Lgp0HhCz1j8/uQ5ldoDszCnu/iro0NAsNtudTMD+YoLQxLqdleIh6CW+illc2VdXwj7mn6J04yns9jfE2jRjW/yTLFuQ==` |
| 19 | type args struct { |
| 20 | d *caddyfile.Dispenser |
| 21 | } |
| 22 | tests := []struct { |
| 23 | name string |
| 24 | args args |
| 25 | expectedTLSConfig TLSConfig |
| 26 | wantErr bool |
| 27 | }{ |
| 28 | { |
| 29 | name: "tls_trust_pool without a module argument returns an error", |
| 30 | args: args{ |
| 31 | d: caddyfile.NewTestDispenser( |
| 32 | `http { |
| 33 | tls_trust_pool |
| 34 | }`), |
| 35 | }, |
| 36 | wantErr: true, |
| 37 | }, |
| 38 | { |
| 39 | name: "providing both 'tls_trust_pool' and 'tls_trusted_ca_certs' returns an error", |
| 40 | args: args{ |
| 41 | d: caddyfile.NewTestDispenser(fmt.Sprintf( |
| 42 | `http { |
| 43 | tls_trust_pool inline %s |
| 44 | tls_trusted_ca_certs %s |
| 45 | }`, test_der_1, test_der_1)), |
| 46 | }, |
| 47 | wantErr: true, |
| 48 | }, |
| 49 | { |
| 50 | name: "setting 'tls_trust_pool' and 'tls_trusted_ca_certs' produces an error", |
| 51 | args: args{ |
| 52 | d: caddyfile.NewTestDispenser(fmt.Sprintf( |
| 53 | `http { |
| 54 | tls_trust_pool inline { |
| 55 | trust_der %s |
| 56 | } |
| 57 | tls_trusted_ca_certs %s |
| 58 | }`, test_der_1, test_der_1)), |
| 59 | }, |
| 60 | wantErr: true, |
| 61 | }, |
| 62 | { |
| 63 | name: "using 'inline' tls_trust_pool loads the module successfully", |
| 64 | args: args{ |
| 65 | d: caddyfile.NewTestDispenser(fmt.Sprintf( |
| 66 | `http { |
| 67 | tls_trust_pool inline { |
| 68 | trust_der %s |
| 69 | } |
| 70 | } |
| 71 | `, test_der_1)), |
| 72 | }, |
| 73 | expectedTLSConfig: TLSConfig{CARaw: json.RawMessage(fmt.Sprintf(`{"provider":"inline","trusted_ca_certs":["%s"]}`, test_der_1))}, |
| 74 | }, |
| 75 | { |
| 76 | name: "setting 'tls_trusted_ca_certs' and 'tls_trust_pool' produces an error", |
| 77 | args: args{ |
| 78 | d: caddyfile.NewTestDispenser(fmt.Sprintf( |
| 79 | `http { |
| 80 | tls_trusted_ca_certs %s |
| 81 | tls_trust_pool inline { |
| 82 | trust_der %s |
| 83 | } |
| 84 | }`, test_der_1, test_der_1)), |
| 85 | }, |
| 86 | wantErr: true, |
| 87 | }, |
| 88 | } |
| 89 | for _, tt := range tests { |
| 90 | t.Run(tt.name, func(t *testing.T) { |
| 91 | ht := &HTTPTransport{} |
| 92 | if err := ht.UnmarshalCaddyfile(tt.args.d); (err != nil) != tt.wantErr { |
| 93 | t.Errorf("HTTPTransport.UnmarshalCaddyfile() error = %v, wantErr %v", err, tt.wantErr) |
| 94 | return |
| 95 | } |
| 96 | if !tt.wantErr && !reflect.DeepEqual(&tt.expectedTLSConfig, ht.TLS) { |
| 97 | t.Errorf("HTTPTransport.UnmarshalCaddyfile() = %v, want %v", ht, tt.expectedTLSConfig) |
| 98 | } |
| 99 | }) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func TestHTTPTransport_RequestHeaderOps_TLS(t *testing.T) { |
| 104 | var ht HTTPTransport |
| 105 | // When TLS is nil, expect no header ops |
| 106 | if ops := ht.RequestHeaderOps(); ops != nil { |
| 107 | t.Fatalf("expected nil HeaderOps when TLS is nil, got: %#v", ops) |
| 108 | } |
| 109 | |
| 110 | // When TLS is configured, expect a HeaderOps that sets Host |
| 111 | ht.TLS = &TLSConfig{} |
| 112 | ops := ht.RequestHeaderOps() |
| 113 | if ops == nil { |
| 114 | t.Fatal("expected non-nil HeaderOps when TLS is set") |
| 115 | } |
| 116 | if ops.Set == nil { |
| 117 | t.Fatalf("expected ops.Set to be non-nil, got nil") |
| 118 | } |
| 119 | if got := ops.Set.Get("Host"); got != "{http.reverse_proxy.upstream.hostport}" { |
| 120 | t.Fatalf("unexpected Host value; want placeholder, got: %s", got) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // TestHTTPTransport_DialTLSContext_ProxyProtocol verifies that when TLS and |
| 125 | // ProxyProtocol are both enabled, DialTLSContext is set. This is critical because |
| 126 | // ProxyProtocol modifies req.URL.Host to include client info with "->" separator |
| 127 | // (e.g., "[2001:db8::1]:12345->127.0.0.1:443"), which breaks Go's address parsing. |
| 128 | // Without a custom DialTLSContext, Go's HTTP library would fail with |
| 129 | // "too many colons in address" when trying to parse the mangled host. |
| 130 | func TestHTTPTransport_DialTLSContext_ProxyProtocol(t *testing.T) { |
| 131 | ctx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()}) |
| 132 | defer cancel() |
| 133 | |
| 134 | tests := []struct { |
| 135 | name string |
| 136 | tls *TLSConfig |
| 137 | proxyProtocol string |
| 138 | serverNameHasPlaceholder bool |
| 139 | expectDialTLSContext bool |
| 140 | }{ |
| 141 | { |
| 142 | name: "no TLS, no proxy protocol", |
| 143 | tls: nil, |
| 144 | proxyProtocol: "", |
| 145 | expectDialTLSContext: false, |
| 146 | }, |
| 147 | { |
| 148 | name: "TLS without proxy protocol", |
| 149 | tls: &TLSConfig{}, |
| 150 | proxyProtocol: "", |
| 151 | expectDialTLSContext: false, |
| 152 | }, |
| 153 | { |
| 154 | name: "TLS with proxy protocol v1", |
| 155 | tls: &TLSConfig{}, |
| 156 | proxyProtocol: "v1", |
| 157 | expectDialTLSContext: true, |
| 158 | }, |
| 159 | { |
| 160 | name: "TLS with proxy protocol v2", |
| 161 | tls: &TLSConfig{}, |
| 162 | proxyProtocol: "v2", |
| 163 | expectDialTLSContext: true, |
| 164 | }, |
| 165 | { |
| 166 | name: "TLS with placeholder ServerName", |
| 167 | tls: &TLSConfig{ServerName: "{http.request.host}"}, |
| 168 | proxyProtocol: "", |
| 169 | serverNameHasPlaceholder: true, |
| 170 | expectDialTLSContext: true, |
| 171 | }, |
| 172 | { |
| 173 | name: "TLS with placeholder ServerName and proxy protocol", |
| 174 | tls: &TLSConfig{ServerName: "{http.request.host}"}, |
| 175 | proxyProtocol: "v2", |
| 176 | serverNameHasPlaceholder: true, |
| 177 | expectDialTLSContext: true, |
| 178 | }, |
| 179 | } |
| 180 | |
| 181 | for _, tt := range tests { |
| 182 | t.Run(tt.name, func(t *testing.T) { |
| 183 | ht := &HTTPTransport{ |
| 184 | TLS: tt.tls, |
| 185 | ProxyProtocol: tt.proxyProtocol, |
| 186 | } |
| 187 | |
| 188 | rt, err := ht.NewTransport(ctx) |
| 189 | if err != nil { |
| 190 | t.Fatalf("NewTransport() error = %v", err) |
| 191 | } |
| 192 | |
| 193 | hasDialTLSContext := rt.DialTLSContext != nil |
| 194 | if hasDialTLSContext != tt.expectDialTLSContext { |
| 195 | t.Errorf("DialTLSContext set = %v, want %v", hasDialTLSContext, tt.expectDialTLSContext) |
| 196 | } |
| 197 | }) |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // TestHTTPTransport_DialContext_DialInfoOverride is a regression test for |
| 202 | // issue #6447: a `tcp4/`-prefixed upstream silently fell back to plain `tcp` |
| 203 | // because dialContext only honored DialInfo for unix networks. PR #7300 widened |
| 204 | // the condition so DialInfo is honored when no upstream HTTP proxy is in use, |
| 205 | // and skipped (for non-unix networks) when one is. Both halves are pinned here. |
| 206 | func TestHTTPTransport_DialContext_DialInfoOverride(t *testing.T) { |
| 207 | ctx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()}) |
| 208 | defer cancel() |
| 209 | |
| 210 | ln, err := net.Listen("tcp4", "127.0.0.1:0") |
| 211 | if err != nil { |
| 212 | t.Fatalf("listen: %v", err) |
| 213 | } |
| 214 | t.Cleanup(func() { ln.Close() }) |
| 215 | go func() { |
| 216 | for { |
| 217 | c, err := ln.Accept() |
| 218 | if err != nil { |
| 219 | return |
| 220 | } |
| 221 | c.Close() |
| 222 | } |
| 223 | }() |
| 224 | |
| 225 | ht := &HTTPTransport{} |
| 226 | rt, err := ht.NewTransport(ctx) |
| 227 | if err != nil { |
| 228 | t.Fatalf("NewTransport: %v", err) |
| 229 | } |
| 230 | |
| 231 | proxyURL, err := url.Parse("http://proxy.example:8080") |
| 232 | if err != nil { |
| 233 | t.Fatalf("parse proxy URL: %v", err) |
| 234 | } |
| 235 | |
| 236 | tests := []struct { |
| 237 | name string |
| 238 | proxy bool |
| 239 | dialInfo string |
| 240 | defaultAddr string |
| 241 | }{ |
| 242 | { |
| 243 | // no proxy: DialInfo should be applied, so the dial lands on |
| 244 | // the live listener despite the bogus default address. |
| 245 | name: "honors DialInfo when no proxy", |
| 246 | proxy: false, |
| 247 | dialInfo: ln.Addr().String(), |
| 248 | defaultAddr: "127.0.0.1:1", |
| 249 | }, |
| 250 | { |
| 251 | // proxy active: DialInfo must NOT be applied for non-unix |
| 252 | // networks; the default address (the live listener) is used. |
| 253 | name: "skips DialInfo when proxy active", |
| 254 | proxy: true, |
| 255 | dialInfo: "127.0.0.1:1", |
| 256 | defaultAddr: ln.Addr().String(), |
| 257 | }, |
| 258 | } |
| 259 | |
| 260 | for _, tt := range tests { |
| 261 | t.Run(tt.name, func(t *testing.T) { |
| 262 | dialCtx := context.WithValue(context.Background(), caddyhttp.VarsCtxKey, make(map[string]any)) |
| 263 | caddyhttp.SetVar(dialCtx, dialInfoVarKey, DialInfo{ |
| 264 | Network: "tcp4", |
| 265 | Address: tt.dialInfo, |
| 266 | }) |
| 267 | if tt.proxy { |
| 268 | caddyhttp.SetVar(dialCtx, proxyVarKey, proxyURL) |
| 269 | } |
| 270 | |
| 271 | conn, err := rt.DialContext(dialCtx, "tcp", tt.defaultAddr) |
| 272 | if err != nil { |
| 273 | t.Fatalf("DialContext: %v", err) |
| 274 | } |
| 275 | t.Cleanup(func() { conn.Close() }) |
| 276 | if got := conn.RemoteAddr().String(); got != ln.Addr().String() { |
| 277 | t.Fatalf("conn.RemoteAddr() = %s, want %s", got, ln.Addr().String()) |
| 278 | } |
| 279 | }) |
| 280 | } |
| 281 | } |