|
13 | 13 | #include "transport.h"
|
14 | 14 | #include "packfile.h"
|
15 | 15 | #include "protocol.h"
|
| 16 | +#include "string-list.h" |
16 | 17 |
|
17 | 18 | static struct trace_key trace_curl = TRACE_KEY_INIT(CURL);
|
| 19 | +static int trace_curl_data = 1; |
| 20 | +static struct string_list cookies_to_redact = STRING_LIST_INIT_DUP; |
18 | 21 | #if LIBCURL_VERSION_NUM >= 0x070a08
|
19 | 22 | long int git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
|
20 | 23 | #else
|
@@ -575,6 +578,54 @@ static void redact_sensitive_header(struct strbuf *header)
|
575 | 578 | /* Everything else is opaque and possibly sensitive */
|
576 | 579 | strbuf_setlen(header, sensitive_header - header->buf);
|
577 | 580 | strbuf_addstr(header, " <redacted>");
|
| 581 | + } else if (cookies_to_redact.nr && |
| 582 | + skip_prefix(header->buf, "Cookie:", &sensitive_header)) { |
| 583 | + struct strbuf redacted_header = STRBUF_INIT; |
| 584 | + char *cookie; |
| 585 | + |
| 586 | + while (isspace(*sensitive_header)) |
| 587 | + sensitive_header++; |
| 588 | + |
| 589 | + /* |
| 590 | + * The contents of header starting from sensitive_header will |
| 591 | + * subsequently be overridden, so it is fine to mutate this |
| 592 | + * string (hence the assignment to "char *"). |
| 593 | + */ |
| 594 | + cookie = (char *) sensitive_header; |
| 595 | + |
| 596 | + while (cookie) { |
| 597 | + char *equals; |
| 598 | + char *semicolon = strstr(cookie, "; "); |
| 599 | + if (semicolon) |
| 600 | + *semicolon = 0; |
| 601 | + equals = strchrnul(cookie, '='); |
| 602 | + if (!equals) { |
| 603 | + /* invalid cookie, just append and continue */ |
| 604 | + strbuf_addstr(&redacted_header, cookie); |
| 605 | + continue; |
| 606 | + } |
| 607 | + *equals = 0; /* temporarily set to NUL for lookup */ |
| 608 | + if (string_list_lookup(&cookies_to_redact, cookie)) { |
| 609 | + strbuf_addstr(&redacted_header, cookie); |
| 610 | + strbuf_addstr(&redacted_header, "=<redacted>"); |
| 611 | + } else { |
| 612 | + *equals = '='; |
| 613 | + strbuf_addstr(&redacted_header, cookie); |
| 614 | + } |
| 615 | + if (semicolon) { |
| 616 | + /* |
| 617 | + * There are more cookies. (Or, for some |
| 618 | + * reason, the input string ends in "; ".) |
| 619 | + */ |
| 620 | + strbuf_addstr(&redacted_header, "; "); |
| 621 | + cookie = semicolon + strlen("; "); |
| 622 | + } else { |
| 623 | + cookie = NULL; |
| 624 | + } |
| 625 | + } |
| 626 | + |
| 627 | + strbuf_setlen(header, sensitive_header - header->buf); |
| 628 | + strbuf_addbuf(header, &redacted_header); |
578 | 629 | }
|
579 | 630 | }
|
580 | 631 |
|
@@ -645,24 +696,32 @@ static int curl_trace(CURL *handle, curl_infotype type, char *data, size_t size,
|
645 | 696 | curl_dump_header(text, (unsigned char *)data, size, DO_FILTER);
|
646 | 697 | break;
|
647 | 698 | case CURLINFO_DATA_OUT:
|
648 |
| - text = "=> Send data"; |
649 |
| - curl_dump_data(text, (unsigned char *)data, size); |
| 699 | + if (trace_curl_data) { |
| 700 | + text = "=> Send data"; |
| 701 | + curl_dump_data(text, (unsigned char *)data, size); |
| 702 | + } |
650 | 703 | break;
|
651 | 704 | case CURLINFO_SSL_DATA_OUT:
|
652 |
| - text = "=> Send SSL data"; |
653 |
| - curl_dump_data(text, (unsigned char *)data, size); |
| 705 | + if (trace_curl_data) { |
| 706 | + text = "=> Send SSL data"; |
| 707 | + curl_dump_data(text, (unsigned char *)data, size); |
| 708 | + } |
654 | 709 | break;
|
655 | 710 | case CURLINFO_HEADER_IN:
|
656 | 711 | text = "<= Recv header";
|
657 | 712 | curl_dump_header(text, (unsigned char *)data, size, NO_FILTER);
|
658 | 713 | break;
|
659 | 714 | case CURLINFO_DATA_IN:
|
660 |
| - text = "<= Recv data"; |
661 |
| - curl_dump_data(text, (unsigned char *)data, size); |
| 715 | + if (trace_curl_data) { |
| 716 | + text = "<= Recv data"; |
| 717 | + curl_dump_data(text, (unsigned char *)data, size); |
| 718 | + } |
662 | 719 | break;
|
663 | 720 | case CURLINFO_SSL_DATA_IN:
|
664 |
| - text = "<= Recv SSL data"; |
665 |
| - curl_dump_data(text, (unsigned char *)data, size); |
| 721 | + if (trace_curl_data) { |
| 722 | + text = "<= Recv SSL data"; |
| 723 | + curl_dump_data(text, (unsigned char *)data, size); |
| 724 | + } |
666 | 725 | break;
|
667 | 726 |
|
668 | 727 | default: /* we ignore unknown types by default */
|
@@ -807,6 +866,13 @@ static CURL *get_curl_handle(void)
|
807 | 866 | if (getenv("GIT_CURL_VERBOSE"))
|
808 | 867 | curl_easy_setopt(result, CURLOPT_VERBOSE, 1L);
|
809 | 868 | setup_curl_trace(result);
|
| 869 | + if (getenv("GIT_TRACE_CURL_NO_DATA")) |
| 870 | + trace_curl_data = 0; |
| 871 | + if (getenv("GIT_REDACT_COOKIES")) { |
| 872 | + string_list_split(&cookies_to_redact, |
| 873 | + getenv("GIT_REDACT_COOKIES"), ',', -1); |
| 874 | + string_list_sort(&cookies_to_redact); |
| 875 | + } |
810 | 876 |
|
811 | 877 | curl_easy_setopt(result, CURLOPT_USERAGENT,
|
812 | 878 | user_agent ? user_agent : git_user_agent());
|
|
0 commit comments