Debian Codegraph @

modules/caddyhttp/replacer_test.go

raw symbols SCIP
1 // Copyright 2015 Matthew Holt and The Caddy Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package caddyhttp
16
17 import (
18 "context"
19 "crypto/tls"
20 "crypto/x509"
21 "encoding/pem"
22 "net"
23 "net/http"
24 "net/http/httptest"
25 "testing"
26
27 "github.com/caddyserver/caddy/v2"
28 )
29
30 func TestHTTPVarReplacement(t *testing.T) {
31 req, _ := http.NewRequest(http.MethodGet, "/foo/bar.tar.gz?a=1&b=2", nil)
32 repl := caddy.NewReplacer()
33 localAddr, _ := net.ResolveTCPAddr("tcp", "192.168.159.1:80")
34 ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
35 ctx = context.WithValue(ctx, http.LocalAddrContextKey, localAddr)
36 req = req.WithContext(ctx)
37 req.Host = "example.com:80"
38 req.RemoteAddr = "192.168.159.32:1234"
39
40 clientCert := []byte(`-----BEGIN CERTIFICATE-----
41 MIIB9jCCAV+gAwIBAgIBAjANBgkqhkiG9w0BAQsFADAYMRYwFAYDVQQDDA1DYWRk
42 eSBUZXN0IENBMB4XDTE4MDcyNDIxMzUwNVoXDTI4MDcyMTIxMzUwNVowHTEbMBkG
43 A1UEAwwSY2xpZW50LmxvY2FsZG9tYWluMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB
44 iQKBgQDFDEpzF0ew68teT3xDzcUxVFaTII+jXH1ftHXxxP4BEYBU4q90qzeKFneF
45 z83I0nC0WAQ45ZwHfhLMYHFzHPdxr6+jkvKPASf0J2v2HDJuTM1bHBbik5Ls5eq+
46 fVZDP8o/VHKSBKxNs8Goc2NTsr5b07QTIpkRStQK+RJALk4x9QIDAQABo0swSTAJ
47 BgNVHRMEAjAAMAsGA1UdDwQEAwIHgDAaBgNVHREEEzARgglsb2NhbGhvc3SHBH8A
48 AAEwEwYDVR0lBAwwCgYIKwYBBQUHAwIwDQYJKoZIhvcNAQELBQADgYEANSjz2Sk+
49 eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV
50 3Q9fgDkiUod+uIK0IynzIKvw+Cjg+3nx6NQ0IM0zo8c7v398RzB4apbXKZyeeqUH
51 9fNwfEi+OoXR6s+upSKobCmLGLGi9Na5s5g=
52 -----END CERTIFICATE-----`)
53
54 block, _ := pem.Decode(clientCert)
55 if block == nil {
56 t.Fatalf("failed to decode PEM certificate")
57 }
58
59 cert, err := x509.ParseCertificate(block.Bytes)
60 if err != nil {
61 t.Fatalf("failed to decode PEM certificate: %v", err)
62 }
63
64 req.TLS = &tls.ConnectionState{
65 Version: tls.VersionTLS13,
66 HandshakeComplete: true,
67 ServerName: "example.com",
68 CipherSuite: tls.TLS_AES_256_GCM_SHA384,
69 PeerCertificates: []*x509.Certificate{cert},
70 NegotiatedProtocol: "h2",
71 NegotiatedProtocolIsMutual: true,
72 }
73
74 res := httptest.NewRecorder()
75 addHTTPVarsToReplacer(repl, req, res)
76
77 for i, tc := range []struct {
78 get string
79 expect string
80 }{
81 {
82 get: "http.request.scheme",
83 expect: "https",
84 },
85 {
86 get: "http.request.method",
87 expect: http.MethodGet,
88 },
89 {
90 get: "http.request.host",
91 expect: "example.com",
92 },
93 {
94 get: "http.request.port",
95 expect: "80",
96 },
97 {
98 get: "http.request.hostport",
99 expect: "example.com:80",
100 },
101 {
102 get: "http.request.local.host",
103 expect: "192.168.159.1",
104 },
105 {
106 get: "http.request.local.port",
107 expect: "80",
108 },
109 {
110 get: "http.request.local",
111 expect: "192.168.159.1:80",
112 },
113 {
114 get: "http.request.remote.host",
115 expect: "192.168.159.32",
116 },
117 {
118 get: "http.request.remote.host/24",
119 expect: "192.168.159.0/24",
120 },
121 {
122 get: "http.request.remote.host/24,32",
123 expect: "192.168.159.0/24",
124 },
125 {
126 get: "http.request.remote.host/999",
127 expect: "",
128 },
129 {
130 get: "http.request.remote.port",
131 expect: "1234",
132 },
133 {
134 get: "http.request.host.labels.0",
135 expect: "com",
136 },
137 {
138 get: "http.request.host.labels.1",
139 expect: "example",
140 },
141 {
142 get: "http.request.host.labels.2",
143 expect: "",
144 },
145 {
146 get: "http.request.uri",
147 expect: "/foo/bar.tar.gz?a=1&b=2",
148 },
149 {
150 get: "http.request.uri_escaped",
151 expect: "%2Ffoo%2Fbar.tar.gz%3Fa%3D1%26b%3D2",
152 },
153 {
154 get: "http.request.uri.path",
155 expect: "/foo/bar.tar.gz",
156 },
157 {
158 get: "http.request.uri.path_escaped",
159 expect: "%2Ffoo%2Fbar.tar.gz",
160 },
161 {
162 get: "http.request.uri.path.file",
163 expect: "bar.tar.gz",
164 },
165 {
166 get: "http.request.uri.path.file.base",
167 expect: "bar.tar",
168 },
169 {
170 // not ideal, but also most correct, given that files can have dots (example: index.<SHA>.html) TODO: maybe this isn't right..
171 get: "http.request.uri.path.file.ext",
172 expect: ".gz",
173 },
174 {
175 get: "http.request.uri.query",
176 expect: "a=1&b=2",
177 },
178 {
179 get: "http.request.uri.query_escaped",
180 expect: "a%3D1%26b%3D2",
181 },
182 {
183 get: "http.request.uri.query.a",
184 expect: "1",
185 },
186 {
187 get: "http.request.uri.query.b",
188 expect: "2",
189 },
190 {
191 get: "http.request.uri.prefixed_query",
192 expect: "?a=1&b=2",
193 },
194 {
195 get: "http.request.tls.cipher_suite",
196 expect: "TLS_AES_256_GCM_SHA384",
197 },
198 {
199 get: "http.request.tls.proto",
200 expect: "h2",
201 },
202 {
203 get: "http.request.tls.proto_mutual",
204 expect: "true",
205 },
206 {
207 get: "http.request.tls.resumed",
208 expect: "false",
209 },
210 {
211 get: "http.request.tls.server_name",
212 expect: "example.com",
213 },
214 {
215 get: "http.request.tls.version",
216 expect: "tls1.3",
217 },
218 {
219 get: "http.request.tls.client.fingerprint",
220 expect: "9f57b7b497cceacc5459b76ac1c3afedbc12b300e728071f55f84168ff0f7702",
221 },
222 {
223 get: "http.request.tls.client.issuer",
224 expect: "CN=Caddy Test CA",
225 },
226 {
227 get: "http.request.tls.client.serial",
228 expect: "2",
229 },
230 {
231 get: "http.request.tls.client.subject",
232 expect: "CN=client.localdomain",
233 },
234 {
235 get: "http.request.tls.client.san.dns_names",
236 expect: "[localhost]",
237 },
238 {
239 get: "http.request.tls.client.san.dns_names.0",
240 expect: "localhost",
241 },
242 {
243 get: "http.request.tls.client.san.dns_names.1",
244 expect: "",
245 },
246 {
247 get: "http.request.tls.client.san.ips",
248 expect: "[127.0.0.1]",
249 },
250 {
251 get: "http.request.tls.client.san.ips.0",
252 expect: "127.0.0.1",
253 },
254 {
255 get: "http.request.tls.client.certificate_pem",
256 expect: string(clientCert) + "\n", // returned value comes with a newline appended to it
257 },
258 } {
259 actual, got := repl.GetString(tc.get)
260 if !got {
261 t.Errorf("Test %d: Expected to recognize the placeholder name, but didn't", i)
262 }
263 if actual != tc.expect {
264 t.Errorf("Test %d: Expected %s to be '%s' but got '%s'",
265 i, tc.get, tc.expect, actual)
266 }
267 }
268 }