Skip to content

Commit e891a5c

Browse files
committed
Merge branch 'jk/trace-curl-redact' into HEAD
* jk/trace-curl-redact: curl: handle non-http protocols in trace
2 parents 9829a0a + ec9df65 commit e891a5c

1 file changed

Lines changed: 60 additions & 7 deletions

File tree

http.c

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ static int has_proxy_cert_password(void)
762762
}
763763

764764
/* Return 1 if redactions have been made, 0 otherwise. */
765-
static int redact_sensitive_header(struct strbuf *header, size_t offset)
765+
static int redact_http_header(struct strbuf *header, size_t offset)
766766
{
767767
int ret = 0;
768768
char *sensitive_header;
@@ -859,14 +859,67 @@ static void redact_sensitive_info_header(struct strbuf *header)
859859

860860
if (trace_curl_redact &&
861861
match_curl_h2_trace(header->buf, &sensitive_header)) {
862-
if (redact_sensitive_header(header, sensitive_header - header->buf)) {
862+
if (redact_http_header(header, sensitive_header - header->buf)) {
863863
/* redaction ate our closing bracket */
864864
strbuf_addch(header, ']');
865865
}
866866
}
867867
}
868868

869-
static void curl_dump_header(const char *text, unsigned char *ptr, size_t size, int hide_sensitive_header)
869+
static void redact_imap_header(struct strbuf *header)
870+
{
871+
const char *p;
872+
873+
/* skip past the command tag */
874+
p = strchr(header->buf, ' ');
875+
if (!p)
876+
return; /* no tag */
877+
p++;
878+
879+
if (skip_prefix(p, "AUTHENTICATE ", &p)) {
880+
/* the first token is the auth type, which is OK to log */
881+
while (*p && !isspace(*p))
882+
p++;
883+
/* the rest is an opaque blob; fall through to redact */
884+
} else if (skip_prefix(p, "LOGIN ", &p)) {
885+
/* fall through to redact both login and password */
886+
} else {
887+
/* not a sensitive header */
888+
return;
889+
}
890+
891+
strbuf_setlen(header, p - header->buf);
892+
strbuf_addstr(header, " <redacted>");
893+
}
894+
895+
static void redact_sensitive_header(CURL *handle, struct strbuf *header)
896+
{
897+
const char *url;
898+
int ret;
899+
900+
ret = curl_easy_getinfo(handle, CURLINFO_EFFECTIVE_URL, &url);
901+
if (!ret && url) {
902+
if (starts_with(url, "http")) {
903+
redact_http_header(header, 0);
904+
return;
905+
}
906+
if (starts_with(url, "imap")) {
907+
redact_imap_header(header);
908+
return;
909+
}
910+
}
911+
912+
/*
913+
* We weren't able to figure out the protocol. Err on the side of
914+
* redacting too much.
915+
*/
916+
redact_http_header(header, 0);
917+
redact_imap_header(header);
918+
}
919+
920+
static void curl_dump_header(CURL *handle, const char *text,
921+
unsigned char *ptr, size_t size,
922+
int hide_sensitive_header)
870923
{
871924
struct strbuf out = STRBUF_INIT;
872925
struct strbuf **headers, **header;
@@ -880,7 +933,7 @@ static void curl_dump_header(const char *text, unsigned char *ptr, size_t size,
880933

881934
for (header = headers; *header; header++) {
882935
if (hide_sensitive_header)
883-
redact_sensitive_header(*header, 0);
936+
redact_sensitive_header(handle, *header);
884937
strbuf_insertstr((*header), 0, text);
885938
strbuf_insertstr((*header), strlen(text), ": ");
886939
strbuf_rtrim((*header));
@@ -931,7 +984,7 @@ static void curl_dump_info(char *data, size_t size)
931984
strbuf_release(&buf);
932985
}
933986

934-
static int curl_trace(CURL *handle UNUSED, curl_infotype type,
987+
static int curl_trace(CURL *handle, curl_infotype type,
935988
char *data, size_t size,
936989
void *userp UNUSED)
937990
{
@@ -944,7 +997,7 @@ static int curl_trace(CURL *handle UNUSED, curl_infotype type,
944997
break;
945998
case CURLINFO_HEADER_OUT:
946999
text = "=> Send header";
947-
curl_dump_header(text, (unsigned char *)data, size, DO_FILTER);
1000+
curl_dump_header(handle, text, (unsigned char *)data, size, DO_FILTER);
9481001
break;
9491002
case CURLINFO_DATA_OUT:
9501003
if (trace_curl_data) {
@@ -960,7 +1013,7 @@ static int curl_trace(CURL *handle UNUSED, curl_infotype type,
9601013
break;
9611014
case CURLINFO_HEADER_IN:
9621015
text = "<= Recv header";
963-
curl_dump_header(text, (unsigned char *)data, size, NO_FILTER);
1016+
curl_dump_header(handle, text, (unsigned char *)data, size, NO_FILTER);
9641017
break;
9651018
case CURLINFO_DATA_IN:
9661019
if (trace_curl_data) {

0 commit comments

Comments
 (0)