| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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 |
|
| 31 |
if (bits > 32) { |
| 32 |
|
| 33 |
return false; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
if (aws_parse_ipv4_address(network_part, &check) != AWS_OP_SUCCESS) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
if (bits > 0 && bits < 32) { |
| 42 |
|
| 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 |
|
| 48 |
return (host_network & mask) == (check_network & mask); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
return address == check; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 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 |
|
| 74 |
if (!bits) { |
| 75 |
bits = 128; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
if (bits > 128) { |
| 80 |
goto cleanup; |
| 81 |
} |
| 82 |
|
| 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 |
|
| 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 |
|
| 99 |
if (rest > 0) { |
| 100 |
|
| 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 |
|
| 111 |
if ((address_byte & mask) != (check_byte & mask)) { |
| 112 |
goto cleanup; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 150 |
if (aws_byte_cursor_split_on_char(&no_proxy_cur, ',', &no_proxy_list)) { |
| 151 |
goto cleanup; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
uint32_t ipv4_addr = 0; |
| 156 |
|
| 157 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 181 |
type = HOSTNAME_TYPE_REGULAR; |
| 182 |
|
| 183 |
host = aws_byte_cursor_right_trim_pred(&host, s_is_dot); |
| 184 |
} |
| 185 |
if (host_str != host_str_copy) { |
| 186 |
|
| 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 |
|
| 198 |
pattern = aws_byte_cursor_trim_pred(&pattern, aws_isspace); |
| 199 |
if (pattern.len == 0) { |
| 200 |
|
| 201 |
continue; |
| 202 |
} |
| 203 |
switch (type) { |
| 204 |
case HOSTNAME_TYPE_REGULAR: { |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
pattern = aws_byte_cursor_trim_pred(&pattern, s_is_dot); |
| 212 |
if (pattern.len == 0) { |
| 213 |
|
| 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 |
|
| 225 |
|
| 226 |
|
| 227 |
struct aws_byte_cursor tail_with_extra_byte = host; |
| 228 |
|
| 229 |
aws_byte_cursor_advance(&tail_with_extra_byte, host.len - pattern.len - 1); |
| 230 |
uint8_t var = 0; |
| 231 |
|
| 232 |
aws_byte_cursor_read_u8(&tail_with_extra_byte, &var); |
| 233 |
if (var != '.') { |
| 234 |
continue; |
| 235 |
} |
| 236 |
|
| 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 |
|
| 248 |
struct aws_byte_cursor substr = {0}; |
| 249 |
struct aws_byte_cursor network_part = {0}; |
| 250 |
|
| 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 |
|
| 257 |
|
| 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 |
|
| 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 |
} |