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

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