From: Willy Tarreau Date: Thu, 5 Nov 2026 17:44:09 +0000 (+0100) Subject: BUG/MINOR: h1: restrict a bit more the syntax of chunk extensions X-Git-Url: https://git.kaiwu.me/web/%22data:,/doc/static/%7B@url%7D?a=commitdiff_plain;h=896e7ca258adb6259680211b3ff7b27f0fcd0424;p=haproxy.git BUG/MINOR: h1: restrict a bit more the syntax of chunk extensions This is a complement of commit 35d63cc3c7 ("MEDIUM: h1: strictly verify quoting in chunk extensions") in 3.4-dev4, which focused on better dealing with quotes in chunk extensions due to some agents being confused by the quotes there. The exact syntax of chunk extensions is: *( BWS ';' BWS [ BWS '=' BWS ] ) where: name: value: / BWS stands for "bad whitespace", a WS that might be received from legacy senders but that new ones must not send. There were a few aspects above that we didn't check: - no space is permitted before the CRLF - the value may not be empty - name must exclusively be a token - value must exclusively be a token if not a quoted string This is important because bad parsers have been caught in the past swalloing the CRLF at wrong places (inside quotes mainly), and we must make sure that we're not receiving a mis-quoted extension that could have fooled an upstream reverse-proxy that got its framing wrong. This commit improves the state machine to take care ofthe equal sign, check for BWS around and non-empty value, and make sure that is a token. It looks like a big change but actually it's a linear parsing of simple elements and it simplifies the previous parser that was relying on multiple states in a loop and was wrong. This is marked minor because most of the sensitive job was in the commit above, this one only slightly tightens it (and anyway nobody uses chunk extensions). It could make sense to backport this where the commit above was backported, after a period of observation. --- diff --git a/src/h1_htx.c b/src/h1_htx.c index 5055456b7..548fd89aa 100644 --- a/src/h1_htx.c +++ b/src/h1_htx.c @@ -732,6 +732,10 @@ static size_t h1_parse_full_contig_chunks(struct h1m *h1m, struct htx **dsthtx, lmax = htx_get_blksz(htxret.blk) - dpos; while (1) { + int quote = 0; + int equal = 0; + int bws = 0; + /* The chunk size is in the following form, though we are only * interested in the size and CRLF : * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF @@ -772,18 +776,19 @@ static size_t h1_parse_full_contig_chunks(struct h1m *h1m, struct htx **dsthtx, goto parsing_error; } - /* Skip spaces */ - while (HTTP_IS_SPHT(end[ridx])) { - if (!++ridx) - goto end_parsing; - } - /* Up to there, we know that at least one byte is present. Check - * for the end of chunk size. + * for the end of chunk size. We may run into optional extensions + * (fairly unlikely), the syntax of the extension after the size + * must be strictly followed: + * *( BWS ';' BWS [ BWS '=' BWS ] ) */ while (1) { if (likely(end[ridx] == '\r')) { /* Parse CRLF */ + if (unlikely(bws || equal || quote)) { + /* BWS or equal or quote before the end */ + goto parsing_error; + } if (!++ridx) goto end_parsing; if (unlikely(end[ridx] != '\n')) { @@ -797,43 +802,115 @@ static size_t h1_parse_full_contig_chunks(struct h1m *h1m, struct htx **dsthtx, } else if (likely(end[ridx] == ';')) { int backslash = 0; - int quote = 0; + int token = 0; /* chunk extension, ends at next CRLF */ if (!++ridx) goto end_parsing; - /* The loop seeks the first CRLF or non-tab CTL char - * and stops there. If a backslash/quote is active, - * it's an error. If none, we assume it's the CRLF - * and go back to the top of the loop checking for - * CR then LF. This way CTLs, lone LF etc are handled - * in the fallback path. This allows to protect - * remotes against their own possibly non-compliant - * chunk-ext parser which could mistakenly skip a - * quoted CRLF. Chunk-ext are not used anyway, except - * by attacks. + /* skip second BWS (before name) */ + bws = 0; + while (unlikely(HTTP_IS_SPHT(end[ridx]))) { + bws = 1; + if (!++ridx) + goto end_parsing; + } + + /* token */ + token = 0; + while (HTTP_IS_TOKEN(end[ridx])) { + token = 1; + if (!++ridx) + goto end_parsing; + } + + if (!token) // empty name + goto parsing_error; + + /* BWS before '=' */ + bws = 0; + while (unlikely(HTTP_IS_SPHT(end[ridx]))) { + bws = 1; + if (!++ridx) + goto end_parsing; + } + + /* if not '=', must be another ext or CR. Pending + * bws will be handled there. */ + if (end[ridx] != '=') + continue; + + if (!++ridx) + goto end_parsing; + + /* We have a value; it must be a non-empty token + * or quoted string after BWS. + */ + equal = 1; + + /* BWS after '=' */ + bws = 0; + while (unlikely(HTTP_IS_SPHT(end[ridx]))) { + bws = 1; + if (!++ridx) + goto end_parsing; + } + + if (HTTP_IS_TOKEN(end[ridx])) { + token = 1; + bws = equal = 0; + do { + if (!++ridx) + goto end_parsing; + } while (HTTP_IS_TOKEN(end[ridx])); + + /* we have a token, the rest will be validated + * with the next ext or the CR. bws is zero in + * this case. + */ + equal = 0; + continue; + } + + /* not a token ? must be a quoted string then */ + if (end[ridx] != '"') + goto parsing_error; + + if (!++ridx) + goto end_parsing; + + quote = 1; + bws = 0; while (!HTTP_IS_CTL(end[ridx]) || HTTP_IS_SPHT(end[ridx])) { if (backslash) backslash = 0; // escaped char - else if (end[ridx] == '\\' && quote) + else if (end[ridx] == '"') { // end of quoted-pair + quote = 0; + if (!++ridx) + goto end_parsing; + break; + } + else if (quote && end[ridx] == '\\') backslash = 1; - else if (end[ridx] == '\\') // backslash not permitted outside quotes - goto parsing_error; - else if (end[ridx] == '"') // begin/end of quoted-pair - quote = !quote; if (!++ridx) goto end_parsing; } - /* mismatched quotes / backslashes end here */ - if (quote || backslash) - goto parsing_error; - - /* CTLs (CRLF) fall to the common check */ + /* closing quote met, here we must have CR, BWS or ';', + * ready for another run. + */ + equal = 0; continue; } + else if (unlikely(HTTP_IS_SPHT(end[ridx]))) { + /* Skip spaces; they're only allowed before the ';' */ + bws = 1; + do { + if (!++ridx) + goto end_parsing; + } while (HTTP_IS_SPHT(end[ridx])); + } else { /* all other characters are unexpected, especially LF alone */ goto parsing_error;