| 1 |
|
| #ifndef HM_PARSE_H |
| 2 |
|
| #define HM_PARSE_H |
| 3 |
|
| |
| 4 |
|
| /* Pure-C HTTP/1 request-framing helpers, with no Perl dependency, so they can |
| 5 |
|
| * be unit-tested and fuzzed standalone (see tools/fuzz/). Included by |
| 6 |
|
| * hm_core.h; the SV/HV-building parser (hm_build_env) stays there. |
| 7 |
|
| * |
| 8 |
|
| * These decide how a request body is framed and decode chunked bodies, and are |
| 9 |
|
| * the security-critical, attacker-controlled path - hardened against request |
| 10 |
|
| * smuggling (RFC 7230 3.3.3) and fuzzed for memory safety. */ |
| 11 |
|
| |
| 12 |
|
| #include <stddef.h> |
| 13 |
|
| #include <string.h> |
| 14 |
|
| #include <limits.h> /* LONG_MAX, ULLONG_MAX */ |
| 15 |
|
| |
| 16 |
|
| /* Case-insensitive compare. POSIX puts it in <strings.h>; MSVC has no such |
| 17 |
|
| * header at all and MinGW's is a thin inline over the CRT's _strnicmp, so we |
| 18 |
|
| * name the CRT call directly there. Prefixed rather than #defined over the |
| 19 |
|
| * system name: perl.h and the platform headers are already in this |
| 20 |
|
| * translation unit, and redefining an ambient symbol they may have declared |
| 21 |
|
| * is the mistake Eshu spent three releases undoing. This file stays |
| 22 |
|
| * dependency-free (tools/fuzz/ compiles it alone), so the shim lives here |
| 23 |
|
| * and not in hm_win.h. */ |
| 24 |
|
| /* ASCII-only, and deliberately not the C library's. |
| 25 |
|
| * |
| 26 |
|
| * strncasecmp folds according to the CURRENT LOCALE, and every caller here is |
| 27 |
|
| * comparing an HTTP header name or token - which RFC 9110 defines as ASCII. |
| 28 |
|
| * Under a Turkish locale the library folds 'I' to the dotless form, so |
| 29 |
|
| * "CONTENT-LENGTH" stops matching "Content-Length" and the framing header |
| 30 |
|
| * goes unrecognised. A server's parse must not depend on the operator's |
| 31 |
|
| * LC_CTYPE. |
| 32 |
|
| * |
| 33 |
|
| * It is also faster: on glibc and on Darwin the call lands in strncasecmp_l, |
| 34 |
|
| * which resolves the thread's locale before it compares a byte. This is |
| 35 |
|
| * ~0.3% of the request path, so the reason to prefer it is the first |
| 36 |
|
| * paragraph and the speed is a bonus. */ |
| 37 |
769 |
| static int hm_strncasecmp(const char *a, const char *b, size_t n) { |
| 38 |
|
| size_t i; |
| 39 |
8293 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
7584 |
709 |
8293 |
|
| 40 |
7584 |
| unsigned char ca = (unsigned char)a[i], cb = (unsigned char)b[i]; |
| 41 |
7584 |
▶ | if (ca >= 'A' && ca <= 'Z') ca |= 32; |
Branch: 2 branches
| Branch | True | False | Total |
| if (ca >= 'A' && ca <= 'Z') ca |= 32; |
7092 |
492 |
7584 |
| if (ca >= 'A' && ca <= 'Z') ca |= 32; |
904 |
6188 |
7092 |
|
| 42 |
7584 |
▶ | if (cb >= 'A' && cb <= 'Z') cb |= 32; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cb >= 'A' && cb <= 'Z') cb |= 32; |
7091 |
493 |
7584 |
| if (cb >= 'A' && cb <= 'Z') cb |= 32; |
842 |
6249 |
7091 |
|
| 43 |
7584 |
▶ | if (ca != cb) return (int)ca - (int)cb; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ca != cb) return (int)ca - (int)cb; |
60 |
7524 |
7584 |
|
| 44 |
|
| /* |
| 45 |
|
| # uncoverable branch true |
| 46 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 47 |
7524 |
▶ | if (!ca) break; /* both NUL: equal within n */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ca) break; /* both NUL: equal within n */ |
0 |
7524 |
7524 |
|
| 48 |
|
| } |
| 49 |
709 |
| return 0; |
| 50 |
|
| } |
| 51 |
|
| |
| 52 |
|
| /* One parsed header line: name at head+off (klen bytes), value at head+voff |
| 53 |
|
| * (vlen bytes, leading OWS trimmed; trailing OWS kept, matching what the env |
| 54 |
|
| * builder has always exposed). Only lines with a non-empty name before a |
| 55 |
|
| * colon are recorded - the same lines the env builder consumes. */ |
| 56 |
|
| typedef struct { unsigned off, klen, voff, vlen; } hm_hline; |
| 57 |
|
| |
| 58 |
|
| /* Single pass over the header block: decide how the request body is framed |
| 59 |
|
| * (hardened against request smuggling, RFC 7230 3.3.3) and, when idx is |
| 60 |
|
| * given, record every header line so the env builder never rescans. |
| 61 |
|
| * |
| 62 |
|
| * - Transfer-Encoding: "chunked" is decoded (signalled by *clen = -1); any |
| 63 |
|
| * other coding is 501; a repeated Transfer-Encoding, or Transfer-Encoding |
| 64 |
|
| * together with Content-Length, is the classic TE.CL / CL.TE smuggling |
| 65 |
|
| * vector and is rejected 400. |
| 66 |
|
| * - Content-Length must appear at most once and be a single run of ASCII |
| 67 |
|
| * digits (no sign, no spaces, no trailing junk, no overflow); a duplicate, |
| 68 |
|
| * conflicting, or malformed value is 400. |
| 69 |
|
| * |
| 70 |
|
| * Returns 0 and sets *clen (>= 0 for a fixed length, -1 for chunked); an HTTP |
| 71 |
|
| * error status (400/501) to reject the request; or -2 when idx was too small, |
| 72 |
|
| * with *nlines set to the count needed (rejection takes precedence). */ |
| 73 |
974 |
| static int hm_parse_index(const char *head, size_t headlen, long *clen, |
| 74 |
|
| hm_hline *idx, int idx_cap, int *nlines) { |
| 75 |
974 |
| const char *line_end = (const char *)memchr(head, '\r', headlen); |
| 76 |
974 |
▶ | const char *p = line_end ? line_end + 2 : head + headlen; /* skip req line */ |
Branch: 1 branch
| Branch | True | False | Total |
| const char *p = line_end ? line_end + 2 : head + headlen; /* skip req line */ |
739 |
235 |
974 |
|
| 77 |
974 |
| const char *hend = head + headlen; |
| 78 |
974 |
| int seen_cl = 0, seen_te = 0, chunked = 0, n = 0; |
| 79 |
974 |
| long cl = 0; |
| 80 |
|
| /* Line terminators are CRLF and nothing else (RFC 9112 2.2). A bare LF |
| 81 |
|
| * is not a terminator here, so a front end that honours one would frame |
| 82 |
|
| * a different request from the same bytes; a bare CR likewise. Either |
| 83 |
|
| * is 400. The one memchr pass over the block is the whole cost. */ |
| 84 |
|
| { |
| 85 |
974 |
| const char *q = head; |
| 86 |
2372 |
▶ | while (q < hend && (q = (const char *)memchr(q, '\n', hend - q))) { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| while (q < hend && (q = (const char *)memchr(q, '\n', hend - q))) { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
2372 |
0 |
2372 |
| while (q < hend && (q = (const char *)memchr(q, '\n', hend - q))) { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
1399 |
973 |
2372 |
|
| 87 |
1399 |
▶ | if (q == head || q[-1] != '\r') return 400; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (q == head || q[-1] != '\r') return 400; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
1399 |
0 |
1399 |
| if (q == head || q[-1] != '\r') return 400; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
1 |
1398 |
1399 |
|
| 88 |
1398 |
| q++; |
| 89 |
|
| } |
| 90 |
|
| } |
| 91 |
|
| /* The request line must exist and hold a method and a target. An empty |
| 92 |
|
| * line here is a client that led with CRLF beyond the one hm_process |
| 93 |
|
| * strips, or a line with no SP at all. */ |
| 94 |
|
| { |
| 95 |
973 |
▶ | size_t ll = line_end ? (size_t)(line_end - head) : headlen; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t ll = line_end ? (size_t)(line_end - head) : headlen; |
738 |
235 |
973 |
|
| 96 |
|
| const char *sp1, *sp2, *ver; |
| 97 |
|
| /* |
| 98 |
|
| # uncoverable branch true |
| 99 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 100 |
973 |
▶ | if (ll == 0) return 400; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ll == 0) return 400; |
0 |
973 |
973 |
|
| 101 |
|
| /* METHOD SP request-target SP HTTP/d.d, and nothing else on the |
| 102 |
|
| * line (RFC 9112 3). A line with more tokens, or without the |
| 103 |
|
| * version, is not a request: a PROXY protocol header sent to a |
| 104 |
|
| * listener that did not ask for one reads as six tokens and used |
| 105 |
|
| * to be served as a request with method "PROXY". */ |
| 106 |
973 |
| sp1 = (const char *)memchr(head, ' ', ll); |
| 107 |
|
| /* |
| 108 |
|
| # uncoverable branch true class:ignore_covered_err |
| 109 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 110 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 111 |
973 |
▶ | if (!sp1 || sp1 == head) return 400; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!sp1 || sp1 == head) return 400; |
973 |
0 |
973 |
| if (!sp1 || sp1 == head) return 400; |
0 |
973 |
973 |
|
| 112 |
973 |
| sp2 = (const char *)memchr(sp1 + 1, ' ', ll - (size_t)(sp1 + 1 - head)); |
| 113 |
|
| /* |
| 114 |
|
| # uncoverable branch true class:ignore_covered_err |
| 115 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 116 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 117 |
973 |
▶ | if (!sp2 || sp2 == sp1 + 1) return 400; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!sp2 || sp2 == sp1 + 1) return 400; |
973 |
0 |
973 |
| if (!sp2 || sp2 == sp1 + 1) return 400; |
0 |
973 |
973 |
|
| 118 |
973 |
| ver = sp2 + 1; |
| 119 |
|
| /* |
| 120 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 121 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 122 |
|
| # uncoverable condition left count:4 class:ignore_covered_err |
| 123 |
|
| # uncoverable condition left count:5 class:ignore_covered_err |
| 124 |
|
| # uncoverable condition left count:6 class:ignore_covered_err |
| 125 |
|
| # uncoverable condition left count:7 class:ignore_covered_err */ |
| 126 |
973 |
▶ | if ((size_t)(head + ll - ver) != 8 || memcmp(ver, "HTTP/", 5) != 0 |
Branch: 2 branches
| Branch | True | False | Total |
| if ((size_t)(head + ll - ver) != 8 || memcmp(ver, "HTTP/", 5) != 0 |
972 |
1 |
973 |
| if ((size_t)(head + ll - ver) != 8 || memcmp(ver, "HTTP/", 5) != 0 |
972 |
0 |
972 |
|
| 127 |
972 |
▶ | || ver[5] < '0' || ver[5] > '9' || ver[6] != '.' |
Branch: 3 branches
| Branch | True | False | Total |
| || ver[5] < '0' || ver[5] > '9' || ver[6] != '.' |
972 |
0 |
972 |
| || ver[5] < '0' || ver[5] > '9' || ver[6] != '.' |
972 |
0 |
972 |
| || ver[5] < '0' || ver[5] > '9' || ver[6] != '.' |
972 |
0 |
972 |
|
| 128 |
973 |
▶ | || ver[7] < '0' || ver[7] > '9') return 400; |
Branch: 2 branches
| Branch | True | False | Total |
| || ver[7] < '0' || ver[7] > '9') return 400; |
972 |
0 |
972 |
| || ver[7] < '0' || ver[7] > '9') return 400; |
0 |
972 |
972 |
|
| 129 |
|
| /* |
| 130 |
|
| # uncoverable branch true class:ignore_covered_err |
| 131 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 132 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 133 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 134 |
972 |
▶ | if (line_end && (line_end + 1 >= hend || line_end[1] != '\n')) return 400; |
Branch: 3 branches
| Branch | True | False | Total |
| if (line_end && (line_end + 1 >= hend || line_end[1] != '\n')) return 400; |
737 |
235 |
972 |
| if (line_end && (line_end + 1 >= hend || line_end[1] != '\n')) return 400; |
737 |
0 |
737 |
| if (line_end && (line_end + 1 >= hend || line_end[1] != '\n')) return 400; |
0 |
737 |
737 |
|
| 135 |
|
| } |
| 136 |
2361 |
▶ | while (p < hend) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < hend) { |
1395 |
966 |
2361 |
|
| 137 |
1395 |
| const char *eol = (const char *)memchr(p, '\r', hend - p); |
| 138 |
1395 |
▶ | size_t len = eol ? (size_t)(eol - p) : (size_t)(hend - p); |
Branch: 1 branch
| Branch | True | False | Total |
| size_t len = eol ? (size_t)(eol - p) : (size_t)(hend - p); |
658 |
737 |
1395 |
|
| 139 |
1395 |
| const char *colon = (const char *)memchr(p, ':', len); |
| 140 |
|
| /* |
| 141 |
|
| # uncoverable branch true class:ignore_covered_err |
| 142 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 143 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 144 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 145 |
1395 |
▶ | if (eol && (eol + 1 >= hend || eol[1] != '\n')) return 400; |
Branch: 3 branches
| Branch | True | False | Total |
| if (eol && (eol + 1 >= hend || eol[1] != '\n')) return 400; |
658 |
737 |
1395 |
| if (eol && (eol + 1 >= hend || eol[1] != '\n')) return 400; |
658 |
0 |
658 |
| if (eol && (eol + 1 >= hend || eol[1] != '\n')) return 400; |
0 |
658 |
658 |
|
| 146 |
1395 |
▶ | if (colon && colon > p) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (colon && colon > p) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
1395 |
0 |
1395 |
| if (colon && colon > p) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
1395 |
0 |
1395 |
|
| 147 |
1395 |
| size_t nk = (size_t)(colon - p); |
| 148 |
1395 |
| const char *v = colon + 1, *vend = p + len; |
| 149 |
|
| /* No whitespace between a field name and its colon (RFC 9112 |
| 150 |
|
| * 5.1): "Content-Length : 5" would otherwise be stored under a |
| 151 |
|
| * name with a trailing space and frame the body as empty, |
| 152 |
|
| * leaving the body to be read as the next request. */ |
| 153 |
1395 |
▶ | if (colon[-1] == ' ' || colon[-1] == '\t') return 400; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (colon[-1] == ' ' || colon[-1] == '\t') return 400; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
1394 |
1 |
1395 |
| if (colon[-1] == ' ' || colon[-1] == '\t') return 400; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
0 |
1394 |
1394 |
|
| 154 |
|
| /* |
| 155 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 156 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 157 |
2788 |
▶ | while (v < vend && (*v == ' ' || *v == '\t')) v++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (v < vend && (*v == ' ' || *v == '\t')) v++; |
2788 |
0 |
2788 |
| while (v < vend && (*v == ' ' || *v == '\t')) v++; |
1394 |
1394 |
2788 |
| while (v < vend && (*v == ' ' || *v == '\t')) v++; |
0 |
1394 |
1394 |
|
| 158 |
1394 |
▶ | if (idx && n < idx_cap) { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (idx && n < idx_cap) { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
1394 |
0 |
1394 |
| if (idx && n < idx_cap) { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
1385 |
9 |
1394 |
|
| 159 |
1385 |
| idx[n].off = (unsigned)(p - head); |
| 160 |
1385 |
| idx[n].klen = (unsigned)nk; |
| 161 |
1385 |
| idx[n].voff = (unsigned)(v - head); |
| 162 |
1385 |
| idx[n].vlen = (unsigned)(vend - v); |
| 163 |
|
| } |
| 164 |
1394 |
| n++; |
| 165 |
1394 |
▶ | if (nk == 17 && hm_strncasecmp(p, "Transfer-Encoding", 17) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (nk == 17 && hm_strncasecmp(p, "Transfer-Encoding", 17) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
23 |
1371 |
1394 |
| if (nk == 17 && hm_strncasecmp(p, "Transfer-Encoding", 17) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
23 |
0 |
23 |
|
| 166 |
23 |
| const char *tvend = vend; |
| 167 |
|
| /* |
| 168 |
|
| # uncoverable branch true |
| 169 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 170 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 171 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 172 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 173 |
23 |
▶ | while (tvend > v && (tvend[-1] == ' ' || tvend[-1] == '\t')) tvend--; |
Branch: 3 branches
| Branch | True | False | Total |
| while (tvend > v && (tvend[-1] == ' ' || tvend[-1] == '\t')) tvend--; |
23 |
0 |
23 |
| while (tvend > v && (tvend[-1] == ' ' || tvend[-1] == '\t')) tvend--; |
0 |
23 |
23 |
| while (tvend > v && (tvend[-1] == ' ' || tvend[-1] == '\t')) tvend--; |
0 |
23 |
23 |
|
| 174 |
|
| /* |
| 175 |
|
| # uncoverable branch true |
| 176 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 177 |
23 |
▶ | if (seen_te) return 400; /* repeated TE: ambiguous */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (seen_te) return 400; /* repeated TE: ambiguous */ |
0 |
23 |
23 |
|
| 178 |
23 |
| seen_te = 1; |
| 179 |
23 |
▶ | if ((size_t)(tvend - v) == 7 && hm_strncasecmp(v, "chunked", 7) == 0) /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if ((size_t)(tvend - v) == 7 && hm_strncasecmp(v, "chunked", 7) == 0) /* # uncoverable condition right count:2 class:ignore_covered_err */ |
22 |
1 |
23 |
| if ((size_t)(tvend - v) == 7 && hm_strncasecmp(v, "chunked", 7) == 0) /* # uncoverable condition right count:2 class:ignore_covered_err */ |
22 |
0 |
22 |
|
| 180 |
22 |
| chunked = 1; |
| 181 |
|
| else |
| 182 |
1 |
| return 501; /* unsupported coding */ |
| 183 |
1371 |
▶ | } else if (nk == 14 && hm_strncasecmp(p, "Content-Length", 14) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (nk == 14 && hm_strncasecmp(p, "Content-Length", 14) == 0) { |
79 |
1292 |
1371 |
| } else if (nk == 14 && hm_strncasecmp(p, "Content-Length", 14) == 0) { |
79 |
0 |
79 |
|
| 184 |
79 |
| const char *d, *dvend = vend; |
| 185 |
|
| /* |
| 186 |
|
| # uncoverable branch true |
| 187 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 188 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 189 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 190 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 191 |
79 |
▶ | while (dvend > v && (dvend[-1] == ' ' || dvend[-1] == '\t')) dvend--; |
Branch: 3 branches
| Branch | True | False | Total |
| while (dvend > v && (dvend[-1] == ' ' || dvend[-1] == '\t')) dvend--; |
79 |
0 |
79 |
| while (dvend > v && (dvend[-1] == ' ' || dvend[-1] == '\t')) dvend--; |
0 |
79 |
79 |
| while (dvend > v && (dvend[-1] == ' ' || dvend[-1] == '\t')) dvend--; |
0 |
79 |
79 |
|
| 192 |
79 |
▶ | if (seen_cl || v == dvend) return 400; /* duplicate or empty */ /* # uncoverable condition left count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (seen_cl || v == dvend) return 400; /* duplicate or empty */ /* # uncoverable condition left count:2 class:ignore_covered_err */ |
78 |
1 |
79 |
| if (seen_cl || v == dvend) return 400; /* duplicate or empty */ /* # uncoverable condition left count:2 class:ignore_covered_err */ |
0 |
78 |
78 |
|
| 193 |
78 |
| seen_cl = 1; |
| 194 |
358 |
▶ | for (d = v; d < dvend; d++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (d = v; d < dvend; d++) { |
283 |
75 |
358 |
|
| 195 |
283 |
▶ | if (*d < '0' || *d > '9') return 400; /* non-digit */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (*d < '0' || *d > '9') return 400; /* non-digit */ |
281 |
2 |
283 |
| if (*d < '0' || *d > '9') return 400; /* non-digit */ |
1 |
280 |
281 |
|
| 196 |
|
| /* |
| 197 |
|
| # uncoverable branch true |
| 198 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 199 |
280 |
▶ | if (cl > (LONG_MAX - (*d - '0')) / 10) return 400; /* overflow */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (cl > (LONG_MAX - (*d - '0')) / 10) return 400; /* overflow */ |
0 |
280 |
280 |
|
| 200 |
280 |
| cl = cl * 10 + (*d - '0'); |
| 201 |
|
| } |
| 202 |
|
| } |
| 203 |
|
| } |
| 204 |
1389 |
▶ | p = eol ? eol + 2 : hend; |
Branch: 1 branch
| Branch | True | False | Total |
| p = eol ? eol + 2 : hend; |
658 |
731 |
1389 |
|
| 205 |
|
| } |
| 206 |
966 |
▶ | if (seen_te && seen_cl) return 400; /* TE + CL together: smuggling */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (seen_te && seen_cl) return 400; /* TE + CL together: smuggling */ |
22 |
944 |
966 |
| if (seen_te && seen_cl) return 400; /* TE + CL together: smuggling */ |
1 |
21 |
22 |
|
| 207 |
|
| /* |
| 208 |
|
| # uncoverable branch false class:ignore_covered_err |
| 209 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 210 |
965 |
▶ | if (nlines) *nlines = n; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nlines) *nlines = n; |
965 |
0 |
965 |
|
| 211 |
965 |
▶ | if (idx && n > idx_cap) return -2; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (idx && n > idx_cap) return -2; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
965 |
0 |
965 |
| if (idx && n > idx_cap) return -2; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
1 |
964 |
965 |
|
| 212 |
964 |
▶ | *clen = chunked ? -1 : cl; |
Branch: 1 branch
| Branch | True | False | Total |
| *clen = chunked ? -1 : cl; |
943 |
21 |
964 |
|
| 213 |
964 |
| return 0; |
| 214 |
|
| } |
| 215 |
|
| |
| 216 |
|
| /* Framing decision alone (no line index) - the fuzzer's entry point. */ |
| 217 |
|
| /* |
| 218 |
|
| # uncoverable statement |
| 219 |
|
| # uncoverable subroutine */ |
| 220 |
0 |
| static int hm_frame_length(const char *head, size_t headlen, long *clen) { |
| 221 |
0 |
| return hm_parse_index(head, headlen, clen, NULL, 0, NULL); /* # uncoverable statement */ |
| 222 |
|
| } |
| 223 |
|
| |
| 224 |
|
| /* Validate a chunked request body in buf[0..avail) WITHOUT mutating it (so an |
| 225 |
|
| * incomplete body can be re-checked as more bytes arrive). On a complete body |
| 226 |
|
| * set *encoded (input octets through the terminating CRLF) and *decoded (total |
| 227 |
|
| * data octets) and return 1; return 0 if more input is needed, -1 if |
| 228 |
|
| * malformed, and HM_CHUNKED_TOOBIG if the decoded size would exceed max. |
| 229 |
|
| * |
| 230 |
|
| * "Too large" is a separate answer from "malformed" because they are |
| 231 |
|
| * different answers to the CLIENT: 400 says your framing is broken and |
| 232 |
|
| * retrying identically is pointless, 413 says the framing was fine and you |
| 233 |
|
| * should send less. Folding them together (as this did before 0.25) told |
| 234 |
|
| * every oversize chunked upload to go and fix its syntax. */ |
| 235 |
|
| #define HM_CHUNKED_TOOBIG (-2) |
| 236 |
|
| /* A chunk-size line is at most this many hex digits. Sixteen cover a 64-bit |
| 237 |
|
| * size; anything longer is leading zeros, which a client can send by the |
| 238 |
|
| * megabyte to keep the scanner busy on one chunk. */ |
| 239 |
|
| #define HM_CHUNK_DIGITS_MAX 16 |
| 240 |
|
| |
| 241 |
|
| /* The scan, resumable: *rpos / *rdec name the last chunk boundary already |
| 242 |
|
| * validated (octets consumed, data octets so far) and are advanced to the |
| 243 |
|
| * newest one before returning 0, so a body arriving in many segments costs |
| 244 |
|
| * each chunk one look rather than one per segment. Pass zeros to start. */ |
| 245 |
21 |
| static int hm_chunked_scan_from(const char *buf, size_t avail, |
| 246 |
|
| size_t *encoded, size_t *decoded, size_t max, |
| 247 |
|
| size_t *rpos, size_t *rdec) { |
| 248 |
21 |
| size_t pos = *rpos, dec = *rdec; |
| 249 |
|
| /* |
| 250 |
|
| # uncoverable branch true class:ignore_covered_err |
| 251 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 252 |
21 |
▶ | if (pos > avail) { pos = 0; dec = 0; } /* stale: restart */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (pos > avail) { pos = 0; dec = 0; } /* stale: restart */ |
0 |
21 |
21 |
|
| 253 |
30 |
| for (;;) { |
| 254 |
51 |
| size_t i = pos; |
| 255 |
51 |
| unsigned long long size = 0; |
| 256 |
51 |
| int ndig = 0; |
| 257 |
51 |
| *rpos = pos; *rdec = dec; |
| 258 |
199 |
▶ | while (i < avail && buf[i] != '\r' && buf[i] != ';') { |
Branch: 3 branches
| Branch | True | False | Total |
| while (i < avail && buf[i] != '\r' && buf[i] != ';') { |
196 |
3 |
199 |
| while (i < avail && buf[i] != '\r' && buf[i] != ';') { |
153 |
43 |
196 |
| while (i < avail && buf[i] != '\r' && buf[i] != ';') { |
150 |
3 |
153 |
|
| 259 |
150 |
| int ch = (unsigned char)buf[i], v; |
| 260 |
150 |
▶ | if (ch >= '0' && ch <= '9') v = ch - '0'; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (ch >= '0' && ch <= '9') v = ch - '0'; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
150 |
0 |
150 |
| if (ch >= '0' && ch <= '9') v = ch - '0'; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
146 |
4 |
150 |
|
| 261 |
4 |
▶ | else if (ch >= 'a' && ch <= 'f') v = ch - 'a' + 10; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| else if (ch >= 'a' && ch <= 'f') v = ch - 'a' + 10; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
1 |
3 |
4 |
| else if (ch >= 'a' && ch <= 'f') v = ch - 'a' + 10; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
1 |
0 |
1 |
|
| 262 |
3 |
▶ | else if (ch >= 'A' && ch <= 'F') v = ch - 'A' + 10; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| else if (ch >= 'A' && ch <= 'F') v = ch - 'A' + 10; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
3 |
0 |
3 |
| else if (ch >= 'A' && ch <= 'F') v = ch - 'A' + 10; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
2 |
1 |
3 |
|
| 263 |
1 |
| else return -1; /* bad hex digit */ |
| 264 |
|
| /* |
| 265 |
|
| # uncoverable branch true |
| 266 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 267 |
149 |
▶ | if (size > (ULLONG_MAX >> 4)) return -1; /* size overflow */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (size > (ULLONG_MAX >> 4)) return -1; /* size overflow */ |
0 |
149 |
149 |
|
| 268 |
149 |
▶ | if (++ndig > HM_CHUNK_DIGITS_MAX) return -1; /* digit flood */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (++ndig > HM_CHUNK_DIGITS_MAX) return -1; /* digit flood */ |
1 |
148 |
149 |
|
| 269 |
148 |
| size = (size << 4) | (unsigned)v; |
| 270 |
148 |
| i++; |
| 271 |
|
| } |
| 272 |
|
| /* No digits. Malformed only if the bytes to say so are HERE: a |
| 273 |
|
| * chunk-size line that has not arrived yet is indistinguishable from |
| 274 |
|
| * a missing one by length alone, and calling it malformed refuses |
| 275 |
|
| * every chunked request whose header block lands in its own packet - |
| 276 |
|
| * which is every chunked request that waits for a 100-continue, and |
| 277 |
|
| * any other whose body simply follows a segment boundary. */ |
| 278 |
49 |
▶ | if (ndig == 0) return i >= avail ? 0 : -1; /* need more / no size */ /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (ndig == 0) return i >= avail ? 0 : -1; /* need more / no size */ /* # uncoverable condition right count:2 class:ignore_covered_err */ |
3 |
46 |
49 |
| if (ndig == 0) return i >= avail ? 0 : -1; /* need more / no size */ /* # uncoverable condition right count:2 class:ignore_covered_err */ |
3 |
0 |
3 |
|
| 279 |
|
| /* A bare LF or CR ends the scan and fails the CRLF test below: the |
| 280 |
|
| * header lines refuse them, and a chunk extension or a trailer that |
| 281 |
|
| * accepted one let an LF-tolerant front end see the body end where |
| 282 |
|
| * this parser did not (request smuggling). */ |
| 283 |
58 |
▶ | while (i < avail && buf[i] != '\r' && buf[i] != '\n') i++; /* skip chunk-ext */ /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 3 branches
| Branch | True | False | Total |
| while (i < avail && buf[i] != '\r' && buf[i] != '\n') i++; /* skip chunk-ext */ /* # uncoverable condition right count:1 class:ignore_covered_err */ |
58 |
0 |
58 |
| while (i < avail && buf[i] != '\r' && buf[i] != '\n') i++; /* skip chunk-ext */ /* # uncoverable condition right count:1 class:ignore_covered_err */ |
13 |
45 |
58 |
| while (i < avail && buf[i] != '\r' && buf[i] != '\n') i++; /* skip chunk-ext */ /* # uncoverable condition right count:1 class:ignore_covered_err */ |
12 |
1 |
13 |
|
| 284 |
|
| /* |
| 285 |
|
| # uncoverable branch true |
| 286 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 287 |
46 |
▶ | if (i + 1 >= avail) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i + 1 >= avail) return 0; |
0 |
46 |
46 |
|
| 288 |
46 |
▶ | if (buf[i] != '\r' || buf[i + 1] != '\n') return -1; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (buf[i] != '\r' || buf[i + 1] != '\n') return -1; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
45 |
1 |
46 |
| if (buf[i] != '\r' || buf[i + 1] != '\n') return -1; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
0 |
45 |
45 |
|
| 289 |
45 |
| i += 2; |
| 290 |
45 |
▶ | if (size == 0) { /* last-chunk + trailers */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (size == 0) { /* last-chunk + trailers */ |
11 |
34 |
45 |
|
| 291 |
1 |
| for (;;) { |
| 292 |
12 |
| size_t j = i; |
| 293 |
36 |
▶ | while (j < avail && buf[j] != '\r' && buf[j] != '\n') j++; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 3 branches
| Branch | True | False | Total |
| while (j < avail && buf[j] != '\r' && buf[j] != '\n') j++; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
36 |
0 |
36 |
| while (j < avail && buf[j] != '\r' && buf[j] != '\n') j++; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
25 |
11 |
36 |
| while (j < avail && buf[j] != '\r' && buf[j] != '\n') j++; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
24 |
1 |
25 |
|
| 294 |
|
| /* |
| 295 |
|
| # uncoverable branch true |
| 296 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 297 |
12 |
▶ | if (j + 1 >= avail) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (j + 1 >= avail) return 0; |
0 |
12 |
12 |
|
| 298 |
12 |
▶ | if (buf[j] != '\r' || buf[j + 1] != '\n') return -1; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (buf[j] != '\r' || buf[j + 1] != '\n') return -1; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
11 |
1 |
12 |
| if (buf[j] != '\r' || buf[j + 1] != '\n') return -1; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
0 |
11 |
11 |
|
| 299 |
11 |
▶ | if (j == i) { *encoded = j + 2; *decoded = dec; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (j == i) { *encoded = j + 2; *decoded = dec; return 1; } |
10 |
1 |
11 |
|
| 300 |
1 |
| i = j + 2; /* skip a trailer line */ |
| 301 |
|
| } |
| 302 |
|
| } |
| 303 |
34 |
▶ | if (size > max || dec + size > max) return HM_CHUNKED_TOOBIG; |
Branch: 2 branches
| Branch | True | False | Total |
| if (size > max || dec + size > max) return HM_CHUNKED_TOOBIG; |
33 |
1 |
34 |
| if (size > max || dec + size > max) return HM_CHUNKED_TOOBIG; |
1 |
32 |
33 |
|
| 304 |
32 |
▶ | if (i + size + 2 > avail) return 0; /* await chunk + CRLF */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (i + size + 2 > avail) return 0; /* await chunk + CRLF */ |
2 |
30 |
32 |
|
| 305 |
|
| /* |
| 306 |
|
| # uncoverable branch true class:ignore_covered_err |
| 307 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 308 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 309 |
30 |
▶ | if (buf[i + size] != '\r' || buf[i + size + 1] != '\n') return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (buf[i + size] != '\r' || buf[i + size + 1] != '\n') return -1; |
30 |
0 |
30 |
| if (buf[i + size] != '\r' || buf[i + size + 1] != '\n') return -1; |
0 |
30 |
30 |
|
| 310 |
30 |
| dec += (size_t)size; |
| 311 |
30 |
| pos = i + (size_t)size + 2; |
| 312 |
|
| } |
| 313 |
|
| } |
| 314 |
|
| |
| 315 |
|
| /* |
| 316 |
|
| # uncoverable statement |
| 317 |
|
| # uncoverable subroutine */ |
| 318 |
0 |
| static int hm_chunked_scan(const char *buf, size_t avail, |
| 319 |
|
| size_t *encoded, size_t *decoded, size_t max) { |
| 320 |
0 |
| size_t pos = 0, dec = 0; /* # uncoverable statement */ |
| 321 |
0 |
| return hm_chunked_scan_from(buf, avail, encoded, decoded, max, &pos, &dec); /* # uncoverable statement */ |
| 322 |
|
| } |
| 323 |
|
| |
| 324 |
|
| /* Compact a chunked body already validated by hm_chunked_scan to its decoded |
| 325 |
|
| * octets at the front of buf; returns the decoded length. Structure is trusted |
| 326 |
|
| * (scan verified every bound over the same, unmodified bytes). */ |
| 327 |
10 |
| static size_t hm_chunked_compact(char *buf) { |
| 328 |
10 |
| size_t pos = 0, w = 0; |
| 329 |
29 |
| for (;;) { |
| 330 |
39 |
| size_t i = pos; |
| 331 |
39 |
| size_t size = 0; |
| 332 |
147 |
▶ | while (buf[i] != '\r' && buf[i] != ';') { |
Branch: 2 branches
| Branch | True | False | Total |
| while (buf[i] != '\r' && buf[i] != ';') { |
110 |
37 |
147 |
| while (buf[i] != '\r' && buf[i] != ';') { |
108 |
2 |
110 |
|
| 333 |
108 |
| int ch = (unsigned char)buf[i]; |
| 334 |
108 |
▶ | size = (size << 4) | (unsigned)(ch <= '9' ? ch - '0' : (ch | 32) - 'a' + 10); |
Branch: 1 branch
| Branch | True | False | Total |
| size = (size << 4) | (unsigned)(ch <= '9' ? ch - '0' : (ch | 32) - 'a' + 10); |
105 |
3 |
108 |
|
| 335 |
108 |
| i++; |
| 336 |
|
| } |
| 337 |
47 |
▶ | while (buf[i] != '\r') i++; |
Branch: 1 branch
| Branch | True | False | Total |
| while (buf[i] != '\r') i++; |
8 |
39 |
47 |
|
| 338 |
39 |
| i += 2; |
| 339 |
39 |
▶ | if (size == 0) return w; |
Branch: 1 branch
| Branch | True | False | Total |
| if (size == 0) return w; |
10 |
29 |
39 |
|
| 340 |
29 |
| memmove(buf + w, buf + i, size); |
| 341 |
29 |
| w += size; |
| 342 |
29 |
| pos = i + size + 2; |
| 343 |
|
| } |
| 344 |
|
| } |
| 345 |
|
| |
| 346 |
|
| #endif |