]> git.ipfire.org Git - thirdparty/git.git/commitdiff
http.c: cookie file tightening
authorJunio C Hamano <gitster@pobox.com>
Tue, 9 Jul 2024 23:03:48 +0000 (16:03 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 10 Jul 2024 04:28:38 +0000 (21:28 -0700)
The http.cookiefile configuration variable is used to call
curl_easy_setopt() to set CURLOPT_COOKIEFILE and if http.savecookies
is set, the same value is used for CURLOPT_COOKIEJAR.  The former is
used only to read cookies at startup, the latter is used to write
cookies at the end.

The manual pages https://curl.se/libcurl/c/CURLOPT_COOKIEFILE.html
and https://curl.se/libcurl/c/CURLOPT_COOKIEJAR.html talk about two
interesting special values.

 * "" (an empty string) given to CURLOPT_COOKIEFILE means not to
   read cookies from any file upon startup.

 * It is not specified what "" (an empty string) given to
   CURLOPT_COOKIEJAR does; presumably open a file whose name is an
   empty string and write cookies to it?  In any case, that is not
   what we want to see happen, ever.

 * "-" (a dash) given to CURLOPT_COOKIEFILE makes cURL read cookies
   from the standard input, and given to CURLOPT_COOKIEJAR makes
   cURL write cookies to the standard output.  Neither of which we
   want ever to happen.

So, let's make sure we avoid these nonsense cases.  Specifically,
when http.cookies is set to "-", ignore it with a warning, and when
it is set to "" and http.savecookies is set, ignore http.savecookies
with a warning.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
http.c

diff --git a/http.c b/http.c
index 92c5a22f262836ff5953540c44bb4864e921182f..3226347d2d279d4d454a39fd3f5e51b705ff6ed7 100644 (file)
--- a/http.c
+++ b/http.c
@@ -1316,7 +1316,16 @@ struct active_request_slot *get_active_slot(void)
        slot->finished = NULL;
        slot->callback_data = NULL;
        slot->callback_func = NULL;
+
+       if (curl_cookie_file && !strcmp(curl_cookie_file, "-")) {
+               warning(_("refusing to read cookies from http.cookiefile '-'"));
+               FREE_AND_NULL(curl_cookie_file);
+       }
        curl_easy_setopt(slot->curl, CURLOPT_COOKIEFILE, curl_cookie_file);
+       if (curl_save_cookies && (!curl_cookie_file || !curl_cookie_file[0])) {
+               curl_save_cookies = 0;
+               warning(_("ignoring http.savecookies for empty http.cookiefile"));
+       }
        if (curl_save_cookies)
                curl_easy_setopt(slot->curl, CURLOPT_COOKIEJAR, curl_cookie_file);
        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);