valid_urls.c 22 KB

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