Skip to content

Commit

Permalink
Merge pull request #78 from h2o/kazuho/chunked-strict-size-parsing
Browse files Browse the repository at this point in the history
Tighten the parsing logic of chunked encoding
  • Loading branch information
kazuho authored Dec 12, 2023
2 parents 066d2b1 + d236094 commit 3bd03c2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
12 changes: 12 additions & 0 deletions picohttpparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,18 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_
ret = -1;
goto Exit;
}
/* the only characters that may appear after the chunk size are BWS, semicolon, or CRLF */
switch (buf[src]) {
case ' ':
case '\011':
case ';':
case '\012':
case '\015':
break;
default:
ret = -1;
goto Exit;
}
break;
}
if (decoder->_hex_count == sizeof(size_t) * 2) {
Expand Down
2 changes: 2 additions & 0 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ static void test_chunked(void)
chunked_test_runners[i](__LINE__, 0, "b\r\nhello world\r\n0\r\n", "hello world", 0);
chunked_test_runners[i](__LINE__, 0, "6\r\nhello \r\n5\r\nworld\r\n0\r\n", "hello world", 0);
chunked_test_runners[i](__LINE__, 0, "6;comment=hi\r\nhello \r\n5\r\nworld\r\n0\r\n", "hello world", 0);
chunked_test_runners[i](__LINE__, 0, "6 ; comment\r\nhello \r\n5\r\nworld\r\n0\r\n", "hello world", 0);
chunked_test_runners[i](__LINE__, 0, "6\r\nhello \r\n5\r\nworld\r\n0\r\na: b\r\nc: d\r\n\r\n", "hello world",
sizeof("a: b\r\nc: d\r\n\r\n") - 1);
chunked_test_runners[i](__LINE__, 0, "b\r\nhello world\r\n0\r\n", "hello world", 0);
Expand All @@ -421,6 +422,7 @@ static void test_chunked(void)
test_chunked_failure(__LINE__, "6\r\nhello \r\nffffffffffffffff\r\nabcdefg", -2);
test_chunked_failure(__LINE__, "6\r\nhello \r\nfffffffffffffffff\r\nabcdefg", -1);
}
test_chunked_failure(__LINE__, "1x\r\na\r\n0\r\n", -1);
}

static void test_chunked_consume_trailer(void)
Expand Down

0 comments on commit 3bd03c2

Please sign in to comment.