1
0

url_parser_fuzzer.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <cstdint>
  2. #include <cstring>
  3. #include <string>
  4. #include <httplib.h>
  5. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  6. if (size < 2) return 0;
  7. // Use first byte to select which parsing function to exercise
  8. uint8_t selector = data[0];
  9. const char *payload = reinterpret_cast<const char *>(data + 1);
  10. size_t payload_size = size - 1;
  11. std::string input(payload, payload_size);
  12. switch (selector % 13) {
  13. case 0: {
  14. // parse_query_text
  15. httplib::Params params;
  16. httplib::detail::parse_query_text(payload, payload_size, params);
  17. break;
  18. }
  19. case 1: {
  20. // decode_query_component
  21. httplib::decode_query_component(input, true);
  22. httplib::decode_query_component(input, false);
  23. break;
  24. }
  25. case 2: {
  26. // decode_path_component
  27. httplib::decode_path_component(input);
  28. break;
  29. }
  30. case 3: {
  31. // encode_query_component
  32. httplib::encode_query_component(input);
  33. break;
  34. }
  35. case 4: {
  36. // normalize_query_string
  37. httplib::detail::normalize_query_string(input);
  38. break;
  39. }
  40. case 5: {
  41. // is_valid_path
  42. httplib::detail::is_valid_path(input);
  43. break;
  44. }
  45. case 6: {
  46. // parse_url
  47. httplib::detail::UrlComponents uc;
  48. httplib::detail::parse_url(input, uc);
  49. break;
  50. }
  51. case 7: {
  52. // remove_dot_segments
  53. httplib::detail::remove_dot_segments(input);
  54. break;
  55. }
  56. case 8: {
  57. // resolve_relative_location, with the input as "<location>\n<base>"
  58. auto nl = input.find('\n');
  59. auto location = input.substr(0, nl);
  60. auto base = nl == std::string::npos ? std::string("/index.html")
  61. : input.substr(nl + 1);
  62. httplib::detail::resolve_relative_location(location, base);
  63. break;
  64. }
  65. case 9: {
  66. // encode_path
  67. httplib::detail::encode_path(input);
  68. break;
  69. }
  70. case 10: {
  71. // encode_uri_component, encode_uri
  72. httplib::encode_uri_component(input);
  73. httplib::encode_uri(input);
  74. break;
  75. }
  76. case 11: {
  77. // decode_uri_component, decode_uri
  78. httplib::decode_uri_component(input);
  79. httplib::decode_uri(input);
  80. break;
  81. }
  82. case 12: {
  83. // sanitize_filename
  84. httplib::sanitize_filename(input);
  85. break;
  86. }
  87. }
  88. return 0;
  89. }