diff --git a/libi2pd/HTTP.cpp b/libi2pd/HTTP.cpp index 03321cf5..f5c0b213 100644 --- a/libi2pd/HTTP.cpp +++ b/libi2pd/HTTP.cpp @@ -616,6 +616,11 @@ namespace http return false; /* too large chunk */ char * buf = new char[len]; in.read (buf, len); + if (in.gcount () != len) /* chunk is shorter than announced */ + { + delete[] buf; + return false; + } out.write (buf, len); delete[] buf; std::getline (in, hexLen); // read \r\n after chunk diff --git a/tests/test-http-merge_chunked.cpp b/tests/test-http-merge_chunked.cpp index 31b6a298..bdba5c3b 100644 --- a/tests/test-http-merge_chunked.cpp +++ b/tests/test-http-merge_chunked.cpp @@ -21,5 +21,11 @@ int main() { assert(MergeChunkedResponse(in, out) == true); assert(out.str() == "HTTP response with \r\nchunks."); + /* a chunk that promises more bytes than the response carries */ + std::stringstream truncated("20\r\nshort\r\n"); + std::stringstream out2; + + assert(MergeChunkedResponse(truncated, out2) == false); + return 0; }