Blame SOURCES/0174-curl-return-URLs-without-userinfo.patch

562801
From e474cf3e6f937f0bc26a0f4171bacb468ebd2241 Mon Sep 17 00:00:00 2001
562801
From: Jakub Filak <jfilak@redhat.com>
562801
Date: Wed, 21 Oct 2015 14:40:28 +0200
562801
Subject: [PATCH] curl: return URLs without userinfo
562801
562801
All clients should work with URLs without userinfo. This commit will
562801
ensure that.
562801
562801
This commit also changes log messages from:
562801
562801
    Sending /var/tmp/problem_dir.tar.gz to scp://localhost/tmp/tmp.x5WVgpgUsY/target/
562801
    Error while uploading: 'curl_easy_perform: Login denied'
562801
    Please enter user name for 'scp://localhost/tmp/tmp.x5WVgpgUsY/target/': root
562801
    Please enter password for 'root':
562801
    Sending /var/tmp/problem_dir.tar.gz to scp://localhost/tmp/tmp.x5WVgpgUsY/target/
562801
    Successfully sent /var/tmp/problem_dir.tar.gz to scp://localhost/tmp/tmp.x5WVgpgUsY/target/
562801
562801
to:
562801
562801
    Sending /var/tmp/problem_dir.tar.gz to scp://localhost
562801
    Error while uploading: 'curl_easy_perform: Login denied'
562801
    Please enter user name for 'scp://localhost': root
562801
    Please enter password for 'scp://root@localhost':
562801
    Sending /var/tmp/problem_dir.tar.gz to scp://localhost
562801
    Successfully created scp://localhost/tmp/tmp.x5WVgpgUsY/target/problem_dir.tar.gz
562801
562801
Signed-off-by: Jakub Filak <jfilak@redhat.com>
562801
---
562801
 src/include/libreport_curl.h |  9 ++++++
562801
 src/lib/curl.c               | 67 +++++++++++++++++++++++++++++++-------------
562801
 2 files changed, 56 insertions(+), 20 deletions(-)
562801
562801
diff --git a/src/include/libreport_curl.h b/src/include/libreport_curl.h
562801
index 812738c..b9277ad 100644
562801
--- a/src/include/libreport_curl.h
562801
+++ b/src/include/libreport_curl.h
562801
@@ -128,6 +128,15 @@ enum {
562801
 #define upload_file libreport_upload_file
562801
 char *upload_file(const char *url, const char *filename);
562801
 
562801
+/* Uploads filename to url.
562801
+ *
562801
+ * If url does not ends with '/', base name of filename will be amended.
562801
+ *
562801
+ * Fails if the url does not have scheme or hostname.
562801
+ *
562801
+ * @return Resulting URL on success (the URL does not contain userinfo);
562801
+ * otherwise NULL.
562801
+ */
562801
 #define upload_file_ext libreport_upload_file_ext
562801
 char *upload_file_ext(post_state_t *post_state,
562801
                 const char *url,
562801
diff --git a/src/lib/curl.c b/src/lib/curl.c
562801
index 606d9ea..a64c464 100644
562801
--- a/src/lib/curl.c
562801
+++ b/src/lib/curl.c
562801
@@ -600,27 +600,48 @@ char *upload_file_ext(post_state_t *state, const char *url, const char *filename
562801
 {
562801
     /* we don't want to print the whole url as it may contain password
562801
      * rhbz#856960
562801
-     * there can be '@' in the login or password so let's try to find the
562801
-     * first '@' from the end
562801
+     *
562801
+     * jfilak:
562801
+     * We want to print valid URLs in useful messages.
562801
+     *
562801
+     * The old code had this approach:
562801
+     *   there can be '@' in the login or password so let's try to find the
562801
+     *   first '@' from the end
562801
+     *
562801
+     * The new implementation decomposes URI to its base elements and uses only
562801
+     * scheme and hostname for the logging purpose. These elements should not
562801
+     * contain any sensitive information.
562801
      */
562801
-    const char *clean_url = strrchr(url, '@');
562801
-    if (clean_url)
562801
-        clean_url++;
562801
-    else
562801
-        clean_url = url;
562801
-
562801
-    char *whole_url;
562801
-    unsigned len = strlen(url);
562801
-    if (len > 0 && url[len-1] == '/')
562801
-        whole_url = concat_path_file(url, strrchr(filename, '/') ? : filename);
562801
-    else
562801
-        whole_url = xstrdup(url);
562801
-
562801
-
562801
     const char *username_bck = state->username;
562801
     const char *password_bck = state->password;
562801
+
562801
+    char *whole_url = NULL;
562801
+    char *scheme = NULL;
562801
+    char *hostname = NULL;
562801
     char *username = NULL;
562801
     char *password = NULL;
562801
+    char *clean_url = NULL;
562801
+
562801
+    if (uri_userinfo_remove(url, &clean_url, &scheme, &hostname, &username, &password, NULL) != 0)
562801
+        goto finito;
562801
+
562801
+    if (scheme == NULL || hostname == NULL)
562801
+    {
562801
+        log_warning(_("Ingoring URL without scheme and hostname"));
562801
+        goto finito;
562801
+    }
562801
+
562801
+    if (username && (state->username == NULL || state->username[0] == '\0'))
562801
+    {
562801
+        state->username = username;
562801
+        state->password = password;
562801
+    }
562801
+
562801
+    unsigned len = strlen(clean_url);
562801
+    if (len > 0 && clean_url[len-1] == '/')
562801
+        whole_url = concat_path_file(clean_url, strrchr(filename, '/') ? : filename);
562801
+    else
562801
+        whole_url = xstrdup(clean_url);
562801
 
562801
     /* work around bug in libssh2(curl with scp://)
562801
      * libssh2_aget_disconnect() calls close(0)
562801
@@ -634,7 +655,9 @@ char *upload_file_ext(post_state_t *state, const char *url, const char *filename
562801
      */
562801
   do_post:
562801
 
562801
-    log(_("Sending %s to %s"), filename, clean_url);
562801
+    /* Do not include the path part of the URL as it can contain sensitive data
562801
+     * in case of typos */
562801
+    log(_("Sending %s to %s//%s"), filename, scheme, hostname);
562801
     post(state,
562801
                 whole_url,
562801
                 /*content_type:*/ "application/octet-stream",
562801
@@ -658,13 +681,13 @@ char *upload_file_ext(post_state_t *state, const char *url, const char *filename
562801
                 (state->curl_result == CURLE_LOGIN_DENIED
562801
                  || state->curl_result == CURLE_REMOTE_ACCESS_DENIED))
562801
         {
562801
-            char *msg = xasprintf(_("Please enter user name for '%s':"), clean_url);
562801
+            char *msg = xasprintf(_("Please enter user name for '%s//%s':"), scheme, hostname);
562801
             free(username);
562801
             username = ask(msg);
562801
             free(msg);
562801
             if (username != NULL && username[0] != '\0')
562801
             {
562801
-                msg = xasprintf(_("Please enter password for '%s':"), username);
562801
+                msg = xasprintf(_("Please enter password for '%s//%s@%s':"), scheme, username, hostname);
562801
                 free(password);
562801
                 password = ask_password(msg);
562801
                 free(msg);
562801
@@ -687,13 +710,17 @@ char *upload_file_ext(post_state_t *state, const char *url, const char *filename
562801
     else
562801
     {
562801
         /* This ends up a "reporting status message" in abrtd */
562801
-        log(_("Successfully sent %s to %s"), filename, clean_url);
562801
+        log(_("Successfully created %s"), whole_url);
562801
     }
562801
 
562801
     close(stdin_bck);
562801
 
562801
+finito:
562801
     free(password);
562801
     free(username);
562801
+    free(hostname);
562801
+    free(scheme);
562801
+    free(clean_url);
562801
 
562801
     state->username = username_bck;
562801
     state->password = password_bck;
562801
-- 
562801
1.8.3.1
562801