2017-09-21 03:30:46 -04:00
|
|
|
/*
|
|
|
|
|
* HTTP/1 protocol analyzer
|
|
|
|
|
*
|
|
|
|
|
* Copyright 2000-2017 Willy Tarreau <w@1wt.eu>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2017-07-26 03:07:47 -04:00
|
|
|
#include <ctype.h>
|
2020-12-11 11:53:03 -05:00
|
|
|
|
|
|
|
|
#include <import/sha1.h>
|
|
|
|
|
|
2020-05-27 06:58:42 -04:00
|
|
|
#include <haproxy/api.h>
|
2020-12-11 11:53:03 -05:00
|
|
|
#include <haproxy/base64.h>
|
2024-07-26 09:14:58 -04:00
|
|
|
#include <haproxy/cfgparse.h>
|
2020-06-02 13:33:08 -04:00
|
|
|
#include <haproxy/h1.h>
|
2020-06-02 13:25:28 -04:00
|
|
|
#include <haproxy/http-hdr.h>
|
2020-12-11 11:53:07 -05:00
|
|
|
#include <haproxy/tools.h>
|
2017-09-21 03:30:46 -04:00
|
|
|
|
2024-07-26 09:14:58 -04:00
|
|
|
/* by default, RFC9112#6.1 applies, t-e combined with c-l represents a risk of
|
|
|
|
|
* smuggling if it crosses another 1.0 agent so we must close at the end of the
|
|
|
|
|
* transaction. But it may cause difficulties to some very old broken devices.
|
|
|
|
|
*/
|
|
|
|
|
int h1_do_not_close_on_insecure_t_e = 0;
|
|
|
|
|
|
2018-09-14 10:34:47 -04:00
|
|
|
/* Parse the Transfer-Encoding: header field of an HTTP/1 request, looking for
|
2021-09-28 03:36:25 -04:00
|
|
|
* "chunked" encoding to perform some checks (it must be the last encoding for
|
|
|
|
|
* the request and must not be performed twice for any message). The
|
|
|
|
|
* H1_MF_TE_CHUNKED is set if a valid "chunked" encoding is found. The
|
|
|
|
|
* H1_MF_TE_OTHER flag is set if any other encoding is found. The H1_MF_XFER_ENC
|
|
|
|
|
* flag is always set. The H1_MF_CHNK is set when "chunked" encoding is the last
|
|
|
|
|
* one. Note that transfer codings are case-insensitive (cf RFC7230#4). This
|
2024-09-11 09:02:07 -04:00
|
|
|
* function returns -2 for a fatal error, -1 for an error that may be hiidden by
|
|
|
|
|
* config, 0 if the whole header can be dropped (not used yet), or >0 if the
|
|
|
|
|
* value can be indexed.
|
2018-09-14 10:34:47 -04:00
|
|
|
*/
|
2021-09-28 03:36:25 -04:00
|
|
|
int h1_parse_xfer_enc_header(struct h1m *h1m, struct ist value)
|
2018-09-14 10:34:47 -04:00
|
|
|
{
|
|
|
|
|
char *e, *n;
|
|
|
|
|
struct ist word;
|
2024-09-11 09:02:07 -04:00
|
|
|
int ret = 1;
|
2018-09-14 10:34:47 -04:00
|
|
|
|
2024-07-09 01:55:58 -04:00
|
|
|
/* Reject empty header */
|
2024-09-11 09:02:07 -04:00
|
|
|
if (istptr(value) == istend(value)) {
|
|
|
|
|
ret = -1;
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
2024-07-09 01:55:58 -04:00
|
|
|
|
2018-09-14 10:34:47 -04:00
|
|
|
h1m->flags |= H1_MF_XFER_ENC;
|
|
|
|
|
|
|
|
|
|
word.ptr = value.ptr - 1; // -1 for next loop's pre-increment
|
2021-11-06 10:14:44 -04:00
|
|
|
e = istend(value);
|
2018-09-14 10:34:47 -04:00
|
|
|
|
|
|
|
|
while (++word.ptr < e) {
|
2020-06-21 12:42:57 -04:00
|
|
|
/* skip leading delimiter and blanks */
|
2018-09-14 10:34:47 -04:00
|
|
|
if (HTTP_IS_LWS(*word.ptr))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
n = http_find_hdr_value_end(word.ptr, e); // next comma or end of line
|
2024-07-09 02:57:53 -04:00
|
|
|
|
|
|
|
|
/* a comma at the end means the last value is empty */
|
|
|
|
|
if (n+1 == e)
|
2024-09-11 09:02:07 -04:00
|
|
|
ret = -1;
|
2018-09-14 10:34:47 -04:00
|
|
|
word.len = n - word.ptr;
|
|
|
|
|
|
|
|
|
|
/* trim trailing blanks */
|
|
|
|
|
while (word.len && HTTP_IS_LWS(word.ptr[word.len-1]))
|
|
|
|
|
word.len--;
|
|
|
|
|
|
|
|
|
|
h1m->flags &= ~H1_MF_CHNK;
|
2024-07-09 02:15:14 -04:00
|
|
|
|
|
|
|
|
/* empty values are forbidden */
|
|
|
|
|
if (!word.len)
|
2024-09-11 09:02:07 -04:00
|
|
|
ret = -1;
|
2024-07-09 02:15:14 -04:00
|
|
|
else if (isteqi(word, ist("chunked"))) {
|
2021-09-28 03:36:25 -04:00
|
|
|
if (h1m->flags & H1_MF_TE_CHUNKED) {
|
|
|
|
|
/* cf RFC7230#3.3.1 : A sender MUST NOT apply
|
|
|
|
|
* chunked more than once to a message body
|
|
|
|
|
* (i.e., chunking an already chunked message is
|
|
|
|
|
* not allowed)
|
|
|
|
|
*/
|
2024-09-11 09:02:07 -04:00
|
|
|
ret = -1;
|
2021-09-28 03:36:25 -04:00
|
|
|
}
|
|
|
|
|
h1m->flags |= (H1_MF_TE_CHUNKED|H1_MF_CHNK);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if ((h1m->flags & (H1_MF_RESP|H1_MF_TE_CHUNKED)) == H1_MF_TE_CHUNKED) {
|
|
|
|
|
/* cf RFC7230#3.3.1 : If any transfer coding
|
|
|
|
|
* other than chunked is applied to a request
|
|
|
|
|
* payload body, the sender MUST apply chunked
|
|
|
|
|
* as the final transfer coding to ensure that
|
|
|
|
|
* the message is properly framed.
|
|
|
|
|
*/
|
2024-09-11 09:02:07 -04:00
|
|
|
ret = -2;
|
|
|
|
|
goto end;
|
2021-09-28 03:36:25 -04:00
|
|
|
}
|
|
|
|
|
h1m->flags |= H1_MF_TE_OTHER;
|
|
|
|
|
}
|
2018-09-14 10:34:47 -04:00
|
|
|
|
|
|
|
|
word.ptr = n;
|
|
|
|
|
}
|
2021-09-28 03:36:25 -04:00
|
|
|
|
2024-09-11 09:02:07 -04:00
|
|
|
end:
|
|
|
|
|
return ret;
|
2018-09-14 10:34:47 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-05 08:50:17 -04:00
|
|
|
/* Validate the authority and the host header value for CONNECT method. If there
|
|
|
|
|
* is hast header, its value is normalized. 0 is returned on success, -1 if the
|
|
|
|
|
* authority is invalid and -2 if the host is invalid.
|
|
|
|
|
*/
|
2024-05-14 09:06:48 -04:00
|
|
|
static int h1_validate_connect_authority(struct ist scheme, struct ist authority, struct ist *host_hdr)
|
2022-07-05 08:50:17 -04:00
|
|
|
{
|
|
|
|
|
struct ist uri_host, uri_port, host, host_port;
|
|
|
|
|
|
2024-05-14 09:06:48 -04:00
|
|
|
if (isttest(scheme) || !isttest(authority))
|
2022-07-05 08:50:17 -04:00
|
|
|
goto invalid_authority;
|
|
|
|
|
uri_host = authority;
|
|
|
|
|
uri_port = http_get_host_port(authority);
|
2022-11-22 04:27:54 -05:00
|
|
|
if (!istlen(uri_port))
|
2022-07-05 08:50:17 -04:00
|
|
|
goto invalid_authority;
|
|
|
|
|
uri_host.len -= (istlen(uri_port) + 1);
|
|
|
|
|
|
|
|
|
|
if (!host_hdr || !isttest(*host_hdr))
|
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
|
|
/* Get the port of the host header value, if any */
|
|
|
|
|
host = *host_hdr;
|
|
|
|
|
host_port = http_get_host_port(*host_hdr);
|
2022-11-22 04:27:54 -05:00
|
|
|
if (isttest(host_port))
|
2022-07-05 08:50:17 -04:00
|
|
|
host.len -= (istlen(host_port) + 1);
|
2022-11-22 04:27:54 -05:00
|
|
|
|
|
|
|
|
if (istlen(host_port)) {
|
2022-07-05 08:50:17 -04:00
|
|
|
if (!isteqi(host, uri_host) || !isteq(host_port, uri_port))
|
|
|
|
|
goto invalid_host;
|
|
|
|
|
if (http_is_default_port(IST_NULL, uri_port))
|
|
|
|
|
*host_hdr = host; /* normalize */
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (!http_is_default_port(IST_NULL, uri_port) || !isteqi(host, uri_host))
|
|
|
|
|
goto invalid_host;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
end:
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
invalid_authority:
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
invalid_host:
|
|
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-22 04:04:16 -05:00
|
|
|
|
|
|
|
|
/* Validate the authority and the host header value for non-CONNECT method, when
|
|
|
|
|
* an absolute-URI is detected but when it does not exactly match the host
|
|
|
|
|
* value. The idea is to detect default port (http or https). authority and host
|
|
|
|
|
* are defined here. 0 is returned on success, -1 if the host is does not match
|
|
|
|
|
* the authority.
|
|
|
|
|
*/
|
|
|
|
|
static int h1_validate_mismatch_authority(struct ist scheme, struct ist authority, struct ist host_hdr)
|
|
|
|
|
{
|
|
|
|
|
struct ist uri_host, uri_port, host, host_port;
|
|
|
|
|
|
|
|
|
|
if (!isttest(scheme))
|
|
|
|
|
goto mismatch;
|
|
|
|
|
|
|
|
|
|
uri_host = authority;
|
|
|
|
|
uri_port = http_get_host_port(authority);
|
|
|
|
|
if (isttest(uri_port))
|
|
|
|
|
uri_host.len -= (istlen(uri_port) + 1);
|
|
|
|
|
|
|
|
|
|
host = host_hdr;
|
|
|
|
|
host_port = http_get_host_port(host_hdr);
|
|
|
|
|
if (isttest(host_port))
|
|
|
|
|
host.len -= (istlen(host_port) + 1);
|
|
|
|
|
|
|
|
|
|
if (!isttest(uri_port) && !isttest(host_port)) {
|
|
|
|
|
/* No port on both: we already know the authority does not match
|
|
|
|
|
* the host value
|
|
|
|
|
*/
|
|
|
|
|
goto mismatch;
|
|
|
|
|
}
|
|
|
|
|
else if (isttest(uri_port) && !http_is_default_port(scheme, uri_port)) {
|
|
|
|
|
/* here there is no port for the host value and the port for the
|
|
|
|
|
* authority is not the default one
|
|
|
|
|
*/
|
|
|
|
|
goto mismatch;
|
|
|
|
|
}
|
|
|
|
|
else if (isttest(host_port) && !http_is_default_port(scheme, host_port)) {
|
|
|
|
|
/* here there is no port for the authority and the port for the
|
|
|
|
|
* host value is not the default one
|
|
|
|
|
*/
|
|
|
|
|
goto mismatch;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* the authority or the host value contain a default port and
|
|
|
|
|
* there is no port on the other value
|
|
|
|
|
*/
|
|
|
|
|
if (!isteqi(uri_host, host))
|
|
|
|
|
goto mismatch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
mismatch:
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-09-13 08:15:58 -04:00
|
|
|
/* Parse the Connection: header of an HTTP/1 request, looking for "close",
|
|
|
|
|
* "keep-alive", and "upgrade" values, and updating h1m->flags according to
|
|
|
|
|
* what was found there. Note that flags are only added, not removed, so the
|
|
|
|
|
* function is safe for being called multiple times if multiple occurrences
|
2019-03-29 10:03:13 -04:00
|
|
|
* are found. If the flag H1_MF_CLEAN_CONN_HDR, the header value is cleaned
|
|
|
|
|
* up from "keep-alive" and "close" values. To do so, the header value is
|
|
|
|
|
* rewritten in place and its length is updated.
|
2018-09-13 08:15:58 -04:00
|
|
|
*/
|
2019-03-29 10:03:13 -04:00
|
|
|
void h1_parse_connection_header(struct h1m *h1m, struct ist *value)
|
2018-09-13 08:15:58 -04:00
|
|
|
{
|
2019-03-29 10:03:13 -04:00
|
|
|
char *e, *n, *p;
|
2018-09-13 08:15:58 -04:00
|
|
|
struct ist word;
|
|
|
|
|
|
2019-03-29 10:03:13 -04:00
|
|
|
word.ptr = value->ptr - 1; // -1 for next loop's pre-increment
|
|
|
|
|
p = value->ptr;
|
|
|
|
|
e = value->ptr + value->len;
|
|
|
|
|
if (h1m->flags & H1_MF_CLEAN_CONN_HDR)
|
|
|
|
|
value->len = 0;
|
2018-09-13 08:15:58 -04:00
|
|
|
|
|
|
|
|
while (++word.ptr < e) {
|
2020-06-21 12:42:57 -04:00
|
|
|
/* skip leading delimiter and blanks */
|
2018-09-13 08:15:58 -04:00
|
|
|
if (HTTP_IS_LWS(*word.ptr))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
n = http_find_hdr_value_end(word.ptr, e); // next comma or end of line
|
|
|
|
|
word.len = n - word.ptr;
|
|
|
|
|
|
|
|
|
|
/* trim trailing blanks */
|
|
|
|
|
while (word.len && HTTP_IS_LWS(word.ptr[word.len-1]))
|
|
|
|
|
word.len--;
|
|
|
|
|
|
2019-03-29 10:03:13 -04:00
|
|
|
if (isteqi(word, ist("keep-alive"))) {
|
2018-09-13 08:15:58 -04:00
|
|
|
h1m->flags |= H1_MF_CONN_KAL;
|
2019-03-29 10:03:13 -04:00
|
|
|
if (h1m->flags & H1_MF_CLEAN_CONN_HDR)
|
|
|
|
|
goto skip_val;
|
|
|
|
|
}
|
|
|
|
|
else if (isteqi(word, ist("close"))) {
|
2018-09-13 08:15:58 -04:00
|
|
|
h1m->flags |= H1_MF_CONN_CLO;
|
2019-03-29 10:03:13 -04:00
|
|
|
if (h1m->flags & H1_MF_CLEAN_CONN_HDR)
|
|
|
|
|
goto skip_val;
|
|
|
|
|
}
|
2018-09-13 08:15:58 -04:00
|
|
|
else if (isteqi(word, ist("upgrade")))
|
|
|
|
|
h1m->flags |= H1_MF_CONN_UPG;
|
|
|
|
|
|
2019-03-29 10:03:13 -04:00
|
|
|
if (h1m->flags & H1_MF_CLEAN_CONN_HDR) {
|
|
|
|
|
if (value->ptr + value->len == p) {
|
|
|
|
|
/* no rewrite done till now */
|
|
|
|
|
value->len = n - value->ptr;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (value->len)
|
|
|
|
|
value->ptr[value->len++] = ',';
|
|
|
|
|
istcat(value, word, e - value->ptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
skip_val:
|
|
|
|
|
word.ptr = p = n;
|
2018-09-13 08:15:58 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-11 11:53:02 -05:00
|
|
|
/* Parse the Upgrade: header of an HTTP/1 request.
|
|
|
|
|
* If "websocket" is found, set H1_MF_UPG_WEBSOCKET flag
|
2024-08-01 09:35:06 -04:00
|
|
|
* If "h2c" or "h2" found, set H1_MF_UPG_H2C flag.
|
2020-12-11 11:53:02 -05:00
|
|
|
*/
|
|
|
|
|
void h1_parse_upgrade_header(struct h1m *h1m, struct ist value)
|
|
|
|
|
{
|
|
|
|
|
char *e, *n;
|
|
|
|
|
struct ist word;
|
|
|
|
|
|
2024-08-01 09:35:06 -04:00
|
|
|
h1m->flags &= ~(H1_MF_UPG_WEBSOCKET|H1_MF_UPG_H2C);
|
2020-12-11 11:53:02 -05:00
|
|
|
|
|
|
|
|
word.ptr = value.ptr - 1; // -1 for next loop's pre-increment
|
2021-11-06 10:14:44 -04:00
|
|
|
e = istend(value);
|
2020-12-11 11:53:02 -05:00
|
|
|
|
|
|
|
|
while (++word.ptr < e) {
|
|
|
|
|
/* skip leading delimiter and blanks */
|
|
|
|
|
if (HTTP_IS_LWS(*word.ptr))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
n = http_find_hdr_value_end(word.ptr, e); // next comma or end of line
|
|
|
|
|
word.len = n - word.ptr;
|
|
|
|
|
|
|
|
|
|
/* trim trailing blanks */
|
|
|
|
|
while (word.len && HTTP_IS_LWS(word.ptr[word.len-1]))
|
|
|
|
|
word.len--;
|
|
|
|
|
|
|
|
|
|
if (isteqi(word, ist("websocket")))
|
|
|
|
|
h1m->flags |= H1_MF_UPG_WEBSOCKET;
|
2024-08-01 09:35:06 -04:00
|
|
|
else if (isteqi(word, ist("h2c")) || isteqi(word, ist("h2")))
|
|
|
|
|
h1m->flags |= H1_MF_UPG_H2C;
|
2020-12-11 11:53:02 -05:00
|
|
|
|
|
|
|
|
word.ptr = n;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-11 04:59:20 -05:00
|
|
|
/* Macros used in the HTTP/1 parser, to check for the expected presence of
|
|
|
|
|
* certain bytes (ef: LF) or to skip to next byte and yield in case of failure.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* Expects to find an LF at <ptr>. If not, set <state> to <where> and jump to
|
|
|
|
|
* <bad>.
|
|
|
|
|
*/
|
|
|
|
|
#define EXPECT_LF_HERE(ptr, bad, state, where) \
|
|
|
|
|
do { \
|
|
|
|
|
if (unlikely(*(ptr) != '\n')) { \
|
|
|
|
|
state = (where); \
|
|
|
|
|
goto bad; \
|
|
|
|
|
} \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
/* Increments pointer <ptr>, continues to label <more> if it's still below
|
|
|
|
|
* pointer <end>, or goes to <stop> and sets <state> to <where> if the end
|
|
|
|
|
* of buffer was reached.
|
|
|
|
|
*/
|
|
|
|
|
#define EAT_AND_JUMP_OR_RETURN(ptr, end, more, stop, state, where) \
|
|
|
|
|
do { \
|
|
|
|
|
if (likely(++(ptr) < (end))) \
|
|
|
|
|
goto more; \
|
|
|
|
|
else { \
|
|
|
|
|
state = (where); \
|
|
|
|
|
goto stop; \
|
|
|
|
|
} \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
2017-07-26 03:07:47 -04:00
|
|
|
/* This function parses a contiguous HTTP/1 headers block starting at <start>
|
|
|
|
|
* and ending before <stop>, at once, and converts it a list of (name,value)
|
|
|
|
|
* pairs representing header fields into the array <hdr> of size <hdr_num>,
|
|
|
|
|
* whose last entry will have an empty name and an empty value. If <hdr_num> is
|
2018-09-11 09:33:32 -04:00
|
|
|
* too small to represent the whole message, an error is returned. Some
|
|
|
|
|
* protocol elements such as content-length and transfer-encoding will be
|
2018-09-11 10:04:48 -04:00
|
|
|
* parsed and stored into h1m as well. <hdr> may be null, in which case only
|
|
|
|
|
* the parsing state will be updated. This may be used to restart the parsing
|
|
|
|
|
* where it stopped for example.
|
2017-07-26 03:07:47 -04:00
|
|
|
*
|
|
|
|
|
* For now it's limited to the response. If the header block is incomplete,
|
|
|
|
|
* 0 is returned, waiting to be called again with more data to try it again.
|
2018-09-11 09:33:32 -04:00
|
|
|
* The caller is responsible for initializing h1m->state to H1_MSG_RPBEFORE,
|
2018-09-11 10:20:30 -04:00
|
|
|
* and h1m->next to zero on the first call, the parser will do the rest. If
|
|
|
|
|
* an incomplete message is seen, the caller only needs to present h1m->state
|
|
|
|
|
* and h1m->next again, with an empty header list so that the parser can start
|
|
|
|
|
* again. In this case, it will detect that it interrupted a previous session
|
|
|
|
|
* and will first look for the end of the message before reparsing it again and
|
|
|
|
|
* indexing it at the same time. This ensures that incomplete messages fed 1
|
|
|
|
|
* character at a time are never processed entirely more than exactly twice,
|
|
|
|
|
* and that there is no need to store all the internal state and pre-parsed
|
|
|
|
|
* headers or start line between calls.
|
2017-07-26 03:07:47 -04:00
|
|
|
*
|
2018-09-11 09:34:50 -04:00
|
|
|
* A pointer to a start line descriptor may be passed in <slp>, in which case
|
|
|
|
|
* the parser will fill it with whatever it found.
|
|
|
|
|
*
|
2017-07-26 03:07:47 -04:00
|
|
|
* The code derived from the main HTTP/1 parser above but was simplified and
|
|
|
|
|
* optimized to process responses produced or forwarded by haproxy. The caller
|
|
|
|
|
* is responsible for ensuring that the message doesn't wrap, and should ensure
|
|
|
|
|
* it is complete to avoid having to retry the operation after a failed
|
|
|
|
|
* attempt. The message is not supposed to be invalid, which is why a few
|
|
|
|
|
* properties such as the character set used in the header field names are not
|
|
|
|
|
* checked. In case of an unparsable response message, a negative value will be
|
|
|
|
|
* returned with h1m->err_pos and h1m->err_state matching the location and
|
|
|
|
|
* state where the error was met. Leading blank likes are tolerated but not
|
2019-01-04 04:48:03 -05:00
|
|
|
* recommended. If flag H1_MF_HDRS_ONLY is set in h1m->flags, only headers are
|
|
|
|
|
* parsed and the start line is skipped. It is not required to set h1m->state
|
|
|
|
|
* nor h1m->next in this case.
|
2017-07-26 03:07:47 -04:00
|
|
|
*
|
|
|
|
|
* This function returns :
|
|
|
|
|
* -1 in case of error. In this case, h1m->err_state is filled (if h1m is
|
2018-09-11 05:45:04 -04:00
|
|
|
* set) with the state the error occurred in and h1m->err_pos with the
|
2017-07-26 03:07:47 -04:00
|
|
|
* the position relative to <start>
|
|
|
|
|
* -2 if the output is full (hdr_num reached). err_state and err_pos also
|
|
|
|
|
* indicate where it failed.
|
|
|
|
|
* 0 in case of missing data.
|
|
|
|
|
* > 0 on success, it then corresponds to the number of bytes read since
|
|
|
|
|
* <start> so that the caller can go on with the payload.
|
|
|
|
|
*/
|
|
|
|
|
int h1_headers_to_hdr_list(char *start, const char *stop,
|
|
|
|
|
struct http_hdr *hdr, unsigned int hdr_num,
|
2018-09-11 09:34:50 -04:00
|
|
|
struct h1m *h1m, union h1_sl *slp)
|
2017-07-26 03:07:47 -04:00
|
|
|
{
|
2018-09-11 10:20:30 -04:00
|
|
|
enum h1m_state state;
|
|
|
|
|
register char *ptr;
|
|
|
|
|
register const char *end;
|
|
|
|
|
unsigned int hdr_count;
|
|
|
|
|
unsigned int skip; /* number of bytes skipped at the beginning */
|
|
|
|
|
unsigned int sol; /* start of line */
|
|
|
|
|
unsigned int col; /* position of the colon */
|
|
|
|
|
unsigned int eol; /* end of line */
|
|
|
|
|
unsigned int sov; /* start of value */
|
2018-09-11 09:34:50 -04:00
|
|
|
union h1_sl sl;
|
2018-09-11 10:20:30 -04:00
|
|
|
int skip_update;
|
|
|
|
|
int restarting;
|
2019-10-11 03:01:44 -04:00
|
|
|
int host_idx;
|
2017-07-26 03:07:47 -04:00
|
|
|
struct ist n, v; /* header name and value during parsing */
|
|
|
|
|
|
2018-09-11 10:20:30 -04:00
|
|
|
skip = 0; // do it only once to keep track of the leading CRLF.
|
|
|
|
|
|
|
|
|
|
try_again:
|
|
|
|
|
hdr_count = sol = col = eol = sov = 0;
|
2018-09-11 09:34:50 -04:00
|
|
|
sl.st.status = 0;
|
2018-09-11 10:20:30 -04:00
|
|
|
skip_update = restarting = 0;
|
2019-10-11 03:01:44 -04:00
|
|
|
host_idx = -1;
|
2018-09-11 10:20:30 -04:00
|
|
|
|
2019-01-04 04:48:03 -05:00
|
|
|
if (h1m->flags & H1_MF_HDRS_ONLY) {
|
|
|
|
|
state = H1_MSG_HDR_FIRST;
|
|
|
|
|
h1m->next = 0;
|
|
|
|
|
}
|
2019-01-04 10:06:48 -05:00
|
|
|
else {
|
2019-01-04 04:48:03 -05:00
|
|
|
state = h1m->state;
|
2019-01-04 10:06:48 -05:00
|
|
|
if (h1m->state != H1_MSG_RQBEFORE && h1m->state != H1_MSG_RPBEFORE)
|
|
|
|
|
restarting = 1;
|
|
|
|
|
}
|
2019-01-04 04:48:03 -05:00
|
|
|
|
2018-09-11 10:20:30 -04:00
|
|
|
ptr = start + h1m->next;
|
|
|
|
|
end = stop;
|
|
|
|
|
|
2017-07-26 03:07:47 -04:00
|
|
|
if (unlikely(ptr >= end))
|
|
|
|
|
goto http_msg_ood;
|
|
|
|
|
|
2018-09-11 10:20:30 -04:00
|
|
|
/* don't update output if hdr is NULL or if we're restarting */
|
|
|
|
|
if (!hdr || restarting)
|
2018-09-11 10:04:48 -04:00
|
|
|
skip_update = 1;
|
|
|
|
|
|
2017-07-26 03:07:47 -04:00
|
|
|
switch (state) {
|
2018-09-11 11:57:05 -04:00
|
|
|
case H1_MSG_RQBEFORE:
|
|
|
|
|
http_msg_rqbefore:
|
|
|
|
|
if (likely(HTTP_IS_TOKEN(*ptr))) {
|
|
|
|
|
/* we have a start of message, we may have skipped some
|
|
|
|
|
* heading CRLF. Skip them now.
|
|
|
|
|
*/
|
|
|
|
|
skip += ptr - start;
|
|
|
|
|
start = ptr;
|
|
|
|
|
|
|
|
|
|
sol = 0;
|
2018-10-08 09:34:02 -04:00
|
|
|
sl.rq.m.ptr = ptr;
|
2018-09-11 11:57:05 -04:00
|
|
|
hdr_count = 0;
|
|
|
|
|
state = H1_MSG_RQMETH;
|
|
|
|
|
goto http_msg_rqmeth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (unlikely(!HTTP_IS_CRLF(*ptr))) {
|
|
|
|
|
state = H1_MSG_RQBEFORE;
|
|
|
|
|
goto http_msg_invalid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (unlikely(*ptr == '\n'))
|
|
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, H1_MSG_RQBEFORE);
|
|
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore_cr, http_msg_ood, state, H1_MSG_RQBEFORE_CR);
|
|
|
|
|
/* stop here */
|
|
|
|
|
|
|
|
|
|
case H1_MSG_RQBEFORE_CR:
|
|
|
|
|
http_msg_rqbefore_cr:
|
|
|
|
|
EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RQBEFORE_CR);
|
|
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, H1_MSG_RQBEFORE);
|
|
|
|
|
/* stop here */
|
|
|
|
|
|
|
|
|
|
case H1_MSG_RQMETH:
|
|
|
|
|
http_msg_rqmeth:
|
|
|
|
|
if (likely(HTTP_IS_TOKEN(*ptr)))
|
|
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth, http_msg_ood, state, H1_MSG_RQMETH);
|
|
|
|
|
|
|
|
|
|
if (likely(HTTP_IS_SPHT(*ptr))) {
|
2018-10-08 09:34:02 -04:00
|
|
|
sl.rq.m.len = ptr - sl.rq.m.ptr;
|
|
|
|
|
sl.rq.meth = find_http_meth(start, sl.rq.m.len);
|
2018-09-11 11:57:05 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, H1_MSG_RQMETH_SP);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (likely(HTTP_IS_CRLF(*ptr))) {
|
|
|
|
|
/* HTTP 0.9 request */
|
2018-10-08 09:34:02 -04:00
|
|
|
sl.rq.m.len = ptr - sl.rq.m.ptr;
|
|
|
|
|
sl.rq.meth = find_http_meth(sl.rq.m.ptr, sl.rq.m.len);
|
2018-09-11 11:57:05 -04:00
|
|
|
http_msg_req09_uri:
|
2018-10-08 09:34:02 -04:00
|
|
|
sl.rq.u.ptr = ptr;
|
2018-09-11 11:57:05 -04:00
|
|
|
http_msg_req09_uri_e:
|
2018-10-08 09:34:02 -04:00
|
|
|
sl.rq.u.len = ptr - sl.rq.u.ptr;
|
2018-09-11 11:57:05 -04:00
|
|
|
http_msg_req09_ver:
|
2022-03-15 08:11:06 -04:00
|
|
|
sl.rq.v = ist2(ptr, 0);
|
2018-09-11 11:57:05 -04:00
|
|
|
goto http_msg_rqline_eol;
|
|
|
|
|
}
|
|
|
|
|
state = H1_MSG_RQMETH;
|
|
|
|
|
goto http_msg_invalid;
|
|
|
|
|
|
|
|
|
|
case H1_MSG_RQMETH_SP:
|
|
|
|
|
http_msg_rqmeth_sp:
|
|
|
|
|
if (likely(!HTTP_IS_LWS(*ptr))) {
|
2018-10-08 09:34:02 -04:00
|
|
|
sl.rq.u.ptr = ptr;
|
2018-09-11 11:57:05 -04:00
|
|
|
goto http_msg_rquri;
|
|
|
|
|
}
|
|
|
|
|
if (likely(HTTP_IS_SPHT(*ptr)))
|
|
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, H1_MSG_RQMETH_SP);
|
|
|
|
|
/* so it's a CR/LF, meaning an HTTP 0.9 request */
|
|
|
|
|
goto http_msg_req09_uri;
|
|
|
|
|
|
|
|
|
|
case H1_MSG_RQURI:
|
|
|
|
|
http_msg_rquri:
|
2020-02-21 10:31:22 -05:00
|
|
|
#ifdef HA_UNALIGNED_LE
|
BUG/MINOR: h1: do not accept '#' as part of the URI component
Seth Manesse and Paul Plasil reported that the "path" sample fetch
function incorrectly accepts '#' as part of the path component. This
can in some cases lead to misrouted requests for rules that would apply
on the suffix:
use_backend static if { path_end .png .jpg .gif .css .js }
Note that this behavior can be selectively configured using
"normalize-uri fragment-encode" and "normalize-uri fragment-strip".
The problem is that while the RFC says that this '#' must never be
emitted, as often it doesn't suggest how servers should handle it. A
diminishing number of servers still do accept it and trim it silently,
while others are rejecting it, as indicated in the conversation below
with other implementers:
https://lists.w3.org/Archives/Public/ietf-http-wg/2023JulSep/0070.html
Looking at logs from publicly exposed servers, such requests appear at
a rate of roughly 1 per million and only come from attacks or poorly
written web crawlers incorrectly following links found on various pages.
Thus it looks like the best solution to this problem is to simply reject
such ambiguous requests by default, and include this in the list of
controls that can be disabled using "option accept-invalid-http-request".
We're already rejecting URIs containing any control char anyway, so we
should also reject '#'.
In the H1 parser for the H1_MSG_RQURI state, there is an accelerated
parser for bytes 0x21..0x7e that has been tightened to 0x24..0x7e (it
should not impact perf since 0x21..0x23 are not supposed to appear in
a URI anyway). This way '#' falls through the fine-grained filter and
we can add the special case for it also conditionned by a check on the
proxy's option "accept-invalid-http-request", with no overhead for the
vast majority of valid URIs. Here this information is available through
h1m->err_pos that's set to -2 when the option is here (so we don't need
to change the API to expose the proxy). Example with a trivial GET
through netcat:
[08/Aug/2023:16:16:52.651] frontend layer1 (#2): invalid request
backend <NONE> (#-1), server <NONE> (#-1), event #0, src 127.0.0.1:50812
buffer starts at 0 (including 0 out), 16361 free,
len 23, wraps at 16336, error at position 7
H1 connection flags 0x00000000, H1 stream flags 0x00000810
H1 msg state MSG_RQURI(4), H1 msg flags 0x00001400
H1 chunk len 0 bytes, H1 body len 0 bytes :
00000 GET /aa#bb HTTP/1.0\r\n
00021 \r\n
This should be progressively backported to all stable versions along with
the following patch:
REGTESTS: http-rules: add accept-invalid-http-request for normalize-uri tests
Similar fixes for h2 and h3 will come in followup patches.
Thanks to Seth Manesse and Paul Plasil for reporting this problem with
detailed explanations.
2023-08-08 10:17:22 -04:00
|
|
|
/* speedup: skip bytes not between 0x24 and 0x7e inclusive */
|
2018-09-11 11:57:05 -04:00
|
|
|
while (ptr <= end - sizeof(int)) {
|
2024-04-24 09:57:15 -04:00
|
|
|
if (is_char4_outside(*(uint *)ptr, 0x24, 0x7e))
|
2018-09-11 11:57:05 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
ptr += sizeof(int);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
if (ptr >= end) {
|
|
|
|
|
state = H1_MSG_RQURI;
|
|
|
|
|
goto http_msg_ood;
|
|
|
|
|
}
|
|
|
|
|
http_msg_rquri2:
|
BUG/MINOR: h1: do not accept '#' as part of the URI component
Seth Manesse and Paul Plasil reported that the "path" sample fetch
function incorrectly accepts '#' as part of the path component. This
can in some cases lead to misrouted requests for rules that would apply
on the suffix:
use_backend static if { path_end .png .jpg .gif .css .js }
Note that this behavior can be selectively configured using
"normalize-uri fragment-encode" and "normalize-uri fragment-strip".
The problem is that while the RFC says that this '#' must never be
emitted, as often it doesn't suggest how servers should handle it. A
diminishing number of servers still do accept it and trim it silently,
while others are rejecting it, as indicated in the conversation below
with other implementers:
https://lists.w3.org/Archives/Public/ietf-http-wg/2023JulSep/0070.html
Looking at logs from publicly exposed servers, such requests appear at
a rate of roughly 1 per million and only come from attacks or poorly
written web crawlers incorrectly following links found on various pages.
Thus it looks like the best solution to this problem is to simply reject
such ambiguous requests by default, and include this in the list of
controls that can be disabled using "option accept-invalid-http-request".
We're already rejecting URIs containing any control char anyway, so we
should also reject '#'.
In the H1 parser for the H1_MSG_RQURI state, there is an accelerated
parser for bytes 0x21..0x7e that has been tightened to 0x24..0x7e (it
should not impact perf since 0x21..0x23 are not supposed to appear in
a URI anyway). This way '#' falls through the fine-grained filter and
we can add the special case for it also conditionned by a check on the
proxy's option "accept-invalid-http-request", with no overhead for the
vast majority of valid URIs. Here this information is available through
h1m->err_pos that's set to -2 when the option is here (so we don't need
to change the API to expose the proxy). Example with a trivial GET
through netcat:
[08/Aug/2023:16:16:52.651] frontend layer1 (#2): invalid request
backend <NONE> (#-1), server <NONE> (#-1), event #0, src 127.0.0.1:50812
buffer starts at 0 (including 0 out), 16361 free,
len 23, wraps at 16336, error at position 7
H1 connection flags 0x00000000, H1 stream flags 0x00000810
H1 msg state MSG_RQURI(4), H1 msg flags 0x00001400
H1 chunk len 0 bytes, H1 body len 0 bytes :
00000 GET /aa#bb HTTP/1.0\r\n
00021 \r\n
This should be progressively backported to all stable versions along with
the following patch:
REGTESTS: http-rules: add accept-invalid-http-request for normalize-uri tests
Similar fixes for h2 and h3 will come in followup patches.
Thanks to Seth Manesse and Paul Plasil for reporting this problem with
detailed explanations.
2023-08-08 10:17:22 -04:00
|
|
|
if (likely((unsigned char)(*ptr - 33) <= 93)) { /* 33 to 126 included */
|
|
|
|
|
if (*ptr == '#') {
|
|
|
|
|
if (h1m->err_pos < -1) /* PR_O2_REQBUG_OK not set */
|
|
|
|
|
goto invalid_char;
|
|
|
|
|
if (h1m->err_pos == -1) /* PR_O2_REQBUG_OK set: just log */
|
|
|
|
|
h1m->err_pos = ptr - start + skip;
|
|
|
|
|
}
|
2018-09-11 11:57:05 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri2, http_msg_ood, state, H1_MSG_RQURI);
|
BUG/MINOR: h1: do not accept '#' as part of the URI component
Seth Manesse and Paul Plasil reported that the "path" sample fetch
function incorrectly accepts '#' as part of the path component. This
can in some cases lead to misrouted requests for rules that would apply
on the suffix:
use_backend static if { path_end .png .jpg .gif .css .js }
Note that this behavior can be selectively configured using
"normalize-uri fragment-encode" and "normalize-uri fragment-strip".
The problem is that while the RFC says that this '#' must never be
emitted, as often it doesn't suggest how servers should handle it. A
diminishing number of servers still do accept it and trim it silently,
while others are rejecting it, as indicated in the conversation below
with other implementers:
https://lists.w3.org/Archives/Public/ietf-http-wg/2023JulSep/0070.html
Looking at logs from publicly exposed servers, such requests appear at
a rate of roughly 1 per million and only come from attacks or poorly
written web crawlers incorrectly following links found on various pages.
Thus it looks like the best solution to this problem is to simply reject
such ambiguous requests by default, and include this in the list of
controls that can be disabled using "option accept-invalid-http-request".
We're already rejecting URIs containing any control char anyway, so we
should also reject '#'.
In the H1 parser for the H1_MSG_RQURI state, there is an accelerated
parser for bytes 0x21..0x7e that has been tightened to 0x24..0x7e (it
should not impact perf since 0x21..0x23 are not supposed to appear in
a URI anyway). This way '#' falls through the fine-grained filter and
we can add the special case for it also conditionned by a check on the
proxy's option "accept-invalid-http-request", with no overhead for the
vast majority of valid URIs. Here this information is available through
h1m->err_pos that's set to -2 when the option is here (so we don't need
to change the API to expose the proxy). Example with a trivial GET
through netcat:
[08/Aug/2023:16:16:52.651] frontend layer1 (#2): invalid request
backend <NONE> (#-1), server <NONE> (#-1), event #0, src 127.0.0.1:50812
buffer starts at 0 (including 0 out), 16361 free,
len 23, wraps at 16336, error at position 7
H1 connection flags 0x00000000, H1 stream flags 0x00000810
H1 msg state MSG_RQURI(4), H1 msg flags 0x00001400
H1 chunk len 0 bytes, H1 body len 0 bytes :
00000 GET /aa#bb HTTP/1.0\r\n
00021 \r\n
This should be progressively backported to all stable versions along with
the following patch:
REGTESTS: http-rules: add accept-invalid-http-request for normalize-uri tests
Similar fixes for h2 and h3 will come in followup patches.
Thanks to Seth Manesse and Paul Plasil for reporting this problem with
detailed explanations.
2023-08-08 10:17:22 -04:00
|
|
|
}
|
2018-09-11 11:57:05 -04:00
|
|
|
|
|
|
|
|
if (likely(HTTP_IS_SPHT(*ptr))) {
|
2018-10-08 09:34:02 -04:00
|
|
|
sl.rq.u.len = ptr - sl.rq.u.ptr;
|
2018-09-11 11:57:05 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, H1_MSG_RQURI_SP);
|
|
|
|
|
}
|
|
|
|
|
if (likely((unsigned char)*ptr >= 128)) {
|
|
|
|
|
/* non-ASCII chars are forbidden unless option
|
2024-09-12 03:33:32 -04:00
|
|
|
* accept-unsafe-violations-in-http-request is enabled in the frontend.
|
2018-09-11 11:57:05 -04:00
|
|
|
* In any case, we capture the faulty char.
|
|
|
|
|
*/
|
|
|
|
|
if (h1m->err_pos < -1)
|
|
|
|
|
goto invalid_char;
|
|
|
|
|
if (h1m->err_pos == -1)
|
|
|
|
|
h1m->err_pos = ptr - start + skip;
|
|
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri, http_msg_ood, state, H1_MSG_RQURI);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (likely(HTTP_IS_CRLF(*ptr))) {
|
|
|
|
|
/* so it's a CR/LF, meaning an HTTP 0.9 request */
|
|
|
|
|
goto http_msg_req09_uri_e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* OK forbidden chars, 0..31 or 127 */
|
|
|
|
|
invalid_char:
|
|
|
|
|
state = H1_MSG_RQURI;
|
|
|
|
|
goto http_msg_invalid;
|
|
|
|
|
|
|
|
|
|
case H1_MSG_RQURI_SP:
|
|
|
|
|
http_msg_rquri_sp:
|
|
|
|
|
if (likely(!HTTP_IS_LWS(*ptr))) {
|
2018-10-08 09:34:02 -04:00
|
|
|
sl.rq.v.ptr = ptr;
|
2018-09-11 11:57:05 -04:00
|
|
|
goto http_msg_rqver;
|
|
|
|
|
}
|
|
|
|
|
if (likely(HTTP_IS_SPHT(*ptr)))
|
|
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, H1_MSG_RQURI_SP);
|
|
|
|
|
/* so it's a CR/LF, meaning an HTTP 0.9 request */
|
|
|
|
|
goto http_msg_req09_ver;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case H1_MSG_RQVER:
|
|
|
|
|
http_msg_rqver:
|
|
|
|
|
if (likely(HTTP_IS_VER_TOKEN(*ptr)))
|
|
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqver, http_msg_ood, state, H1_MSG_RQVER);
|
|
|
|
|
|
|
|
|
|
if (likely(HTTP_IS_CRLF(*ptr))) {
|
2018-10-08 09:34:02 -04:00
|
|
|
sl.rq.v.len = ptr - sl.rq.v.ptr;
|
2018-09-11 11:57:05 -04:00
|
|
|
http_msg_rqline_eol:
|
|
|
|
|
/* We have seen the end of line. Note that we do not
|
|
|
|
|
* necessarily have the \n yet, but at least we know that we
|
|
|
|
|
* have EITHER \r OR \n, otherwise the request would not be
|
|
|
|
|
* complete. We can then record the request length and return
|
|
|
|
|
* to the caller which will be able to register it.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (likely(!skip_update)) {
|
2018-10-08 09:34:02 -04:00
|
|
|
if ((sl.rq.v.len == 8) &&
|
|
|
|
|
(*(sl.rq.v.ptr + 5) > '1' ||
|
|
|
|
|
(*(sl.rq.v.ptr + 5) == '1' && *(sl.rq.v.ptr + 7) >= '1')))
|
2018-09-13 05:32:51 -04:00
|
|
|
h1m->flags |= H1_MF_VER_11;
|
|
|
|
|
|
2018-09-11 11:57:05 -04:00
|
|
|
if (unlikely(hdr_count >= hdr_num)) {
|
|
|
|
|
state = H1_MSG_RQVER;
|
|
|
|
|
goto http_output_full;
|
|
|
|
|
}
|
2018-10-08 09:50:15 -04:00
|
|
|
if (!(h1m->flags & H1_MF_NO_PHDR))
|
|
|
|
|
http_set_hdr(&hdr[hdr_count++], ist(":method"), sl.rq.m);
|
2018-09-11 11:57:05 -04:00
|
|
|
|
|
|
|
|
if (unlikely(hdr_count >= hdr_num)) {
|
|
|
|
|
state = H1_MSG_RQVER;
|
|
|
|
|
goto http_output_full;
|
|
|
|
|
}
|
2018-10-08 09:50:15 -04:00
|
|
|
if (!(h1m->flags & H1_MF_NO_PHDR))
|
|
|
|
|
http_set_hdr(&hdr[hdr_count++], ist(":path"), sl.rq.u);
|
2018-09-11 11:57:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sol = ptr - start;
|
|
|
|
|
if (likely(*ptr == '\r'))
|
|
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqline_end, http_msg_ood, state, H1_MSG_RQLINE_END);
|
|
|
|
|
goto http_msg_rqline_end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* neither an HTTP_VER token nor a CRLF */
|
|
|
|
|
state = H1_MSG_RQVER;
|
|
|
|
|
goto http_msg_invalid;
|
|
|
|
|
|
|
|
|
|
case H1_MSG_RQLINE_END:
|
|
|
|
|
http_msg_rqline_end:
|
|
|
|
|
/* check for HTTP/0.9 request : no version information
|
|
|
|
|
* available. sol must point to the first of CR or LF. However
|
|
|
|
|
* since we don't save these elements between calls, if we come
|
|
|
|
|
* here from a restart, we don't necessarily know. Thus in this
|
|
|
|
|
* case we simply start over.
|
|
|
|
|
*/
|
|
|
|
|
if (restarting)
|
|
|
|
|
goto restart;
|
|
|
|
|
|
2018-10-08 09:34:02 -04:00
|
|
|
if (unlikely(sl.rq.v.len == 0))
|
2018-09-11 11:57:05 -04:00
|
|
|
goto http_msg_last_lf;
|
|
|
|
|
|
|
|
|
|
EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RQLINE_END);
|
|
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_first, http_msg_ood, state, H1_MSG_HDR_FIRST);
|
|
|
|
|
/* stop here */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Common states below
|
|
|
|
|
*/
|
2018-09-11 05:45:04 -04:00
|
|
|
case H1_MSG_RPBEFORE:
|
2017-07-26 03:07:47 -04:00
|
|
|
http_msg_rpbefore:
|
|
|
|
|
if (likely(HTTP_IS_TOKEN(*ptr))) {
|
|
|
|
|
/* we have a start of message, we may have skipped some
|
|
|
|
|
* heading CRLF. Skip them now.
|
|
|
|
|
*/
|
|
|
|
|
skip += ptr - start;
|
|
|
|
|
start = ptr;
|
|
|
|
|
|
|
|
|
|
sol = 0;
|
2018-10-08 09:34:02 -04:00
|
|
|
sl.st.v.ptr = ptr;
|
2017-07-26 03:07:47 -04:00
|
|
|
hdr_count = 0;
|
2018-09-11 05:45:04 -04:00
|
|
|
state = H1_MSG_RPVER;
|
2017-07-26 03:07:47 -04:00
|
|
|
goto http_msg_rpver;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (unlikely(!HTTP_IS_CRLF(*ptr))) {
|
2018-09-11 05:45:04 -04:00
|
|
|
state = H1_MSG_RPBEFORE;
|
2017-07-26 03:07:47 -04:00
|
|
|
goto http_msg_invalid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (unlikely(*ptr == '\n'))
|
2018-09-11 05:45:04 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, H1_MSG_RPBEFORE);
|
|
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore_cr, http_msg_ood, state, H1_MSG_RPBEFORE_CR);
|
2017-07-26 03:07:47 -04:00
|
|
|
/* stop here */
|
|
|
|
|
|
2018-09-11 05:45:04 -04:00
|
|
|
case H1_MSG_RPBEFORE_CR:
|
2017-07-26 03:07:47 -04:00
|
|
|
http_msg_rpbefore_cr:
|
2018-09-11 05:45:04 -04:00
|
|
|
EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RPBEFORE_CR);
|
|
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, H1_MSG_RPBEFORE);
|
2017-07-26 03:07:47 -04:00
|
|
|
/* stop here */
|
|
|
|
|
|
2018-09-11 05:45:04 -04:00
|
|
|
case H1_MSG_RPVER:
|
2017-07-26 03:07:47 -04:00
|
|
|
http_msg_rpver:
|
|
|
|
|
if (likely(HTTP_IS_VER_TOKEN(*ptr)))
|
2018-09-11 05:45:04 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpver, http_msg_ood, state, H1_MSG_RPVER);
|
2017-07-26 03:07:47 -04:00
|
|
|
|
|
|
|
|
if (likely(HTTP_IS_SPHT(*ptr))) {
|
2018-10-08 09:34:02 -04:00
|
|
|
sl.st.v.len = ptr - sl.st.v.ptr;
|
2018-09-13 05:32:51 -04:00
|
|
|
|
2018-10-08 09:34:02 -04:00
|
|
|
if ((sl.st.v.len == 8) &&
|
|
|
|
|
(*(sl.st.v.ptr + 5) > '1' ||
|
|
|
|
|
(*(sl.st.v.ptr + 5) == '1' && *(sl.st.v.ptr + 7) >= '1')))
|
2018-09-13 05:32:51 -04:00
|
|
|
h1m->flags |= H1_MF_VER_11;
|
|
|
|
|
|
2018-09-11 05:45:04 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpver_sp, http_msg_ood, state, H1_MSG_RPVER_SP);
|
2017-07-26 03:07:47 -04:00
|
|
|
}
|
2018-09-11 05:45:04 -04:00
|
|
|
state = H1_MSG_RPVER;
|
2017-07-26 03:07:47 -04:00
|
|
|
goto http_msg_invalid;
|
|
|
|
|
|
2018-09-11 05:45:04 -04:00
|
|
|
case H1_MSG_RPVER_SP:
|
2017-07-26 03:07:47 -04:00
|
|
|
http_msg_rpver_sp:
|
|
|
|
|
if (likely(!HTTP_IS_LWS(*ptr))) {
|
2018-09-11 09:34:50 -04:00
|
|
|
sl.st.status = 0;
|
2018-10-08 09:34:02 -04:00
|
|
|
sl.st.c.ptr = ptr;
|
2017-07-26 03:07:47 -04:00
|
|
|
goto http_msg_rpcode;
|
|
|
|
|
}
|
|
|
|
|
if (likely(HTTP_IS_SPHT(*ptr)))
|
2018-09-11 05:45:04 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpver_sp, http_msg_ood, state, H1_MSG_RPVER_SP);
|
2017-07-26 03:07:47 -04:00
|
|
|
/* so it's a CR/LF, this is invalid */
|
2018-09-11 05:45:04 -04:00
|
|
|
state = H1_MSG_RPVER_SP;
|
2017-07-26 03:07:47 -04:00
|
|
|
goto http_msg_invalid;
|
|
|
|
|
|
2018-09-11 05:45:04 -04:00
|
|
|
case H1_MSG_RPCODE:
|
2017-07-26 03:07:47 -04:00
|
|
|
http_msg_rpcode:
|
2017-11-09 05:15:45 -05:00
|
|
|
if (likely(HTTP_IS_DIGIT(*ptr))) {
|
2018-09-11 09:34:50 -04:00
|
|
|
sl.st.status = sl.st.status * 10 + *ptr - '0';
|
2018-09-11 05:45:04 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpcode, http_msg_ood, state, H1_MSG_RPCODE);
|
2017-07-26 03:07:47 -04:00
|
|
|
}
|
|
|
|
|
|
2017-11-09 05:15:45 -05:00
|
|
|
if (unlikely(!HTTP_IS_LWS(*ptr))) {
|
2018-09-11 05:45:04 -04:00
|
|
|
state = H1_MSG_RPCODE;
|
2017-11-09 05:15:45 -05:00
|
|
|
goto http_msg_invalid;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 03:07:47 -04:00
|
|
|
if (likely(HTTP_IS_SPHT(*ptr))) {
|
2018-10-08 09:34:02 -04:00
|
|
|
sl.st.c.len = ptr - sl.st.c.ptr;
|
2018-09-11 05:45:04 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpcode_sp, http_msg_ood, state, H1_MSG_RPCODE_SP);
|
2017-07-26 03:07:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* so it's a CR/LF, so there is no reason phrase */
|
2018-10-08 09:34:02 -04:00
|
|
|
sl.st.c.len = ptr - sl.st.c.ptr;
|
2017-07-26 03:07:47 -04:00
|
|
|
|
|
|
|
|
http_msg_rsp_reason:
|
2022-03-15 08:11:06 -04:00
|
|
|
sl.st.r = ist2(ptr, 0);
|
2017-07-26 03:07:47 -04:00
|
|
|
goto http_msg_rpline_eol;
|
|
|
|
|
|
2018-09-11 05:45:04 -04:00
|
|
|
case H1_MSG_RPCODE_SP:
|
2017-07-26 03:07:47 -04:00
|
|
|
http_msg_rpcode_sp:
|
|
|
|
|
if (likely(!HTTP_IS_LWS(*ptr))) {
|
2018-10-08 09:34:02 -04:00
|
|
|
sl.st.r.ptr = ptr;
|
2017-07-26 03:07:47 -04:00
|
|
|
goto http_msg_rpreason;
|
|
|
|
|
}
|
|
|
|
|
if (likely(HTTP_IS_SPHT(*ptr)))
|
2018-09-11 05:45:04 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpcode_sp, http_msg_ood, state, H1_MSG_RPCODE_SP);
|
2017-07-26 03:07:47 -04:00
|
|
|
/* so it's a CR/LF, so there is no reason phrase */
|
|
|
|
|
goto http_msg_rsp_reason;
|
|
|
|
|
|
2018-09-11 05:45:04 -04:00
|
|
|
case H1_MSG_RPREASON:
|
2017-07-26 03:07:47 -04:00
|
|
|
http_msg_rpreason:
|
|
|
|
|
if (likely(!HTTP_IS_CRLF(*ptr)))
|
2018-09-11 05:45:04 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpreason, http_msg_ood, state, H1_MSG_RPREASON);
|
2018-10-08 09:34:02 -04:00
|
|
|
sl.st.r.len = ptr - sl.st.r.ptr;
|
2017-07-26 03:07:47 -04:00
|
|
|
http_msg_rpline_eol:
|
|
|
|
|
/* We have seen the end of line. Note that we do not
|
|
|
|
|
* necessarily have the \n yet, but at least we know that we
|
|
|
|
|
* have EITHER \r OR \n, otherwise the response would not be
|
|
|
|
|
* complete. We can then record the response length and return
|
|
|
|
|
* to the caller which will be able to register it.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-09-11 10:04:48 -04:00
|
|
|
if (likely(!skip_update)) {
|
|
|
|
|
if (unlikely(hdr_count >= hdr_num)) {
|
|
|
|
|
state = H1_MSG_RPREASON;
|
|
|
|
|
goto http_output_full;
|
|
|
|
|
}
|
2018-10-08 09:50:15 -04:00
|
|
|
if (!(h1m->flags & H1_MF_NO_PHDR))
|
|
|
|
|
http_set_hdr(&hdr[hdr_count++], ist(":status"), sl.st.c);
|
2017-07-26 03:07:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sol = ptr - start;
|
|
|
|
|
if (likely(*ptr == '\r'))
|
2018-09-11 05:45:04 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpline_end, http_msg_ood, state, H1_MSG_RPLINE_END);
|
2017-07-26 03:07:47 -04:00
|
|
|
goto http_msg_rpline_end;
|
|
|
|
|
|
2018-09-11 05:45:04 -04:00
|
|
|
case H1_MSG_RPLINE_END:
|
2017-07-26 03:07:47 -04:00
|
|
|
http_msg_rpline_end:
|
|
|
|
|
/* sol must point to the first of CR or LF. */
|
2018-09-11 05:45:04 -04:00
|
|
|
EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RPLINE_END);
|
|
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_first, http_msg_ood, state, H1_MSG_HDR_FIRST);
|
2017-07-26 03:07:47 -04:00
|
|
|
/* stop here */
|
|
|
|
|
|
2018-09-11 05:45:04 -04:00
|
|
|
case H1_MSG_HDR_FIRST:
|
2017-07-26 03:07:47 -04:00
|
|
|
http_msg_hdr_first:
|
|
|
|
|
sol = ptr - start;
|
|
|
|
|
if (likely(!HTTP_IS_CRLF(*ptr))) {
|
|
|
|
|
goto http_msg_hdr_name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (likely(*ptr == '\r'))
|
2018-09-11 05:45:04 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_last_lf, http_msg_ood, state, H1_MSG_LAST_LF);
|
2017-07-26 03:07:47 -04:00
|
|
|
goto http_msg_last_lf;
|
|
|
|
|
|
2018-09-11 05:45:04 -04:00
|
|
|
case H1_MSG_HDR_NAME:
|
2017-07-26 03:07:47 -04:00
|
|
|
http_msg_hdr_name:
|
|
|
|
|
/* assumes sol points to the first char */
|
|
|
|
|
if (likely(HTTP_IS_TOKEN(*ptr))) {
|
2018-09-19 08:01:04 -04:00
|
|
|
if (!skip_update) {
|
|
|
|
|
/* turn it to lower case if needed */
|
|
|
|
|
if (isupper((unsigned char)*ptr) && h1m->flags & H1_MF_TOLOWER)
|
2020-07-05 15:46:32 -04:00
|
|
|
*ptr = tolower((unsigned char)*ptr);
|
2018-09-19 08:01:04 -04:00
|
|
|
}
|
2018-09-11 05:45:04 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_name, http_msg_ood, state, H1_MSG_HDR_NAME);
|
2017-07-26 03:07:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (likely(*ptr == ':')) {
|
|
|
|
|
col = ptr - start;
|
BUG/CRITICAL: http: properly reject empty http header field names
The HTTP header parsers surprizingly accepts empty header field names,
and this is a leftover from the original code that was agnostic to this.
When muxes were introduced, for H2 first, the HPACK decompressor needed
to feed headers lists, and since empty header names were strictly
forbidden by the protocol, the lists of headers were purposely designed
to be terminated by an empty header field name (a principle that is
similar to H1's empty line termination). This principle was preserved
and generalized to other protocols migrated to muxes (H1/FCGI/H3 etc)
without anyone ever noticing that the H1 parser was still able to deliver
empty header field names to this list. In addition to this it turns out
that the HPACK decompressor, despite a comment in the code, may
successfully decompress an empty header field name, and this mistake
was propagated to the QPACK decompressor as well.
The impact is that an empty header field name may be used to truncate
the list of headers and thus make some headers disappear. While for
H2/H3 the impact is limited as haproxy sees a request with missing
headers, and headers are not used to delimit messages, in the case of
HTTP/1, the impact is significant because the presence (and sometimes
contents) of certain sensitive headers is detected during the parsing.
Thus, some of these headers may be seen, marked as present, their value
extracted, but never delivered to upper layers and obviously not
forwarded to the other side either. This can have for consequence that
certain important header fields such as Connection, Upgrade, Host,
Content-length, Transfer-Encoding etc are possibly seen as different
between what haproxy uses to parse/forward/route and what is observed
in http-request rules and of course, forwarded. One direct consequence
is that it is possible to exploit this property in HTTP/1 to make
affected versions of haproxy forward more data than is advertised on
the other side, and bypass some access controls or routing rules by
crafting extraneous requests. Note, however, that responses to such
requests will normally not be passed back to the client, but this can
still cause some harm.
This specific risk can be mostly worked around in configuration using
the following rule that will rely on the bug's impact to precisely
detect the inconsistency between the known body size and the one
expected to be advertised to the server (the rule works from 2.0 to
2.8-dev):
http-request deny if { fc_http_major 1 } !{ req.body_size 0 } !{ req.hdr(content-length) -m found } !{ req.hdr(transfer-encoding) -m found } !{ method CONNECT }
This will exclusively block such carefully crafted requests delivered
over HTTP/1. HTTP/2 and HTTP/3 do not need content-length, and a body
that arrives without being announced with a content-length will be
forwarded using transfer-encoding, hence will not cause discrepancies.
In HAProxy 2.0 in legacy mode ("no option http-use-htx"), this rule will
simply have no effect but will not cause trouble either.
A clean solution would consist in modifying the loops iterating over
these headers lists to check the header name's pointer instead of its
length (since both are zero at the end of the list), but this requires
to touch tens of places and it's very easy to miss one. Functions such
as htx_add_header(), htx_add_trailer(), htx_add_all_headers() would be
good starting points for such a possible future change.
Instead the current fix focuses on blocking empty headers where they
are first inserted, hence in the H1/HPACK/QPACK decoders. One benefit
of the current solution (for H1) is that it allows "show errors" to
report a precise diagnostic when facing such invalid HTTP/1 requests,
with the exact location of the problem and the originating address:
$ printf "GET / HTTP/1.1\r\nHost: localhost\r\n:empty header\r\n\r\n" | nc 0 8001
HTTP/1.1 400 Bad request
Content-length: 90
Cache-Control: no-cache
Connection: close
Content-Type: text/html
<html><body><h1>400 Bad request</h1>
Your browser sent an invalid request.
</body></html>
$ socat /var/run/haproxy.stat <<< "show errors"
Total events captured on [10/Feb/2023:16:29:37.530] : 1
[10/Feb/2023:16:29:34.155] frontend decrypt (#2): invalid request
backend <NONE> (#-1), server <NONE> (#-1), event #0, src 127.0.0.1:31092
buffer starts at 0 (including 0 out), 16334 free,
len 50, wraps at 16336, error at position 33
H1 connection flags 0x00000000, H1 stream flags 0x00000810
H1 msg state MSG_HDR_NAME(17), H1 msg flags 0x00001410
H1 chunk len 0 bytes, H1 body len 0 bytes :
00000 GET / HTTP/1.1\r\n
00016 Host: localhost\r\n
00033 :empty header\r\n
00048 \r\n
I want to address sincere and warm thanks for their great work to the
team composed of the following security researchers who found the issue
together and reported it: Bahruz Jabiyev, Anthony Gavazzi, and Engin
Kirda from Northeastern University, Kaan Onarlioglu from Akamai
Technologies, Adi Peleg and Harvey Tuch from Google. And kudos to Amaury
Denoyelle from HAProxy Technologies for spotting that the HPACK and
QPACK decoders would let this pass despite the comment explicitly
saying otherwise.
This fix must be backported as far as 2.0. The QPACK changes can be
dropped before 2.6. In 2.0 there is also the equivalent code for legacy
mode, which doesn't suffer from the list truncation, but it would better
be fixed regardless.
CVE-2023-25725 was assigned to this issue.
2023-02-09 15:36:54 -05:00
|
|
|
if (col <= sol) {
|
|
|
|
|
state = H1_MSG_HDR_NAME;
|
|
|
|
|
goto http_msg_invalid;
|
|
|
|
|
}
|
2018-09-11 05:45:04 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l1_sp, http_msg_ood, state, H1_MSG_HDR_L1_SP);
|
2017-07-26 03:07:47 -04:00
|
|
|
}
|
|
|
|
|
|
2018-09-12 03:20:40 -04:00
|
|
|
if (likely(h1m->err_pos < -1) || *ptr == '\n') {
|
2018-09-11 05:45:04 -04:00
|
|
|
state = H1_MSG_HDR_NAME;
|
2017-07-26 03:07:47 -04:00
|
|
|
goto http_msg_invalid;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 03:20:40 -04:00
|
|
|
if (h1m->err_pos == -1) /* capture the error pointer */
|
|
|
|
|
h1m->err_pos = ptr - start + skip; /* >= 0 now */
|
|
|
|
|
|
|
|
|
|
/* and we still accept this non-token character */
|
2018-09-11 05:45:04 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_name, http_msg_ood, state, H1_MSG_HDR_NAME);
|
2017-07-26 03:07:47 -04:00
|
|
|
|
2018-09-11 05:45:04 -04:00
|
|
|
case H1_MSG_HDR_L1_SP:
|
2017-07-26 03:07:47 -04:00
|
|
|
http_msg_hdr_l1_sp:
|
|
|
|
|
/* assumes sol points to the first char */
|
|
|
|
|
if (likely(HTTP_IS_SPHT(*ptr)))
|
2018-09-11 05:45:04 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l1_sp, http_msg_ood, state, H1_MSG_HDR_L1_SP);
|
2017-07-26 03:07:47 -04:00
|
|
|
|
|
|
|
|
/* header value can be basically anything except CR/LF */
|
|
|
|
|
sov = ptr - start;
|
|
|
|
|
|
|
|
|
|
if (likely(!HTTP_IS_CRLF(*ptr))) {
|
|
|
|
|
goto http_msg_hdr_val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (likely(*ptr == '\r'))
|
2018-09-11 05:45:04 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l1_lf, http_msg_ood, state, H1_MSG_HDR_L1_LF);
|
2017-07-26 03:07:47 -04:00
|
|
|
goto http_msg_hdr_l1_lf;
|
|
|
|
|
|
2018-09-11 05:45:04 -04:00
|
|
|
case H1_MSG_HDR_L1_LF:
|
2017-07-26 03:07:47 -04:00
|
|
|
http_msg_hdr_l1_lf:
|
2018-09-11 05:45:04 -04:00
|
|
|
EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_HDR_L1_LF);
|
|
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l1_lws, http_msg_ood, state, H1_MSG_HDR_L1_LWS);
|
2017-07-26 03:07:47 -04:00
|
|
|
|
2018-09-11 05:45:04 -04:00
|
|
|
case H1_MSG_HDR_L1_LWS:
|
2017-07-26 03:07:47 -04:00
|
|
|
http_msg_hdr_l1_lws:
|
|
|
|
|
if (likely(HTTP_IS_SPHT(*ptr))) {
|
2018-09-19 08:01:04 -04:00
|
|
|
if (!skip_update) {
|
|
|
|
|
/* replace HT,CR,LF with spaces */
|
|
|
|
|
for (; start + sov < ptr; sov++)
|
|
|
|
|
start[sov] = ' ';
|
|
|
|
|
}
|
2017-07-26 03:07:47 -04:00
|
|
|
goto http_msg_hdr_l1_sp;
|
|
|
|
|
}
|
|
|
|
|
/* we had a header consisting only in spaces ! */
|
|
|
|
|
eol = sov;
|
|
|
|
|
goto http_msg_complete_header;
|
|
|
|
|
|
2018-09-11 05:45:04 -04:00
|
|
|
case H1_MSG_HDR_VAL:
|
2017-07-26 03:07:47 -04:00
|
|
|
http_msg_hdr_val:
|
|
|
|
|
/* assumes sol points to the first char, and sov
|
|
|
|
|
* points to the first character of the value.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D
|
|
|
|
|
* and lower. In fact since most of the time is spent in the loop, we
|
|
|
|
|
* also remove the sign bit test so that bytes 0x8e..0x0d break the
|
|
|
|
|
* loop, but we don't care since they're very rare in header values.
|
|
|
|
|
*/
|
2020-02-21 10:31:22 -05:00
|
|
|
#ifdef HA_UNALIGNED_LE64
|
2017-07-26 03:07:47 -04:00
|
|
|
while (ptr <= end - sizeof(long)) {
|
2024-04-24 09:57:15 -04:00
|
|
|
if (is_char8_below_opt(*(ulong *)ptr, 0x0e))
|
2017-07-26 03:07:47 -04:00
|
|
|
goto http_msg_hdr_val2;
|
|
|
|
|
ptr += sizeof(long);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2020-02-21 10:31:22 -05:00
|
|
|
#ifdef HA_UNALIGNED_LE
|
2017-07-26 03:07:47 -04:00
|
|
|
while (ptr <= end - sizeof(int)) {
|
2024-04-24 09:57:15 -04:00
|
|
|
if (is_char4_below_opt(*(uint *)ptr, 0x0e))
|
2017-07-26 03:07:47 -04:00
|
|
|
goto http_msg_hdr_val2;
|
|
|
|
|
ptr += sizeof(int);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
if (ptr >= end) {
|
2018-09-11 05:45:04 -04:00
|
|
|
state = H1_MSG_HDR_VAL;
|
2017-07-26 03:07:47 -04:00
|
|
|
goto http_msg_ood;
|
|
|
|
|
}
|
|
|
|
|
http_msg_hdr_val2:
|
2024-01-31 09:10:39 -05:00
|
|
|
if (likely(!*ptr)) {
|
|
|
|
|
/* RFC9110 clarified that NUL is explicitly forbidden in header values
|
|
|
|
|
* (like CR and LF).
|
|
|
|
|
*/
|
|
|
|
|
if (h1m->err_pos < -1) { /* PR_O2_REQBUG_OK not set */
|
|
|
|
|
state = H1_MSG_HDR_VAL;
|
|
|
|
|
goto http_msg_invalid;
|
|
|
|
|
}
|
|
|
|
|
if (h1m->err_pos == -1) /* PR_O2_REQBUG_OK set: just log */
|
|
|
|
|
h1m->err_pos = ptr - start + skip;
|
|
|
|
|
}
|
2017-07-26 03:07:47 -04:00
|
|
|
if (likely(!HTTP_IS_CRLF(*ptr)))
|
2018-09-11 05:45:04 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_val2, http_msg_ood, state, H1_MSG_HDR_VAL);
|
2017-07-26 03:07:47 -04:00
|
|
|
|
|
|
|
|
eol = ptr - start;
|
|
|
|
|
/* Note: we could also copy eol into ->eoh so that we have the
|
|
|
|
|
* real header end in case it ends with lots of LWS, but is this
|
|
|
|
|
* really needed ?
|
|
|
|
|
*/
|
|
|
|
|
if (likely(*ptr == '\r'))
|
2018-09-11 05:45:04 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l2_lf, http_msg_ood, state, H1_MSG_HDR_L2_LF);
|
2017-07-26 03:07:47 -04:00
|
|
|
goto http_msg_hdr_l2_lf;
|
|
|
|
|
|
2018-09-11 05:45:04 -04:00
|
|
|
case H1_MSG_HDR_L2_LF:
|
2017-07-26 03:07:47 -04:00
|
|
|
http_msg_hdr_l2_lf:
|
2018-09-11 05:45:04 -04:00
|
|
|
EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_HDR_L2_LF);
|
|
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l2_lws, http_msg_ood, state, H1_MSG_HDR_L2_LWS);
|
2017-07-26 03:07:47 -04:00
|
|
|
|
2018-09-11 05:45:04 -04:00
|
|
|
case H1_MSG_HDR_L2_LWS:
|
2017-07-26 03:07:47 -04:00
|
|
|
http_msg_hdr_l2_lws:
|
|
|
|
|
if (unlikely(HTTP_IS_SPHT(*ptr))) {
|
2018-09-19 08:01:04 -04:00
|
|
|
if (!skip_update) {
|
|
|
|
|
/* LWS: replace HT,CR,LF with spaces */
|
|
|
|
|
for (; start + eol < ptr; eol++)
|
|
|
|
|
start[eol] = ' ';
|
|
|
|
|
}
|
2017-07-26 03:07:47 -04:00
|
|
|
goto http_msg_hdr_val;
|
|
|
|
|
}
|
|
|
|
|
http_msg_complete_header:
|
|
|
|
|
/*
|
|
|
|
|
* It was a new header, so the last one is finished. Assumes
|
|
|
|
|
* <sol> points to the first char of the name, <col> to the
|
|
|
|
|
* colon, <sov> points to the first character of the value and
|
|
|
|
|
* <eol> to the first CR or LF so we know how the line ends. We
|
|
|
|
|
* will trim spaces around the value. It's possible to do it by
|
|
|
|
|
* adjusting <eol> and <sov> which are no more used after this.
|
|
|
|
|
* We can add the header field to the list.
|
|
|
|
|
*/
|
2018-09-19 08:01:04 -04:00
|
|
|
if (likely(!skip_update)) {
|
|
|
|
|
while (sov < eol && HTTP_IS_LWS(start[sov]))
|
|
|
|
|
sov++;
|
2017-07-26 03:07:47 -04:00
|
|
|
|
2018-09-19 08:01:04 -04:00
|
|
|
while (eol - 1 > sov && HTTP_IS_LWS(start[eol - 1]))
|
|
|
|
|
eol--;
|
2017-07-26 03:07:47 -04:00
|
|
|
|
|
|
|
|
|
2018-09-19 08:01:04 -04:00
|
|
|
n = ist2(start + sol, col - sol);
|
|
|
|
|
v = ist2(start + sov, eol - sov);
|
2017-07-26 03:07:47 -04:00
|
|
|
|
2018-09-19 08:01:04 -04:00
|
|
|
do {
|
|
|
|
|
int ret;
|
2018-09-11 10:04:48 -04:00
|
|
|
|
2018-09-19 08:01:04 -04:00
|
|
|
if (unlikely(hdr_count >= hdr_num)) {
|
2018-09-14 11:11:33 -04:00
|
|
|
state = H1_MSG_HDR_L2_LWS;
|
2018-09-19 08:01:04 -04:00
|
|
|
goto http_output_full;
|
2018-09-14 11:11:33 -04:00
|
|
|
}
|
2018-09-19 08:01:04 -04:00
|
|
|
|
|
|
|
|
if (isteqi(n, ist("transfer-encoding"))) {
|
2021-09-28 03:36:25 -04:00
|
|
|
ret = h1_parse_xfer_enc_header(h1m, v);
|
|
|
|
|
if (ret < 0) {
|
2024-09-11 09:02:07 -04:00
|
|
|
/* For the response only, don't report error if PR_O2_RSPBUG_OK is set
|
|
|
|
|
* and the error can be hidden */
|
|
|
|
|
if (ret == -2 || !(h1m->flags & H1_MF_RESP) || (h1m->err_pos < -1)) {
|
|
|
|
|
state = H1_MSG_HDR_L2_LWS;
|
|
|
|
|
ptr = v.ptr; /* Set ptr on the error */
|
|
|
|
|
goto http_msg_invalid;
|
|
|
|
|
}
|
|
|
|
|
if (h1m->err_pos == -1)
|
|
|
|
|
h1m->err_pos = ptr - start + skip;
|
2021-09-28 03:36:25 -04:00
|
|
|
}
|
|
|
|
|
else if (ret == 0) {
|
|
|
|
|
/* skip it */
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-09-19 08:01:04 -04:00
|
|
|
}
|
|
|
|
|
else if (isteqi(n, ist("content-length"))) {
|
2025-04-15 13:14:31 -04:00
|
|
|
unsigned long long body_len = h1m->body_len;
|
2018-09-19 08:01:04 -04:00
|
|
|
|
2025-04-15 13:14:31 -04:00
|
|
|
ret = http_parse_cont_len_header(&v, &body_len, (h1m->flags & H1_MF_CLEN));
|
2018-09-19 08:01:04 -04:00
|
|
|
if (ret < 0) {
|
|
|
|
|
state = H1_MSG_HDR_L2_LWS;
|
2020-01-06 07:41:01 -05:00
|
|
|
ptr = v.ptr; /* Set ptr on the error */
|
2018-09-19 08:01:04 -04:00
|
|
|
goto http_msg_invalid;
|
|
|
|
|
}
|
|
|
|
|
else if (ret == 0) {
|
|
|
|
|
/* skip it */
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-04-15 13:14:31 -04:00
|
|
|
h1m->flags |= H1_MF_CLEN;
|
|
|
|
|
h1m->curr_len = h1m->body_len = body_len;
|
2018-09-19 08:01:04 -04:00
|
|
|
}
|
|
|
|
|
else if (isteqi(n, ist("connection"))) {
|
2019-03-29 10:03:13 -04:00
|
|
|
h1_parse_connection_header(h1m, &v);
|
|
|
|
|
if (!v.len) {
|
|
|
|
|
/* skip it */
|
|
|
|
|
break;
|
2019-10-11 03:01:44 -04:00
|
|
|
}
|
|
|
|
|
}
|
2020-12-11 11:53:02 -05:00
|
|
|
else if (isteqi(n, ist("upgrade"))) {
|
|
|
|
|
h1_parse_upgrade_header(h1m, v);
|
|
|
|
|
}
|
2022-07-05 08:50:17 -04:00
|
|
|
else if (!(h1m->flags & H1_MF_RESP) && isteqi(n, ist("host"))) {
|
2025-05-16 08:58:52 -04:00
|
|
|
if (host_idx == -1) {
|
2019-10-11 03:01:44 -04:00
|
|
|
host_idx = hdr_count;
|
2025-05-16 08:58:52 -04:00
|
|
|
if (http_authority_has_forbidden_char(v)) {
|
|
|
|
|
state = H1_MSG_HDR_L2_LWS;
|
|
|
|
|
ptr = v.ptr; /* Set ptr on the error */
|
|
|
|
|
goto http_msg_invalid;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-11 03:01:44 -04:00
|
|
|
else {
|
|
|
|
|
if (!isteqi(v, hdr[host_idx].v)) {
|
|
|
|
|
state = H1_MSG_HDR_L2_LWS;
|
2020-01-06 07:41:01 -05:00
|
|
|
ptr = v.ptr; /* Set ptr on the error */
|
2019-10-11 03:01:44 -04:00
|
|
|
goto http_msg_invalid;
|
|
|
|
|
}
|
|
|
|
|
/* if the same host, skip it */
|
|
|
|
|
break;
|
2019-03-29 10:03:13 -04:00
|
|
|
}
|
2018-09-14 11:11:33 -04:00
|
|
|
}
|
2018-09-14 10:28:15 -04:00
|
|
|
|
2018-09-19 08:01:04 -04:00
|
|
|
http_set_hdr(&hdr[hdr_count++], n, v);
|
|
|
|
|
} while (0);
|
|
|
|
|
}
|
2017-07-26 03:07:47 -04:00
|
|
|
|
|
|
|
|
sol = ptr - start;
|
2018-09-19 08:01:04 -04:00
|
|
|
|
2017-07-26 03:07:47 -04:00
|
|
|
if (likely(!HTTP_IS_CRLF(*ptr)))
|
|
|
|
|
goto http_msg_hdr_name;
|
|
|
|
|
|
|
|
|
|
if (likely(*ptr == '\r'))
|
2018-09-11 05:45:04 -04:00
|
|
|
EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_last_lf, http_msg_ood, state, H1_MSG_LAST_LF);
|
2017-07-26 03:07:47 -04:00
|
|
|
goto http_msg_last_lf;
|
|
|
|
|
|
2018-09-11 05:45:04 -04:00
|
|
|
case H1_MSG_LAST_LF:
|
2017-07-26 03:07:47 -04:00
|
|
|
http_msg_last_lf:
|
2018-09-11 05:45:04 -04:00
|
|
|
EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_LAST_LF);
|
2017-07-26 03:07:47 -04:00
|
|
|
ptr++;
|
|
|
|
|
/* <ptr> now points to the first byte of payload. If needed sol
|
|
|
|
|
* still points to the first of either CR or LF of the empty
|
|
|
|
|
* line ending the headers block.
|
|
|
|
|
*/
|
2018-09-11 10:04:48 -04:00
|
|
|
if (likely(!skip_update)) {
|
|
|
|
|
if (unlikely(hdr_count >= hdr_num)) {
|
|
|
|
|
state = H1_MSG_LAST_LF;
|
|
|
|
|
goto http_output_full;
|
|
|
|
|
}
|
2018-09-25 07:59:46 -04:00
|
|
|
http_set_hdr(&hdr[hdr_count++], ist2(start+sol, 0), ist(""));
|
2017-07-26 03:07:47 -04:00
|
|
|
}
|
2018-09-12 11:25:32 -04:00
|
|
|
|
|
|
|
|
/* reaching here we've parsed the whole message. We may detect
|
|
|
|
|
* that we were already continuing an interrupted parsing pass
|
|
|
|
|
* so we were silently looking for the end of message not
|
|
|
|
|
* updating anything before deciding to parse it fully at once.
|
|
|
|
|
* It's guaranteed that we won't match this test twice in a row
|
|
|
|
|
* since restarting will turn zero.
|
|
|
|
|
*/
|
|
|
|
|
if (restarting)
|
|
|
|
|
goto restart;
|
|
|
|
|
|
2022-07-05 08:50:17 -04:00
|
|
|
|
|
|
|
|
if (!(h1m->flags & (H1_MF_HDRS_ONLY|H1_MF_RESP))) {
|
|
|
|
|
struct http_uri_parser parser = http_uri_parser_init(sl.rq.u);
|
2024-05-14 05:42:21 -04:00
|
|
|
struct ist scheme, authority = IST_NULL;
|
2022-11-22 04:04:16 -05:00
|
|
|
int ret;
|
2022-07-05 08:50:17 -04:00
|
|
|
|
BUG/MAJOR: h1: Be stricter on request target validation during message parsing
As stated in issue #2565, checks on the request target during H1 message
parsing are not good enough. Invalid paths, not starting by a slash are in
fact parsed as authorities. The same error is repeated at the sample fetch
level. This last point is annoying because routing rules may be fooled. It
is also an issue when the URI or the Host header are updated.
Because the error is repeated at different places, it must be fixed. We
cannot be lax by arguing it is the server's job to accept or reject invalid
request targets. With this patch, we strengthen the checks performed on the
request target during H1 parsing. Idea is to reject invalid requests at this
step to be sure it is safe to manipulate the path or the authority at other
places.
So now, the asterisk-form is only allowed for OPTIONS and OTHER methods.
This last point was added to not reject the H2 preface. In addition, we take
care to have only one asterisk and nothing more. For the CONNECT method, we
take care to have a valid authority-form. All other form are rejected. The
authority-form is now only supported for CONNECT method. No specific check
is performed on the origin-form (except for the CONNECT method). For the
absolute-form, we take care to have a scheme and a valid authority.
These checks are not perfect but should be good enough to properly identify
each part of the request target for a relative small cost. But, it is a
breaking change. Some requests are now be rejected while they was not on
older versions. However, nowadays, it is most probably not an issue. If it
turns out it's really an issue for legitimate use-cases, an option would be
to supports these kinds of requests when the "accept-invalid-http-request"
option is set, with the consequence of seeing some sample fetches having an
unexpected behavior.
This patch should fix the issue #2665. It MUST NOT be backported. First
because it is a breaking change. And then because by avoiding backporting
it, it remains possible to relax the parsing with the
"accept-invalid-http-request" option.
2024-05-15 10:53:50 -04:00
|
|
|
/* WT: gcc seems to see a path where sl.rq.u.ptr was used
|
|
|
|
|
* uninitialized, but it doesn't know that the function is
|
|
|
|
|
* called with initial states making this impossible.
|
|
|
|
|
*/
|
|
|
|
|
ALREADY_CHECKED(sl.rq.u.ptr);
|
|
|
|
|
switch (parser.format) {
|
|
|
|
|
case URI_PARSER_FORMAT_ASTERISK:
|
|
|
|
|
/* We must take care "PRI * HTTP/2.0" is supported here. check for OTHER methods here is enough */
|
|
|
|
|
if ((sl.rq.meth != HTTP_METH_OTHER && sl.rq.meth != HTTP_METH_OPTIONS) || istlen(sl.rq.u) != 1) {
|
|
|
|
|
ptr = sl.rq.u.ptr; /* Set ptr on the error */
|
|
|
|
|
goto http_msg_invalid;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2024-05-14 05:42:21 -04:00
|
|
|
|
BUG/MAJOR: h1: Be stricter on request target validation during message parsing
As stated in issue #2565, checks on the request target during H1 message
parsing are not good enough. Invalid paths, not starting by a slash are in
fact parsed as authorities. The same error is repeated at the sample fetch
level. This last point is annoying because routing rules may be fooled. It
is also an issue when the URI or the Host header are updated.
Because the error is repeated at different places, it must be fixed. We
cannot be lax by arguing it is the server's job to accept or reject invalid
request targets. With this patch, we strengthen the checks performed on the
request target during H1 parsing. Idea is to reject invalid requests at this
step to be sure it is safe to manipulate the path or the authority at other
places.
So now, the asterisk-form is only allowed for OPTIONS and OTHER methods.
This last point was added to not reject the H2 preface. In addition, we take
care to have only one asterisk and nothing more. For the CONNECT method, we
take care to have a valid authority-form. All other form are rejected. The
authority-form is now only supported for CONNECT method. No specific check
is performed on the origin-form (except for the CONNECT method). For the
absolute-form, we take care to have a scheme and a valid authority.
These checks are not perfect but should be good enough to properly identify
each part of the request target for a relative small cost. But, it is a
breaking change. Some requests are now be rejected while they was not on
older versions. However, nowadays, it is most probably not an issue. If it
turns out it's really an issue for legitimate use-cases, an option would be
to supports these kinds of requests when the "accept-invalid-http-request"
option is set, with the consequence of seeing some sample fetches having an
unexpected behavior.
This patch should fix the issue #2665. It MUST NOT be backported. First
because it is a breaking change. And then because by avoiding backporting
it, it remains possible to relax the parsing with the
"accept-invalid-http-request" option.
2024-05-15 10:53:50 -04:00
|
|
|
case URI_PARSER_FORMAT_ABSPATH:
|
|
|
|
|
if (sl.rq.meth == HTTP_METH_CONNECT) {
|
|
|
|
|
ptr = sl.rq.u.ptr; /* Set ptr on the error */
|
|
|
|
|
goto http_msg_invalid;
|
2022-07-05 08:50:17 -04:00
|
|
|
}
|
BUG/MAJOR: h1: Be stricter on request target validation during message parsing
As stated in issue #2565, checks on the request target during H1 message
parsing are not good enough. Invalid paths, not starting by a slash are in
fact parsed as authorities. The same error is repeated at the sample fetch
level. This last point is annoying because routing rules may be fooled. It
is also an issue when the URI or the Host header are updated.
Because the error is repeated at different places, it must be fixed. We
cannot be lax by arguing it is the server's job to accept or reject invalid
request targets. With this patch, we strengthen the checks performed on the
request target during H1 parsing. Idea is to reject invalid requests at this
step to be sure it is safe to manipulate the path or the authority at other
places.
So now, the asterisk-form is only allowed for OPTIONS and OTHER methods.
This last point was added to not reject the H2 preface. In addition, we take
care to have only one asterisk and nothing more. For the CONNECT method, we
take care to have a valid authority-form. All other form are rejected. The
authority-form is now only supported for CONNECT method. No specific check
is performed on the origin-form (except for the CONNECT method). For the
absolute-form, we take care to have a scheme and a valid authority.
These checks are not perfect but should be good enough to properly identify
each part of the request target for a relative small cost. But, it is a
breaking change. Some requests are now be rejected while they was not on
older versions. However, nowadays, it is most probably not an issue. If it
turns out it's really an issue for legitimate use-cases, an option would be
to supports these kinds of requests when the "accept-invalid-http-request"
option is set, with the consequence of seeing some sample fetches having an
unexpected behavior.
This patch should fix the issue #2665. It MUST NOT be backported. First
because it is a breaking change. And then because by avoiding backporting
it, it remains possible to relax the parsing with the
"accept-invalid-http-request" option.
2024-05-15 10:53:50 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case URI_PARSER_FORMAT_ABSURI_OR_AUTHORITY:
|
|
|
|
|
scheme = http_parse_scheme(&parser);
|
|
|
|
|
if (!isttest(scheme)) { /* scheme not found: MUST be an authority */
|
|
|
|
|
struct ist *host = NULL;
|
2022-07-05 08:50:17 -04:00
|
|
|
|
BUG/MAJOR: h1: Be stricter on request target validation during message parsing
As stated in issue #2565, checks on the request target during H1 message
parsing are not good enough. Invalid paths, not starting by a slash are in
fact parsed as authorities. The same error is repeated at the sample fetch
level. This last point is annoying because routing rules may be fooled. It
is also an issue when the URI or the Host header are updated.
Because the error is repeated at different places, it must be fixed. We
cannot be lax by arguing it is the server's job to accept or reject invalid
request targets. With this patch, we strengthen the checks performed on the
request target during H1 parsing. Idea is to reject invalid requests at this
step to be sure it is safe to manipulate the path or the authority at other
places.
So now, the asterisk-form is only allowed for OPTIONS and OTHER methods.
This last point was added to not reject the H2 preface. In addition, we take
care to have only one asterisk and nothing more. For the CONNECT method, we
take care to have a valid authority-form. All other form are rejected. The
authority-form is now only supported for CONNECT method. No specific check
is performed on the origin-form (except for the CONNECT method). For the
absolute-form, we take care to have a scheme and a valid authority.
These checks are not perfect but should be good enough to properly identify
each part of the request target for a relative small cost. But, it is a
breaking change. Some requests are now be rejected while they was not on
older versions. However, nowadays, it is most probably not an issue. If it
turns out it's really an issue for legitimate use-cases, an option would be
to supports these kinds of requests when the "accept-invalid-http-request"
option is set, with the consequence of seeing some sample fetches having an
unexpected behavior.
This patch should fix the issue #2665. It MUST NOT be backported. First
because it is a breaking change. And then because by avoiding backporting
it, it remains possible to relax the parsing with the
"accept-invalid-http-request" option.
2024-05-15 10:53:50 -04:00
|
|
|
if (sl.rq.meth != HTTP_METH_CONNECT) {
|
|
|
|
|
ptr = sl.rq.u.ptr; /* Set ptr on the error */
|
|
|
|
|
goto http_msg_invalid;
|
|
|
|
|
}
|
|
|
|
|
if (host_idx != -1)
|
|
|
|
|
host = &hdr[host_idx].v;
|
|
|
|
|
authority = http_parse_authority(&parser, 1);
|
|
|
|
|
ret = h1_validate_connect_authority(scheme, authority, host);
|
2022-11-22 04:04:16 -05:00
|
|
|
if (ret < 0) {
|
|
|
|
|
if (h1m->err_pos < -1) {
|
|
|
|
|
state = H1_MSG_LAST_LF;
|
BUG/MAJOR: h1: Be stricter on request target validation during message parsing
As stated in issue #2565, checks on the request target during H1 message
parsing are not good enough. Invalid paths, not starting by a slash are in
fact parsed as authorities. The same error is repeated at the sample fetch
level. This last point is annoying because routing rules may be fooled. It
is also an issue when the URI or the Host header are updated.
Because the error is repeated at different places, it must be fixed. We
cannot be lax by arguing it is the server's job to accept or reject invalid
request targets. With this patch, we strengthen the checks performed on the
request target during H1 parsing. Idea is to reject invalid requests at this
step to be sure it is safe to manipulate the path or the authority at other
places.
So now, the asterisk-form is only allowed for OPTIONS and OTHER methods.
This last point was added to not reject the H2 preface. In addition, we take
care to have only one asterisk and nothing more. For the CONNECT method, we
take care to have a valid authority-form. All other form are rejected. The
authority-form is now only supported for CONNECT method. No specific check
is performed on the origin-form (except for the CONNECT method). For the
absolute-form, we take care to have a scheme and a valid authority.
These checks are not perfect but should be good enough to properly identify
each part of the request target for a relative small cost. But, it is a
breaking change. Some requests are now be rejected while they was not on
older versions. However, nowadays, it is most probably not an issue. If it
turns out it's really an issue for legitimate use-cases, an option would be
to supports these kinds of requests when the "accept-invalid-http-request"
option is set, with the consequence of seeing some sample fetches having an
unexpected behavior.
This patch should fix the issue #2665. It MUST NOT be backported. First
because it is a breaking change. And then because by avoiding backporting
it, it remains possible to relax the parsing with the
"accept-invalid-http-request" option.
2024-05-15 10:53:50 -04:00
|
|
|
/* WT: gcc seems to see a path where sl.rq.u.ptr was used
|
|
|
|
|
* uninitialized, but it doesn't know that the function is
|
|
|
|
|
* called with initial states making this impossible.
|
|
|
|
|
*/
|
|
|
|
|
ALREADY_CHECKED(sl.rq.u.ptr);
|
|
|
|
|
ptr = ((ret == -1) ? sl.rq.u.ptr : host->ptr); /* Set ptr on the error */
|
2022-11-22 04:04:16 -05:00
|
|
|
goto http_msg_invalid;
|
|
|
|
|
}
|
|
|
|
|
if (h1m->err_pos == -1) /* capture the error pointer */
|
BUG/MAJOR: h1: Be stricter on request target validation during message parsing
As stated in issue #2565, checks on the request target during H1 message
parsing are not good enough. Invalid paths, not starting by a slash are in
fact parsed as authorities. The same error is repeated at the sample fetch
level. This last point is annoying because routing rules may be fooled. It
is also an issue when the URI or the Host header are updated.
Because the error is repeated at different places, it must be fixed. We
cannot be lax by arguing it is the server's job to accept or reject invalid
request targets. With this patch, we strengthen the checks performed on the
request target during H1 parsing. Idea is to reject invalid requests at this
step to be sure it is safe to manipulate the path or the authority at other
places.
So now, the asterisk-form is only allowed for OPTIONS and OTHER methods.
This last point was added to not reject the H2 preface. In addition, we take
care to have only one asterisk and nothing more. For the CONNECT method, we
take care to have a valid authority-form. All other form are rejected. The
authority-form is now only supported for CONNECT method. No specific check
is performed on the origin-form (except for the CONNECT method). For the
absolute-form, we take care to have a scheme and a valid authority.
These checks are not perfect but should be good enough to properly identify
each part of the request target for a relative small cost. But, it is a
breaking change. Some requests are now be rejected while they was not on
older versions. However, nowadays, it is most probably not an issue. If it
turns out it's really an issue for legitimate use-cases, an option would be
to supports these kinds of requests when the "accept-invalid-http-request"
option is set, with the consequence of seeing some sample fetches having an
unexpected behavior.
This patch should fix the issue #2665. It MUST NOT be backported. First
because it is a breaking change. And then because by avoiding backporting
it, it remains possible to relax the parsing with the
"accept-invalid-http-request" option.
2024-05-15 10:53:50 -04:00
|
|
|
h1m->err_pos = ((ret == -1) ? sl.rq.u.ptr : host->ptr) - start + skip; /* >= 0 now */
|
2022-07-05 08:50:17 -04:00
|
|
|
}
|
|
|
|
|
}
|
BUG/MAJOR: h1: Be stricter on request target validation during message parsing
As stated in issue #2565, checks on the request target during H1 message
parsing are not good enough. Invalid paths, not starting by a slash are in
fact parsed as authorities. The same error is repeated at the sample fetch
level. This last point is annoying because routing rules may be fooled. It
is also an issue when the URI or the Host header are updated.
Because the error is repeated at different places, it must be fixed. We
cannot be lax by arguing it is the server's job to accept or reject invalid
request targets. With this patch, we strengthen the checks performed on the
request target during H1 parsing. Idea is to reject invalid requests at this
step to be sure it is safe to manipulate the path or the authority at other
places.
So now, the asterisk-form is only allowed for OPTIONS and OTHER methods.
This last point was added to not reject the H2 preface. In addition, we take
care to have only one asterisk and nothing more. For the CONNECT method, we
take care to have a valid authority-form. All other form are rejected. The
authority-form is now only supported for CONNECT method. No specific check
is performed on the origin-form (except for the CONNECT method). For the
absolute-form, we take care to have a scheme and a valid authority.
These checks are not perfect but should be good enough to properly identify
each part of the request target for a relative small cost. But, it is a
breaking change. Some requests are now be rejected while they was not on
older versions. However, nowadays, it is most probably not an issue. If it
turns out it's really an issue for legitimate use-cases, an option would be
to supports these kinds of requests when the "accept-invalid-http-request"
option is set, with the consequence of seeing some sample fetches having an
unexpected behavior.
This patch should fix the issue #2665. It MUST NOT be backported. First
because it is a breaking change. And then because by avoiding backporting
it, it remains possible to relax the parsing with the
"accept-invalid-http-request" option.
2024-05-15 10:53:50 -04:00
|
|
|
else { /* Scheme found: MUST be an absolute-URI */
|
|
|
|
|
struct ist host = IST_NULL;
|
|
|
|
|
|
|
|
|
|
if (sl.rq.meth == HTTP_METH_CONNECT) {
|
|
|
|
|
ptr = sl.rq.u.ptr; /* Set ptr on the error */
|
|
|
|
|
goto http_msg_invalid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (host_idx != -1)
|
|
|
|
|
host = hdr[host_idx].v;
|
|
|
|
|
authority = http_parse_authority(&parser, 1);
|
|
|
|
|
/* For non-CONNECT method, the authority must match the host header value */
|
|
|
|
|
if (isttest(host) && !isteqi(authority, host)) {
|
|
|
|
|
ret = h1_validate_mismatch_authority(scheme, authority, host);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
if (h1m->err_pos < -1) {
|
|
|
|
|
state = H1_MSG_LAST_LF;
|
|
|
|
|
ptr = host.ptr; /* Set ptr on the error */
|
|
|
|
|
goto http_msg_invalid;
|
|
|
|
|
}
|
|
|
|
|
if (h1m->err_pos == -1) /* capture the error pointer */
|
|
|
|
|
h1m->err_pos = v.ptr - start + skip; /* >= 0 now */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
ptr = sl.rq.u.ptr; /* Set ptr on the error */
|
|
|
|
|
goto http_msg_invalid;
|
2022-07-05 08:50:17 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-14 10:34:47 -04:00
|
|
|
state = H1_MSG_DATA;
|
|
|
|
|
if (h1m->flags & H1_MF_XFER_ENC) {
|
|
|
|
|
if (h1m->flags & H1_MF_CLEN) {
|
2021-09-27 03:47:03 -04:00
|
|
|
/* T-E + C-L: force close and remove C-L */
|
2024-07-26 09:14:58 -04:00
|
|
|
if (!h1_do_not_close_on_insecure_t_e)
|
|
|
|
|
h1m->flags |= H1_MF_CONN_CLO;
|
|
|
|
|
|
2018-09-14 10:34:47 -04:00
|
|
|
h1m->flags &= ~H1_MF_CLEN;
|
2023-09-27 09:21:28 -04:00
|
|
|
h1m->curr_len = h1m->body_len = 0;
|
2018-09-14 10:34:47 -04:00
|
|
|
hdr_count = http_del_hdr(hdr, ist("content-length"));
|
|
|
|
|
}
|
2021-09-27 03:47:03 -04:00
|
|
|
else if (!(h1m->flags & H1_MF_VER_11)) {
|
|
|
|
|
/* T-E + HTTP/1.0: force close */
|
|
|
|
|
h1m->flags |= H1_MF_CONN_CLO;
|
|
|
|
|
}
|
2018-09-14 10:34:47 -04:00
|
|
|
|
|
|
|
|
if (h1m->flags & H1_MF_CHNK)
|
|
|
|
|
state = H1_MSG_CHUNK_SIZE;
|
|
|
|
|
else if (!(h1m->flags & H1_MF_RESP)) {
|
|
|
|
|
/* cf RFC7230#3.3.3 : transfer-encoding in
|
|
|
|
|
* request without chunked encoding is invalid.
|
|
|
|
|
*/
|
|
|
|
|
goto http_msg_invalid;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-26 03:07:47 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
/* impossible states */
|
|
|
|
|
goto http_msg_invalid;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 11:25:32 -04:00
|
|
|
/* Now we've left the headers state and are either in H1_MSG_DATA or
|
|
|
|
|
* H1_MSG_CHUNK_SIZE.
|
2017-07-26 03:07:47 -04:00
|
|
|
*/
|
2018-09-11 10:20:30 -04:00
|
|
|
|
2018-09-11 10:04:48 -04:00
|
|
|
if (slp && !skip_update)
|
2018-09-11 09:34:50 -04:00
|
|
|
*slp = sl;
|
|
|
|
|
|
2018-09-11 09:33:32 -04:00
|
|
|
h1m->state = state;
|
|
|
|
|
h1m->next = ptr - start + skip;
|
|
|
|
|
return h1m->next;
|
2017-07-26 03:07:47 -04:00
|
|
|
|
|
|
|
|
http_msg_ood:
|
|
|
|
|
/* out of data at <ptr> during state <state> */
|
2018-09-11 10:04:48 -04:00
|
|
|
if (slp && !skip_update)
|
2018-09-11 09:34:50 -04:00
|
|
|
*slp = sl;
|
|
|
|
|
|
2018-09-11 09:33:32 -04:00
|
|
|
h1m->state = state;
|
|
|
|
|
h1m->next = ptr - start + skip;
|
2017-07-26 03:07:47 -04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
http_msg_invalid:
|
|
|
|
|
/* invalid message, error at <ptr> */
|
2018-09-11 10:04:48 -04:00
|
|
|
if (slp && !skip_update)
|
2018-09-11 09:34:50 -04:00
|
|
|
*slp = sl;
|
|
|
|
|
|
2018-09-11 09:33:32 -04:00
|
|
|
h1m->err_state = h1m->state = state;
|
|
|
|
|
h1m->err_pos = h1m->next = ptr - start + skip;
|
2017-07-26 03:07:47 -04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
http_output_full:
|
|
|
|
|
/* no more room to store the current header, error at <ptr> */
|
2018-09-11 10:04:48 -04:00
|
|
|
if (slp && !skip_update)
|
2018-09-11 09:34:50 -04:00
|
|
|
*slp = sl;
|
|
|
|
|
|
2018-09-11 09:33:32 -04:00
|
|
|
h1m->err_state = h1m->state = state;
|
|
|
|
|
h1m->err_pos = h1m->next = ptr - start + skip;
|
2017-07-26 03:07:47 -04:00
|
|
|
return -2;
|
2018-09-11 10:20:30 -04:00
|
|
|
|
|
|
|
|
restart:
|
2021-12-01 12:01:48 -05:00
|
|
|
h1m->flags &= H1_MF_RESTART_MASK;
|
2019-09-03 10:05:31 -04:00
|
|
|
h1m->curr_len = h1m->body_len = h1m->next = 0;
|
2018-09-11 11:57:05 -04:00
|
|
|
if (h1m->flags & H1_MF_RESP)
|
|
|
|
|
h1m->state = H1_MSG_RPBEFORE;
|
|
|
|
|
else
|
|
|
|
|
h1m->state = H1_MSG_RQBEFORE;
|
2018-09-11 10:20:30 -04:00
|
|
|
goto try_again;
|
2017-07-26 03:07:47 -04:00
|
|
|
}
|
|
|
|
|
|
2020-12-11 11:53:07 -05:00
|
|
|
/* Generate a random key for a WebSocket Handshake in respect with rfc6455
|
|
|
|
|
* The key is 128-bits long encoded as a base64 string in <key_out> parameter
|
|
|
|
|
* (25 bytes long).
|
|
|
|
|
*/
|
|
|
|
|
void h1_generate_random_ws_input_key(char key_out[25])
|
|
|
|
|
{
|
|
|
|
|
/* generate a random websocket key */
|
|
|
|
|
const uint64_t rand1 = ha_random64(), rand2 = ha_random64();
|
|
|
|
|
char key[16];
|
|
|
|
|
|
|
|
|
|
memcpy(key, &rand1, 8);
|
|
|
|
|
memcpy(&key[8], &rand2, 8);
|
|
|
|
|
a2base64(key, 16, key_out, 25);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-11 11:53:03 -05:00
|
|
|
#define H1_WS_KEY_SUFFIX_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Calculate the WebSocket handshake response key from <key_in>. Following the
|
|
|
|
|
* rfc6455, <key_in> must be 24 bytes longs. The result is stored in <key_out>
|
|
|
|
|
* as a 29 bytes long string.
|
|
|
|
|
*/
|
|
|
|
|
void h1_calculate_ws_output_key(const char *key, char *result)
|
|
|
|
|
{
|
|
|
|
|
blk_SHA_CTX sha1_ctx;
|
|
|
|
|
char hash_in[60], hash_out[20];
|
|
|
|
|
|
|
|
|
|
/* concatenate the key with a fixed suffix */
|
|
|
|
|
memcpy(hash_in, key, 24);
|
|
|
|
|
memcpy(&hash_in[24], H1_WS_KEY_SUFFIX_GUID, 36);
|
|
|
|
|
|
|
|
|
|
/* sha1 the result */
|
|
|
|
|
blk_SHA1_Init(&sha1_ctx);
|
|
|
|
|
blk_SHA1_Update(&sha1_ctx, hash_in, 60);
|
|
|
|
|
blk_SHA1_Final((unsigned char *)hash_out, &sha1_ctx);
|
|
|
|
|
|
|
|
|
|
/* encode in base64 the hash */
|
|
|
|
|
a2base64(hash_out, 20, result, 29);
|
|
|
|
|
}
|
2024-07-26 09:14:58 -04:00
|
|
|
|
|
|
|
|
/* config parser for global "h1-do-not-close-on-insecure-transfer-encoding" */
|
|
|
|
|
static int cfg_parse_h1_do_not_close_insecure_t_e(char **args, int section_type, struct proxy *curpx,
|
|
|
|
|
const struct proxy *defpx, const char *file, int line,
|
|
|
|
|
char **err)
|
|
|
|
|
{
|
|
|
|
|
if (too_many_args(0, args, err, NULL))
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
h1_do_not_close_on_insecure_t_e = 1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* config keyword parsers */
|
|
|
|
|
static struct cfg_kw_list cfg_kws = {{ }, {
|
|
|
|
|
{ CFG_GLOBAL, "h1-do-not-close-on-insecure-transfer-encoding", cfg_parse_h1_do_not_close_insecure_t_e },
|
|
|
|
|
{ 0, NULL, NULL },
|
|
|
|
|
}};
|
|
|
|
|
|
|
|
|
|
INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
|