Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

forbid use of bare LF as chunk header terminator #82

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions picohttpparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,10 @@ int phr_parse_headers(const char *buf_start, size_t len, struct phr_header *head
enum {
CHUNKED_IN_CHUNK_SIZE,
CHUNKED_IN_CHUNK_EXT,
CHUNKED_IN_CHUNK_HEADER_EXPECT_LF,
CHUNKED_IN_CHUNK_DATA,
CHUNKED_IN_CHUNK_CRLF,
CHUNKED_IN_CHUNK_DATA_EXPECT_CR,
CHUNKED_IN_CHUNK_DATA_EXPECT_LF,
CHUNKED_IN_TRAILERS_LINE_HEAD,
CHUNKED_IN_TRAILERS_LINE_MIDDLE
};
Expand Down Expand Up @@ -588,8 +590,22 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_
for (;; ++src) {
if (src == bufsz)
goto Exit;
if (buf[src] == '\012')
if (buf[src] == '\015') {
break;
} else if (buf[src] == '\012') {
ret = -1;
goto Exit;
}
}
++src;
decoder->_state = CHUNKED_IN_CHUNK_HEADER_EXPECT_LF;
/* fallthru */
case CHUNKED_IN_CHUNK_HEADER_EXPECT_LF:
if (src == bufsz)
goto Exit;
if (buf[src] != '\012') {
ret = -1;
goto Exit;
}
++src;
if (decoder->bytes_left_in_chunk == 0) {
Expand Down Expand Up @@ -617,16 +633,22 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_
src += decoder->bytes_left_in_chunk;
dst += decoder->bytes_left_in_chunk;
decoder->bytes_left_in_chunk = 0;
decoder->_state = CHUNKED_IN_CHUNK_CRLF;
decoder->_state = CHUNKED_IN_CHUNK_DATA_EXPECT_CR;
}
/* fallthru */
case CHUNKED_IN_CHUNK_CRLF:
for (;; ++src) {
if (src == bufsz)
goto Exit;
if (buf[src] != '\015')
break;
case CHUNKED_IN_CHUNK_DATA_EXPECT_CR:
if (src == bufsz)
goto Exit;
if (buf[src] != '\015') {
ret = -1;
goto Exit;
}
++src;
decoder->_state = CHUNKED_IN_CHUNK_DATA_EXPECT_LF;
/* fallthru */
case CHUNKED_IN_CHUNK_DATA_EXPECT_LF:
if (src == bufsz)
goto Exit;
if (buf[src] != '\012') {
ret = -1;
goto Exit;
Expand Down
12 changes: 11 additions & 1 deletion test.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,14 @@ static void test_chunked(void)
test_chunked_failure(__LINE__, "6\r\nhello \r\nfffffffffffffffff\r\nabcdefg", -1);
}
test_chunked_failure(__LINE__, "1x\r\na\r\n0\r\n", -1);

/* bare lf cannot be used in chunk header */
test_chunked_failure(__LINE__, "6\nhello \r\n5\r\nworld\r\n0\r\n", -1);
test_chunked_failure(__LINE__, "6\r\nhello \n5\r\nworld\r\n0\r\n", -1);
test_chunked_failure(__LINE__, "6\r\nhello \r\n5\r\nworld\n0\r\n", -1);
test_chunked_failure(__LINE__, "6\r\nhello \r\n5\r\nworld\n0\r\n", -1);
test_chunked_failure(__LINE__, "6\r\nhello \r\n5\r\nworld\r\n0\n", -1);
test_chunked_failure(__LINE__, "6\rX\nhello \n5\r\nworld\r\n0\r\n", -1);
}

static void test_chunked_consume_trailer(void)
Expand All @@ -434,8 +442,10 @@ static void test_chunked_consume_trailer(void)
chunked_test_runners[i](__LINE__, 1, "6\r\nhello \r\n5\r\nworld\r\n0\r\n", "hello world", -2);
chunked_test_runners[i](__LINE__, 1, "6;comment=hi\r\nhello \r\n5\r\nworld\r\n0\r\n", "hello world", -2);
chunked_test_runners[i](__LINE__, 1, "b\r\nhello world\r\n0\r\n\r\n", "hello world", 0);
chunked_test_runners[i](__LINE__, 1, "b\nhello world\n0\n\n", "hello world", 0);
chunked_test_runners[i](__LINE__, 1, "6\r\nhello \r\n5\r\nworld\r\n0\r\na: b\r\nc: d\r\n\r\n", "hello world", 0);
/* bare lf is allowed in trailers, for consistency to when they are parsed using phr_parse_headers */
chunked_test_runners[i](__LINE__, 1, "b\r\nhello world\r\n0\r\n\n", "hello world", 0);
chunked_test_runners[i](__LINE__, 1, "6\r\nhello \r\n5\r\nworld\r\n0\r\na: b\nc: d\n\n", "hello world", 0);
}
}

Expand Down
Loading