|
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 struct string_list cookies_to_redact = STRING_LIST_INIT_DUP; |
18 | 20 | #if LIBCURL_VERSION_NUM >= 0x070a08
|
19 | 21 | long int git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
|
20 | 22 | #else
|
@@ -575,6 +577,54 @@ static void redact_sensitive_header(struct strbuf *header)
|
575 | 577 | /* Everything else is opaque and possibly sensitive */
|
576 | 578 | strbuf_setlen(header, sensitive_header - header->buf);
|
577 | 579 | strbuf_addstr(header, " <redacted>");
|
| 580 | + } else if (cookies_to_redact.nr && |
| 581 | + skip_prefix(header->buf, "Cookie:", &sensitive_header)) { |
| 582 | + struct strbuf redacted_header = STRBUF_INIT; |
| 583 | + char *cookie; |
| 584 | + |
| 585 | + while (isspace(*sensitive_header)) |
| 586 | + sensitive_header++; |
| 587 | + |
| 588 | + /* |
| 589 | + * The contents of header starting from sensitive_header will |
| 590 | + * subsequently be overridden, so it is fine to mutate this |
| 591 | + * string (hence the assignment to "char *"). |
| 592 | + */ |
| 593 | + cookie = (char *) sensitive_header; |
| 594 | + |
| 595 | + while (cookie) { |
| 596 | + char *equals; |
| 597 | + char *semicolon = strstr(cookie, "; "); |
| 598 | + if (semicolon) |
| 599 | + *semicolon = 0; |
| 600 | + equals = strchrnul(cookie, '='); |
| 601 | + if (!equals) { |
| 602 | + /* invalid cookie, just append and continue */ |
| 603 | + strbuf_addstr(&redacted_header, cookie); |
| 604 | + continue; |
| 605 | + } |
| 606 | + *equals = 0; /* temporarily set to NUL for lookup */ |
| 607 | + if (string_list_lookup(&cookies_to_redact, cookie)) { |
| 608 | + strbuf_addstr(&redacted_header, cookie); |
| 609 | + strbuf_addstr(&redacted_header, "=<redacted>"); |
| 610 | + } else { |
| 611 | + *equals = '='; |
| 612 | + strbuf_addstr(&redacted_header, cookie); |
| 613 | + } |
| 614 | + if (semicolon) { |
| 615 | + /* |
| 616 | + * There are more cookies. (Or, for some |
| 617 | + * reason, the input string ends in "; ".) |
| 618 | + */ |
| 619 | + strbuf_addstr(&redacted_header, "; "); |
| 620 | + cookie = semicolon + strlen("; "); |
| 621 | + } else { |
| 622 | + cookie = NULL; |
| 623 | + } |
| 624 | + } |
| 625 | + |
| 626 | + strbuf_setlen(header, sensitive_header - header->buf); |
| 627 | + strbuf_addbuf(header, &redacted_header); |
578 | 628 | }
|
579 | 629 | }
|
580 | 630 |
|
@@ -807,6 +857,11 @@ static CURL *get_curl_handle(void)
|
807 | 857 | if (getenv("GIT_CURL_VERBOSE"))
|
808 | 858 | curl_easy_setopt(result, CURLOPT_VERBOSE, 1L);
|
809 | 859 | setup_curl_trace(result);
|
| 860 | + if (getenv("GIT_REDACT_COOKIES")) { |
| 861 | + string_list_split(&cookies_to_redact, |
| 862 | + getenv("GIT_REDACT_COOKIES"), ',', -1); |
| 863 | + string_list_sort(&cookies_to_redact); |
| 864 | + } |
810 | 865 |
|
811 | 866 | curl_easy_setopt(result, CURLOPT_USERAGENT,
|
812 | 867 | user_agent ? user_agent : git_user_agent());
|
|
0 commit comments