Debian Codegraph @

crt/aws-c-http/tests/test_no_proxy.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/http/connection.h>
7 #include <aws/http/proxy.h>
8
9 #include <aws/common/environment.h>
10 #include <aws/common/string.h>
11 #include <aws/io/uri.h>
12
13 #include <aws/http/private/no_proxy.h>
14 #include <aws/testing/aws_test_harness.h>
15 #include <aws/testing/io_testing_channel.h>
16
17 static int s_test_no_proxy_helper(
18 struct aws_allocator *allocator,
19 const char *host,
20 const char *no_proxy_value,
21 bool expected_bypass) {
22 /* Create a no_proxy string for testing */
23 struct aws_string *no_proxy_str = aws_string_new_from_c_str(allocator, no_proxy_value);
24
25 /* Call the function that checks NO_PROXY */
26 ASSERT_UINT_EQUALS(
27 aws_http_host_matches_no_proxy(allocator, aws_byte_cursor_from_c_str(host), no_proxy_str), expected_bypass);
28
29 /* Clean up */
30 aws_string_destroy(no_proxy_str);
31
32 return AWS_OP_SUCCESS;
33 }
34
35 /**
36 * Test subdomain matching with NO_PROXY
37 */
38 static int s_test_no_proxy_subdomain_matching(struct aws_allocator *allocator, void *ctx) {
39 (void)ctx;
40 const char *no_proxy_value = ".example.com";
41
42 aws_http_library_init(allocator);
43
44 /* Test that a subdomain matches when NO_PROXY contains a domain with leading dot */
45 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "sub.example.com", no_proxy_value, true));
46 /* cannot match */
47 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "sub.subexample.com", no_proxy_value, false));
48
49 aws_http_library_clean_up();
50 return AWS_OP_SUCCESS;
51 }
52 AWS_TEST_CASE(test_no_proxy_subdomain_matching, s_test_no_proxy_subdomain_matching);
53
54 /**
55 * Test wildcard patterns in NO_PROXY
56 */
57 static int s_test_no_proxy_wildcard_patterns(struct aws_allocator *allocator, void *ctx) {
58 (void)ctx;
59 const char *no_proxy_value = "*";
60
61 aws_http_library_init(allocator);
62
63 /* Test that a wildcard pattern matches all hosts */
64 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "any.example.com", no_proxy_value, true));
65
66 aws_http_library_clean_up();
67 return AWS_OP_SUCCESS;
68 }
69 AWS_TEST_CASE(test_no_proxy_wildcard_patterns, s_test_no_proxy_wildcard_patterns);
70
71 /**
72 * Test case insensitivity in NO_PROXY
73 */
74 static int s_test_no_proxy_case_insensitivity(struct aws_allocator *allocator, void *ctx) {
75 (void)ctx;
76 const char *no_proxy_value = "example.COM";
77
78 aws_http_library_init(allocator);
79
80 /* Test that case insensitive matching works for both host and NO_PROXY entries */
81 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "EXAMPLE.com", no_proxy_value, true));
82
83 aws_http_library_clean_up();
84 return AWS_OP_SUCCESS;
85 }
86 AWS_TEST_CASE(test_no_proxy_case_insensitivity, s_test_no_proxy_case_insensitivity);
87
88 /**
89 * Test IPv6 addresses
90 */
91 static int s_test_no_proxy_ipv6_address(struct aws_allocator *allocator, void *ctx) {
92 (void)ctx;
93 /* Pattern don't allow `[]`, just follows what curl does. */
94 const char *no_proxy_value = "2001:db8::1, ::1, [2001:db8::2]";
95
96 aws_http_library_init(allocator);
97
98 /* Test that an IPv6 address in brackets matches */
99 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[2001:db8::1]", no_proxy_value, true));
100
101 /* Test another IPv6 address format (localhost) */
102 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "::1", no_proxy_value, true));
103
104 /* Test a non-matching IPv6 address */
105 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[2001:db8::2]", no_proxy_value, false));
106
107 aws_http_library_clean_up();
108 return AWS_OP_SUCCESS;
109 }
110 AWS_TEST_CASE(test_no_proxy_ipv6_address, s_test_no_proxy_ipv6_address);
111
112 /**
113 * Test multiple patterns in NO_PROXY
114 */
115 static int s_test_no_proxy_multiple_patterns(struct aws_allocator *allocator, void *ctx) {
116 (void)ctx;
117 /* Only wildcard support is a single `*`, if it's in the list, it will be ignored. */
118 /* commas at start or end won't affect it. */
119 const char *no_proxy_value = ",,foo.bar,example.com,other.net,*,";
120
121 aws_http_library_init(allocator);
122
123 /* Test that a host matches when it's in the middle of a comma-separated list */
124 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "example.com", no_proxy_value, true));
125
126 /* Test that another host in the list also matches */
127 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "foo.bar", no_proxy_value, true));
128
129 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "foo.", no_proxy_value, false));
130
131 aws_http_library_clean_up();
132 return AWS_OP_SUCCESS;
133 }
134 AWS_TEST_CASE(test_no_proxy_multiple_patterns, s_test_no_proxy_multiple_patterns);
135
136 /**
137 * Test whitespace handling in NO_PROXY
138 */
139 static int s_test_no_proxy_whitespace_handling(struct aws_allocator *allocator, void *ctx) {
140 (void)ctx;
141 const char *no_proxy_value = " example.com , foo.bar ";
142
143 aws_http_library_init(allocator);
144
145 /* Test that whitespace is properly handled in NO_PROXY entries */
146 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "example.com", no_proxy_value, true));
147
148 /* Test that another host with whitespace also matches */
149 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "foo.bar", no_proxy_value, true));
150
151 aws_http_library_clean_up();
152 return AWS_OP_SUCCESS;
153 }
154 AWS_TEST_CASE(test_no_proxy_whitespace_handling, s_test_no_proxy_whitespace_handling);
155
156 /**
157 * Test IP addresses in NO_PROXY
158 */
159 static int s_test_no_proxy_ip_address(struct aws_allocator *allocator, void *ctx) {
160 (void)ctx;
161 const char *no_proxy_value = "192.168.1.1,10.0.0.0";
162
163 aws_http_library_init(allocator);
164
165 /* Test that IP address matching works */
166 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.1.1", no_proxy_value, true));
167
168 /* Test that a different IP doesn't match */
169 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.2.1", no_proxy_value, false));
170
171 aws_http_library_clean_up();
172 return AWS_OP_SUCCESS;
173 }
174 AWS_TEST_CASE(test_no_proxy_ip_address, s_test_no_proxy_ip_address);
175
176 /**
177 * Test port-specific exclusions in NO_PROXY
178 *
179 * NOTE: This tests a curl-specific feature where entries like "example.com:8080"
180 * can be used to bypass the proxy only for specific ports. The current implementation
181 * doesn't support this feature, so this test documents that behavior.
182 */
183 static int s_test_no_proxy_port_specific(struct aws_allocator *allocator, void *ctx) {
184 (void)ctx;
185 const char *no_proxy_value = "example.com:8080";
186
187 aws_http_library_init(allocator);
188
189 /* Our implementation only does hostname matching and ignores port information.
190 * In curl, this would bypass only on port 8080, but in our implementation it
191 * bypasses for all ports. */
192 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "example.com", no_proxy_value, true));
193
194 aws_http_library_clean_up();
195 return AWS_OP_SUCCESS;
196 }
197 AWS_TEST_CASE(test_no_proxy_port_specific, s_test_no_proxy_port_specific);
198
199 /**
200 * Test CIDR notation in NO_PROXY
201 *
202 * Tests the CIDR notation support (similar to curl 7.86.0) where "192.168.0.0/16"
203 * would match all addresses starting with "192.168".
204 */
205 static int s_test_no_proxy_cidr_notation(struct aws_allocator *allocator, void *ctx) {
206 (void)ctx;
207 const char *no_proxy_value_16 = "192.168.0.0/16";
208 const char *no_proxy_value_24 = "192.168.5.0/24";
209
210 aws_http_library_init(allocator);
211
212 /* Test that an IP address in a CIDR range matches */
213 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.5.10", no_proxy_value_16, true));
214
215 /* Test that an IP address outside the CIDR range doesn't match */
216 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "10.0.5.10", no_proxy_value_16, false));
217
218 /* Test that an IP address in a more specific CIDR range matches */
219 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.5.10", no_proxy_value_24, true));
220
221 /* Test that an IP address outside the specific CIDR range doesn't match */
222 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.6.10", no_proxy_value_24, false));
223
224 aws_http_library_clean_up();
225 return AWS_OP_SUCCESS;
226 }
227 AWS_TEST_CASE(test_no_proxy_cidr_notation, s_test_no_proxy_cidr_notation);
228
229 /**
230 * Test IPv6 CIDR notation in NO_PROXY
231 *
232 * Tests the CIDR notation support for IPv6 addresses where "2001:db8::/32"
233 * would match all addresses starting with "2001:db8".
234 */
235 static int s_test_no_proxy_ipv6_cidr_notation(struct aws_allocator *allocator, void *ctx) {
236 (void)ctx;
237 const char *no_proxy_value_32 = "2001:db8::/32";
238 const char *no_proxy_value_64 = "2001:db8:1:2::/64";
239
240 aws_http_library_init(allocator);
241
242 /* Test that an IPv6 address in a CIDR range matches */
243 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[2001:db8:1:2::3]", no_proxy_value_32, true));
244
245 /* Test that an IPv6 address outside the CIDR range doesn't match */
246 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[2001:db9:1:2::3]", no_proxy_value_32, false));
247
248 /* Test that an IPv6 address in a more specific CIDR range matches */
249 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[2001:db8:1:2::3]", no_proxy_value_64, true));
250
251 /* Test that an IPv6 address outside the specific CIDR range doesn't match */
252 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[2001:db8:1:3::3]", no_proxy_value_64, false));
253
254 aws_http_library_clean_up();
255 return AWS_OP_SUCCESS;
256 }
257 AWS_TEST_CASE(test_no_proxy_ipv6_cidr_notation, s_test_no_proxy_ipv6_cidr_notation);
258
259 /**
260 * Test invalid IP addresses and CIDR blocks in NO_PROXY
261 *
262 * Verifies that the NO_PROXY implementation safely handles and ignores invalid
263 * IP addresses and CIDR blocks without crashing.
264 */
265 static int s_test_no_proxy_invalid_patterns(struct aws_allocator *allocator, void *ctx) {
266 (void)ctx;
267
268 aws_http_library_init(allocator);
269
270 /* Test with invalid IP addresses and CIDR notations mixed with valid entries */
271 const char *no_proxy_value = "example.com,999.999.999.999,192.168.1.3/33,192.168.b.c,"
272 "2001:xyz::bad:ipv6,2001:db8::/129,not:a:valid:ip/64,"
273 "[malformed],192.168.1.2,"
274 "192.168.1.1/99999999999999999," /* Invalid network bits */
275 "192.168.1.1/9/9/9," /* Invalid network bits */
276 "2001:db8::/999999"; /* Invalid IPv6 prefix */
277 // const char *no_proxy_value = "192.168.1.1/9/9/9";
278
279 /* Test that invalid IP addresses and CIDR blocks are safely ignored */
280 /* The last valid entry (192.168.1.2) should still match */
281 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.1.2", no_proxy_value, true));
282
283 /* Test that the valid hostname entry still works */
284 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "example.com", no_proxy_value, true));
285
286 /* Test with an invalid host address parameter */
287 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.1.1", no_proxy_value, false));
288
289 /* An invalid IP address will be treated as regular hostname and match as regular hostname. */
290 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "999.999.999.999", no_proxy_value, true));
291 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "999.999.999.999.999", no_proxy_value, true));
292
293 /* Test with only invalid entries */
294 const char *invalid_only = "999.999.999.999,192.168.1.1/33,not:an:ip:addr,2001:xyz::bad";
295
296 /* Test that a valid IP doesn't match when NO_PROXY contains only invalid entries */
297 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.1.1", invalid_only, false));
298
299 /* Test with empty host parameter */
300 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "", invalid_only, false));
301
302 /* Test with a very malformed CIDR input that could cause parsing issues */
303 const char *malformed_cidr = "192.168.1.1/abcdef,2001:db8::/xyz";
304
305 /* Malformed CIDR will be taken as the entry */
306 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "2001:db8::", malformed_cidr, false));
307
308 /* Test with very large CIDR parts that would be rejected in the buffer size check */
309 char large_value[200];
310 memset(large_value, 'x', sizeof(large_value) - 1);
311 large_value[sizeof(large_value) - 1] = '\0';
312
313 char large_cidr[256];
314 snprintf(large_cidr, sizeof(large_cidr), "192.168.1.1/%s", large_value);
315
316 /* Test with oversized CIDR notation */
317 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.1.1", large_cidr, false));
318
319 aws_http_library_clean_up();
320 return AWS_OP_SUCCESS;
321 }
322 AWS_TEST_CASE(test_no_proxy_invalid_patterns, s_test_no_proxy_invalid_patterns);
323
324 /**
325 * Test invalid host inputs to aws_http_host_matches_no_proxy
326 *
327 * Verifies that the aws_http_host_matches_no_proxy function handles malformed host inputs
328 * gracefully without crashing. These tests specifically check the host parameter
329 * rather than the NO_PROXY environment variable content.
330 */
331 static int s_test_no_proxy_invalid_host_inputs(struct aws_allocator *allocator, void *ctx) {
332 (void)ctx;
333
334 aws_http_library_init(allocator);
335
336 /* Set up a valid NO_PROXY value for testing */
337 const char *no_proxy_value = "example.com,192.168.1.0/24,2001:db8::/32";
338
339 /* Test with invalid IPv4 address */
340 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "999.999.999.999", no_proxy_value, false));
341
342 /* Test with malformed IPv4 address formats */
343 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.1", no_proxy_value, false));
344 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168..1", no_proxy_value, false));
345 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.1.", no_proxy_value, false));
346 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, ".192.168.1", no_proxy_value, false));
347
348 /* Test with invalid IPv6 address variants */
349 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[2001:db8::xyz]", no_proxy_value, false));
350 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[2001:db8::]:", no_proxy_value, false));
351 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "2001:db8:::", no_proxy_value, false));
352
353 /* Test with malformed IPv6 address brackets */
354 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[2001:db8::1", no_proxy_value, false));
355 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "2001:db8::1]", no_proxy_value, false));
356 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[[2001:db8::1]]", no_proxy_value, false));
357
358 /* Test with empty host */
359 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "", no_proxy_value, false));
360
361 /* Test with extremely long host that exceeds buffer sizes */
362 char long_host[1024];
363 memset(long_host, 'a', sizeof(long_host) - 1);
364 long_host[sizeof(long_host) - 1] = '\0';
365 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, long_host, no_proxy_value, false));
366
367 /* Test with extremely long IPv4 address that would hit buffer checks */
368 char long_ipv4[150] = "192.168.1.1";
369 for (int i = 0; i < 130; i++) {
370 long_ipv4[11 + i] = '9'; /* Padding with extra digits */
371 }
372 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, long_ipv4, no_proxy_value, false));
373
374 /* Test with extremely long IPv6 address that would hit buffer checks */
375 char long_ipv6[150] = "[2001:db8::1";
376 for (int i = 0; i < 130; i++) {
377 long_ipv6[11 + i] = '1'; /* Padding with extra digits */
378 }
379 long_ipv6[141] = ']';
380 long_ipv6[142] = '\0';
381 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, long_ipv6, no_proxy_value, false));
382
383 aws_http_library_clean_up();
384 return AWS_OP_SUCCESS;
385 }
386 AWS_TEST_CASE(test_no_proxy_invalid_host_inputs, s_test_no_proxy_invalid_host_inputs);
387
388 /**
389 * Test behavior when NO_PROXY is empty
390 *
391 * Verifies that the aws_http_host_matches_no_proxy function correctly handles cases where
392 * the NO_PROXY value is empty.
393 */
394 static int s_test_no_proxy_empty(struct aws_allocator *allocator, void *ctx) {
395 (void)ctx;
396
397 aws_http_library_init(allocator);
398
399 /* Test with empty NO_PROXY */
400 const char *empty_no_proxy_value = "";
401
402 /* With empty NO_PROXY, aws_http_host_matches_no_proxy should return false for any host */
403 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "example.com", empty_no_proxy_value, false));
404 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "192.168.1.1", empty_no_proxy_value, false));
405 ASSERT_SUCCESS(s_test_no_proxy_helper(allocator, "[2001:db8::1]", empty_no_proxy_value, false));
406
407 aws_http_library_clean_up();
408 return AWS_OP_SUCCESS;
409 }
410 AWS_TEST_CASE(test_no_proxy_empty, s_test_no_proxy_empty);
411
412 /**
413 * Test using the direct no_proxy parameter
414 *
415 * Verifies that the aws_http_host_matches_no_proxy function correctly uses the provided no_proxy parameter.
416 */
417 static int s_test_no_proxy_direct_parameter(struct aws_allocator *allocator, void *ctx) {
418 (void)ctx;
419
420 aws_http_library_init(allocator);
421
422 /* Create different no_proxy values to test with */
423 const char *direct_no_proxy_value = "direct.example.com";
424 const char *wildcard_no_proxy_value = "*";
425
426 /* Test with specific host */
427 ASSERT_TRUE(aws_http_host_matches_no_proxy(
428 allocator,
429 aws_byte_cursor_from_c_str("direct.example.com"),
430 aws_string_new_from_c_str(allocator, direct_no_proxy_value)));
431
432 ASSERT_FALSE(aws_http_host_matches_no_proxy(
433 allocator,
434 aws_byte_cursor_from_c_str("other.example.com"),
435 aws_string_new_from_c_str(allocator, direct_no_proxy_value)));
436
437 /* Test with wildcard */
438 ASSERT_TRUE(aws_http_host_matches_no_proxy(
439 allocator,
440 aws_byte_cursor_from_c_str("any.host.com"),
441 aws_string_new_from_c_str(allocator, wildcard_no_proxy_value)));
442
443 aws_http_library_clean_up();
444 return AWS_OP_SUCCESS;
445 }
446 AWS_TEST_CASE(test_no_proxy_direct_parameter, s_test_no_proxy_direct_parameter);