diff options
| author | Dirk Engling <erdgeist@erdgeist.org> | 2026-11-06 18:27:44 +0100 |
|---|---|---|
| committer | Dirk Engling <erdgeist@erdgeist.org> | 2026-11-06 18:27:44 +0100 |
| commit | 92c27bbda9e809683537e5cc48adad0d5c390587 (patch) | |
| tree | 51c1de9d3c50e571d6a3d23857cd4198c0958d2f /src | |
| parent | 355c8ae57d582974f4f6a4ba42f635f2253b84a7 (diff) | |
Gives urlencoded_parse a destination bound
Diffstat (limited to 'src')
| -rw-r--r-- | src/ot_http.c | 34 | ||||
| -rw-r--r-- | src/ot_urlencoded.c | 12 | ||||
| -rw-r--r-- | src/ot_urlencoded.h | 4 |
3 files changed, 28 insertions, 22 deletions
diff --git a/src/ot_http.c b/src/ot_http.c index 29f41a3..fc49333 100644 --- a/src/ot_http.c +++ b/src/ot_http.c | |||
| @@ -313,7 +313,7 @@ static ssize_t http_handle_stats(const int64_t sock, struct ot_workstruct *ws, c | |||
| 313 | #endif | 313 | #endif |
| 314 | 314 | ||
| 315 | while (scanon) { | 315 | while (scanon) { |
| 316 | switch (urlencoded_match_keyword(keywords_main, &read_ptr, SCAN_SEARCHPATH_PARAM)) { | 316 | switch (urlencoded_match_keyword(keywords_main, &read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_PARAM)) { |
| 317 | case -2: | 317 | case -2: |
| 318 | scanon = 0; | 318 | scanon = 0; |
| 319 | break; /* TERMINATOR */ | 319 | break; /* TERMINATOR */ |
| @@ -323,11 +323,11 @@ static ssize_t http_handle_stats(const int64_t sock, struct ot_workstruct *ws, c | |||
| 323 | urlencoded_skip_value(&read_ptr); | 323 | urlencoded_skip_value(&read_ptr); |
| 324 | break; | 324 | break; |
| 325 | case 1: /* matched "mode" */ | 325 | case 1: /* matched "mode" */ |
| 326 | if ((mode = urlencoded_match_keyword(keywords_mode, &read_ptr, SCAN_SEARCHPATH_VALUE)) <= 0) | 326 | if ((mode = urlencoded_match_keyword(keywords_mode, &read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_VALUE)) <= 0) |
| 327 | HTTPERROR_400_PARAM; | 327 | HTTPERROR_400_PARAM; |
| 328 | break; | 328 | break; |
| 329 | case 2: /* matched "format" */ | 329 | case 2: /* matched "format" */ |
| 330 | if ((format = urlencoded_match_keyword(keywords_format, &read_ptr, SCAN_SEARCHPATH_VALUE)) <= 0) | 330 | if ((format = urlencoded_match_keyword(keywords_format, &read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_VALUE)) <= 0) |
| 331 | HTTPERROR_400_PARAM; | 331 | HTTPERROR_400_PARAM; |
| 332 | break; | 332 | break; |
| 333 | case 3: | 333 | case 3: |
| @@ -505,7 +505,7 @@ static ssize_t http_handle_scrape(const int64_t sock, struct ot_workstruct *ws, | |||
| 505 | } | 505 | } |
| 506 | 506 | ||
| 507 | while (scanon) { | 507 | while (scanon) { |
| 508 | switch (urlencoded_match_keyword(keywords_scrape, &read_ptr, SCAN_SEARCHPATH_PARAM)) { | 508 | switch (urlencoded_match_keyword(keywords_scrape, &read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_PARAM)) { |
| 509 | case -2: | 509 | case -2: |
| 510 | scanon = 0; | 510 | scanon = 0; |
| 511 | break; /* TERMINATOR */ | 511 | break; /* TERMINATOR */ |
| @@ -515,10 +515,12 @@ static ssize_t http_handle_scrape(const int64_t sock, struct ot_workstruct *ws, | |||
| 515 | urlencoded_skip_value(&read_ptr); | 515 | urlencoded_skip_value(&read_ptr); |
| 516 | break; | 516 | break; |
| 517 | case 1: /* matched "info_hash" */ | 517 | case 1: /* matched "info_hash" */ |
| 518 | { | ||
| 518 | /* ignore this, when we have less than 20 bytes */ | 519 | /* ignore this, when we have less than 20 bytes */ |
| 519 | if (urlencoded_parse(&read_ptr, (char *)(multiscrape_buf + numwant++), SCAN_SEARCHPATH_VALUE) != (ssize_t)sizeof(ot_hash)) | 520 | ot_hash *slot = multiscrape_buf + numwant++; |
| 521 | if (urlencoded_parse(&read_ptr, (char *)slot, (char *)(slot + 1), SCAN_SEARCHPATH_VALUE) != (ssize_t)sizeof(ot_hash)) | ||
| 520 | HTTPERROR_400_PARAM; | 522 | HTTPERROR_400_PARAM; |
| 521 | break; | 523 | } break; |
| 522 | } | 524 | } |
| 523 | } | 525 | } |
| 524 | 526 | ||
| @@ -622,7 +624,7 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct | |||
| 622 | scanon = 1; | 624 | scanon = 1; |
| 623 | 625 | ||
| 624 | while (scanon) { | 626 | while (scanon) { |
| 625 | switch (urlencoded_match_keyword(keywords_announce, &read_ptr, SCAN_SEARCHPATH_PARAM)) { | 627 | switch (urlencoded_match_keyword(keywords_announce, &read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_PARAM)) { |
| 626 | case -2: | 628 | case -2: |
| 627 | scanon = 0; | 629 | scanon = 0; |
| 628 | break; /* TERMINATOR */ | 630 | break; /* TERMINATOR */ |
| @@ -632,14 +634,14 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct | |||
| 632 | urlencoded_skip_value(&read_ptr); | 634 | urlencoded_skip_value(&read_ptr); |
| 633 | break; | 635 | break; |
| 634 | case 1: /* matched "port" */ | 636 | case 1: /* matched "port" */ |
| 635 | len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE); | 637 | len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_VALUE); |
| 636 | if ((len <= 0) || scan_int(write_ptr, len, &tmp) || (tmp > 0xffff)) | 638 | if ((len <= 0) || scan_int(write_ptr, len, &tmp) || (tmp > 0xffff)) |
| 637 | HTTPERROR_400_PARAM; | 639 | HTTPERROR_400_PARAM; |
| 638 | port = htons((uint16_t)tmp); | 640 | port = htons((uint16_t)tmp); |
| 639 | OT_SETPORT(&ws->peer, &port); | 641 | OT_SETPORT(&ws->peer, &port); |
| 640 | break; | 642 | break; |
| 641 | case 2: /* matched "left" */ | 643 | case 2: /* matched "left" */ |
| 642 | if ((len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE)) <= 0) | 644 | if ((len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_VALUE)) <= 0) |
| 643 | HTTPERROR_400_PARAM; | 645 | HTTPERROR_400_PARAM; |
| 644 | if (scan_int(write_ptr, len, &tmp)) | 646 | if (scan_int(write_ptr, len, &tmp)) |
| 645 | tmp = 0; | 647 | tmp = 0; |
| @@ -647,7 +649,7 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct | |||
| 647 | OT_PEERFLAG(&ws->peer) |= PEER_FLAG_SEEDING; | 649 | OT_PEERFLAG(&ws->peer) |= PEER_FLAG_SEEDING; |
| 648 | break; | 650 | break; |
| 649 | case 3: /* matched "event" */ | 651 | case 3: /* matched "event" */ |
| 650 | switch (urlencoded_match_keyword(keywords_announce_event, &read_ptr, SCAN_SEARCHPATH_VALUE)) { | 652 | switch (urlencoded_match_keyword(keywords_announce_event, &read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_VALUE)) { |
| 651 | case -1: | 653 | case -1: |
| 652 | HTTPERROR_400_PARAM; | 654 | HTTPERROR_400_PARAM; |
| 653 | case 1: /* matched "completed" */ | 655 | case 1: /* matched "completed" */ |
| @@ -661,7 +663,7 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct | |||
| 661 | } | 663 | } |
| 662 | break; | 664 | break; |
| 663 | case 4: /* matched "numwant" */ | 665 | case 4: /* matched "numwant" */ |
| 664 | len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE); | 666 | len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_VALUE); |
| 665 | if ((len <= 0) || scan_int(write_ptr, len, &tmp)) | 667 | if ((len <= 0) || scan_int(write_ptr, len, &tmp)) |
| 666 | HTTPERROR_400_PARAM; | 668 | HTTPERROR_400_PARAM; |
| 667 | if (tmp < 0) | 669 | if (tmp < 0) |
| @@ -671,7 +673,7 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct | |||
| 671 | numwant = (size_t)tmp; | 673 | numwant = (size_t)tmp; |
| 672 | break; | 674 | break; |
| 673 | case 5: /* matched "compact" */ | 675 | case 5: /* matched "compact" */ |
| 674 | len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE); | 676 | len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_VALUE); |
| 675 | if ((len <= 0) || scan_int(write_ptr, len, &tmp)) | 677 | if ((len <= 0) || scan_int(write_ptr, len, &tmp)) |
| 676 | HTTPERROR_400_PARAM; | 678 | HTTPERROR_400_PARAM; |
| 677 | if (!tmp) | 679 | if (!tmp) |
| @@ -681,7 +683,7 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct | |||
| 681 | if (ws->hash) | 683 | if (ws->hash) |
| 682 | HTTPERROR_400_DOUBLEHASH; | 684 | HTTPERROR_400_DOUBLEHASH; |
| 683 | /* ignore this, when we have less than 20 bytes */ | 685 | /* ignore this, when we have less than 20 bytes */ |
| 684 | if (urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE) != 20) | 686 | if (urlencoded_parse(&read_ptr, write_ptr = read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_VALUE) != 20) |
| 685 | HTTPERROR_400_PARAM; | 687 | HTTPERROR_400_PARAM; |
| 686 | ws->hash = (ot_hash *)write_ptr; | 688 | ws->hash = (ot_hash *)write_ptr; |
| 687 | break; | 689 | break; |
| @@ -689,7 +691,7 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct | |||
| 689 | case 7: /* matched "ip" */ | 691 | case 7: /* matched "ip" */ |
| 690 | { | 692 | { |
| 691 | char *tmp_buf1 = ws->reply, *tmp_buf2 = ws->reply + 16; | 693 | char *tmp_buf1 = ws->reply, *tmp_buf2 = ws->reply + 16; |
| 692 | len = urlencoded_parse(&read_ptr, tmp_buf2, SCAN_SEARCHPATH_VALUE); | 694 | len = urlencoded_parse(&read_ptr, tmp_buf2, ws->outbuf + G_OUTBUF_SIZE, SCAN_SEARCHPATH_VALUE); |
| 693 | tmp_buf2[len] = 0; | 695 | tmp_buf2[len] = 0; |
| 694 | if ((len <= 0) || ip_parse(tmp_buf2, tmp_buf1) != (size_t)len) | 696 | if ((len <= 0) || ip_parse(tmp_buf2, tmp_buf1) != (size_t)len) |
| 695 | HTTPERROR_400_PARAM; | 697 | HTTPERROR_400_PARAM; |
| @@ -698,7 +700,7 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct | |||
| 698 | #endif | 700 | #endif |
| 699 | case 8: /* matched "peer_id" */ | 701 | case 8: /* matched "peer_id" */ |
| 700 | /* ignore this, when we have less than 20 bytes */ | 702 | /* ignore this, when we have less than 20 bytes */ |
| 701 | if (urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE) != 20) | 703 | if (urlencoded_parse(&read_ptr, write_ptr = read_ptr, ws->request + ws->request_size, SCAN_SEARCHPATH_VALUE) != 20) |
| 702 | HTTPERROR_400_PARAM; | 704 | HTTPERROR_400_PARAM; |
| 703 | ws->peer_id = write_ptr; | 705 | ws->peer_id = write_ptr; |
| 704 | break; | 706 | break; |
| @@ -772,7 +774,7 @@ ssize_t http_handle_request(const int64_t sock, struct ot_workstruct *ws) { | |||
| 772 | In reality we abandoned requiring the url to be correct. This now | 774 | In reality we abandoned requiring the url to be correct. This now |
| 773 | only decodes url encoded characters, we check for announces and | 775 | only decodes url encoded characters, we check for announces and |
| 774 | scrapes by looking for "a*" or "sc" */ | 776 | scrapes by looking for "a*" or "sc" */ |
| 775 | len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_PATH); | 777 | len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, ws->request + ws->request_size, SCAN_PATH); |
| 776 | 778 | ||
| 777 | /* If parsing returned an error, leave with not found */ | 779 | /* If parsing returned an error, leave with not found */ |
| 778 | if (g_redirecturl && (len == -2)) | 780 | if (g_redirecturl && (len == -2)) |
diff --git a/src/ot_urlencoded.c b/src/ot_urlencoded.c index 4109bb5..c977adb 100644 --- a/src/ot_urlencoded.c +++ b/src/ot_urlencoded.c | |||
| @@ -68,17 +68,18 @@ void urlencoded_skip_value(char **string) { | |||
| 68 | *string = (char *)s; | 68 | *string = (char *)s; |
| 69 | } | 69 | } |
| 70 | 70 | ||
| 71 | int urlencoded_match_keyword(const ot_keywords *keywords, char **string, SCAN_SEARCHPATH_FLAG flags) { | 71 | int urlencoded_match_keyword(const ot_keywords *keywords, char **string, const char *deststring_end, SCAN_SEARCHPATH_FLAG flags) { |
| 72 | char *deststring = *string; | 72 | char *deststring = *string; |
| 73 | ssize_t match_length = urlencoded_parse(string, deststring, flags); | 73 | ssize_t match_length = urlencoded_parse(string, deststring, deststring_end, flags); |
| 74 | 74 | ||
| 75 | if (match_length < 0) | 75 | if (match_length < 0) |
| 76 | return match_length; | 76 | return match_length; |
| 77 | if (match_length == 0) | 77 | if (match_length == 0) |
| 78 | return -3; | 78 | return -3; |
| 79 | 79 | ||
| 80 | /* Compare on the exact decoded length. */ | ||
| 80 | while (keywords->key) { | 81 | while (keywords->key) { |
| 81 | if (!strncmp(keywords->key, deststring, match_length) && !keywords->key[match_length]) | 82 | if ((size_t)match_length == strlen(keywords->key) && !memcmp(keywords->key, deststring, (size_t)match_length)) |
| 82 | return keywords->value; | 83 | return keywords->value; |
| 83 | keywords++; | 84 | keywords++; |
| 84 | } | 85 | } |
| @@ -86,7 +87,7 @@ int urlencoded_match_keyword(const ot_keywords *keywords, char **string, SCAN_SE | |||
| 86 | return -3; | 87 | return -3; |
| 87 | } | 88 | } |
| 88 | 89 | ||
| 89 | ssize_t urlencoded_parse(char **string, char *deststring, SCAN_SEARCHPATH_FLAG flags) { | 90 | ssize_t urlencoded_parse(char **string, char *deststring, const char *deststring_end, SCAN_SEARCHPATH_FLAG flags) { |
| 90 | const unsigned char *s = *(const unsigned char **)string; | 91 | const unsigned char *s = *(const unsigned char **)string; |
| 91 | unsigned char *d = (unsigned char *)deststring; | 92 | unsigned char *d = (unsigned char *)deststring; |
| 92 | unsigned char b, c; | 93 | unsigned char b, c; |
| @@ -106,6 +107,9 @@ ssize_t urlencoded_parse(char **string, char *deststring, SCAN_SEARCHPATH_FLAG f | |||
| 106 | c |= (b << 4); | 107 | c |= (b << 4); |
| 107 | } | 108 | } |
| 108 | 109 | ||
| 110 | if ((const char *)d >= deststring_end) | ||
| 111 | return -1; | ||
| 112 | |||
| 109 | /* Write (possibly decoded) character to output */ | 113 | /* Write (possibly decoded) character to output */ |
| 110 | *d++ = c; | 114 | *d++ = c; |
| 111 | } | 115 | } |
diff --git a/src/ot_urlencoded.h b/src/ot_urlencoded.h index 9882468..053e079 100644 --- a/src/ot_urlencoded.h +++ b/src/ot_urlencoded.h | |||
| @@ -28,7 +28,7 @@ typedef enum { | |||
| 28 | or -1 for parse error | 28 | or -1 for parse error |
| 29 | or -2 for terminator found | 29 | or -2 for terminator found |
| 30 | */ | 30 | */ |
| 31 | ssize_t urlencoded_parse(char **string, char *deststring, SCAN_SEARCHPATH_FLAG flags); | 31 | ssize_t urlencoded_parse(char **string, char *deststring, const char *deststring_end, SCAN_SEARCHPATH_FLAG flags); |
| 32 | 32 | ||
| 33 | /* string in: pointer to source | 33 | /* string in: pointer to source |
| 34 | out: pointer to next scan position | 34 | out: pointer to next scan position |
| @@ -38,7 +38,7 @@ ssize_t urlencoded_parse(char **string, char *deststring, SCAN_SEARCHPATH_FLAG f | |||
| 38 | or -2 for terminator found | 38 | or -2 for terminator found |
| 39 | or -3 for no keyword matched | 39 | or -3 for no keyword matched |
| 40 | */ | 40 | */ |
| 41 | int urlencoded_match_keyword(const ot_keywords *keywords, char **string, SCAN_SEARCHPATH_FLAG flags); | 41 | int urlencoded_match_keyword(const ot_keywords *keywords, char **string, const char *deststring_end, SCAN_SEARCHPATH_FLAG flags); |
| 42 | 42 | ||
| 43 | /* string in: pointer to value of a param=value pair to skip | 43 | /* string in: pointer to value of a param=value pair to skip |
| 44 | out: pointer to next scan position on return | 44 | out: pointer to next scan position on return |
