]> git.kaiwu.me - haproxy.git/commitdiff
REORG: h1: move chunk extensions parsing to a dedicated inline function
authorWilly Tarreau <w@1wt.eu>
Mon, 23 Nov 2026 22:23:17 +0000 (22:23 +0000)
committerWilly Tarreau <w@1wt.eu>
Tue, 24 Nov 2026 09:50:37 +0000 (10:50 +0100)
The chunk extensions parser of the contiguous chunks parser is moved as-is
to h1_parse_chunk_ext() in h1.h so that it can be shared with the generic
chunk-size parser instead of being duplicated and maintained twice. It
works on contiguous memory with the reverse index convention of the
contiguous parser (end[ridx], ridx < 0 reaching zero at the end of the
input) and returns the new index of the final CR, 0 if more data are
needed, or the opposite of the faulty char's index. It is force-inlined
since streams of tiny chunks each carrying an extension (e.g.
"1;1\r\n1\r\n") must not cost a function call per chunk.

Only the exits were adapted: on the CR, the function returns its index
after checking the pending BWS/equal/quote states, and the CRLF is now
checked by the caller. Running out of data returns 0, and errors return
the opposite of the faulty char's index.

include/haproxy/h1.h
src/h1_htx.c

index 7ae60a00337118c0104912bab00d9e87f9059e60..d9f4112c391ac59d5413112bbbd21abccc6888ae 100644 (file)
@@ -249,6 +249,164 @@ static inline int h1_skip_chunk_crlf(const struct buffer *buf, int start, int st
        return bytes;
 }
 
+/* Parses the chunk extensions of a chunk-size line, which must exactly follow
+ * this syntax (RFC9112#7.1.1):
+ *
+ *     *( BWS ';' BWS name [ BWS '=' BWS value ] )
+ *
+ *   with:
+ *     name:  token
+ *     value: token / quoted-string
+ *
+ * BWS are only permitted before a ';' or around the '=', so no space may
+ * appear before the CR. It works on contiguous memory, using the same reverse
+ * index convention as the contiguous chunks parser: the current character is
+ * end[ridx], with ridx < 0 and reaching zero at the end of the input. It
+ * must be called on the first character after the chunk size when it is not
+ * a CR. It returns:
+ *   < 0 : extensions are valid, this is the new reverse index of the final CR
+ *   = 0 : more data are needed
+ *   > 0 : invalid syntax, this is the opposite of the faulty char's index
+ *
+ * It is force-inlined because some attacks consist in sending tons of tiny
+ * chunks carrying an extension each (e.g. "1;1\r\n1\r\n"), so this must cost
+ * no more than the size parsing itself.
+ */
+static forceinline ssize_t h1_parse_chunk_ext(const char *end, ssize_t ridx)
+{
+       int quote = 0;
+       int equal = 0;
+       int bws = 0;
+
+       while (1) {
+               if (likely(end[ridx] == '\r')) {
+                       /* CR, the CRLF is checked by the caller */
+                       if (unlikely(bws || equal || quote)) {
+                               /* BWS or equal or quote before the end */
+                               goto out;
+                       }
+                       return ridx;
+               }
+               else if (likely(end[ridx] == ';')) {
+                       int backslash = 0;
+                       int token = 0;
+
+                       /* chunk extension, ends at next CRLF */
+                       if (!++ridx)
+                               return 0;
+
+                       /* skip second BWS (before name) */
+                       bws = 0;
+                       while (unlikely(HTTP_IS_SPHT(end[ridx]))) {
+                               bws = 1;
+                               if (!++ridx)
+                                       return 0;
+                       }
+
+                       /* token */
+                       token = 0;
+                       while (HTTP_IS_TOKEN(end[ridx])) {
+                               token = 1;
+                               if (!++ridx)
+                                       return 0;
+                       }
+
+                       if (!token) // empty name
+                               goto out;
+
+                       /* BWS before '=' */
+                       bws = 0;
+                       while (unlikely(HTTP_IS_SPHT(end[ridx]))) {
+                               bws = 1;
+                               if (!++ridx)
+                                       return 0;
+                       }
+
+                       /* if not '=', must be another ext or CR. Pending
+                        * bws will be handled there.
+                        */
+                       if (end[ridx] != '=')
+                               continue;
+
+                       if (!++ridx)
+                               return 0;
+
+                       /* 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)
+                                       return 0;
+                       }
+
+                       if (HTTP_IS_TOKEN(end[ridx])) {
+                               token = 1;
+                               bws = equal = 0;
+                               do {
+                                       if (!++ridx)
+                                               return 0;
+                               } 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 out;
+
+                       if (!++ridx)
+                               return 0;
+
+                       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] == '"') { // end of quoted-pair
+                                       quote = 0;
+                                       if (!++ridx)
+                                               return 0;
+                                       break;
+                               }
+                               else if (quote && end[ridx] == '\\')
+                                       backslash = 1;
+                               if (!++ridx)
+                                       return 0;
+                       }
+
+                       /* 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)
+                                       return 0;
+                       } while (HTTP_IS_SPHT(end[ridx]));
+               }
+               else {
+                       /* all other characters are unexpected, especially LF alone */
+                       goto out;
+               }
+       }
+ out:
+       return -ridx;
+}
+
 /* Parse the chunk size start at buf + start and stops before buf + stop. The
  * positions are relative to the buffer's head.
  * It returns the chunk size in <res> and the amount of bytes read this way :
index 548fd89aa6f17d520cdc03645eea9a65c3f3abf0..f3364c18cece9d0f4915293942cdc08acf9002d9 100644 (file)
@@ -693,6 +693,7 @@ static size_t h1_parse_full_contig_chunks(struct h1m *h1m, struct htx **dsthtx,
        size_t lmax, total = 0;
        uint64_t chksz;
        struct htx_ret htxret;
+       ssize_t ext;
 
        lmax = *max;
        if (lmax <= sizeof(struct htx_blk))
@@ -732,10 +733,6 @@ 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
@@ -777,145 +774,20 @@ static size_t h1_parse_full_contig_chunks(struct h1m *h1m, struct htx **dsthtx,
                }
 
                /* Up to there, we know that at least one byte is present. Check
-                * 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 <name> [ BWS '=' BWS <value> ] )
+                * for the end of chunk size. Anything but a CR must be chunk
+                * extensions, whose syntax must be strictly followed.
                 */
-               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')) {
-                                       /* CR must be followed by LF */
-                                       goto parsing_error;
-                               }
-
-                               /* done */
-                               ++ridx;
-                               break;
-                       }
-                       else if (likely(end[ridx] == ';')) {
-                               int backslash = 0;
-                               int token = 0;
-
-                               /* chunk extension, ends at next CRLF */
-                               if (!++ridx)
-                                       goto end_parsing;
-
-                               /* 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] == '"') { // end of quoted-pair
-                                               quote = 0;
-                                               if (!++ridx)
-                                                       goto end_parsing;
-                                               break;
-                                       }
-                                       else if (quote && end[ridx] == '\\')
-                                               backslash = 1;
-                                       if (!++ridx)
-                                               goto end_parsing;
-                               }
-
-                               /* 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;
-                       }
+               if (unlikely(end[ridx] != '\r'))
+                       goto chunk_ext;
+       chunk_ext_done:
+               /* Parse CRLF */
+               if (!++ridx)
+                       goto end_parsing;
+               if (unlikely(end[ridx] != '\n')) {
+                       /* CR must be followed by LF */
+                       goto parsing_error;
                }
+               ++ridx;
 
                /* Exit if it is the last chunk */
                if (unlikely(!chksz)) {
@@ -976,8 +848,20 @@ static size_t h1_parse_full_contig_chunks(struct h1m *h1m, struct htx **dsthtx,
        h1m->err_state = h1m->state;
        h1m->err_pos = ofs + end + ridx - start;
        return 0;
+
+  chunk_ext:
+       ext = h1_parse_chunk_ext(end, ridx);
+       if (likely(ext < 0)) {
+               ridx = ext;
+               goto chunk_ext_done;
+       }
+       if (!ext)
+               goto end_parsing;
+       ridx = -ext;
+       goto parsing_error;
 }
 
+
 /* Parse HTTP chunks. This function relies on an optimized function to parse
  * contiguous chunks if possible. Otherwise, when a chunk is incomplete or when
  * the underlying buffer is wrapping, a generic function is used.