Debian Codegraph @

crt/aws-c-http/source/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 #include <aws/common/byte_order.h>
6 #include <aws/common/environment.h>
7 #include <aws/http/private/no_proxy.h>
8 #include <aws/io/socket.h>
9
10 enum hostname_type {
11 HOSTNAME_TYPE_IPV4,
12 HOSTNAME_TYPE_IPV6,
13 HOSTNAME_TYPE_REGULAR,
14 };
15
16 /**
17 * s_cidr4_match() returns true if the given IPv4 address is within the
18 * specified CIDR address range.
19 * Based on the curl implementation Curl_cidr4_match().
20 *
21 * @param bits The number of network bits in the CIDR notation
22 * @param network_part The network pattern to match against (e.g., "192.168.0.0")\
23 * @param host_addr Pre-parsed binary representation of the host IP, or NULL to parse from host
24 * @return true if the IP address matches the CIDR pattern, false otherwise
25 */
26 static bool s_cidr4_match(uint64_t bits, struct aws_string *network_part, uint32_t address) {
27
28 uint32_t check = 0;
29
30 /* Check for valid bits parameter */
31 if (bits > 32) {
32 /* Invalid netmask bits */
33 return false;
34 }
35
36 /* Convert network pattern to binary */
37 if (aws_parse_ipv4_address(network_part, &check) != AWS_OP_SUCCESS) {
38 return false;
39 }
40
41 if (bits > 0 && bits < 32) {
42 /* Apply the network mask for CIDR comparison */
43 uint32_t mask = 0xffffffff << (32 - bits);
44 uint32_t host_network = aws_ntoh32(address);
45 uint32_t check_network = aws_ntoh32(check);
46
47 /* Compare the masked addresses */
48 return (host_network & mask) == (check_network & mask);
49 }
50
51 /* For /32 or no bits specified, use exact match */
52 return address == check;
53 }
54
55 /**
56 * s_cidr6_match() returns true if the given IPv6 address is within the
57 * specified CIDR address range.
58 * Based on the curl implementation Curl_cidr6_match().
59 *
60 * @param bits The number of network bits in the CIDR notation
61 * @param network_part The network pattern to match against (e.g., "2001:db8::")
62 * @param host_addr Pre-parsed binary representation of the host IP, or NULL to parse from host
63 * @return true if the IP address matches the CIDR pattern, false otherwise
64 */
65 static bool s_cidr6_match(
66 struct aws_allocator *allocator,
67 uint64_t bits,
68 struct aws_string *network_part,
69 struct aws_byte_cursor address) {
70 bool result = false;
71 struct aws_byte_buf check_buf;
72 aws_byte_buf_init(&check_buf, allocator, 16);
73 /* If no bits specified, use full 128 bits for IPv6 */
74 if (!bits) {
75 bits = 128;
76 }
77
78 /* Check for valid bits parameter */
79 if (bits > 128) {
80 goto cleanup;
81 }
82 /* Convert network pattern to binary */
83 if (aws_parse_ipv6_address(network_part, &check_buf) != AWS_OP_SUCCESS) {
84 goto cleanup;
85 }
86 struct aws_byte_cursor check = aws_byte_cursor_from_buf(&check_buf);
87
88 /* Calculate full bytes and remaining bits in the netmask */
89 uint64_t bytes = bits / 8;
90 uint64_t rest = bits % 8;
91 if (bytes > address.len || address.len != check_buf.len || check_buf.len != 16) {
92 goto cleanup;
93 }
94 if (bytes > 0 && !aws_array_eq(address.ptr, (size_t)bytes, check.ptr, (size_t)bytes)) {
95 goto cleanup;
96 }
97
98 /* If we have remaining bits, compare the partial byte */
99 if (rest > 0) {
100 /* Create a mask for the remaining bits */
101 unsigned char mask = (unsigned char)(0xff << (8 - rest));
102 aws_byte_cursor_advance(&check, (size_t)bytes);
103 aws_byte_cursor_advance(&address, (size_t)bytes);
104 uint8_t address_byte = 0;
105 uint8_t check_byte = 0;
106 if (aws_byte_cursor_read_u8(&address, &address_byte) == false ||
107 aws_byte_cursor_read_u8(&check, &check_byte) == false) {
108 goto cleanup;
109 }
110 /* Check if the masked bits match */
111 if ((address_byte & mask) != (check_byte & mask)) {
112 goto cleanup;
113 }
114 }
115
116 /* All checks passed, addresses match within the CIDR range */
117 result = true;
118 cleanup:
119 aws_byte_buf_clean_up(&check_buf);
120 return result;
121 }
122
123 static bool s_is_dot(uint8_t c) {
124 return c == '.';
125 }
126
127 /* The host is expected to be the host result from URL parser. */
128 bool aws_http_host_matches_no_proxy(
129 struct aws_allocator *allocator,
130 struct aws_byte_cursor host,
131 struct aws_string *no_proxy_str) {
132 if (host.len == 0 || no_proxy_str == NULL) {
133 return false;
134 }
135 /* Single "*" wildcard matches all hosts */
136 if (aws_string_eq_c_str(no_proxy_str, "*")) {
137 AWS_LOGF_DEBUG(AWS_LS_HTTP_CONNECTION, "wildcard no_proxy found, bypassing any proxy");
138 return true;
139 }
140 bool bypass = false;
141 struct aws_byte_cursor no_proxy_cur = aws_byte_cursor_from_string(no_proxy_str);
142 struct aws_array_list no_proxy_list;
143 struct aws_string *host_str = aws_string_new_from_cursor(allocator, &host);
144 struct aws_byte_buf ipv6_addr = {0};
145
146 if (aws_array_list_init_dynamic(&no_proxy_list, allocator, 10, sizeof(struct aws_byte_cursor))) {
147 goto cleanup;
148 }
149 /* Split the NO_PROXY string by commas */
150 if (aws_byte_cursor_split_on_char(&no_proxy_cur, ',', &no_proxy_list)) {
151 goto cleanup;
152 }
153
154 /* Store parsed binary addresses for reuse */
155 uint32_t ipv4_addr = 0;
156
157 /* Determine host type and parse address if applicable */
158 enum hostname_type type = HOSTNAME_TYPE_REGULAR;
159 if (aws_parse_ipv4_address(host_str, &ipv4_addr) == AWS_OP_SUCCESS) {
160 type = HOSTNAME_TYPE_IPV4;
161 } else {
162 struct aws_string *host_str_copy = host_str;
163 struct aws_byte_cursor host_copy = host;
164 if (host_copy.ptr[0] == '[' && host_copy.ptr[host_copy.len - 1] == ']') {
165 /* Check if the address is enclosed in brackets and strip them for validation */
166 aws_byte_cursor_advance(&host_copy, 1);
167 host_copy.len--;
168 host_str_copy = aws_string_new_from_cursor(allocator, &host_copy);
169 }
170
171 aws_byte_buf_init(&ipv6_addr, allocator, 16);
172 if (aws_parse_ipv6_address(host_str_copy, &ipv6_addr) == AWS_OP_SUCCESS) {
173 /* Update the host str */
174 if (host_str != host_str_copy) {
175 aws_string_destroy(host_str);
176 host_str = host_str_copy;
177 }
178 type = HOSTNAME_TYPE_IPV6;
179 } else {
180 /* Not an IP address, so it's a regular hostname */
181 type = HOSTNAME_TYPE_REGULAR;
182 /* Ignore the trailing dot in the hostname */
183 host = aws_byte_cursor_right_trim_pred(&host, s_is_dot);
184 }
185 if (host_str != host_str_copy) {
186 /* clean up the copy, but don't update the str. */
187 aws_string_destroy(host_str_copy);
188 }
189 }
190
191 for (size_t i = 0; i < aws_array_list_length(&no_proxy_list); i++) {
192 struct aws_byte_cursor pattern;
193 if (aws_array_list_get_at(&no_proxy_list, &pattern, i)) {
194 continue;
195 }
196
197 /* Trim whitespace from both ends for the pattern */
198 pattern = aws_byte_cursor_trim_pred(&pattern, aws_isspace);
199 if (pattern.len == 0) {
200 /* If pattern is empty, ignore it. */
201 continue;
202 }
203 switch (type) {
204 case HOSTNAME_TYPE_REGULAR: {
205 /**
206 * A: example.com matches 'example.com'
207 * B: www.example.com matches 'example.com'
208 * C: nonexample.com DOES NOT match 'example.com'
209 */
210 /* Trim dot from both ends for the pattern */
211 pattern = aws_byte_cursor_trim_pred(&pattern, s_is_dot);
212 if (pattern.len == 0) {
213 /* If pattern is empty, ignore it. */
214 continue;
215 }
216 if (pattern.len == host.len) {
217 if (aws_byte_cursor_eq_ignore_case(&pattern, &host)) {
218 bypass = true;
219 goto cleanup;
220 } else {
221 continue;
222 }
223 } else if (pattern.len < host.len) {
224 /* Check if the pattern is a suffix of the host. All the math is safe since pattern.len <
225 * host.len
226 */
227 struct aws_byte_cursor tail_with_extra_byte = host;
228 /* 1. the byte before the tail should be `.` */
229 aws_byte_cursor_advance(&tail_with_extra_byte, host.len - pattern.len - 1);
230 uint8_t var = 0;
231 /* tail_with_extra_byte will be updated to move over the `.` */
232 aws_byte_cursor_read_u8(&tail_with_extra_byte, &var);
233 if (var != '.') {
234 continue;
235 }
236 /* 2. the tail of the host should match the pattern */
237 if (aws_byte_cursor_eq_ignore_case(&pattern, &tail_with_extra_byte)) {
238 bypass = true;
239 goto cleanup;
240 } else {
241 continue;
242 }
243 }
244 } break;
245 case HOSTNAME_TYPE_IPV4:
246 case HOSTNAME_TYPE_IPV6: {
247 /* Extract network part and bits from CIDR notation */
248 struct aws_byte_cursor substr = {0};
249 struct aws_byte_cursor network_part = {0};
250 /* CIDR found. parse the bits */
251 uint64_t network_bits = 0;
252 if (aws_byte_cursor_next_split(&pattern, '/', &substr)) {
253 network_part = substr;
254 }
255 if (aws_byte_cursor_next_split(&pattern, '/', &substr)) {
256 /* There is a second part of the pattern after `/`. */
257 /* Now, take the rest of the pattern after `/` as the bits */
258 aws_byte_cursor_advance(&pattern, network_part.len + 1);
259 if (aws_byte_cursor_utf8_parse_u64(pattern, &network_bits)) {
260 continue;
261 }
262 }
263 struct aws_string *network_part_str = aws_string_new_from_cursor(allocator, &network_part);
264 if (type == HOSTNAME_TYPE_IPV4) {
265 if (s_cidr4_match(network_bits, network_part_str, ipv4_addr)) {
266 bypass = true;
267 aws_string_destroy(network_part_str);
268 goto cleanup;
269 }
270 } else {
271 if (s_cidr6_match(
272 allocator, network_bits, network_part_str, aws_byte_cursor_from_buf(&ipv6_addr))) {
273 bypass = true;
274 aws_string_destroy(network_part_str);
275 goto cleanup;
276 }
277 }
278 aws_string_destroy(network_part_str);
279 } break;
280
281 default:
282 /* Invalid stage */
283 AWS_FATAL_ASSERT(false);
284 break;
285 }
286 }
287
288 cleanup:
289 aws_byte_buf_clean_up(&ipv6_addr);
290 aws_string_destroy(host_str);
291 aws_array_list_clean_up(&no_proxy_list);
292 return bypass;
293 }