From 3091751b5d09122b589743579d9433513e21785f Mon Sep 17 00:00:00 2001 From: saiviswasb Date: Wed, 2 Dec 2026 13:22:05 +0800 Subject: [PATCH] Add chunked_decoder_fuzzer and a seed corpus for multipart_parser_fuzzer (#2706) * Add chunked_decoder_fuzzer and a seed corpus for multipart_parser_fuzzer * Apply clang-format to chunked_decoder_fuzzer.cc * Simplify chunked_decoder_fuzzer and tidy the seed corpora read_content_chunked() only touches the trailers and headers, which Request and Response share, so decoding into both ran the same code. Decode into a Response only. Drop the SIZE_MAX limit as well: fuzz inputs never get near the 100MB default, so both take the same branches. Adjust the first byte of the chunked seeds to the new selector, and remove the seed for SIZE_MAX. Drop the duplicated number prefixes from the multipart seed names, run both new corpora from the "all" target, and keep the fuzzer rules in the order of FUZZERS. --------- Co-authored-by: Sai Viswas Basetti Co-authored-by: yhirose --- test/fuzzing/Makefile | 9 ++++- test/fuzzing/chunked_decoder_fuzzer.cc | 36 ++++++++++++++++++ test/fuzzing/chunked_decoder_fuzzer.dict | 9 +++++ test/fuzzing/corpus_chunked/extensions | Bin 0 -> 33 bytes test/fuzzing/corpus_chunked/huge_chunk_size | Bin 0 -> 29 bytes test/fuzzing/corpus_chunked/limit_16_exact | 4 ++ test/fuzzing/corpus_chunked/limit_16_plus_1 | 6 +++ test/fuzzing/corpus_chunked/limit_1_exceeded | 4 ++ test/fuzzing/corpus_chunked/limit_default | 4 ++ .../fuzzing/corpus_chunked/missing_final_crlf | Bin 0 -> 12 bytes test/fuzzing/corpus_chunked/trailers | Bin 0 -> 40 bytes test/fuzzing/corpus_chunked/truncated | Bin 0 -> 7 bytes test/fuzzing/corpus_chunked/uppercase_hex | Bin 0 -> 21 bytes test/fuzzing/corpus_chunked/valid_two_chunks | Bin 0 -> 25 bytes .../corpus_multipart/binary_boundary_chunk1 | Bin 0 -> 151 bytes test/fuzzing/corpus_multipart/binary_payload | Bin 0 -> 79 bytes test/fuzzing/corpus_multipart/boundary_16byte | 5 +++ test/fuzzing/corpus_multipart/boundary_1byte | Bin 0 -> 67 bytes .../fuzzing/corpus_multipart/boundary_in_body | 5 +++ .../boundary_lookalikes_in_content | 13 +++++++ .../corpus_multipart/disposition_rejected | 13 +++++++ .../corpus_multipart/disposition_variants | 22 +++++++++++ test/fuzzing/corpus_multipart/empty_parts | 11 ++++++ test/fuzzing/corpus_multipart/extra_headers | 6 +++ test/fuzzing/corpus_multipart/file_parts | Bin 0 -> 356 bytes .../corpus_multipart/junk_after_boundary | 9 +++++ test/fuzzing/corpus_multipart/leading_crlf | 6 +++ .../corpus_multipart/missing_close_boundary | 9 +++++ test/fuzzing/corpus_multipart/missing_crlf | 5 +++ .../missing_crlf_after_boundary | 4 ++ .../corpus_multipart/missing_crlf_header_end | 4 ++ test/fuzzing/corpus_multipart/multi_parts | 9 +++++ test/fuzzing/corpus_multipart/no_disposition | 5 +++ test/fuzzing/corpus_multipart/several_parts | 15 ++++++++ test/fuzzing/corpus_multipart/single_field | 5 +++ test/fuzzing/corpus_multipart/single_part | 5 +++ .../fuzzing/corpus_multipart/spaces_in_header | 5 +++ 37 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 test/fuzzing/chunked_decoder_fuzzer.cc create mode 100644 test/fuzzing/chunked_decoder_fuzzer.dict create mode 100644 test/fuzzing/corpus_chunked/extensions create mode 100644 test/fuzzing/corpus_chunked/huge_chunk_size create mode 100644 test/fuzzing/corpus_chunked/limit_16_exact create mode 100644 test/fuzzing/corpus_chunked/limit_16_plus_1 create mode 100644 test/fuzzing/corpus_chunked/limit_1_exceeded create mode 100644 test/fuzzing/corpus_chunked/limit_default create mode 100644 test/fuzzing/corpus_chunked/missing_final_crlf create mode 100644 test/fuzzing/corpus_chunked/trailers create mode 100644 test/fuzzing/corpus_chunked/truncated create mode 100644 test/fuzzing/corpus_chunked/uppercase_hex create mode 100644 test/fuzzing/corpus_chunked/valid_two_chunks create mode 100644 test/fuzzing/corpus_multipart/binary_boundary_chunk1 create mode 100644 test/fuzzing/corpus_multipart/binary_payload create mode 100644 test/fuzzing/corpus_multipart/boundary_16byte create mode 100644 test/fuzzing/corpus_multipart/boundary_1byte create mode 100644 test/fuzzing/corpus_multipart/boundary_in_body create mode 100644 test/fuzzing/corpus_multipart/boundary_lookalikes_in_content create mode 100644 test/fuzzing/corpus_multipart/disposition_rejected create mode 100644 test/fuzzing/corpus_multipart/disposition_variants create mode 100644 test/fuzzing/corpus_multipart/empty_parts create mode 100644 test/fuzzing/corpus_multipart/extra_headers create mode 100644 test/fuzzing/corpus_multipart/file_parts create mode 100644 test/fuzzing/corpus_multipart/junk_after_boundary create mode 100644 test/fuzzing/corpus_multipart/leading_crlf create mode 100644 test/fuzzing/corpus_multipart/missing_close_boundary create mode 100644 test/fuzzing/corpus_multipart/missing_crlf create mode 100644 test/fuzzing/corpus_multipart/missing_crlf_after_boundary create mode 100644 test/fuzzing/corpus_multipart/missing_crlf_header_end create mode 100644 test/fuzzing/corpus_multipart/multi_parts create mode 100644 test/fuzzing/corpus_multipart/no_disposition create mode 100644 test/fuzzing/corpus_multipart/several_parts create mode 100644 test/fuzzing/corpus_multipart/single_field create mode 100644 test/fuzzing/corpus_multipart/single_part create mode 100644 test/fuzzing/corpus_multipart/spaces_in_header diff --git a/test/fuzzing/Makefile b/test/fuzzing/Makefile index 716a8aac..0599cffa 100644 --- a/test/fuzzing/Makefile +++ b/test/fuzzing/Makefile @@ -13,12 +13,14 @@ ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz BROTLI_DIR = /usr/local/opt/brotli # BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec -FUZZERS = server_fuzzer url_parser_fuzzer header_parser_fuzzer client_fuzzer multipart_parser_fuzzer websocket_fuzzer sse_parser_fuzzer server_mount_fuzzer +FUZZERS = server_fuzzer url_parser_fuzzer header_parser_fuzzer client_fuzzer multipart_parser_fuzzer websocket_fuzzer sse_parser_fuzzer server_mount_fuzzer chunked_decoder_fuzzer # Runs all the tests and also fuzz tests against seed corpus. all : $(FUZZERS) ./server_fuzzer corpus/* ./server_mount_fuzzer corpus_mount/* + ./multipart_parser_fuzzer corpus_multipart/* + ./chunked_decoder_fuzzer corpus_chunked/* # Fuzz target, so that you can choose which $(LIB_FUZZING_ENGINE) to use. server_fuzzer : server_fuzzer.cc fuzzed_stream.h ../../httplib.h @@ -38,6 +40,7 @@ url_parser_fuzzer : url_parser_fuzzer.cc ../../httplib.h multipart_parser_fuzzer : multipart_parser_fuzzer.cc ../../httplib.h $(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl + zip -q -r multipart_parser_fuzzer_seed_corpus.zip corpus_multipart websocket_fuzzer : websocket_fuzzer.cc ../../httplib.h $(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl @@ -49,5 +52,9 @@ server_mount_fuzzer : server_mount_fuzzer.cc fuzzed_stream.h ../../httplib.h $(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl zip -q -r server_mount_fuzzer_seed_corpus.zip corpus_mount +chunked_decoder_fuzzer : chunked_decoder_fuzzer.cc fuzzed_stream.h ../../httplib.h + $(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl + zip -q -r chunked_decoder_fuzzer_seed_corpus.zip corpus_chunked + clean: rm -f server_fuzzer pem *.0 *.o *.1 *.srl *.zip diff --git a/test/fuzzing/chunked_decoder_fuzzer.cc b/test/fuzzing/chunked_decoder_fuzzer.cc new file mode 100644 index 00000000..1556fe10 --- /dev/null +++ b/test/fuzzing/chunked_decoder_fuzzer.cc @@ -0,0 +1,36 @@ +#include + +#include "fuzzed_stream.h" + +namespace { + +void decode(const uint8_t *data, size_t size, size_t payload_max_length) { + FuzzedStream strm(data, size); + httplib::Response res; + httplib::detail::read_content_chunked( + strm, res, payload_max_length, + [](const char *, size_t, uint64_t, uint64_t) { return true; }); +} + +} // namespace + +// The first decode initializes function-local statics, which in a persistent +// fuzzer would otherwise show up as unstable coverage on the first input only. +extern "C" int LLVMFuzzerInitialize(int * /*argc*/, char *** /*argv*/) { + static const char warm_up[] = + "5;ext=1\r\nhello\r\n0\r\nX-Trailer: 1\r\nExpires: 0\r\n\r\n"; + decode(reinterpret_cast(warm_up), sizeof(warm_up) - 1, 0); + return 0; +} + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + if (size < 2) { return 0; } + + // The first byte selects the payload limit, 0 meaning none + static const size_t max_lens[] = {0, 1, 16, 4096, + CPPHTTPLIB_PAYLOAD_MAX_LENGTH}; + auto n = sizeof(max_lens) / sizeof(max_lens[0]); + decode(data + 1, size - 1, max_lens[data[0] % n]); + + return 0; +} diff --git a/test/fuzzing/chunked_decoder_fuzzer.dict b/test/fuzzing/chunked_decoder_fuzzer.dict new file mode 100644 index 00000000..256a9c14 --- /dev/null +++ b/test/fuzzing/chunked_decoder_fuzzer.dict @@ -0,0 +1,9 @@ +crlf="\x0d\x0a" +last_chunk="0\x0d\x0a\x0d\x0a" +ext=";" +ext_kv=";name=value" +ext_quoted=";name=\"a;b\"" +hex_max="ffffffffffffffff" +hex_over="10000000000000000" +trailer="X-Trailer: 1\x0d\x0a" +expires="Expires: 0\x0d\x0a" diff --git a/test/fuzzing/corpus_chunked/extensions b/test/fuzzing/corpus_chunked/extensions new file mode 100644 index 0000000000000000000000000000000000000000..94c1e8f29d68c3c9e94f38b469b900e653ef69bd GIT binary patch literal 33 ocmZQDw$4k;O|>mc%&|_(NlfSEN=!=TKW60H*E<1^@s6 literal 0 HcmV?d00001 diff --git a/test/fuzzing/corpus_chunked/huge_chunk_size b/test/fuzzing/corpus_chunked/huge_chunk_size new file mode 100644 index 0000000000000000000000000000000000000000..ec687fb554525acfec2449091b8a0f4a6bb16ca1 GIT binary patch literal 29 WcmZQbLj$~AiAl-4Tn4;cyj%c~CF literal 0 HcmV?d00001 diff --git a/test/fuzzing/corpus_chunked/trailers b/test/fuzzing/corpus_chunked/trailers new file mode 100644 index 0000000000000000000000000000000000000000..e83810b3d828572fb64202bf777b3fc8434e4712 GIT binary patch literal 40 rcmZQD;^oT7wrYDtNg7w;Kr* literal 0 HcmV?d00001 diff --git a/test/fuzzing/corpus_chunked/truncated b/test/fuzzing/corpus_chunked/truncated new file mode 100644 index 0000000000000000000000000000000000000000..1de5797920af15db854eb0234643001697206d38 GIT binary patch literal 7 OcmZRu;N?n8N(KM~3ITQi literal 0 HcmV?d00001 diff --git a/test/fuzzing/corpus_chunked/uppercase_hex b/test/fuzzing/corpus_chunked/uppercase_hex new file mode 100644 index 0000000000000000000000000000000000000000..7ef8906febcd3cbd38907a14b8c35f8088b74209 GIT binary patch literal 21 acmZQ@KW6070(=y8r+H literal 0 HcmV?d00001 diff --git a/test/fuzzing/corpus_multipart/binary_boundary_chunk1 b/test/fuzzing/corpus_multipart/binary_boundary_chunk1 new file mode 100644 index 0000000000000000000000000000000000000000..6a92537c28448d343ac7db2693a5c2b48c866da7 GIT binary patch literal 151 zcmZQ$U`)!)`|qTy3uW+fIp^nbMo#DapH%;ZFn3HtfTC8;I4#U(|liMhO7AgBvO NP+P%Vm(`tO|?}@%S_El VQR3y|Ae%LJv*b7^mn?sfALz{Vpq4ys%hO2k^GO(SfQTEXR`sFk zRW*yT!?)3lbBGITHJ>5g zCGvUd4e1Wb+KT!z5`u~u3+VjQo|ORaY!pmO>z9tPjUTgecDqYUa6L!SwXtt29p z8-x+KDlpnl&U{WhP0iA1IZTbVw|{VWbbQh{JyXq=KEG&pF0Za{ZtuFi{{6$_)AR3- L)@rMQwOaiFX;p47 literal 0 HcmV?d00001 diff --git a/test/fuzzing/corpus_multipart/junk_after_boundary b/test/fuzzing/corpus_multipart/junk_after_boundary new file mode 100644 index 00000000..f8338f61 --- /dev/null +++ b/test/fuzzing/corpus_multipart/junk_after_boundary @@ -0,0 +1,9 @@ +pad--pad +Content-Disposition: form-data; name="a" + +ok +--pad +Content-Disposition: form-data; name="b" + +x +--pad-- diff --git a/test/fuzzing/corpus_multipart/leading_crlf b/test/fuzzing/corpus_multipart/leading_crlf new file mode 100644 index 00000000..d30a727d --- /dev/null +++ b/test/fuzzing/corpus_multipart/leading_crlf @@ -0,0 +1,6 @@ +BOUND +--BOUND +Content-Disposition: form-data; name="a" + +val +--BOUND-- diff --git a/test/fuzzing/corpus_multipart/missing_close_boundary b/test/fuzzing/corpus_multipart/missing_close_boundary new file mode 100644 index 00000000..04ca9e3b --- /dev/null +++ b/test/fuzzing/corpus_multipart/missing_close_boundary @@ -0,0 +1,9 @@ +unclosed--unclosed +Content-Disposition: form-data; name="a" + +first +--unclosed +Content-Disposition: form-data; name="b" + +truncated content with no final delimiter +--unclo \ No newline at end of file diff --git a/test/fuzzing/corpus_multipart/missing_crlf b/test/fuzzing/corpus_multipart/missing_crlf new file mode 100644 index 00000000..3c431025 --- /dev/null +++ b/test/fuzzing/corpus_multipart/missing_crlf @@ -0,0 +1,5 @@ +BOUND--BOUND +Content-Disposition: form-data; name="a" + +val +--BOUND-- diff --git a/test/fuzzing/corpus_multipart/missing_crlf_after_boundary b/test/fuzzing/corpus_multipart/missing_crlf_after_boundary new file mode 100644 index 00000000..a471d77f --- /dev/null +++ b/test/fuzzing/corpus_multipart/missing_crlf_after_boundary @@ -0,0 +1,4 @@ +nocrlf--nocrlfContent-Disposition: form-data; name="x" + +data +--nocrlf-- diff --git a/test/fuzzing/corpus_multipart/missing_crlf_header_end b/test/fuzzing/corpus_multipart/missing_crlf_header_end new file mode 100644 index 00000000..5293d0f0 --- /dev/null +++ b/test/fuzzing/corpus_multipart/missing_crlf_header_end @@ -0,0 +1,4 @@ +hdrEnd--hdrEnd +Content-Disposition: form-data; name="h" +Content-Type: text/plain +content right after headers--hdrEnd-- diff --git a/test/fuzzing/corpus_multipart/multi_parts b/test/fuzzing/corpus_multipart/multi_parts new file mode 100644 index 00000000..0670f593 --- /dev/null +++ b/test/fuzzing/corpus_multipart/multi_parts @@ -0,0 +1,9 @@ +BOUND--BOUND +Content-Disposition: form-data; name="a" + +1 +--BOUND +Content-Disposition: form-data; name="b" + +2 +--BOUND-- diff --git a/test/fuzzing/corpus_multipart/no_disposition b/test/fuzzing/corpus_multipart/no_disposition new file mode 100644 index 00000000..52830f3c --- /dev/null +++ b/test/fuzzing/corpus_multipart/no_disposition @@ -0,0 +1,5 @@ +BOUND--BOUND +Content-Type: text/plain + +orphaned data +--BOUND-- diff --git a/test/fuzzing/corpus_multipart/several_parts b/test/fuzzing/corpus_multipart/several_parts new file mode 100644 index 00000000..1beeb1d0 --- /dev/null +++ b/test/fuzzing/corpus_multipart/several_parts @@ -0,0 +1,15 @@ +sev3ral--sev3ral +Content-Disposition: form-data; name="a" + +alpha +--sev3ral +Content-Disposition: form-data; name="b" + +bravo +second line +--sev3ral +Content-Disposition: form-data; name="c" +Content-Type: application/json + +{"k":[1,2,3]} +--sev3ral-- diff --git a/test/fuzzing/corpus_multipart/single_field b/test/fuzzing/corpus_multipart/single_field new file mode 100644 index 00000000..43fa293e --- /dev/null +++ b/test/fuzzing/corpus_multipart/single_field @@ -0,0 +1,5 @@ +AaB03x--AaB03x +Content-Disposition: form-data; name="field1" + +value1 +--AaB03x-- diff --git a/test/fuzzing/corpus_multipart/single_part b/test/fuzzing/corpus_multipart/single_part new file mode 100644 index 00000000..6675aa61 --- /dev/null +++ b/test/fuzzing/corpus_multipart/single_part @@ -0,0 +1,5 @@ +BOUND--BOUND +Content-Disposition: form-data; name="user" + +Alice +--BOUND-- diff --git a/test/fuzzing/corpus_multipart/spaces_in_header b/test/fuzzing/corpus_multipart/spaces_in_header new file mode 100644 index 00000000..a4ae6bd1 --- /dev/null +++ b/test/fuzzing/corpus_multipart/spaces_in_header @@ -0,0 +1,5 @@ +BOUND--BOUND +Content-Disposition : form-data ; name = "a" + +val +--BOUND--