valid_urls.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "libnetdata/libnetdata.h"
  3. #include "libnetdata/required_dummies.h"
  4. #include "database/rrd.h"
  5. #include "web/server/web_client.h"
  6. #include <setjmp.h>
  7. #include <cmocka.h>
  8. #include <stdbool.h>
  9. void free_temporary_host(RRDHOST *host)
  10. {
  11. (void) host;
  12. }
  13. void *__wrap_free_temporary_host(RRDHOST *host)
  14. {
  15. (void) host;
  16. return NULL;
  17. }
  18. void repr(char *result, int result_size, char const *buf, int size)
  19. {
  20. int n;
  21. char *end = result + result_size - 1;
  22. unsigned char const *ubuf = (unsigned char const *)buf;
  23. while (size && result_size > 0) {
  24. if (*ubuf <= 0x20 || *ubuf >= 0x80) {
  25. n = snprintf(result, result_size, "\\%02X", *ubuf);
  26. } else {
  27. *result = *ubuf;
  28. n = 1;
  29. }
  30. result += n;
  31. result_size -= n;
  32. ubuf++;
  33. size--;
  34. }
  35. if (result_size > 0)
  36. *(result++) = 0;
  37. else
  38. *end = 0;
  39. }
  40. // ---------------------------------- Mocking accesses from web_client ------------------------------------------------
  41. ssize_t send(int sockfd, const void *buf, size_t len, int flags)
  42. {
  43. netdata_log_info("Mocking send: %zu bytes\n", len);
  44. (void)sockfd;
  45. (void)buf;
  46. (void)flags;
  47. return len;
  48. }
  49. RRDHOST *__wrap_rrdhost_find_by_hostname(const char *hostname, uint32_t hash)
  50. {
  51. (void)hostname;
  52. (void)hash;
  53. return NULL;
  54. }
  55. /* Note: we've got some intricate code inside the global statistics module, might be useful to pull it inside the
  56. test set instead of mocking it. */
  57. void __wrap_finished_web_request_statistics(
  58. uint64_t dt, uint64_t bytes_received, uint64_t bytes_sent, uint64_t content_size, uint64_t compressed_content_size)
  59. {
  60. (void)dt;
  61. (void)bytes_received;
  62. (void)bytes_sent;
  63. (void)content_size;
  64. (void)compressed_content_size;
  65. }
  66. char *__wrap_config_get(struct config *root, const char *section, const char *name, const char *default_value)
  67. {
  68. (void)root;
  69. (void)section;
  70. (void)name;
  71. (void)default_value;
  72. return "UNKNOWN FIX ME";
  73. }
  74. int __wrap_web_client_api_request_v1(RRDHOST *host, struct web_client *w, char *url)
  75. {
  76. char url_repr[160];
  77. repr(url_repr, sizeof(url_repr), url, strlen(url));
  78. printf("web_client_api_request_v1(url=\"%s\")\n", url_repr);
  79. check_expected_ptr(host);
  80. check_expected_ptr(w);
  81. check_expected_ptr(url_repr);
  82. return HTTP_RESP_OK;
  83. }
  84. int __wrap_mysendfile(struct web_client *w, char *filename)
  85. {
  86. (void)w;
  87. printf("mysendfile(filename=\"%s\"\n", filename);
  88. check_expected_ptr(filename);
  89. return HTTP_RESP_OK;
  90. }
  91. int __wrap_rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url)
  92. {
  93. (void)host;
  94. (void)w;
  95. (void)url;
  96. return 0;
  97. }
  98. RRDHOST *__wrap_rrdhost_find_by_guid(const char *guid, uint32_t hash)
  99. {
  100. (void)guid;
  101. (void)hash;
  102. printf("FIXME: rrdset_find_guid\n");
  103. return NULL;
  104. }
  105. RRDSET *__wrap_rrdset_find_byname(RRDHOST *host, const char *name)
  106. {
  107. (void)host;
  108. (void)name;
  109. printf("FIXME: rrdset_find_byname\n");
  110. return NULL;
  111. }
  112. RRDSET *__wrap_rrdset_find(RRDHOST *host, const char *id)
  113. {
  114. (void)host;
  115. (void)id;
  116. printf("FIXME: rrdset_find\n");
  117. return NULL;
  118. }
  119. // -------------------------------- Mocking the log - dump straight through --------------------------------------------
  120. void __wrap_debug_int(const char *file, const char *function, const unsigned long line, const char *fmt, ...)
  121. {
  122. (void)file;
  123. (void)function;
  124. (void)line;
  125. va_list args;
  126. va_start(args, fmt);
  127. printf(" DEBUG: ");
  128. printf(fmt, args);
  129. printf("\n");
  130. va_end(args);
  131. }
  132. void __wrap_info_int(const char *file, const char *function, const unsigned long line, const char *fmt, ...)
  133. {
  134. (void)file;
  135. (void)function;
  136. (void)line;
  137. va_list args;
  138. va_start(args, fmt);
  139. printf(" INFO: ");
  140. printf(fmt, args);
  141. printf("\n");
  142. va_end(args);
  143. }
  144. void __wrap_error_int(
  145. const char *prefix, const char *file, const char *function, const unsigned long line, const char *fmt, ...)
  146. {
  147. (void)prefix;
  148. (void)file;
  149. (void)function;
  150. (void)line;
  151. va_list args;
  152. va_start(args, fmt);
  153. printf(" ERROR: ");
  154. printf(fmt, args);
  155. printf("\n");
  156. va_end(args);
  157. }
  158. void __wrap_fatal_int(const char *file, const char *function, const unsigned long line, const char *fmt, ...)
  159. {
  160. (void)file;
  161. (void)function;
  162. (void)line;
  163. va_list args;
  164. va_start(args, fmt);
  165. printf("FATAL: ");
  166. printf(fmt, args);
  167. printf("\n");
  168. va_end(args);
  169. fail();
  170. }
  171. WEB_SERVER_MODE web_server_mode = WEB_SERVER_MODE_STATIC_THREADED;
  172. char *netdata_configured_web_dir = "UNKNOWN FIXME";
  173. RRDHOST *localhost = NULL;
  174. struct config netdata_config = { .first_section = NULL,
  175. .last_section = NULL,
  176. .mutex = NETDATA_MUTEX_INITIALIZER,
  177. .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  178. .rwlock = AVL_LOCK_INITIALIZER } };
  179. /* Note: this is not a CMocka group_test_setup/teardown pair. This is performed per-test.
  180. */
  181. static struct web_client *setup_fresh_web_client()
  182. {
  183. struct web_client *w = (struct web_client *)malloc(sizeof(struct web_client));
  184. memset(w, 0, sizeof(struct web_client));
  185. w->response.data = buffer_create(NETDATA_WEB_RESPONSE_INITIAL_SIZE);
  186. w->response.header = buffer_create(NETDATA_WEB_RESPONSE_HEADER_SIZE);
  187. w->response.header_output = buffer_create(NETDATA_WEB_RESPONSE_HEADER_SIZE);
  188. strcpy(w->origin, "*"); // Simulate web_client_create_on_fd()
  189. w->cookie1[0] = 0; // Simulate web_client_create_on_fd()
  190. w->cookie2[0] = 0; // Simulate web_client_create_on_fd()
  191. w->acl = 0x1f; // Everything on
  192. return w;
  193. }
  194. static void destroy_web_client(struct web_client *w)
  195. {
  196. buffer_free(w->response.data);
  197. buffer_free(w->response.header);
  198. buffer_free(w->response.header_output);
  199. free(w);
  200. }
  201. //////////////////////////// Test cases ///////////////////////////////////////////////////////////////////////////////
  202. static void only_root(void **state)
  203. {
  204. (void)state;
  205. if (localhost != NULL)
  206. free(localhost);
  207. localhost = malloc(sizeof(RRDHOST));
  208. struct web_client *w = setup_fresh_web_client();
  209. buffer_strcat(w->response.data, "GET / HTTP/1.1\r\n\r\n");
  210. char debug[4096];
  211. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  212. printf("-> \"%s\"\n", debug);
  213. //char expected_url_repr[4096];
  214. //repr(expected_url_repr, sizeof(expected_url_repr), def->url_out_repr, strlen(def->url_out_repr));
  215. expect_string(__wrap_mysendfile, filename, "/");
  216. web_client_process_request(w);
  217. //assert_string_equal(w->decoded_query_string, def->query_out);
  218. destroy_web_client(w);
  219. free(localhost);
  220. localhost = NULL;
  221. }
  222. static void two_slashes(void **state)
  223. {
  224. (void)state;
  225. if (localhost != NULL)
  226. free(localhost);
  227. localhost = malloc(sizeof(RRDHOST));
  228. struct web_client *w = setup_fresh_web_client();
  229. buffer_strcat(w->response.data, "GET // HTTP/1.1\r\n\r\n");
  230. char debug[4096];
  231. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  232. printf("-> \"%s\"\n", debug);
  233. //char expected_url_repr[4096];
  234. //repr(expected_url_repr, sizeof(expected_url_repr), def->url_out_repr, strlen(def->url_out_repr));
  235. expect_string(__wrap_mysendfile, filename, "//");
  236. web_client_process_request(w);
  237. //assert_string_equal(w->decoded_query_string, def->query_out);
  238. destroy_web_client(w);
  239. free(localhost);
  240. localhost = NULL;
  241. }
  242. static void absolute_url(void **state)
  243. {
  244. (void)state;
  245. if (localhost != NULL)
  246. free(localhost);
  247. localhost = malloc(sizeof(RRDHOST));
  248. struct web_client *w = setup_fresh_web_client();
  249. buffer_strcat(w->response.data, "GET http://localhost:19999/api/v1/info HTTP/1.1\r\n\r\n");
  250. char debug[4096];
  251. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  252. printf("-> \"%s\"\n", debug);
  253. //char expected_url_repr[4096];
  254. //repr(expected_url_repr, sizeof(expected_url_repr), def->url_out_repr, strlen(def->url_out_repr));
  255. expect_value(__wrap_web_client_api_request_v1, host, localhost);
  256. expect_value(__wrap_web_client_api_request_v1, w, w);
  257. expect_string(__wrap_web_client_api_request_v1, url_repr, "info");
  258. web_client_process_request(w);
  259. assert_string_equal(w->decoded_query_string, "?blah");
  260. destroy_web_client(w);
  261. free(localhost);
  262. localhost = NULL;
  263. }
  264. static void valid_url(void **state)
  265. {
  266. (void)state;
  267. if (localhost != NULL)
  268. free(localhost);
  269. localhost = malloc(sizeof(RRDHOST));
  270. struct web_client *w = setup_fresh_web_client();
  271. buffer_strcat(w->response.data, "GET /api/v1/info?blah HTTP/1.1\r\n\r\n");
  272. char debug[4096];
  273. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  274. printf("-> \"%s\"\n", debug);
  275. //char expected_url_repr[4096];
  276. //repr(expected_url_repr, sizeof(expected_url_repr), def->url_out_repr, strlen(def->url_out_repr));
  277. expect_value(__wrap_web_client_api_request_v1, host, localhost);
  278. expect_value(__wrap_web_client_api_request_v1, w, w);
  279. expect_string(__wrap_web_client_api_request_v1, url_repr, "info");
  280. web_client_process_request(w);
  281. assert_string_equal(w->decoded_query_string, "?blah");
  282. destroy_web_client(w);
  283. free(localhost);
  284. localhost = NULL;
  285. }
  286. /* RFC2616, section 4.1:
  287. In the interest of robustness, servers SHOULD ignore any empty
  288. line(s) received where a Request-Line is expected. In other words, if
  289. the server is reading the protocol stream at the beginning of a
  290. message and receives a CRLF first, it should ignore the CRLF.
  291. */
  292. static void leading_blanks(void **state)
  293. {
  294. (void)state;
  295. if (localhost != NULL)
  296. free(localhost);
  297. localhost = malloc(sizeof(RRDHOST));
  298. struct web_client *w = setup_fresh_web_client();
  299. buffer_strcat(w->response.data, "\r\n\r\nGET /api/v1/info?blah HTTP/1.1\r\n\r\n");
  300. char debug[4096];
  301. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  302. printf("-> \"%s\"\n", debug);
  303. //char expected_url_repr[4096];
  304. //repr(expected_url_repr, sizeof(expected_url_repr), def->url_out_repr, strlen(def->url_out_repr));
  305. expect_value(__wrap_web_client_api_request_v1, host, localhost);
  306. expect_value(__wrap_web_client_api_request_v1, w, w);
  307. expect_string(__wrap_web_client_api_request_v1, url_repr, "info");
  308. web_client_process_request(w);
  309. assert_string_equal(w->decoded_query_string, "?blah");
  310. destroy_web_client(w);
  311. free(localhost);
  312. localhost = NULL;
  313. }
  314. static void empty_url(void **state)
  315. {
  316. (void)state;
  317. if (localhost != NULL)
  318. free(localhost);
  319. localhost = malloc(sizeof(RRDHOST));
  320. struct web_client *w = setup_fresh_web_client();
  321. buffer_strcat(w->response.data, "GET HTTP/1.1\r\n\r\n");
  322. char debug[4096];
  323. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  324. printf("-> \"%s\"\n", debug);
  325. //char expected_url_repr[4096];
  326. //repr(expected_url_repr, sizeof(expected_url_repr), def->url_out_repr, strlen(def->url_out_repr));
  327. expect_value(__wrap_web_client_api_request_v1, host, localhost);
  328. expect_value(__wrap_web_client_api_request_v1, w, w);
  329. expect_string(__wrap_web_client_api_request_v1, url_repr, "info");
  330. web_client_process_request(w);
  331. assert_string_equal(w->decoded_query_string, "?blah");
  332. destroy_web_client(w);
  333. free(localhost);
  334. localhost = NULL;
  335. }
  336. /* If the %-escape is being performed at the correct time then the url should not be treated as a query, but instead
  337. as a path "/api/v1/info?blah?" which should dispatch into the API with the given values.
  338. */
  339. static void not_a_query(void **state)
  340. {
  341. (void)state;
  342. localhost = malloc(sizeof(RRDHOST));
  343. struct web_client *w = setup_fresh_web_client();
  344. buffer_strcat(w->response.data, "GET /api/v1/info%3fblah%3f HTTP/1.1\r\n\r\n");
  345. char debug[160];
  346. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  347. printf("->%s\n", debug);
  348. char expected_url_repr[160];
  349. repr(expected_url_repr, sizeof(expected_url_repr), "info?blah?", 10);
  350. expect_value(__wrap_web_client_api_request_v1, host, localhost);
  351. expect_value(__wrap_web_client_api_request_v1, w, w);
  352. expect_string(__wrap_web_client_api_request_v1, url_repr, expected_url_repr);
  353. web_client_process_request(w);
  354. assert_string_equal(w->decoded_query_string, "");
  355. destroy_web_client(w);
  356. free(localhost);
  357. }
  358. static void cr_in_url(void **state)
  359. {
  360. (void)state;
  361. localhost = malloc(sizeof(RRDHOST));
  362. struct web_client *w = setup_fresh_web_client();
  363. buffer_strcat(w->response.data, "GET /api/v1/inf\ro\t?blah HTTP/1.1\r\n\r\n");
  364. char debug[160];
  365. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  366. printf("->%s\n", debug);
  367. char expected_url_repr[160];
  368. repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
  369. web_client_process_request(w);
  370. assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
  371. destroy_web_client(w);
  372. free(localhost);
  373. }
  374. static void newline_in_url(void **state)
  375. {
  376. (void)state;
  377. localhost = malloc(sizeof(RRDHOST));
  378. struct web_client *w = setup_fresh_web_client();
  379. buffer_strcat(w->response.data, "GET /api/v1/inf\no\t?blah HTTP/1.1\r\n\r\n");
  380. char debug[160];
  381. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  382. printf("->%s\n", debug);
  383. char expected_url_repr[160];
  384. repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
  385. web_client_process_request(w);
  386. assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
  387. destroy_web_client(w);
  388. free(localhost);
  389. }
  390. static void bad_version(void **state)
  391. {
  392. (void)state;
  393. localhost = malloc(sizeof(RRDHOST));
  394. struct web_client *w = setup_fresh_web_client();
  395. buffer_strcat(w->response.data, "GET /api/v1/info?blah HTTP/1.2\r\n\r\n");
  396. char debug[160];
  397. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  398. printf("->%s\n", debug);
  399. char expected_url_repr[160];
  400. repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
  401. web_client_process_request(w);
  402. assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
  403. destroy_web_client(w);
  404. free(localhost);
  405. }
  406. static void pathless_query(void **state)
  407. {
  408. (void)state;
  409. localhost = malloc(sizeof(RRDHOST));
  410. struct web_client *w = setup_fresh_web_client();
  411. buffer_strcat(w->response.data, "GET ?blah HTTP/1.1\r\n\r\n");
  412. char debug[160];
  413. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  414. printf("->%s\n", debug);
  415. char expected_url_repr[160];
  416. repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
  417. web_client_process_request(w);
  418. assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
  419. destroy_web_client(w);
  420. free(localhost);
  421. }
  422. static void pathless_fragment(void **state)
  423. {
  424. (void)state;
  425. localhost = malloc(sizeof(RRDHOST));
  426. struct web_client *w = setup_fresh_web_client();
  427. buffer_strcat(w->response.data, "GET #blah HTTP/1.1\r\n\r\n");
  428. char debug[160];
  429. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  430. printf("->%s\n", debug);
  431. char expected_url_repr[160];
  432. repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
  433. web_client_process_request(w);
  434. assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
  435. destroy_web_client(w);
  436. free(localhost);
  437. }
  438. static void short_percent(void **state)
  439. {
  440. (void)state;
  441. localhost = malloc(sizeof(RRDHOST));
  442. struct web_client *w = setup_fresh_web_client();
  443. buffer_strcat(w->response.data, "GET % HTTP/1.1\r\n\r\n");
  444. char debug[160];
  445. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  446. printf("->%s\n", debug);
  447. char expected_url_repr[160];
  448. repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
  449. web_client_process_request(w);
  450. assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
  451. destroy_web_client(w);
  452. free(localhost);
  453. }
  454. static void short_percent2(void **state)
  455. {
  456. (void)state;
  457. localhost = malloc(sizeof(RRDHOST));
  458. struct web_client *w = setup_fresh_web_client();
  459. buffer_strcat(w->response.data, "GET %0 HTTP/1.1\r\n\r\n");
  460. char debug[160];
  461. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  462. printf("->%s\n", debug);
  463. char expected_url_repr[160];
  464. repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
  465. web_client_process_request(w);
  466. assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
  467. destroy_web_client(w);
  468. free(localhost);
  469. }
  470. static void short_percent3(void **state)
  471. {
  472. (void)state;
  473. localhost = malloc(sizeof(RRDHOST));
  474. struct web_client *w = setup_fresh_web_client();
  475. buffer_strcat(w->response.data, "GET %");
  476. char debug[160];
  477. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  478. printf("->%s\n", debug);
  479. char expected_url_repr[160];
  480. repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
  481. web_client_process_request(w);
  482. assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
  483. destroy_web_client(w);
  484. free(localhost);
  485. }
  486. static void percent_nulls(void **state)
  487. {
  488. (void)state;
  489. localhost = malloc(sizeof(RRDHOST));
  490. struct web_client *w = setup_fresh_web_client();
  491. buffer_strcat(w->response.data, "GET %00%00%00%00%00%00 HTTP/1.1\r\n");
  492. char debug[160];
  493. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  494. printf("->%s\n", debug);
  495. char expected_url_repr[160];
  496. repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
  497. web_client_process_request(w);
  498. assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
  499. destroy_web_client(w);
  500. free(localhost);
  501. }
  502. static void percent_invalid(void **state)
  503. {
  504. (void)state;
  505. localhost = malloc(sizeof(RRDHOST));
  506. struct web_client *w = setup_fresh_web_client();
  507. buffer_strcat(w->response.data, "GET /%x%x%x%x%x%x HTTP/1.1\r\n");
  508. char debug[160];
  509. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  510. printf("->%s\n", debug);
  511. char expected_url_repr[160];
  512. repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
  513. web_client_process_request(w);
  514. assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
  515. destroy_web_client(w);
  516. free(localhost);
  517. }
  518. static void space_in_url(void **state)
  519. {
  520. (void)state;
  521. localhost = malloc(sizeof(RRDHOST));
  522. struct web_client *w = setup_fresh_web_client();
  523. buffer_strcat(w->response.data, "GET / / HTTP/1.1\r\n\r\n");
  524. char debug[160];
  525. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  526. printf("->%s\n", debug);
  527. char expected_url_repr[160];
  528. repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
  529. web_client_process_request(w);
  530. assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
  531. destroy_web_client(w);
  532. free(localhost);
  533. }
  534. static void random_sploit1(void **state)
  535. {
  536. (void)state;
  537. localhost = malloc(sizeof(RRDHOST));
  538. struct web_client *w = setup_fresh_web_client();
  539. // FIXME: Encoding probably needs to go through printf
  540. buffer_need_bytes(w->response.data, 55);
  541. memcpy(
  542. w->response.data->buffer,
  543. "GET \x03\x00\x00/*\xE0\x00\x00\x00\x00\x00Cookie: mstshash=Administr HTTP/1.1\r\n\r\n", 54);
  544. w->response.data->len = 54;
  545. char debug[160];
  546. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  547. printf("->%s\n", debug);
  548. char expected_url_repr[160];
  549. repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
  550. web_client_process_request(w);
  551. assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
  552. destroy_web_client(w);
  553. free(localhost);
  554. }
  555. static void null_in_url(void **state)
  556. {
  557. (void)state;
  558. localhost = malloc(sizeof(RRDHOST));
  559. struct web_client *w = setup_fresh_web_client();
  560. buffer_strcat(w->response.data, "GET / / HTTP/1.1\r\n\r\n");
  561. w->response.data->buffer[5] = 0;
  562. char debug[160];
  563. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  564. printf("->%s\n", debug);
  565. char expected_url_repr[160];
  566. repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
  567. web_client_process_request(w);
  568. assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
  569. destroy_web_client(w);
  570. free(localhost);
  571. }
  572. static void many_ands(void **state)
  573. {
  574. (void)state;
  575. localhost = malloc(sizeof(RRDHOST));
  576. struct web_client *w = setup_fresh_web_client();
  577. buffer_strcat(w->response.data, "GET foo?");
  578. for (size_t i = 0; i < 600; i++)
  579. buffer_strcat(w->response.data, "&");
  580. buffer_strcat(w->response.data, " HTTP/1.1\r\n\r\n");
  581. char debug[2048];
  582. repr(debug, sizeof(debug), w->response.data->buffer, w->response.data->len);
  583. printf("->%s\n", debug);
  584. char expected_url_repr[160];
  585. repr(expected_url_repr, sizeof(expected_url_repr), "inf\no\t", 6);
  586. web_client_process_request(w);
  587. assert_int_equal(w->response.code, HTTP_RESP_BAD_REQUEST);
  588. destroy_web_client(w);
  589. free(localhost);
  590. }
  591. int main(void)
  592. {
  593. debug_flags = 0xffffffffffff;
  594. int fails = 0;
  595. struct CMUnitTest static_tests[] = {
  596. cmocka_unit_test(only_root), cmocka_unit_test(two_slashes), cmocka_unit_test(valid_url),
  597. cmocka_unit_test(leading_blanks), cmocka_unit_test(empty_url), cmocka_unit_test(newline_in_url),
  598. cmocka_unit_test(not_a_query), cmocka_unit_test(cr_in_url), cmocka_unit_test(pathless_query),
  599. cmocka_unit_test(pathless_fragment), cmocka_unit_test(short_percent), cmocka_unit_test(short_percent2),
  600. cmocka_unit_test(short_percent3), cmocka_unit_test(percent_nulls), cmocka_unit_test(percent_invalid),
  601. cmocka_unit_test(space_in_url), cmocka_unit_test(random_sploit1), cmocka_unit_test(null_in_url),
  602. cmocka_unit_test(absolute_url),
  603. // cmocka_unit_test(many_ands), CMocka cannot recover after this crash
  604. cmocka_unit_test(bad_version)
  605. };
  606. (void)many_ands;
  607. fails += cmocka_run_group_tests_name("static_tests", static_tests, NULL, NULL);
  608. return fails;
  609. }