1 /* 2 * request - HTTP request processing 3 * 4 * Fully-rewritten from original EXCEPT for request_check_hostname() 5 * Copyright(c) 2018 Glenn Strauss gstrauss()gluelogic.com All rights reserved 6 * License: BSD 3-clause (same as lighttpd) 7 */ 8 #include "first.h" 9 10 #include "request.h" 11 #include "burl.h" 12 #include "http_header.h" 13 #include "http_kv.h" 14 #include "log.h" 15 #include "sock_addr.h" 16 17 #include <limits.h> 18 #include <stdint.h> 19 #include <stdlib.h> 20 #include <string.h> 21 22 static int request_check_hostname(buffer * const host) { 23 enum { DOMAINLABEL, TOPLABEL } stage = TOPLABEL; 24 size_t i; 25 int label_len = 0; 26 size_t host_len, hostport_len; 27 char *colon; 28 int is_ip = -1; /* -1 don't know yet, 0 no, 1 yes */ 29 int level = 0; 30 31 /* 32 * hostport = host [ ":" port ] 33 * host = hostname | IPv4address | IPv6address 34 * hostname = *( domainlabel "." ) toplabel [ "." ] 35 * domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum 36 * toplabel = alpha | alpha *( alphanum | "-" ) alphanum 37 * IPv4address = 1*digit "." 1*digit "." 1*digit "." 1*digit 38 * IPv6address = "[" ... "]" 39 * port = *digit 40 */ 41 42 /* IPv6 address */ 43 if (host->ptr[0] == '[') { 44 char *c = host->ptr + 1; 45 int colon_cnt = 0; 46 47 /* check the address inside [...] */ 48 for (; *c && *c != ']'; c++) { 49 if (*c == ':') { 50 if (++colon_cnt > 7) { 51 return -1; 52 } 53 } else if (!light_isxdigit(*c) && '.' != *c) { 54 return -1; 55 } 56 } 57 58 /* missing ] */ 59 if (!*c) { 60 return -1; 61 } 62 63 /* check port */ 64 if (*(c+1) == ':') { 65 for (c += 2; *c; c++) { 66 if (!light_isdigit(*c)) { 67 return -1; 68 } 69 } 70 } 71 else if ('\0' != *(c+1)) { 72 /* only a port is allowed to follow [...] */ 73 return -1; 74 } 75 return 0; 76 } 77 78 hostport_len = host_len = buffer_string_length(host); 79 80 if (NULL != (colon = memchr(host->ptr, ':', host_len))) { 81 char *c = colon + 1; 82 83 /* check portnumber */ 84 for (; *c; c++) { 85 if (!light_isdigit(*c)) return -1; 86 } 87 88 /* remove the port from the host-len */ 89 host_len = colon - host->ptr; 90 } 91 92 /* Host is empty */ 93 if (host_len == 0) return -1; 94 95 /* if the hostname ends in a "." strip it */ 96 if (host->ptr[host_len-1] == '.') { 97 /* shift port info one left */ 98 if (NULL != colon) memmove(colon-1, colon, hostport_len - host_len); 99 buffer_string_set_length(host, --hostport_len); 100 if (--host_len == 0) return -1; 101 } 102 103 104 /* scan from the right and skip the \0 */ 105 for (i = host_len; i-- > 0; ) { 106 const char c = host->ptr[i]; 107 108 switch (stage) { 109 case TOPLABEL: 110 if (c == '.') { 111 /* only switch stage, if this is not the last character */ 112 if (i != host_len - 1) { 113 if (label_len == 0) { 114 return -1; 115 } 116 117 /* check the first character at right of the dot */ 118 if (is_ip == 0) { 119 if (!light_isalnum(host->ptr[i+1])) { 120 return -1; 121 } 122 } else if (!light_isdigit(host->ptr[i+1])) { 123 is_ip = 0; 124 } else if ('-' == host->ptr[i+1]) { 125 return -1; 126 } else { 127 /* just digits */ 128 is_ip = 1; 129 } 130 131 stage = DOMAINLABEL; 132 133 label_len = 0; 134 level++; 135 } else if (i == 0) { 136 /* just a dot and nothing else is evil */ 137 return -1; 138 } 139 } else if (i == 0) { 140 /* the first character of the hostname */ 141 if (!light_isalnum(c)) { 142 return -1; 143 } 144 label_len++; 145 } else { 146 if (c != '-' && !light_isalnum(c)) { 147 return -1; 148 } 149 if (is_ip == -1) { 150 if (!light_isdigit(c)) is_ip = 0; 151 } 152 label_len++; 153 } 154 155 break; 156 case DOMAINLABEL: 157 if (is_ip == 1) { 158 if (c == '.') { 159 if (label_len == 0) { 160 return -1; 161 } 162 163 label_len = 0; 164 level++; 165 } else if (!light_isdigit(c)) { 166 return -1; 167 } else { 168 label_len++; 169 } 170 } else { 171 if (c == '.') { 172 if (label_len == 0) { 173 return -1; 174 } 175 176 /* c is either - or alphanum here */ 177 if ('-' == host->ptr[i+1]) { 178 return -1; 179 } 180 181 label_len = 0; 182 level++; 183 } else if (i == 0) { 184 if (!light_isalnum(c)) { 185 return -1; 186 } 187 label_len++; 188 } else { 189 if (c != '-' && !light_isalnum(c)) { 190 return -1; 191 } 192 label_len++; 193 } 194 } 195 196 break; 197 } 198 } 199 200 /* a IP has to consist of 4 parts */ 201 if (is_ip == 1 && level != 3) { 202 return -1; 203 } 204 205 if (label_len == 0) { 206 return -1; 207 } 208 209 return 0; 210 } 211 212 int http_request_host_normalize(buffer * const b, const int scheme_port) { 213 /* 214 * check for and canonicalize numeric IP address and portnum (optional) 215 * (IP address may be followed by ":portnum" (optional)) 216 * - IPv6: "[...]" 217 * - IPv4: "x.x.x.x" 218 * - IPv4: 12345678 (32-bit decimal number) 219 * - IPv4: 012345678 (32-bit octal number) 220 * - IPv4: 0x12345678 (32-bit hex number) 221 * 222 * allow any chars (except ':' and '\0' and stray '[' or ']') 223 * (other code may check chars more strictly or more pedantically) 224 * ':' delimits (optional) port at end of string 225 * "[]" wraps IPv6 address literal 226 * '\0' should have been rejected earlier were it present 227 * 228 * any chars includes, but is not limited to: 229 * - allow '-' any where, even at beginning of word 230 * (security caution: might be confused for cmd flag if passed to shell) 231 * - allow all-digit TLDs 232 * (might be mistaken for IPv4 addr by inet_aton() 233 * unless non-digits appear in subdomain) 234 */ 235 236 /* Note: not using getaddrinfo() since it does not support "[]" around IPv6 237 * and is not as lenient as inet_aton() and inet_addr() for IPv4 strings. 238 * Not using inet_pton() (when available) on IPv4 for similar reasons. */ 239 240 const char * const p = b->ptr; 241 const size_t blen = buffer_string_length(b); 242 long port = 0; 243 244 if (*p != '[') { 245 char * const colon = (char *)memchr(p, ':', blen); 246 if (colon) { 247 if (*p == ':') return -1; /*(empty host then port, or naked IPv6)*/ 248 if (colon[1] != '\0') { 249 char *e; 250 port = strtol(colon+1, &e, 0); /*(allow decimal, octal, hex)*/ 251 if (0 < port && port <= USHRT_MAX && *e == '\0') { 252 /* valid port */ 253 } else { 254 return -1; 255 } 256 } /*(else ignore stray colon at string end)*/ 257 buffer_string_set_length(b, (size_t)(colon - p)); /*(remove port str)*/ 258 } 259 260 if (light_isdigit(*p)) do { 261 /* (IPv4 address literal or domain starting w/ digit (e.g. 3com))*/ 262 /* (check one-element cache of normalized IPv4 address string) */ 263 static struct { char s[INET_ADDRSTRLEN]; size_t n; } laddr; 264 size_t n = colon ? (size_t)(colon - p) : blen; 265 sock_addr addr; 266 if (n == laddr.n && 0 == memcmp(p, laddr.s, n)) break; 267 if (1 == sock_addr_inet_pton(&addr, p, AF_INET, 0)) { 268 sock_addr_inet_ntop_copy_buffer(b, &addr); 269 n = buffer_string_length(b); 270 if (n < sizeof(laddr.s)) memcpy(laddr.s, b->ptr, (laddr.n = n)); 271 } 272 } while (0); 273 } else do { /* IPv6 addr */ 274 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON) 275 276 /* (check one-element cache of normalized IPv4 address string) */ 277 static struct { char s[INET6_ADDRSTRLEN]; size_t n; } laddr; 278 sock_addr addr; 279 char *bracket = b->ptr+blen-1; 280 char *percent = strchr(b->ptr+1, '%'); 281 size_t len; 282 int rc; 283 char buf[INET6_ADDRSTRLEN+16]; /*(+16 for potential %interface name)*/ 284 if (blen <= 2) return -1; /*(invalid "[]")*/ 285 if (*bracket != ']') { 286 bracket = (char *)memchr(b->ptr+1, ']', blen-1); 287 if (NULL == bracket || bracket[1] != ':' || bracket - b->ptr == 1){ 288 return -1; 289 } 290 if (bracket[2] != '\0') { /*(ignore stray colon at string end)*/ 291 char *e; 292 port = strtol(bracket+2, &e, 0); /*(allow decimal, octal, hex)*/ 293 if (0 < port && port <= USHRT_MAX && *e == '\0') { 294 /* valid port */ 295 } else { 296 return -1; 297 } 298 } 299 } 300 301 len = (size_t)((percent ? percent : bracket) - (b->ptr+1)); 302 if (laddr.n == len && 0 == memcmp(laddr.s, b->ptr+1, len)) { 303 /* truncate after ']' and re-add normalized port, if needed */ 304 buffer_string_set_length(b, (size_t)(bracket - b->ptr + 1)); 305 break; 306 } 307 308 *bracket = '\0';/*(terminate IPv6 string)*/ 309 if (percent) *percent = '\0'; /*(remove %interface from address)*/ 310 rc = sock_addr_inet_pton(&addr, b->ptr+1, AF_INET6, 0); 311 if (percent) *percent = '%'; /*(restore %interface)*/ 312 *bracket = ']'; /*(restore bracket)*/ 313 if (1 != rc) return -1; 314 315 sock_addr_inet_ntop(&addr, buf, sizeof(buf)); 316 len = strlen(buf); 317 if (percent) { 318 if (percent > bracket) return -1; 319 if (len + (size_t)(bracket - percent) >= sizeof(buf)) return -1; 320 if (len < sizeof(laddr.s)) memcpy(laddr.s, buf, (laddr.n = len)); 321 memcpy(buf+len, percent, (size_t)(bracket - percent)); 322 len += (size_t)(bracket - percent); 323 } 324 buffer_string_set_length(b, 1); /* truncate after '[' */ 325 buffer_append_string_len(b, buf, len); 326 buffer_append_string_len(b, CONST_STR_LEN("]")); 327 328 #else 329 330 return -1; 331 332 #endif 333 } while (0); 334 335 if (0 != port && port != scheme_port) { 336 buffer_append_string_len(b, CONST_STR_LEN(":")); 337 buffer_append_int(b, (int)port); 338 } 339 340 return 0; 341 } 342 343 int http_request_host_policy (buffer * const b, const unsigned int http_parseopts, const int scheme_port) { 344 return (((http_parseopts & HTTP_PARSEOPT_HOST_STRICT) 345 && 0 != request_check_hostname(b)) 346 || ((http_parseopts & HTTP_PARSEOPT_HOST_NORMALIZE) 347 && 0 != http_request_host_normalize(b, scheme_port))); 348 } 349 350 __attribute_pure__ /*(could be even more strict and use __attribute_const__)*/ 351 static int request_uri_is_valid_char(const unsigned char c) { 352 return (c > 32 && c != 127 && c != 255); 353 } 354 355 __attribute_cold__ 356 __attribute_noinline__ 357 static int http_request_header_line_invalid(request_st * const restrict r, const int status, const char * const restrict msg) { 358 if (r->conf.log_request_header_on_error) { 359 if (msg) log_error(r->conf.errh, __FILE__, __LINE__, "%s", msg); 360 } 361 return status; 362 } 363 364 __attribute_cold__ 365 __attribute_noinline__ 366 static int http_request_header_char_invalid(request_st * const restrict r, const char ch, const char * const restrict msg) { 367 if (r->conf.log_request_header_on_error) { 368 if ((unsigned char)ch > 32 && ch != 127) { 369 log_error(r->conf.errh, __FILE__, __LINE__, "%s ('%c')", msg, ch); 370 } 371 else { 372 log_error(r->conf.errh, __FILE__, __LINE__, "%s (0x%x)", msg, ch); 373 } 374 } 375 return 400; 376 } 377 378 379 static int64_t 380 li_restricted_strtoint64 (const char *v, const uint32_t vlen, const char ** const err) 381 { 382 /* base 10 strtoll() parsing exactly vlen chars and requiring digits 0-9 */ 383 /* rejects negative numbers and considers values > INT64_MAX an error */ 384 /* note: errno is not set; detect error if *err != v+vlen upon return */ 385 /*(caller must check 0 == vlen if that is to be an error for caller)*/ 386 int64_t rv = 0; 387 uint32_t i; 388 for (i = 0; i < vlen; ++i) { 389 const uint8_t c = ((uint8_t *)v)[i] - '0'; /*(unsigned; underflow ok)*/ 390 if (c > 9) break; 391 if (rv > INT64_MAX/10) break; 392 rv *= 10; 393 if (rv > INT64_MAX - c) break; 394 rv += c; 395 } 396 *err = v+i; 397 return rv; 398 } 399 400 401 /* add header to list of headers 402 * certain headers are also parsed 403 * might drop a header if deemed unnecessary/broken 404 * 405 * returns 0 on success, HTTP status on error 406 */ 407 static int http_request_parse_single_header(request_st * const restrict r, const enum http_header_e id, const char * const restrict k, const size_t klen, const char * const restrict v, const size_t vlen) { 408 buffer **saveb = NULL; 409 410 /* 411 * Note: k might not be '\0'-terminated 412 * Note: v is not '\0'-terminated 413 * With lighttpd HTTP/1.1 parser, v ends with whitespace 414 * (one of '\r' '\n' ' ' '\t') 415 * With lighttpd HTTP/2 parser, v should not be accessed beyond vlen 416 * (care must be taken to avoid libc funcs which expect z-strings) 417 */ 418 /*assert(vlen);*//*(caller must not call this func with 0 klen or 0 vlen)*/ 419 420 switch (id) { 421 /*case HTTP_HEADER_OTHER:*/ 422 default: 423 break; 424 case HTTP_HEADER_HOST: 425 if (!(r->rqst_htags & HTTP_HEADER_HOST)) { 426 saveb = &r->http_host; 427 if (vlen >= 1024) { /*(expecting < 256)*/ 428 return http_request_header_line_invalid(r, 400, "uri-authority too long -> 400"); 429 } 430 } 431 else if (NULL != r->http_host 432 && buffer_is_equal_string(r->http_host, v, vlen)) { 433 /* ignore all Host: headers if match authority in request line */ 434 return 0; /* ignore header */ 435 } 436 else { 437 return http_request_header_line_invalid(r, 400, "duplicate Host header -> 400"); 438 } 439 break; 440 case HTTP_HEADER_CONNECTION: 441 /* "Connection: close" is common case if header is present */ 442 if ((vlen == 5 && buffer_eq_icase_ssn(v, CONST_STR_LEN("close"))) 443 || http_header_str_contains_token(v,vlen,CONST_STR_LEN("close"))) { 444 r->keep_alive = 0; 445 break; 446 } 447 if (http_header_str_contains_token(v,vlen,CONST_STR_LEN("keep-alive"))){ 448 r->keep_alive = 1; 449 break; 450 } 451 break; 452 case HTTP_HEADER_CONTENT_TYPE: 453 if (r->rqst_htags & HTTP_HEADER_CONTENT_TYPE) { 454 return http_request_header_line_invalid(r, 400, "duplicate Content-Type header -> 400"); 455 } 456 break; 457 case HTTP_HEADER_IF_NONE_MATCH: 458 /* if dup, only the first one will survive */ 459 if (r->rqst_htags & HTTP_HEADER_IF_NONE_MATCH) { 460 return 0; /* ignore header */ 461 } 462 break; 463 case HTTP_HEADER_CONTENT_LENGTH: 464 if (!(r->rqst_htags & HTTP_HEADER_CONTENT_LENGTH)) { 465 /*(trailing whitespace was removed from vlen)*/ 466 /*(not using strtoll() since v might not be z-string)*/ 467 const char *err; 468 off_t clen = (off_t)li_restricted_strtoint64(v, vlen, &err); 469 if (err == v+vlen) { 470 /* (set only if not set to -1 by Transfer-Encoding: chunked) */ 471 if (0 == r->reqbody_length) r->reqbody_length = clen; 472 } 473 else { 474 return http_request_header_line_invalid(r, 400, "invalid Content-Length header -> 400"); 475 } 476 } 477 else { 478 return http_request_header_line_invalid(r, 400, "duplicate Content-Length header -> 400"); 479 } 480 break; 481 case HTTP_HEADER_HTTP2_SETTINGS: 482 if (r->rqst_htags & HTTP_HEADER_HTTP2_SETTINGS) { 483 return http_request_header_line_invalid(r, 400, "duplicate HTTP2-Settings header -> 400"); 484 } 485 break; 486 case HTTP_HEADER_IF_MODIFIED_SINCE: 487 if (r->rqst_htags & HTTP_HEADER_IF_MODIFIED_SINCE) { 488 /* Proxies sometimes send dup headers 489 * if they are the same we ignore the second 490 * if not, we raise an error */ 491 const buffer *vb = 492 http_header_request_get(r, HTTP_HEADER_IF_MODIFIED_SINCE, 493 CONST_STR_LEN("If-Modified-Since")); 494 if (vb && buffer_is_equal_caseless_string(vb, v, vlen)) { 495 /* ignore it if they are the same */ 496 return 0; /* ignore header */ 497 } 498 else { 499 return http_request_header_line_invalid(r, 400, "duplicate If-Modified-Since header -> 400"); 500 } 501 } 502 break; 503 case HTTP_HEADER_TRANSFER_ENCODING: 504 if (HTTP_VERSION_1_1 != r->http_version) { 505 return http_request_header_line_invalid(r, 400, 506 HTTP_VERSION_1_0 == r->http_version 507 ? "HTTP/1.0 with Transfer-Encoding (bad HTTP/1.0 proxy?) -> 400" 508 : "HTTP/2 with Transfer-Encoding is invalid -> 400"); 509 } 510 511 if (!buffer_eq_icase_ss(v, vlen, CONST_STR_LEN("chunked"))) { 512 /* Transfer-Encoding might contain additional encodings, 513 * which are not currently supported by lighttpd */ 514 return http_request_header_line_invalid(r, 501, NULL); /* Not Implemented */ 515 } 516 r->reqbody_length = -1; 517 518 /* Transfer-Encoding is a hop-by-hop header, 519 * which must not be blindly forwarded to backends */ 520 return 0; /* skip header */ 521 } 522 523 http_header_request_append(r, id, k, klen, v, vlen); 524 525 if (saveb) { 526 *saveb = http_header_request_get(r, id, k, klen); 527 } 528 529 return 0; 530 } 531 532 __attribute_cold__ 533 static int http_request_parse_proto_loose(request_st * const restrict r, const char * const restrict ptr, const size_t len, const unsigned int http_parseopts) { 534 const char * proto = memchr(ptr, ' ', len); 535 if (NULL == proto) 536 return http_request_header_line_invalid(r, 400, "incomplete request line -> 400"); 537 proto = memchr(proto+1, ' ', len - (proto+1 - ptr)); 538 if (NULL == proto) 539 return http_request_header_line_invalid(r, 400, "incomplete request line -> 400"); 540 ++proto; 541 542 if (proto[0]=='H' && proto[1]=='T' && proto[2]=='T' && proto[3]=='P' && proto[4] == '/') { 543 if (proto[5] == '1' && proto[6] == '.' && (proto[7] == '1' || proto[7] == '0')) { 544 /* length already checked before calling this routine */ 545 /* (len != (size_t)(proto - ptr + 8)) */ 546 if (http_parseopts & HTTP_PARSEOPT_HEADER_STRICT) /*(http_header_strict)*/ 547 return http_request_header_line_invalid(r, 400, "incomplete request line -> 400"); 548 r->http_version = (proto[7] == '1') ? HTTP_VERSION_1_1 : HTTP_VERSION_1_0; 549 } 550 else 551 return http_request_header_line_invalid(r, 505, "unknown HTTP version -> 505"); 552 } 553 else 554 return http_request_header_line_invalid(r, 400, "unknown protocol -> 400"); 555 556 /* keep-alive default: HTTP/1.1 -> true; HTTP/1.0 -> false */ 557 r->keep_alive = (HTTP_VERSION_1_0 != r->http_version); 558 559 return 0; 560 } 561 562 __attribute_cold__ 563 static const char * http_request_parse_reqline_uri(request_st * const restrict r, const char * const restrict uri, const size_t len, const unsigned int http_parseopts) { 564 const char *nuri; 565 if ((len > 7 && buffer_eq_icase_ssn(uri, "http://", 7) 566 && NULL != (nuri = memchr(uri + 7, '/', len-7))) 567 || 568 (len > 8 && buffer_eq_icase_ssn(uri, "https://", 8) 569 && NULL != (nuri = memchr(uri + 8, '/', len-8)))) { 570 const char * const host = uri + (uri[4] == ':' ? 7 : 8); 571 const size_t hostlen = nuri - host; 572 if (0 == hostlen || hostlen >= 1024) { /*(expecting < 256)*/ 573 http_request_header_line_invalid(r, 400, "uri-authority empty or too long -> 400"); 574 return NULL; 575 } 576 /* Insert as host header */ 577 http_header_request_set(r, HTTP_HEADER_HOST, CONST_STR_LEN("Host"), host, hostlen); 578 r->http_host = http_header_request_get(r, HTTP_HEADER_HOST, CONST_STR_LEN("Host")); 579 return nuri; 580 } else if (!(http_parseopts & HTTP_PARSEOPT_HEADER_STRICT) /*(!http_header_strict)*/ 581 || (HTTP_METHOD_CONNECT == r->http_method && (uri[0] == ':' || light_isdigit(uri[0]))) 582 || (HTTP_METHOD_OPTIONS == r->http_method && uri[0] == '*' && 1 == len)) { 583 /* (permitted) */ 584 return uri; 585 } else { 586 http_request_header_line_invalid(r, 400, "request-URI parse error -> 400"); 587 return NULL; 588 } 589 } 590 591 592 __attribute_cold__ 593 __attribute_noinline__ 594 static int http_request_parse_header_other(request_st * const restrict r, const char * const restrict k, const int klen, const unsigned int http_header_strict); 595 596 597 int 598 http_request_validate_pseudohdrs (request_st * const restrict r, const int scheme, const unsigned int http_parseopts) 599 { 600 /* :method is required to indicate method 601 * CONNECT method must have :method and :authority 602 * All other methods must have at least :method :scheme :path */ 603 604 if (HTTP_METHOD_UNSET == r->http_method) 605 return http_request_header_line_invalid(r, 400, 606 "missing pseudo-header method -> 400"); 607 608 if (HTTP_METHOD_CONNECT != r->http_method) { 609 if (!scheme) 610 return http_request_header_line_invalid(r, 400, 611 "missing pseudo-header scheme -> 400"); 612 613 if (buffer_string_is_empty(&r->target)) 614 return http_request_header_line_invalid(r, 400, 615 "missing pseudo-header path -> 400"); 616 617 const char * const uri = r->target.ptr; 618 if (*uri != '/') { /* (common case: (*uri == '/')) */ 619 if (uri[0] != '*' || uri[1] != '\0' 620 || HTTP_METHOD_OPTIONS != r->http_method) 621 return http_request_header_line_invalid(r, 400, 622 "invalid pseudo-header path -> 400"); 623 } 624 } 625 else { /* HTTP_METHOD_CONNECT */ 626 if (NULL == r->http_host) 627 return http_request_header_line_invalid(r, 400, 628 "missing pseudo-header authority -> 400"); 629 if (!buffer_string_is_empty(&r->target) || scheme) 630 return http_request_header_line_invalid(r, 400, 631 "invalid pseudo-header with CONNECT -> 400"); 632 /*(reuse uri and ulen to assign to r->target)*/ 633 buffer_copy_buffer(&r->target, r->http_host); 634 } 635 buffer_copy_buffer(&r->target_orig, &r->target); 636 637 /* r->http_host, if set, is checked with http_request_host_policy() 638 * in http_request_parse() */ 639 640 /* copied and modified from end of http_request_parse_reqline() */ 641 642 /* check uri for invalid characters */ 643 const unsigned int http_header_strict = 644 (http_parseopts & HTTP_PARSEOPT_HEADER_STRICT); 645 if (http_header_strict 646 && (http_parseopts & HTTP_PARSEOPT_URL_NORMALIZE_CTRLS_REJECT)) 647 return 0; /* URI will be checked in http_request_parse_target() */ 648 649 const uint32_t ulen = buffer_string_length(&r->target); 650 const uint8_t * const uri = (uint8_t *)r->target.ptr; 651 if (http_header_strict) { 652 for (uint32_t i = 0; i < ulen; ++i) { 653 if (!request_uri_is_valid_char(uri[i])) 654 return http_request_header_char_invalid(r, uri[i], 655 "invalid character in URI -> 400"); 656 } 657 } 658 else { 659 if (NULL != memchr(uri, '\0', ulen)) 660 return http_request_header_char_invalid(r, '\0', 661 "invalid character in header -> 400"); 662 } 663 664 return 0; 665 } 666 667 668 int 669 http_request_parse_header (request_st * const restrict r, http_header_parse_ctx * const restrict hpctx) 670 { 671 /* Note: k and v might not be '\0' terminated strings; 672 * care must be taken to avoid libc funcs which expect z-strings */ 673 const char * const restrict k = hpctx->k; 674 const char * const restrict v = hpctx->v; 675 const uint32_t klen = hpctx->klen; 676 const uint32_t vlen = hpctx->vlen; 677 678 if (0 == klen) 679 return http_request_header_line_invalid(r, 400, 680 "invalid header key -> 400"); 681 if (0 == vlen) 682 return http_request_header_line_invalid(r, 400, 683 "invalid header value -> 400"); 684 685 if ((hpctx->hlen += klen + vlen + 4) > hpctx->max_request_field_size) { 686 /*(configurable with server.max-request-field-size; default 8k)*/ 687 #if 1 /* emit to error log for people sending large headers */ 688 log_error(r->conf.errh, __FILE__, __LINE__, 689 "oversized request header -> 431"); 690 return 431; /* Request Header Fields Too Large */ 691 #else 692 /* 431 Request Header Fields Too Large */ 693 return http_request_header_line_invalid(r, 431, 694 "oversized request header -> 431"); 695 #endif 696 } 697 698 if (2 == klen && k[0] == 't' && k[1] == 'e' 699 && !buffer_eq_icase_ss(v, vlen, CONST_STR_LEN("trailers"))) 700 return http_request_header_line_invalid(r, 400, 701 "invalid TE header value with HTTP/2 -> 400"); 702 703 if (!hpctx->trailers) { 704 if (*k == ':') { 705 /* HTTP/2 request pseudo-header fields */ 706 if (!hpctx->pseudo) /*(pseudo header after non-pseudo header)*/ 707 return http_request_header_line_invalid(r, 400, 708 "invalid pseudo-header -> 400"); 709 switch (klen-1) { 710 case 4: 711 if (0 == memcmp(k+1, "path", 4)) { 712 if (!buffer_string_is_empty(&r->target)) 713 return http_request_header_line_invalid(r, 400, 714 "repeated pseudo-header -> 400"); 715 buffer_copy_string_len(&r->target, v, vlen); 716 return 0; 717 } 718 break; 719 case 6: 720 if (0 == memcmp(k+1, "method", 6)) { 721 if (HTTP_METHOD_UNSET != r->http_method) 722 return http_request_header_line_invalid(r, 400, 723 "repeated pseudo-header -> 400"); 724 r->http_method = get_http_method_key(v, vlen); 725 if (HTTP_METHOD_UNSET >= r->http_method) 726 return http_request_header_line_invalid(r, 501, 727 "unknown http-method -> 501"); 728 return 0; 729 } 730 else if (0 == memcmp(k+1, "scheme", 6)) { 731 if (hpctx->scheme) 732 return http_request_header_line_invalid(r, 400, 733 "repeated pseudo-header -> 400"); 734 switch (vlen) {/*(validated, but then ignored)*/ 735 case 5: /* "https" */ 736 if (v[4]!='s') break; 737 __attribute_fallthrough__ 738 case 4: /* "http" */ 739 if (v[0]=='h' && v[1]=='t' && v[2]=='t' && v[3]=='p') { 740 hpctx->scheme = 1; 741 return 0; 742 } 743 break; 744 default: 745 break; 746 } 747 return http_request_header_line_invalid(r, 400, 748 "unknown pseudo-header scheme -> 400"); 749 } 750 break; 751 case 9: 752 if (0 == memcmp(k+1, "authority", 9)) { 753 if (r->http_host) 754 return http_request_header_line_invalid(r, 400, 755 "repeated pseudo-header -> 400"); 756 if (vlen >= 1024) /*(expecting < 256)*/ 757 return http_request_header_line_invalid(r, 400, 758 "invalid pseudo-header authority too long -> 400"); 759 /* insert as host header */ 760 http_header_request_set(r, HTTP_HEADER_HOST, 761 CONST_STR_LEN("host"), v, vlen); 762 r->http_host = 763 http_header_request_get(r, HTTP_HEADER_HOST, 764 CONST_STR_LEN("Host")); 765 return 0; 766 } 767 break; 768 default: 769 break; 770 } 771 return http_request_header_line_invalid(r, 400, 772 "invalid pseudo-header -> 400"); 773 } 774 else { /*(non-pseudo headers)*/ 775 if (hpctx->pseudo) { /*(transition to non-pseudo headers)*/ 776 hpctx->pseudo = 0; 777 int status = 778 http_request_validate_pseudohdrs(r, hpctx->scheme, 779 hpctx->http_parseopts); 780 if (0 != status) return status; 781 } 782 783 const unsigned int http_header_strict = 784 (hpctx->http_parseopts & HTTP_PARSEOPT_HEADER_STRICT); 785 786 for (uint32_t j = 0; j < klen; ++j) { 787 if ((k[j] >= 'a' && k[j] <= 'z') || k[j] == '-') 788 continue; /*(common cases)*/ 789 if (k[j] >= 'A' && k[j] <= 'Z') 790 return 400; 791 if (0 != http_request_parse_header_other(r, k+j, klen-j, 792 http_header_strict)) 793 return 400; 794 break; 795 } 796 797 if (http_header_strict) { 798 for (uint32_t j = 0; j < vlen; ++j) { 799 if ((((uint8_t *)v)[j] < 32 && v[j] != '\t') || v[j]==127) 800 return http_request_header_char_invalid(r, v[j], 801 "invalid character in header -> 400"); 802 } 803 } 804 else { 805 if (NULL != memchr(v, '\0', vlen)) 806 return http_request_header_char_invalid(r, '\0', 807 "invalid character in header -> 400"); 808 } 809 810 const enum http_header_e id = http_header_hkey_get(k, klen); 811 return http_request_parse_single_header(r, id, k, klen, v, vlen); 812 } 813 } 814 else { /*(trailers)*/ 815 /* ignore trailers (after required HPACK decoding) if streaming 816 * request body to backend since headers have already been sent 817 * to backend via Common Gateway Interface (CGI) (CGI, FastCGI, 818 * SCGI, etc) or HTTP/1.1 (proxy) (mod_proxy does not currently 819 * support using HTTP/2 to connect to backends) */ 820 #if 0 /* (if needed, save flag in hpctx instead of fdevent.h dependency)*/ 821 if (r->conf.stream_request_body & FDEVENT_STREAM_REQUEST) 822 return 0; 823 #endif 824 /* Note: do not unconditionally merge into headers since if 825 * headers had already been sent to backend, then mod_accesslog 826 * logging of request headers might be inaccurate. 827 * Many simple backends do not support HTTP/1.1 requests sending 828 * Transfer-Encoding: chunked, and even those that do might not 829 * handle trailers. Some backends do not even support HTTP/1.1. 830 * For all these reasons, ignore trailers if streaming request 831 * body to backend. Revisit in future if adding support for 832 * connecting to backends using HTTP/2 (with explicit config 833 * option to force connecting to backends using HTTP/2) */ 834 835 /* XXX: TODO: request trailers not handled if streaming reqbody 836 * XXX: must ensure that trailers are not disallowed field-names 837 */ 838 839 return 0; 840 } 841 } 842 843 844 static int http_request_parse_reqline(request_st * const restrict r, const char * const restrict ptr, const unsigned short * const restrict hoff, const unsigned int http_parseopts) { 845 size_t len = hoff[2]; 846 847 /* parse the first line of the request 848 * <method> <uri> <protocol>\r\n 849 * */ 850 if (len < 13) /* minimum len with (!http_header_strict): "x x HTTP/1.0\n" */ 851 return http_request_header_line_invalid(r, 400, "invalid request line (too short) -> 400"); 852 if (ptr[len-2] == '\r') 853 len-=2; 854 else if (!(http_parseopts & HTTP_PARSEOPT_HEADER_STRICT)) /*(!http_header_strict)*/ 855 len-=1; 856 else 857 return http_request_header_line_invalid(r, 400, "missing CR before LF in header -> 400"); 858 859 /* 860 * RFC7230: 861 * HTTP-version = HTTP-name "/" DIGIT "." DIGIT 862 * HTTP-name = %x48.54.54.50 ; "HTTP", case-sensitive 863 */ 864 865 /* protocol is expected to be " HTTP/1.1" or " HTTP/1.0" at end of line */ 866 union proto_un { 867 char c[8]; 868 uint64_t u; 869 }; 870 static const union proto_un http_1_1 = {{'H','T','T','P','/','1','.','1'}}; 871 static const union proto_un http_1_0 = {{'H','T','T','P','/','1','.','0'}}; 872 const char *p = ptr + len - 8; 873 union proto_un proto8; 874 proto8.c[0]=p[0]; proto8.c[1]=p[1]; proto8.c[2]=p[2]; proto8.c[3]=p[3]; 875 proto8.c[4]=p[4]; proto8.c[5]=p[5]; proto8.c[6]=p[6]; proto8.c[7]=p[7]; 876 if (p[-1] == ' ' && http_1_1.u == proto8.u) { 877 r->http_version = HTTP_VERSION_1_1; 878 r->keep_alive = 1; /* keep-alive default: HTTP/1.1 -> true */ 879 } 880 else if (p[-1] == ' ' && http_1_0.u == proto8.u) { 881 r->http_version = HTTP_VERSION_1_0; 882 r->keep_alive = 0; /* keep-alive default: HTTP/1.0 -> false */ 883 } 884 else { 885 int status = http_request_parse_proto_loose(r,ptr,len,http_parseopts); 886 if (0 != status) return status; 887 /*(space char must exist if http_request_parse_proto_loose() succeeds)*/ 888 for (p = ptr + len - 9; p[-1] != ' '; --p) ; 889 } 890 891 /* method is expected to be a short string in the general case */ 892 size_t i = 0; 893 while (ptr[i] != ' ') ++i; 894 #if 0 /*(space must exist if protocol was parsed successfully)*/ 895 while (i < len && ptr[i] != ' ') ++i; 896 if (ptr[i] != ' ') 897 return http_request_header_line_invalid(r, 400, "incomplete request line -> 400"); 898 #endif 899 900 r->http_method = get_http_method_key(ptr, i); 901 if (HTTP_METHOD_UNSET >= r->http_method) 902 return http_request_header_line_invalid(r, 501, "unknown http-method -> 501"); 903 904 const char *uri = ptr + i + 1; 905 906 if (uri == p) 907 return http_request_header_line_invalid(r, 400, "no uri specified -> 400"); 908 len = (size_t)(p - uri - 1); 909 910 if (*uri != '/') { /* (common case: (*uri == '/')) */ 911 uri = http_request_parse_reqline_uri(r, uri, len, http_parseopts); 912 if (NULL == uri) return 400; 913 len = (size_t)(p - uri - 1); 914 } 915 916 if (0 == len) 917 return http_request_header_line_invalid(r, 400, "no uri specified -> 400"); 918 919 /* check uri for invalid characters */ 920 if (http_parseopts & HTTP_PARSEOPT_HEADER_STRICT) { /* http_header_strict */ 921 if ((http_parseopts & HTTP_PARSEOPT_URL_NORMALIZE_CTRLS_REJECT)) { 922 /* URI will be checked in http_request_parse_target() */ 923 } 924 else { 925 for (i = 0; i < len; ++i) { 926 if (!request_uri_is_valid_char(uri[i])) 927 return http_request_header_char_invalid(r, uri[i], "invalid character in URI -> 400"); 928 } 929 } 930 } 931 else { 932 /* check entire set of request headers for '\0' */ 933 if (NULL != memchr(ptr, '\0', hoff[hoff[0]])) 934 return http_request_header_char_invalid(r, '\0', "invalid character in header -> 400"); 935 } 936 937 buffer_copy_string_len(&r->target, uri, len); 938 buffer_copy_string_len(&r->target_orig, uri, len); 939 return 0; 940 } 941 942 int http_request_parse_target(request_st * const r, int scheme_port) { 943 /* URI is parsed into components at start of request and may 944 * also be re-parsed upon HANDLER_COMEBACK during the request 945 * r->target is expected to be a "/url-part?query-part" 946 * (and *not* a fully-qualified URI starting https://...) 947 * r->uri.authority is expected to be parsed elsewhere into r->http_host 948 */ 949 950 /** 951 * prepare strings 952 * 953 * - uri.path 954 * - uri.query 955 * 956 */ 957 958 /** 959 * Name according to RFC 2396 960 * 961 * - scheme 962 * - authority 963 * - path 964 * - query 965 * 966 * (scheme)://(authority)(path)?(query)#fragment 967 * 968 */ 969 970 /* take initial scheme value from connection-level state 971 * (request r->uri.scheme can be overwritten for later, 972 * for example by mod_extforward or mod_magnet) */ 973 if (scheme_port == 443) 974 buffer_copy_string_len(&r->uri.scheme, CONST_STR_LEN("https")); 975 else 976 buffer_copy_string_len(&r->uri.scheme, CONST_STR_LEN("http")); 977 978 buffer * const target = &r->target; 979 if (r->http_method == HTTP_METHOD_CONNECT 980 || (r->http_method == HTTP_METHOD_OPTIONS 981 && target->ptr[0] == '*' 982 && target->ptr[1] == '\0')) { 983 /* CONNECT ... (or) OPTIONS * ... */ 984 buffer_copy_buffer(&r->uri.path, target); 985 buffer_clear(&r->uri.query); 986 return 0; 987 } 988 989 char *qstr; 990 if (r->conf.http_parseopts & HTTP_PARSEOPT_URL_NORMALIZE) { 991 /*uint32_t len = (uint32_t)buffer_string_length(target);*/ 992 int qs = burl_normalize(target, r->tmp_buf, r->conf.http_parseopts); 993 if (-2 == qs) { 994 log_error(r->conf.errh, __FILE__, __LINE__, 995 "invalid character in URI -> 400 %s", 996 target->ptr); 997 return 400; /* Bad Request */ 998 } 999 qstr = (-1 == qs) ? NULL : target->ptr+qs; 1000 #if 0 /* future: might enable here, or below for all requests */ 1001 /* (Note: total header size not recalculated on HANDLER_COMEBACK 1002 * even if other request headers changed during processing) 1003 * (If (0 != r->loops_per_request), then the generated 1004 * request is too large. Should a different error be returned?) */ 1005 r->rqst_header_len -= len; 1006 len = buffer_string_length(target); 1007 r->rqst_header_len += len; 1008 if (len > MAX_HTTP_REQUEST_URI) { 1009 return 414; /* 414 Request-URI Too Long */ 1010 } 1011 if (r->rqst_header_len > MAX_HTTP_REQUEST_HEADER) { 1012 log_error(r->conf.errh, __FILE__, __LINE__, 1013 "request header fields too large: %u -> 431", 1014 r->rqst_header_len); 1015 return 431; /* Request Header Fields Too Large */ 1016 } 1017 #endif 1018 } 1019 else { 1020 size_t rlen = buffer_string_length(target); 1021 qstr = memchr(target->ptr, '#', rlen);/* discard fragment */ 1022 if (qstr) { 1023 rlen = (size_t)(qstr - target->ptr); 1024 buffer_string_set_length(target, rlen); 1025 } 1026 qstr = memchr(target->ptr, '?', rlen); 1027 } 1028 1029 /** extract query string from target */ 1030 const char * const pstr = target->ptr; 1031 const uint32_t rlen = buffer_string_length(target); 1032 uint32_t plen; 1033 if (NULL != qstr) { 1034 plen = (uint32_t)(qstr - pstr); 1035 buffer_copy_string_len(&r->uri.query, qstr + 1, rlen - plen - 1); 1036 } 1037 else { 1038 plen = rlen; 1039 buffer_clear(&r->uri.query); 1040 } 1041 buffer_copy_string_len(&r->uri.path, pstr, plen); 1042 1043 /* decode url to path 1044 * 1045 * - decode url-encodings (e.g. %20 -> ' ') 1046 * - remove path-modifiers (e.g. /../) 1047 */ 1048 1049 buffer_urldecode_path(&r->uri.path); 1050 buffer_path_simplify(&r->uri.path, &r->uri.path); 1051 if (r->uri.path.ptr[0] != '/') { 1052 log_error(r->conf.errh, __FILE__, __LINE__, 1053 "uri-path does not begin with '/': %s -> 400", r->uri.path.ptr); 1054 return 400; /* Bad Request */ 1055 } 1056 1057 return 0; 1058 } 1059 1060 __attribute_cold__ 1061 __attribute_noinline__ 1062 static int http_request_parse_header_other(request_st * const restrict r, const char * const restrict k, const int klen, const unsigned int http_header_strict) { 1063 for (int i = 0; i < klen; ++i) { 1064 if (light_isalpha(k[i]) || k[i] == '-') continue; /*(common cases)*/ 1065 /** 1066 * 1*<any CHAR except CTLs or separators> 1067 * CTLs == 0-31 + 127, CHAR = 7-bit ascii (0..127) 1068 * 1069 */ 1070 switch(k[i]) { 1071 case ' ': 1072 case '\t': 1073 return http_request_header_line_invalid(r, 400, "WS character in key -> 400"); 1074 case '(': 1075 case ')': 1076 case '<': 1077 case '>': 1078 case '@': 1079 case ',': 1080 case ';': 1081 case '\\': 1082 case '\"': 1083 case '/': 1084 case '[': 1085 case ']': 1086 case '?': 1087 case '=': 1088 case '{': 1089 case '}': 1090 return http_request_header_char_invalid(r, k[i], "invalid character in header key -> 400"); 1091 default: 1092 if (http_header_strict ? (k[i] < 32 || ((unsigned char *)k)[i] >= 127) : k[i] == '\0') 1093 return http_request_header_char_invalid(r, k[i], "invalid character in header key -> 400"); 1094 break; /* ok */ 1095 } 1096 } 1097 return 0; 1098 } 1099 1100 static int http_request_parse_headers(request_st * const restrict r, char * const restrict ptr, const unsigned short * const restrict hoff, const unsigned int http_parseopts) { 1101 const unsigned int http_header_strict = (http_parseopts & HTTP_PARSEOPT_HEADER_STRICT); 1102 1103 #if 0 /*(not checked here; will later result in invalid label for HTTP header)*/ 1104 int i = hoff[2]; 1105 1106 if (ptr[i] == ' ' || ptr[i] == '\t') { 1107 return http_request_header_line_invalid(r, 400, "WS at the start of first line -> 400"); 1108 } 1109 #endif 1110 1111 for (int i = 2; i < hoff[0]; ++i) { 1112 const char *k = ptr + hoff[i]; 1113 /* one past last line hoff[hoff[0]] is to final "\r\n" */ 1114 char *end = ptr + hoff[i+1]; 1115 1116 const char *colon = memchr(k, ':', end - k); 1117 if (NULL == colon) 1118 return http_request_header_line_invalid(r, 400, "invalid header missing ':' -> 400"); 1119 1120 const char *v = colon + 1; 1121 1122 /* RFC7230 Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing 1123 * 3.2.4. Field Parsing 1124 * [...] 1125 * No whitespace is allowed between the header field-name and colon. In 1126 * the past, differences in the handling of such whitespace have led to 1127 * security vulnerabilities in request routing and response handling. A 1128 * server MUST reject any received request message that contains 1129 * whitespace between a header field-name and colon with a response code 1130 * of 400 (Bad Request). A proxy MUST remove any such whitespace from a 1131 * response message before forwarding the message downstream. 1132 */ 1133 /* (line k[-1] is always preceded by a '\n', 1134 * including first header after request-line, 1135 * so no need to check colon != k) */ 1136 if (colon[-1] == ' ' || colon[-1] == '\t') { 1137 if (http_header_strict) { 1138 return http_request_header_line_invalid(r, 400, "invalid whitespace between field-name and colon -> 400"); 1139 } 1140 else { 1141 /* remove trailing whitespace from key(if !http_header_strict)*/ 1142 do { --colon; } while (colon[-1] == ' ' || colon[-1] == '\t'); 1143 } 1144 } 1145 1146 const int klen = (int)(colon - k); 1147 if (0 == klen) 1148 return http_request_header_line_invalid(r, 400, "invalid header key -> 400"); 1149 const enum http_header_e id = http_header_hkey_get(k, klen); 1150 1151 if (id == HTTP_HEADER_OTHER) { 1152 for (int j = 0; j < klen; ++j) { 1153 if (light_isalpha(k[j]) || k[j] == '-') continue; /*(common cases)*/ 1154 if (0 != http_request_parse_header_other(r, k+j, klen-j, http_header_strict)) 1155 return 400; 1156 break; 1157 } 1158 } 1159 1160 /* remove leading whitespace from value */ 1161 while (*v == ' ' || *v == '\t') ++v; 1162 1163 for (; i+1 <= hoff[0]; ++i) { 1164 end = ptr + hoff[i+1]; 1165 if (end[0] != ' ' && end[0] != '\t') break; 1166 1167 /* line folding */ 1168 #ifdef __COVERITY__ 1169 force_assert(end - k >= 2); 1170 #endif 1171 if (end[-2] == '\r') 1172 end[-2] = ' '; 1173 else if (http_header_strict) 1174 return http_request_header_line_invalid(r, 400, "missing CR before LF in header -> 400"); 1175 end[-1] = ' '; 1176 } 1177 #ifdef __COVERITY__ 1178 /*(buf holding k has non-zero request-line, so end[-2] valid)*/ 1179 force_assert(end >= k + 2); 1180 #endif 1181 if (end[-2] == '\r') 1182 --end; 1183 else if (http_header_strict) 1184 return http_request_header_line_invalid(r, 400, "missing CR before LF in header -> 400"); 1185 /* remove trailing whitespace from value (+ remove '\r\n') */ 1186 /* (line k[-1] is always preceded by a '\n', 1187 * including first header after request-line, 1188 * so no need to check (end != k)) */ 1189 do { --end; } while (end[-1] == ' ' || end[-1] == '\t'); 1190 1191 const int vlen = (int)(end - v); 1192 /* empty header-fields are not allowed by HTTP-RFC, we just ignore them */ 1193 if (vlen <= 0) continue; /* ignore header */ 1194 1195 if (http_header_strict) { 1196 for (int j = 0; j < vlen; ++j) { 1197 if ((((unsigned char *)v)[j] < 32 && v[j] != '\t') || v[j]==127) 1198 return http_request_header_char_invalid(r, v[j], "invalid character in header -> 400"); 1199 } 1200 } /* else URI already checked in http_request_parse_reqline() for any '\0' */ 1201 1202 int status = http_request_parse_single_header(r, id, k, (size_t)klen, v, (size_t)vlen); 1203 if (0 != status) return status; 1204 } 1205 1206 return 0; 1207 } 1208 1209 1210 static int 1211 http_request_parse (request_st * const restrict r, const int scheme_port) 1212 { 1213 int status = http_request_parse_target(r, scheme_port); 1214 if (0 != status) return status; 1215 1216 /*(r->http_host might not be set until after parsing request headers)*/ 1217 buffer_copy_buffer(&r->uri.authority, r->http_host);/*(copy even if empty)*/ 1218 buffer_to_lower(&r->uri.authority); 1219 1220 /* post-processing */ 1221 const unsigned int http_parseopts = r->conf.http_parseopts; 1222 1223 /* check hostname field if it is set */ 1224 if (r->http_host) { 1225 if (0 != http_request_host_policy(r->http_host, 1226 http_parseopts, scheme_port)) 1227 return http_request_header_line_invalid(r, 400, "Invalid Hostname -> 400"); 1228 } 1229 else { 1230 if (r->http_version >= HTTP_VERSION_1_1) 1231 return http_request_header_line_invalid(r, 400, "HTTP/1.1 but Host missing -> 400"); 1232 } 1233 1234 if (0 == r->reqbody_length) { 1235 /* POST requires Content-Length (or Transfer-Encoding) 1236 * (-1 == r->reqbody_length when Transfer-Encoding: chunked)*/ 1237 if (HTTP_METHOD_POST == r->http_method 1238 && !(r->rqst_htags & HTTP_HEADER_CONTENT_LENGTH)) { 1239 return http_request_header_line_invalid(r, 411, "POST-request, but content-length missing -> 411"); 1240 } 1241 } 1242 else { 1243 /* (-1 == r->reqbody_length when Transfer-Encoding: chunked)*/ 1244 if (-1 == r->reqbody_length 1245 && (r->rqst_htags & HTTP_HEADER_CONTENT_LENGTH)) { 1246 /* RFC7230 Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing 1247 * 3.3.3. Message Body Length 1248 * [...] 1249 * If a message is received with both a Transfer-Encoding and a 1250 * Content-Length header field, the Transfer-Encoding overrides the 1251 * Content-Length. Such a message might indicate an attempt to 1252 * perform request smuggling (Section 9.5) or response splitting 1253 * (Section 9.4) and ought to be handled as an error. A sender MUST 1254 * remove the received Content-Length field prior to forwarding such 1255 * a message downstream. 1256 */ 1257 const unsigned int http_header_strict = 1258 (http_parseopts & HTTP_PARSEOPT_HEADER_STRICT); 1259 if (http_header_strict) { 1260 return http_request_header_line_invalid(r, 400, "invalid Transfer-Encoding + Content-Length -> 400"); 1261 } 1262 else { 1263 /* ignore Content-Length */ 1264 http_header_request_unset(r, HTTP_HEADER_CONTENT_LENGTH, CONST_STR_LEN("Content-Length")); 1265 } 1266 } 1267 if (http_method_get_or_head(r->http_method) 1268 && !(http_parseopts & HTTP_PARSEOPT_METHOD_GET_BODY)) { 1269 return http_request_header_line_invalid(r, 400, "GET/HEAD with content-length -> 400"); 1270 } 1271 } 1272 1273 return 0; 1274 } 1275 1276 1277 static int 1278 http_request_parse_hoff (request_st * const restrict r, char * const restrict hdrs, const unsigned short * const restrict hoff, const int scheme_port) 1279 { 1280 /* 1281 * Request: "^(GET|POST|HEAD|...) ([^ ]+(\\?[^ ]+|)) (HTTP/1\\.[01])$" 1282 * Header : "^([-a-zA-Z]+): (.+)$" 1283 * End : "^$" 1284 */ 1285 1286 int status; 1287 const unsigned int http_parseopts = r->conf.http_parseopts; 1288 1289 status = http_request_parse_reqline(r, hdrs, hoff, http_parseopts); 1290 if (0 != status) return status; 1291 1292 status = http_request_parse_headers(r, hdrs, hoff, http_parseopts); 1293 if (0 != status) return status; 1294 1295 return http_request_parse(r, scheme_port); 1296 } 1297 1298 1299 static void 1300 http_request_headers_fin (request_st * const restrict r) 1301 { 1302 if (0 == r->http_status) { 1303 #if 0 1304 r->conditional_is_valid = (1 << COMP_SERVER_SOCKET) 1305 | (1 << COMP_HTTP_SCHEME) 1306 | (1 << COMP_HTTP_HOST) 1307 | (1 << COMP_HTTP_REMOTE_IP) 1308 | (1 << COMP_HTTP_REQUEST_METHOD) 1309 | (1 << COMP_HTTP_URL) 1310 | (1 << COMP_HTTP_QUERY_STRING) 1311 | (1 << COMP_HTTP_REQUEST_HEADER); 1312 #else 1313 /* all config conditions are valid after parsing header 1314 * (set all bits; remove dependency on plugin_config.h) */ 1315 r->conditional_is_valid = ~0u; 1316 #endif 1317 } 1318 else { 1319 r->keep_alive = 0; 1320 r->reqbody_length = 0; 1321 } 1322 } 1323 1324 1325 void 1326 http_request_headers_process (request_st * const restrict r, char * const restrict hdrs, const unsigned short * const restrict hoff, const int scheme_port) 1327 { 1328 r->http_status = http_request_parse_hoff(r, hdrs, hoff, scheme_port); 1329 1330 http_request_headers_fin(r); 1331 1332 if (0 != r->http_status) { 1333 if (r->conf.log_request_header_on_error) { 1334 /*(http_request_parse_headers() modifies hdrs only to 1335 * undo line-wrapping in-place using spaces)*/ 1336 log_error(r->conf.errh, __FILE__, __LINE__, 1337 "request-header:\n%.*s", (int)r->rqst_header_len, hdrs); 1338 } 1339 } 1340 } 1341 1342 1343 void 1344 http_request_headers_process_h2 (request_st * const restrict r, const int scheme_port) 1345 { 1346 if (0 == r->http_status) 1347 r->http_status = http_request_parse(r, scheme_port); 1348 1349 if (0 == r->http_status) { 1350 if (r->rqst_htags & HTTP_HEADER_CONNECTION) 1351 r->http_status = http_request_header_line_invalid(r, 400, 1352 "invalid Connection header with HTTP/2 -> 400"); 1353 } 1354 1355 http_request_headers_fin(r); 1356 1357 #if 0 /* not supported; headers not collected into a single buf for HTTP/2 */ 1358 if (0 != r->http_status) { 1359 if (r->conf.log_request_header_on_error) { 1360 log_error(r->conf.errh, __FILE__, __LINE__, 1361 "request-header:\n%.*s", (int)r->rqst_header_len, hdrs); 1362 } 1363 } 1364 #endif 1365 1366 /* ignore Upgrade if using HTTP/2 */ 1367 if (r->rqst_htags & HTTP_HEADER_UPGRADE) 1368 http_header_request_unset(r, HTTP_HEADER_UPGRADE, 1369 CONST_STR_LEN("upgrade")); 1370 /* XXX: should filter out other hop-by-hop connection headers, too */ 1371 } 1372