Skip to content

Commit

Permalink
detect and reject chunk encoding with too much overhead
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuho committed Jan 24, 2024
1 parent 3bd03c2 commit 3665241
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
8 changes: 8 additions & 0 deletions picohttpparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,8 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_
size_t dst = 0, src = 0, bufsz = *_bufsz;
ssize_t ret = -2; /* incomplete */

decoder->_total_read += bufsz;

while (1) {
switch (decoder->_state) {
case CHUNKED_IN_CHUNK_SIZE:
Expand Down Expand Up @@ -664,6 +666,12 @@ ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_
if (dst != src)
memmove(buf + dst, buf + src, bufsz - src);
*_bufsz = dst;
/* if incomplete but the overhead of the chunked encoding is >=100KB and >80%, signal an error */
if (ret == -2) {
decoder->_total_overhead += bufsz - dst;
if (decoder->_total_overhead >= 100 * 1024 && decoder->_total_read - decoder->_total_overhead < decoder->_total_read / 4)
ret = -1;
}
return ret;
}

Expand Down
3 changes: 3 additions & 0 deletions picohttpparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#ifndef picohttpparser_h
#define picohttpparser_h

#include <stdint.h>
#include <sys/types.h>

#ifdef _MSC_VER
Expand Down Expand Up @@ -64,6 +65,8 @@ struct phr_chunked_decoder {
char consume_trailer; /* if trailing headers should be consumed */
char _hex_count;
char _state;
uint64_t _total_read;
uint64_t _total_overhead;
};

/* the function rewrites the buffer given as (buf, bufsz) removing the chunked-
Expand Down

0 comments on commit 3665241

Please sign in to comment.