summaryrefslogtreecommitdiff
path: root/lib/transfer.c
diff options
context:
space:
mode:
authorViktor Szakats <commit@vsz.me>2021-02-23 14:54:46 +0100
committerDaniel Stenberg <daniel@haxx.se>2021-03-28 23:19:55 +0200
commit7214288898f5625a6cc196e22a74232eada7861c (patch)
tree3db8599cd34164f6005cb8aa423cb7cd9ddc853d /lib/transfer.c
parent184ffc0bdfcab17eb96d1dd64f9c0cecc139e3e9 (diff)
downloadcurl-7214288898f5625a6cc196e22a74232eada7861c.tar.gz
transfer: strip credentials from the auto-referer header field
Added test 2081 to verify. CVE-2021-22876 Bug: https://curl.se/docs/CVE-2021-22876.html
Diffstat (limited to 'lib/transfer.c')
-rw-r--r--lib/transfer.c25
1 files changed, 23 insertions, 2 deletions
diff --git a/lib/transfer.c b/lib/transfer.c
index 1976bc033..a68c021c8 100644
--- a/lib/transfer.c
+++ b/lib/transfer.c
@@ -1581,6 +1581,9 @@ CURLcode Curl_follow(struct Curl_easy *data,
data->state.followlocation++; /* count location-followers */
if(data->set.http_auto_referer) {
+ CURLU *u;
+ char *referer;
+
/* We are asked to automatically set the previous URL as the referer
when we get the next URL. We pick the ->url field, which may or may
not be 100% correct */
@@ -1590,9 +1593,27 @@ CURLcode Curl_follow(struct Curl_easy *data,
data->state.referer_alloc = FALSE;
}
- data->state.referer = strdup(data->state.url);
- if(!data->state.referer)
+ /* Make a copy of the URL without crenditals and fragment */
+ u = curl_url();
+ if(!u)
+ return CURLE_OUT_OF_MEMORY;
+
+ uc = curl_url_set(u, CURLUPART_URL, data->state.url, 0);
+ if(!uc)
+ uc = curl_url_set(u, CURLUPART_FRAGMENT, NULL, 0);
+ if(!uc)
+ uc = curl_url_set(u, CURLUPART_USER, NULL, 0);
+ if(!uc)
+ uc = curl_url_set(u, CURLUPART_PASSWORD, NULL, 0);
+ if(!uc)
+ uc = curl_url_get(u, CURLUPART_URL, &referer, 0);
+
+ curl_url_cleanup(u);
+
+ if(uc || referer == NULL)
return CURLE_OUT_OF_MEMORY;
+
+ data->state.referer = referer;
data->state.referer_alloc = TRUE; /* yes, free this later */
}
}