handle_form.inl 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. /* Copyright (c) 2016-2021 the Civetweb developers
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. static int
  22. url_encoded_field_found(const struct mg_connection *conn,
  23. const char *key,
  24. size_t key_len,
  25. const char *filename,
  26. size_t filename_len,
  27. char *path,
  28. size_t path_len,
  29. struct mg_form_data_handler *fdh)
  30. {
  31. char key_dec[1024];
  32. char filename_dec[1024];
  33. int key_dec_len;
  34. int filename_dec_len;
  35. int ret;
  36. key_dec_len =
  37. mg_url_decode(key, (int)key_len, key_dec, (int)sizeof(key_dec), 1);
  38. if (((size_t)key_dec_len >= (size_t)sizeof(key_dec)) || (key_dec_len < 0)) {
  39. return MG_FORM_FIELD_STORAGE_SKIP;
  40. }
  41. if (filename) {
  42. filename_dec_len = mg_url_decode(filename,
  43. (int)filename_len,
  44. filename_dec,
  45. (int)sizeof(filename_dec),
  46. 1);
  47. if (((size_t)filename_dec_len >= (size_t)sizeof(filename_dec))
  48. || (filename_dec_len < 0)) {
  49. /* Log error message and skip this field. */
  50. mg_cry_internal(conn, "%s: Cannot decode filename", __func__);
  51. return MG_FORM_FIELD_STORAGE_SKIP;
  52. }
  53. remove_dot_segments(filename_dec);
  54. } else {
  55. filename_dec[0] = 0;
  56. }
  57. ret =
  58. fdh->field_found(key_dec, filename_dec, path, path_len, fdh->user_data);
  59. if ((ret & 0xF) == MG_FORM_FIELD_STORAGE_GET) {
  60. if (fdh->field_get == NULL) {
  61. mg_cry_internal(conn,
  62. "%s: Function \"Get\" not available",
  63. __func__);
  64. return MG_FORM_FIELD_STORAGE_SKIP;
  65. }
  66. }
  67. if ((ret & 0xF) == MG_FORM_FIELD_STORAGE_STORE) {
  68. if (fdh->field_store == NULL) {
  69. mg_cry_internal(conn,
  70. "%s: Function \"Store\" not available",
  71. __func__);
  72. return MG_FORM_FIELD_STORAGE_SKIP;
  73. }
  74. }
  75. return ret;
  76. }
  77. static int
  78. url_encoded_field_get(
  79. const struct mg_connection *conn,
  80. const char *key,
  81. size_t key_len,
  82. const char *value,
  83. size_t *value_len, /* IN: number of bytes available in "value", OUT: number
  84. of bytes processed */
  85. struct mg_form_data_handler *fdh)
  86. {
  87. char key_dec[1024];
  88. char *value_dec = (char *)mg_malloc_ctx(*value_len + 1, conn->phys_ctx);
  89. int value_dec_len, ret;
  90. if (!value_dec) {
  91. /* Log error message and stop parsing the form data. */
  92. mg_cry_internal(conn,
  93. "%s: Not enough memory (required: %lu)",
  94. __func__,
  95. (unsigned long)(*value_len + 1));
  96. return MG_FORM_FIELD_STORAGE_ABORT;
  97. }
  98. mg_url_decode(key, (int)key_len, key_dec, (int)sizeof(key_dec), 1);
  99. if (*value_len >= 2 && value[*value_len - 2] == '%')
  100. *value_len -= 2;
  101. else if (*value_len >= 1 && value[*value_len - 1] == '%')
  102. (*value_len)--;
  103. value_dec_len = mg_url_decode(
  104. value, (int)*value_len, value_dec, ((int)*value_len) + 1, 1);
  105. ret = fdh->field_get(key_dec,
  106. value_dec,
  107. (size_t)value_dec_len,
  108. fdh->user_data);
  109. mg_free(value_dec);
  110. return ret;
  111. }
  112. static int
  113. unencoded_field_get(const struct mg_connection *conn,
  114. const char *key,
  115. size_t key_len,
  116. const char *value,
  117. size_t value_len,
  118. struct mg_form_data_handler *fdh)
  119. {
  120. char key_dec[1024];
  121. (void)conn;
  122. mg_url_decode(key, (int)key_len, key_dec, (int)sizeof(key_dec), 1);
  123. return fdh->field_get(key_dec, value, value_len, fdh->user_data);
  124. }
  125. static int
  126. field_stored(const struct mg_connection *conn,
  127. const char *path,
  128. long long file_size,
  129. struct mg_form_data_handler *fdh)
  130. {
  131. /* Equivalent to "upload" callback of "mg_upload". */
  132. (void)conn; /* we do not need mg_cry here, so conn is currently unused */
  133. return fdh->field_store(path, file_size, fdh->user_data);
  134. }
  135. static const char *
  136. search_boundary(const char *buf,
  137. size_t buf_len,
  138. const char *boundary,
  139. size_t boundary_len)
  140. {
  141. char *boundary_start = "\r\n--";
  142. size_t boundary_start_len = strlen(boundary_start);
  143. /* We must do a binary search here, not a string search, since the
  144. * buffer may contain '\x00' bytes, if binary data is transferred. */
  145. int clen = (int)buf_len - (int)boundary_len - boundary_start_len;
  146. int i;
  147. for (i = 0; i <= clen; i++) {
  148. if (!memcmp(buf + i, boundary_start, boundary_start_len)) {
  149. if (!memcmp(buf + i + boundary_start_len, boundary, boundary_len)) {
  150. return buf + i;
  151. }
  152. }
  153. }
  154. return NULL;
  155. }
  156. int
  157. mg_handle_form_request(struct mg_connection *conn,
  158. struct mg_form_data_handler *fdh)
  159. {
  160. const char *content_type;
  161. char path[512];
  162. char buf[MG_BUF_LEN]; /* Must not be smaller than ~900 */
  163. int field_storage;
  164. size_t buf_fill = 0;
  165. int r;
  166. int field_count = 0;
  167. struct mg_file fstore = STRUCT_FILE_INITIALIZER;
  168. int64_t file_size = 0; /* init here, to a avoid a false positive
  169. "uninitialized variable used" warning */
  170. int has_body_data =
  171. (conn->request_info.content_length > 0) || (conn->is_chunked);
  172. /* Unused without filesystems */
  173. (void)fstore;
  174. (void)file_size;
  175. /* There are three ways to encode data from a HTML form:
  176. * 1) method: GET (default)
  177. * The form data is in the HTTP query string.
  178. * 2) method: POST, enctype: "application/x-www-form-urlencoded"
  179. * The form data is in the request body.
  180. * The body is url encoded (the default encoding for POST).
  181. * 3) method: POST, enctype: "multipart/form-data".
  182. * The form data is in the request body of a multipart message.
  183. * This is the typical way to handle file upload from a form.
  184. */
  185. if (!has_body_data) {
  186. const char *data;
  187. if (0 != strcmp(conn->request_info.request_method, "GET")) {
  188. /* No body data, but not a GET request.
  189. * This is not a valid form request. */
  190. return -1;
  191. }
  192. /* GET request: form data is in the query string. */
  193. /* The entire data has already been loaded, so there is no need to
  194. * call mg_read. We just need to split the query string into key-value
  195. * pairs. */
  196. data = conn->request_info.query_string;
  197. if (!data) {
  198. /* No query string. */
  199. return -1;
  200. }
  201. /* Split data in a=1&b=xy&c=3&c=4 ... */
  202. while (*data) {
  203. const char *val = strchr(data, '=');
  204. const char *next;
  205. ptrdiff_t keylen, vallen;
  206. if (!val) {
  207. break;
  208. }
  209. keylen = val - data;
  210. /* In every "field_found" callback we ask what to do with the
  211. * data ("field_storage"). This could be:
  212. * MG_FORM_FIELD_STORAGE_SKIP (0):
  213. * ignore the value of this field
  214. * MG_FORM_FIELD_STORAGE_GET (1):
  215. * read the data and call the get callback function
  216. * MG_FORM_FIELD_STORAGE_STORE (2):
  217. * store the data in a file
  218. * MG_FORM_FIELD_STORAGE_READ (3):
  219. * let the user read the data (for parsing long data on the fly)
  220. * MG_FORM_FIELD_STORAGE_ABORT (flag):
  221. * stop parsing
  222. */
  223. memset(path, 0, sizeof(path));
  224. field_count++;
  225. field_storage = url_encoded_field_found(conn,
  226. data,
  227. (size_t)keylen,
  228. NULL,
  229. 0,
  230. path,
  231. sizeof(path) - 1,
  232. fdh);
  233. val++;
  234. next = strchr(val, '&');
  235. if (next) {
  236. vallen = next - val;
  237. } else {
  238. vallen = (ptrdiff_t)strlen(val);
  239. }
  240. if (field_storage == MG_FORM_FIELD_STORAGE_GET) {
  241. /* Call callback */
  242. r = url_encoded_field_get(
  243. conn, data, (size_t)keylen, val, (size_t *)&vallen, fdh);
  244. if (r == MG_FORM_FIELD_HANDLE_ABORT) {
  245. /* Stop request handling */
  246. break;
  247. }
  248. if (r == MG_FORM_FIELD_HANDLE_NEXT) {
  249. /* Skip to next field */
  250. field_storage = MG_FORM_FIELD_STORAGE_SKIP;
  251. }
  252. }
  253. if (next) {
  254. next++;
  255. } else {
  256. /* vallen may have been modified by url_encoded_field_get */
  257. next = val + vallen;
  258. }
  259. #if !defined(NO_FILESYSTEMS)
  260. if (field_storage == MG_FORM_FIELD_STORAGE_STORE) {
  261. /* Store the content to a file */
  262. if (mg_fopen(conn, path, MG_FOPEN_MODE_WRITE, &fstore) == 0) {
  263. fstore.access.fp = NULL;
  264. }
  265. file_size = 0;
  266. if (fstore.access.fp != NULL) {
  267. size_t n = (size_t)
  268. fwrite(val, 1, (size_t)vallen, fstore.access.fp);
  269. if ((n != (size_t)vallen) || (ferror(fstore.access.fp))) {
  270. mg_cry_internal(conn,
  271. "%s: Cannot write file %s",
  272. __func__,
  273. path);
  274. (void)mg_fclose(&fstore.access);
  275. remove_bad_file(conn, path);
  276. }
  277. file_size += (int64_t)n;
  278. if (fstore.access.fp) {
  279. r = mg_fclose(&fstore.access);
  280. if (r == 0) {
  281. /* stored successfully */
  282. r = field_stored(conn, path, file_size, fdh);
  283. if (r == MG_FORM_FIELD_HANDLE_ABORT) {
  284. /* Stop request handling */
  285. break;
  286. }
  287. } else {
  288. mg_cry_internal(conn,
  289. "%s: Error saving file %s",
  290. __func__,
  291. path);
  292. remove_bad_file(conn, path);
  293. }
  294. fstore.access.fp = NULL;
  295. }
  296. } else {
  297. mg_cry_internal(conn,
  298. "%s: Cannot create file %s",
  299. __func__,
  300. path);
  301. }
  302. }
  303. #endif /* NO_FILESYSTEMS */
  304. /* if (field_storage == MG_FORM_FIELD_STORAGE_READ) { */
  305. /* The idea of "field_storage=read" is to let the API user read
  306. * data chunk by chunk and to some data processing on the fly.
  307. * This should avoid the need to store data in the server:
  308. * It should neither be stored in memory, like
  309. * "field_storage=get" does, nor in a file like
  310. * "field_storage=store".
  311. * However, for a "GET" request this does not make any much
  312. * sense, since the data is already stored in memory, as it is
  313. * part of the query string.
  314. */
  315. /* } */
  316. if ((field_storage & MG_FORM_FIELD_STORAGE_ABORT)
  317. == MG_FORM_FIELD_STORAGE_ABORT) {
  318. /* Stop parsing the request */
  319. break;
  320. }
  321. /* Proceed to next entry */
  322. data = next;
  323. }
  324. return field_count;
  325. }
  326. content_type = mg_get_header(conn, "Content-Type");
  327. if (!content_type
  328. || !mg_strncasecmp(content_type,
  329. "APPLICATION/X-WWW-FORM-URLENCODED",
  330. 33)
  331. || !mg_strncasecmp(content_type,
  332. "APPLICATION/WWW-FORM-URLENCODED",
  333. 31)) {
  334. /* The form data is in the request body data, encoded in key/value
  335. * pairs. */
  336. int all_data_read = 0;
  337. /* Read body data and split it in keys and values.
  338. * The encoding is like in the "GET" case above: a=1&b&c=3&c=4.
  339. * Here we use "POST", and read the data from the request body.
  340. * The data read on the fly, so it is not required to buffer the
  341. * entire request in memory before processing it. */
  342. for (;;) {
  343. const char *val;
  344. const char *next;
  345. ptrdiff_t keylen, vallen;
  346. ptrdiff_t used;
  347. int end_of_key_value_pair_found = 0;
  348. int get_block;
  349. if (buf_fill < (sizeof(buf) - 1)) {
  350. size_t to_read = sizeof(buf) - 1 - buf_fill;
  351. r = mg_read(conn, buf + buf_fill, to_read);
  352. if ((r < 0) || ((r == 0) && all_data_read)) {
  353. /* read error */
  354. return -1;
  355. }
  356. if (r == 0) {
  357. /* TODO: Create a function to get "all_data_read" from
  358. * the conn object. All data is read if the Content-Length
  359. * has been reached, or if chunked encoding is used and
  360. * the end marker has been read, or if the connection has
  361. * been closed. */
  362. all_data_read = (buf_fill == 0);
  363. }
  364. buf_fill += r;
  365. buf[buf_fill] = 0;
  366. if (buf_fill < 1) {
  367. break;
  368. }
  369. }
  370. val = strchr(buf, '=');
  371. if (!val) {
  372. break;
  373. }
  374. keylen = val - buf;
  375. val++;
  376. /* Call callback */
  377. memset(path, 0, sizeof(path));
  378. field_count++;
  379. field_storage = url_encoded_field_found(conn,
  380. buf,
  381. (size_t)keylen,
  382. NULL,
  383. 0,
  384. path,
  385. sizeof(path) - 1,
  386. fdh);
  387. if ((field_storage & MG_FORM_FIELD_STORAGE_ABORT)
  388. == MG_FORM_FIELD_STORAGE_ABORT) {
  389. /* Stop parsing the request */
  390. break;
  391. }
  392. #if !defined(NO_FILESYSTEMS)
  393. if (field_storage == MG_FORM_FIELD_STORAGE_STORE) {
  394. if (mg_fopen(conn, path, MG_FOPEN_MODE_WRITE, &fstore) == 0) {
  395. fstore.access.fp = NULL;
  396. }
  397. file_size = 0;
  398. if (!fstore.access.fp) {
  399. mg_cry_internal(conn,
  400. "%s: Cannot create file %s",
  401. __func__,
  402. path);
  403. }
  404. }
  405. #endif /* NO_FILESYSTEMS */
  406. get_block = 0;
  407. /* Loop to read values larger than sizeof(buf)-keylen-2 */
  408. do {
  409. next = strchr(val, '&');
  410. if (next) {
  411. vallen = next - val;
  412. end_of_key_value_pair_found = 1;
  413. } else {
  414. vallen = (ptrdiff_t)strlen(val);
  415. end_of_key_value_pair_found = all_data_read;
  416. }
  417. if (field_storage == MG_FORM_FIELD_STORAGE_GET) {
  418. #if 0
  419. if (!end_of_key_value_pair_found && !all_data_read) {
  420. /* This callback will deliver partial contents */
  421. }
  422. #endif
  423. /* Call callback */
  424. r = url_encoded_field_get(conn,
  425. ((get_block > 0) ? NULL : buf),
  426. ((get_block > 0)
  427. ? 0
  428. : (size_t)keylen),
  429. val,
  430. (size_t *)&vallen,
  431. fdh);
  432. get_block++;
  433. if (r == MG_FORM_FIELD_HANDLE_ABORT) {
  434. /* Stop request handling */
  435. break;
  436. }
  437. if (r == MG_FORM_FIELD_HANDLE_NEXT) {
  438. /* Skip to next field */
  439. field_storage = MG_FORM_FIELD_STORAGE_SKIP;
  440. }
  441. }
  442. if (next) {
  443. next++;
  444. } else {
  445. /* vallen may have been modified by url_encoded_field_get */
  446. next = val + vallen;
  447. }
  448. #if !defined(NO_FILESYSTEMS)
  449. if (fstore.access.fp) {
  450. size_t n = (size_t)
  451. fwrite(val, 1, (size_t)vallen, fstore.access.fp);
  452. if ((n != (size_t)vallen) || (ferror(fstore.access.fp))) {
  453. mg_cry_internal(conn,
  454. "%s: Cannot write file %s",
  455. __func__,
  456. path);
  457. mg_fclose(&fstore.access);
  458. remove_bad_file(conn, path);
  459. }
  460. file_size += (int64_t)n;
  461. }
  462. #endif /* NO_FILESYSTEMS */
  463. if (!end_of_key_value_pair_found) {
  464. used = next - buf;
  465. memmove(buf,
  466. buf + (size_t)used,
  467. sizeof(buf) - (size_t)used);
  468. next = buf;
  469. buf_fill -= used;
  470. if (buf_fill < (sizeof(buf) - 1)) {
  471. size_t to_read = sizeof(buf) - 1 - buf_fill;
  472. r = mg_read(conn, buf + buf_fill, to_read);
  473. if ((r < 0) || ((r == 0) && all_data_read)) {
  474. #if !defined(NO_FILESYSTEMS)
  475. /* read error */
  476. if (fstore.access.fp) {
  477. mg_fclose(&fstore.access);
  478. remove_bad_file(conn, path);
  479. }
  480. return -1;
  481. #endif /* NO_FILESYSTEMS */
  482. }
  483. if (r == 0) {
  484. /* TODO: Create a function to get "all_data_read"
  485. * from the conn object. All data is read if the
  486. * Content-Length has been reached, or if chunked
  487. * encoding is used and the end marker has been
  488. * read, or if the connection has been closed. */
  489. all_data_read = (buf_fill == 0);
  490. }
  491. buf_fill += r;
  492. buf[buf_fill] = 0;
  493. if (buf_fill < 1) {
  494. break;
  495. }
  496. val = buf;
  497. }
  498. }
  499. } while (!end_of_key_value_pair_found);
  500. #if !defined(NO_FILESYSTEMS)
  501. if (fstore.access.fp) {
  502. r = mg_fclose(&fstore.access);
  503. if (r == 0) {
  504. /* stored successfully */
  505. r = field_stored(conn, path, file_size, fdh);
  506. if (r == MG_FORM_FIELD_HANDLE_ABORT) {
  507. /* Stop request handling */
  508. break;
  509. }
  510. } else {
  511. mg_cry_internal(conn,
  512. "%s: Error saving file %s",
  513. __func__,
  514. path);
  515. remove_bad_file(conn, path);
  516. }
  517. fstore.access.fp = NULL;
  518. }
  519. #endif /* NO_FILESYSTEMS */
  520. if (all_data_read && (buf_fill == 0)) {
  521. /* nothing more to process */
  522. break;
  523. }
  524. /* Proceed to next entry */
  525. used = next - buf;
  526. memmove(buf, buf + (size_t)used, sizeof(buf) - (size_t)used);
  527. buf_fill -= used;
  528. }
  529. return field_count;
  530. }
  531. if (!mg_strncasecmp(content_type, "MULTIPART/FORM-DATA;", 20)) {
  532. /* The form data is in the request body data, encoded as multipart
  533. * content (see https://www.ietf.org/rfc/rfc1867.txt,
  534. * https://www.ietf.org/rfc/rfc2388.txt). */
  535. char *boundary;
  536. size_t bl;
  537. ptrdiff_t used;
  538. struct mg_request_info part_header;
  539. char *hbuf;
  540. const char *content_disp, *hend, *fbeg, *fend, *nbeg, *nend;
  541. const char *next;
  542. unsigned part_no;
  543. int all_data_read = 0;
  544. memset(&part_header, 0, sizeof(part_header));
  545. /* Skip all spaces between MULTIPART/FORM-DATA; and BOUNDARY= */
  546. bl = 20;
  547. while (content_type[bl] == ' ') {
  548. bl++;
  549. }
  550. /* There has to be a BOUNDARY definition in the Content-Type header */
  551. if (mg_strncasecmp(content_type + bl, "BOUNDARY=", 9)) {
  552. /* Malformed request */
  553. return -1;
  554. }
  555. /* Copy boundary string to variable "boundary" */
  556. /* fbeg is pointer to start of value of boundary */
  557. fbeg = content_type + bl + 9;
  558. bl = strlen(fbeg);
  559. boundary = (char *)mg_malloc(bl + 1);
  560. if (!boundary) {
  561. /* Out of memory */
  562. mg_cry_internal(conn,
  563. "%s: Cannot allocate memory for boundary [%lu]",
  564. __func__,
  565. (unsigned long)bl);
  566. return -1;
  567. }
  568. memcpy(boundary, fbeg, bl);
  569. boundary[bl] = 0;
  570. /* RFC 2046 permits the boundary string to be quoted. */
  571. /* If the boundary is quoted, trim the quotes */
  572. if (boundary[0] == '"') {
  573. hbuf = strchr(boundary + 1, '"');
  574. if ((!hbuf) || (*hbuf != '"')) {
  575. /* Malformed request */
  576. mg_free(boundary);
  577. return -1;
  578. }
  579. *hbuf = 0;
  580. memmove(boundary, boundary + 1, bl);
  581. bl = strlen(boundary);
  582. }
  583. /* Do some sanity checks for boundary lengths */
  584. if (bl > 70) {
  585. /* From RFC 2046:
  586. * Boundary delimiters must not appear within the
  587. * encapsulated material, and must be no longer
  588. * than 70 characters, not counting the two
  589. * leading hyphens.
  590. */
  591. /* The algorithm can not work if bl >= sizeof(buf), or if buf
  592. * can not hold the multipart header plus the boundary.
  593. * Requests with long boundaries are not RFC compliant, maybe they
  594. * are intended attacks to interfere with this algorithm. */
  595. mg_free(boundary);
  596. return -1;
  597. }
  598. if (bl < 4) {
  599. /* Sanity check: A boundary string of less than 4 bytes makes
  600. * no sense either. */
  601. mg_free(boundary);
  602. return -1;
  603. }
  604. for (part_no = 0;; part_no++) {
  605. size_t towrite, fnlen, n;
  606. int get_block;
  607. size_t to_read = sizeof(buf) - 1 - buf_fill;
  608. /* Unused without filesystems */
  609. (void)n;
  610. r = mg_read(conn, buf + buf_fill, to_read);
  611. if ((r < 0) || ((r == 0) && all_data_read)) {
  612. /* read error */
  613. mg_free(boundary);
  614. return -1;
  615. }
  616. if (r == 0) {
  617. all_data_read = (buf_fill == 0);
  618. }
  619. buf_fill += r;
  620. buf[buf_fill] = 0;
  621. if (buf_fill < 1) {
  622. /* No data */
  623. mg_free(boundary);
  624. return -1;
  625. }
  626. /* @see https://www.rfc-editor.org/rfc/rfc2046.html#section-5.1.1
  627. *
  628. * multipart-body := [preamble CRLF]
  629. * dash-boundary transport-padding CRLF
  630. * body-part *encapsulation
  631. * close-delimiter transport-padding
  632. * [CRLF epilogue]
  633. */
  634. if (part_no == 0) {
  635. size_t preamble_length = 0;
  636. /* skip over the preamble until we find a complete boundary
  637. * limit the preamble length to prevent abuse */
  638. /* +2 for the -- preceding the boundary */
  639. while (preamble_length < 1024
  640. && (preamble_length < buf_fill - bl)
  641. && strncmp(buf + preamble_length + 2, boundary, bl)) {
  642. preamble_length++;
  643. }
  644. /* reset the start of buf to remove the preamble */
  645. if (0 == strncmp(buf + preamble_length + 2, boundary, bl)) {
  646. memmove(buf,
  647. buf + preamble_length,
  648. (unsigned)buf_fill - (unsigned)preamble_length);
  649. buf_fill -= preamble_length;
  650. buf[buf_fill] = 0;
  651. }
  652. }
  653. /* either it starts with a boundary and it's fine, or it's malformed
  654. * because:
  655. * - the preamble was longer than accepted
  656. * - couldn't find a boundary at all in the body
  657. * - didn't have a terminating boundary */
  658. if (buf_fill < (bl + 2) || strncmp(buf, "--", 2)
  659. || strncmp(buf + 2, boundary, bl)) {
  660. /* Malformed request */
  661. mg_free(boundary);
  662. return -1;
  663. }
  664. /* skip the -- */
  665. char *boundary_start = buf + 2;
  666. size_t transport_padding = 0;
  667. while (boundary_start[bl + transport_padding] == ' '
  668. || boundary_start[bl + transport_padding] == '\t') {
  669. transport_padding++;
  670. }
  671. char *boundary_end = boundary_start + bl + transport_padding;
  672. /* after the transport padding, if the boundary isn't
  673. * immediately followed by a \r\n then it is either... */
  674. if (strncmp(boundary_end, "\r\n", 2))
  675. {
  676. /* ...the final boundary, and it is followed by --, (in which
  677. * case it's the end of the request) or it's a malformed
  678. * request */
  679. if (strncmp(boundary_end, "--", 2)) {
  680. /* Malformed request */
  681. mg_free(boundary);
  682. return -1;
  683. }
  684. /* Ingore any epilogue here */
  685. break;
  686. }
  687. /* skip the \r\n */
  688. hbuf = boundary_end + 2;
  689. /* Next, we need to get the part header: Read until \r\n\r\n */
  690. hend = strstr(hbuf, "\r\n\r\n");
  691. if (!hend) {
  692. /* Malformed request */
  693. mg_free(boundary);
  694. return -1;
  695. }
  696. part_header.num_headers =
  697. parse_http_headers(&hbuf, part_header.http_headers);
  698. if ((hend + 2) != hbuf) {
  699. /* Malformed request */
  700. mg_free(boundary);
  701. return -1;
  702. }
  703. /* Skip \r\n\r\n */
  704. hend += 4;
  705. /* According to the RFC, every part has to have a header field like:
  706. * Content-Disposition: form-data; name="..." */
  707. content_disp = get_header(part_header.http_headers,
  708. part_header.num_headers,
  709. "Content-Disposition");
  710. if (!content_disp) {
  711. /* Malformed request */
  712. mg_free(boundary);
  713. return -1;
  714. }
  715. /* Get the mandatory name="..." part of the Content-Disposition
  716. * header. */
  717. nbeg = strstr(content_disp, "name=\"");
  718. while ((nbeg != NULL) && (strcspn(nbeg - 1, ":,; \t") != 0)) {
  719. /* It could be somethingname= instead of name= */
  720. nbeg = strstr(nbeg + 1, "name=\"");
  721. }
  722. /* This line is not required, but otherwise some compilers
  723. * generate spurious warnings. */
  724. nend = nbeg;
  725. /* And others complain, the result is unused. */
  726. (void)nend;
  727. /* If name=" is found, search for the closing " */
  728. if (nbeg) {
  729. nbeg += 6;
  730. nend = strchr(nbeg, '\"');
  731. if (!nend) {
  732. /* Malformed request */
  733. mg_free(boundary);
  734. return -1;
  735. }
  736. } else {
  737. /* name= without quotes is also allowed */
  738. nbeg = strstr(content_disp, "name=");
  739. while ((nbeg != NULL) && (strcspn(nbeg - 1, ":,; \t") != 0)) {
  740. /* It could be somethingname= instead of name= */
  741. nbeg = strstr(nbeg + 1, "name=");
  742. }
  743. if (!nbeg) {
  744. /* Malformed request */
  745. mg_free(boundary);
  746. return -1;
  747. }
  748. nbeg += 5;
  749. /* RFC 2616 Sec. 2.2 defines a list of allowed
  750. * separators, but many of them make no sense
  751. * here, e.g. various brackets or slashes.
  752. * If they are used, probably someone is
  753. * trying to attack with curious hand made
  754. * requests. Only ; , space and tab seem to be
  755. * reasonable here. Ignore everything else. */
  756. nend = nbeg + strcspn(nbeg, ",; \t");
  757. }
  758. /* Get the optional filename="..." part of the Content-Disposition
  759. * header. */
  760. fbeg = strstr(content_disp, "filename=\"");
  761. while ((fbeg != NULL) && (strcspn(fbeg - 1, ":,; \t") != 0)) {
  762. /* It could be somethingfilename= instead of filename= */
  763. fbeg = strstr(fbeg + 1, "filename=\"");
  764. }
  765. /* This line is not required, but otherwise some compilers
  766. * generate spurious warnings. */
  767. fend = fbeg;
  768. /* If filename=" is found, search for the closing " */
  769. if (fbeg) {
  770. fbeg += 10;
  771. fend = strchr(fbeg, '\"');
  772. if (!fend) {
  773. /* Malformed request (the filename field is optional, but if
  774. * it exists, it needs to be terminated correctly). */
  775. mg_free(boundary);
  776. return -1;
  777. }
  778. /* TODO: check Content-Type */
  779. /* Content-Type: application/octet-stream */
  780. }
  781. if (!fbeg) {
  782. /* Try the same without quotes */
  783. fbeg = strstr(content_disp, "filename=");
  784. while ((fbeg != NULL) && (strcspn(fbeg - 1, ":,; \t") != 0)) {
  785. /* It could be somethingfilename= instead of filename= */
  786. fbeg = strstr(fbeg + 1, "filename=");
  787. }
  788. if (fbeg) {
  789. fbeg += 9;
  790. fend = fbeg + strcspn(fbeg, ",; \t");
  791. }
  792. }
  793. if (!fbeg || !fend) {
  794. fbeg = NULL;
  795. fend = NULL;
  796. fnlen = 0;
  797. } else {
  798. fnlen = (size_t)(fend - fbeg);
  799. }
  800. /* In theory, it could be possible that someone crafts
  801. * a request like name=filename=xyz. Check if name and
  802. * filename do not overlap. */
  803. if (!(((ptrdiff_t)fbeg > (ptrdiff_t)nend)
  804. || ((ptrdiff_t)nbeg > (ptrdiff_t)fend))) {
  805. mg_free(boundary);
  806. return -1;
  807. }
  808. /* Call callback for new field */
  809. memset(path, 0, sizeof(path));
  810. field_count++;
  811. field_storage = url_encoded_field_found(conn,
  812. nbeg,
  813. (size_t)(nend - nbeg),
  814. ((fnlen > 0) ? fbeg : NULL),
  815. fnlen,
  816. path,
  817. sizeof(path) - 1,
  818. fdh);
  819. /* If the boundary is already in the buffer, get the address,
  820. * otherwise next will be NULL. */
  821. next = search_boundary(hbuf,
  822. (size_t)((buf - hbuf) + buf_fill),
  823. boundary,
  824. bl);
  825. #if !defined(NO_FILESYSTEMS)
  826. if (field_storage == MG_FORM_FIELD_STORAGE_STORE) {
  827. /* Store the content to a file */
  828. if (mg_fopen(conn, path, MG_FOPEN_MODE_WRITE, &fstore) == 0) {
  829. fstore.access.fp = NULL;
  830. }
  831. file_size = 0;
  832. if (!fstore.access.fp) {
  833. mg_cry_internal(conn,
  834. "%s: Cannot create file %s",
  835. __func__,
  836. path);
  837. }
  838. }
  839. #endif /* NO_FILESYSTEMS */
  840. get_block = 0;
  841. while (!next) {
  842. /* Set "towrite" to the number of bytes available
  843. * in the buffer */
  844. towrite = (size_t)(buf - hend + buf_fill);
  845. if (towrite < bl + 4) {
  846. /* Not enough data stored. */
  847. /* Incomplete request. */
  848. mg_free(boundary);
  849. return -1;
  850. }
  851. /* Subtract the boundary length, to deal with
  852. * cases the boundary is only partially stored
  853. * in the buffer. */
  854. towrite -= bl + 4;
  855. if (field_storage == MG_FORM_FIELD_STORAGE_GET) {
  856. r = unencoded_field_get(conn,
  857. ((get_block > 0) ? NULL : nbeg),
  858. ((get_block > 0)
  859. ? 0
  860. : (size_t)(nend - nbeg)),
  861. hend,
  862. towrite,
  863. fdh);
  864. get_block++;
  865. if (r == MG_FORM_FIELD_HANDLE_ABORT) {
  866. /* Stop request handling */
  867. break;
  868. }
  869. if (r == MG_FORM_FIELD_HANDLE_NEXT) {
  870. /* Skip to next field */
  871. field_storage = MG_FORM_FIELD_STORAGE_SKIP;
  872. }
  873. }
  874. #if !defined(NO_FILESYSTEMS)
  875. if (field_storage == MG_FORM_FIELD_STORAGE_STORE) {
  876. if (fstore.access.fp) {
  877. /* Store the content of the buffer. */
  878. n = (size_t)fwrite(hend, 1, towrite, fstore.access.fp);
  879. if ((n != towrite) || (ferror(fstore.access.fp))) {
  880. mg_cry_internal(conn,
  881. "%s: Cannot write file %s",
  882. __func__,
  883. path);
  884. mg_fclose(&fstore.access);
  885. remove_bad_file(conn, path);
  886. }
  887. file_size += (int64_t)n;
  888. }
  889. }
  890. #endif /* NO_FILESYSTEMS */
  891. memmove(buf, hend + towrite, bl + 4);
  892. buf_fill = bl + 4;
  893. hend = buf;
  894. /* Read new data */
  895. to_read = sizeof(buf) - 1 - buf_fill;
  896. r = mg_read(conn, buf + buf_fill, to_read);
  897. if ((r < 0) || ((r == 0) && all_data_read)) {
  898. #if !defined(NO_FILESYSTEMS)
  899. /* read error */
  900. if (fstore.access.fp) {
  901. mg_fclose(&fstore.access);
  902. remove_bad_file(conn, path);
  903. }
  904. #endif /* NO_FILESYSTEMS */
  905. mg_free(boundary);
  906. return -1;
  907. }
  908. /* r==0 already handled, all_data_read is false here */
  909. buf_fill += r;
  910. buf[buf_fill] = 0;
  911. /* buf_fill is at least 8 here */
  912. /* Find boundary */
  913. next = search_boundary(buf, buf_fill, boundary, bl);
  914. if (!next && (r == 0)) {
  915. /* incomplete request */
  916. all_data_read = 1;
  917. }
  918. }
  919. towrite = (next ? (size_t)(next - hend) : 0);
  920. if (field_storage == MG_FORM_FIELD_STORAGE_GET) {
  921. /* Call callback */
  922. r = unencoded_field_get(conn,
  923. ((get_block > 0) ? NULL : nbeg),
  924. ((get_block > 0)
  925. ? 0
  926. : (size_t)(nend - nbeg)),
  927. hend,
  928. towrite,
  929. fdh);
  930. if (r == MG_FORM_FIELD_HANDLE_ABORT) {
  931. /* Stop request handling */
  932. break;
  933. }
  934. if (r == MG_FORM_FIELD_HANDLE_NEXT) {
  935. /* Skip to next field */
  936. field_storage = MG_FORM_FIELD_STORAGE_SKIP;
  937. }
  938. }
  939. #if !defined(NO_FILESYSTEMS)
  940. if (field_storage == MG_FORM_FIELD_STORAGE_STORE) {
  941. if (fstore.access.fp) {
  942. n = (size_t)fwrite(hend, 1, towrite, fstore.access.fp);
  943. if ((n != towrite) || (ferror(fstore.access.fp))) {
  944. mg_cry_internal(conn,
  945. "%s: Cannot write file %s",
  946. __func__,
  947. path);
  948. mg_fclose(&fstore.access);
  949. remove_bad_file(conn, path);
  950. } else {
  951. file_size += (int64_t)n;
  952. r = mg_fclose(&fstore.access);
  953. if (r == 0) {
  954. /* stored successfully */
  955. r = field_stored(conn, path, file_size, fdh);
  956. if (r == MG_FORM_FIELD_HANDLE_ABORT) {
  957. /* Stop request handling */
  958. break;
  959. }
  960. } else {
  961. mg_cry_internal(conn,
  962. "%s: Error saving file %s",
  963. __func__,
  964. path);
  965. remove_bad_file(conn, path);
  966. }
  967. }
  968. fstore.access.fp = NULL;
  969. }
  970. }
  971. #endif /* NO_FILESYSTEMS */
  972. if ((field_storage & MG_FORM_FIELD_STORAGE_ABORT)
  973. == MG_FORM_FIELD_STORAGE_ABORT) {
  974. /* Stop parsing the request */
  975. break;
  976. }
  977. /* Remove from the buffer */
  978. if (next) {
  979. used = next - buf + 2;
  980. memmove(buf, buf + (size_t)used, sizeof(buf) - (size_t)used);
  981. buf_fill -= used;
  982. } else {
  983. buf_fill = 0;
  984. }
  985. }
  986. /* All parts handled */
  987. mg_free(boundary);
  988. return field_count;
  989. }
  990. /* Unknown Content-Type */
  991. return -1;
  992. }
  993. /* End of handle_form.inl */