Skip to content

Commit 721dce0

Browse files
committed
Merge branch 'jt/http-redact-cookies' into maint
The http tracing code, often used to debug connection issues, learned to redact potentially sensitive information from its output so that it can be more safely sharable. * jt/http-redact-cookies: http: support omitting data from traces http: support cookie redaction when tracing
2 parents b322219 + 8ba18e6 commit 721dce0

File tree

3 files changed

+117
-8
lines changed

3 files changed

+117
-8
lines changed

Documentation/git.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,16 @@ of clones and fetches.
646646
variable.
647647
See `GIT_TRACE` for available trace output options.
648648

649+
`GIT_TRACE_CURL_NO_DATA`::
650+
When a curl trace is enabled (see `GIT_TRACE_CURL` above), do not dump
651+
data (that is, only dump info lines and headers).
652+
653+
`GIT_REDACT_COOKIES`::
654+
This can be set to a comma-separated list of strings. When a curl trace
655+
is enabled (see `GIT_TRACE_CURL` above), whenever a "Cookies:" header
656+
sent by the client is dumped, values of cookies whose key is in that
657+
list (case-sensitive) are redacted.
658+
649659
`GIT_LITERAL_PATHSPECS`::
650660
Setting this variable to `1` will cause Git to treat all
651661
pathspecs literally, rather than as glob patterns. For example,

http.c

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
#include "transport.h"
1414
#include "packfile.h"
1515
#include "protocol.h"
16+
#include "string-list.h"
1617

1718
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;
1821
#if LIBCURL_VERSION_NUM >= 0x070a08
1922
long int git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
2023
#else
@@ -575,6 +578,54 @@ static void redact_sensitive_header(struct strbuf *header)
575578
/* Everything else is opaque and possibly sensitive */
576579
strbuf_setlen(header, sensitive_header - header->buf);
577580
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);
578629
}
579630
}
580631

@@ -645,24 +696,32 @@ static int curl_trace(CURL *handle, curl_infotype type, char *data, size_t size,
645696
curl_dump_header(text, (unsigned char *)data, size, DO_FILTER);
646697
break;
647698
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+
}
650703
break;
651704
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+
}
654709
break;
655710
case CURLINFO_HEADER_IN:
656711
text = "<= Recv header";
657712
curl_dump_header(text, (unsigned char *)data, size, NO_FILTER);
658713
break;
659714
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+
}
662719
break;
663720
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+
}
666725
break;
667726

668727
default: /* we ignore unknown types by default */
@@ -807,6 +866,13 @@ static CURL *get_curl_handle(void)
807866
if (getenv("GIT_CURL_VERBOSE"))
808867
curl_easy_setopt(result, CURLOPT_VERBOSE, 1L);
809868
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+
}
810876

811877
curl_easy_setopt(result, CURLOPT_USERAGENT,
812878
user_agent ? user_agent : git_user_agent());

t/t5551-http-fetch-smart.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,5 +364,38 @@ test_expect_success 'custom http headers' '
364364
submodule update sub
365365
'
366366

367+
test_expect_success 'GIT_REDACT_COOKIES redacts cookies' '
368+
rm -rf clone &&
369+
echo "Set-Cookie: Foo=1" >cookies &&
370+
echo "Set-Cookie: Bar=2" >>cookies &&
371+
GIT_TRACE_CURL=true GIT_REDACT_COOKIES=Bar,Baz \
372+
git -c "http.cookieFile=$(pwd)/cookies" clone \
373+
$HTTPD_URL/smart/repo.git clone 2>err &&
374+
grep "Cookie:.*Foo=1" err &&
375+
grep "Cookie:.*Bar=<redacted>" err &&
376+
! grep "Cookie:.*Bar=2" err
377+
'
378+
379+
test_expect_success 'GIT_REDACT_COOKIES handles empty values' '
380+
rm -rf clone &&
381+
echo "Set-Cookie: Foo=" >cookies &&
382+
GIT_TRACE_CURL=true GIT_REDACT_COOKIES=Foo \
383+
git -c "http.cookieFile=$(pwd)/cookies" clone \
384+
$HTTPD_URL/smart/repo.git clone 2>err &&
385+
grep "Cookie:.*Foo=<redacted>" err
386+
'
387+
388+
test_expect_success 'GIT_TRACE_CURL_NO_DATA prevents data from being traced' '
389+
rm -rf clone &&
390+
GIT_TRACE_CURL=true \
391+
git clone $HTTPD_URL/smart/repo.git clone 2>err &&
392+
grep "=> Send data" err &&
393+
394+
rm -rf clone &&
395+
GIT_TRACE_CURL=true GIT_TRACE_CURL_NO_DATA=1 \
396+
git clone $HTTPD_URL/smart/repo.git clone 2>err &&
397+
! grep "=> Send data" err
398+
'
399+
367400
stop_httpd
368401
test_done

0 commit comments

Comments
 (0)