]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
urlapi: URL encoding for the URL missed the fragment
authorDaniel Stenberg <daniel@haxx.se>
Tue, 4 Apr 2023 14:59:59 +0000 (16:59 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Wed, 5 Apr 2023 06:30:12 +0000 (08:30 +0200)
Meaning that it would wrongly still store the fragment using spaces
instead of %20 if allowing space while also asking for URL encoding.

Discovered when playing with trurl.

Added test to lib1560 to verify the fix.

Closes #10887

lib/urlapi.c
tests/libtest/lib1560.c

index 62e32330648ca0180e7758e3b11af2b62f903c43..7301463b100a3a94bdc7883cfebcac0836440c0d 100644 (file)
@@ -1131,15 +1131,26 @@ static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags)
     fraglen = strlen(fragment);
     if(fraglen > 1) {
       /* skip the leading '#' in the copy but include the terminating null */
-      u->fragment = Curl_memdup(fragment + 1, fraglen);
-      if(!u->fragment) {
-        result = CURLUE_OUT_OF_MEMORY;
-        goto fail;
+      if(flags & CURLU_URLENCODE) {
+        struct dynbuf enc;
+        Curl_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
+        if(urlencode_str(&enc, fragment + 1, fraglen, TRUE, FALSE)) {
+          result = CURLUE_OUT_OF_MEMORY;
+          goto fail;
+        }
+        u->fragment = Curl_dyn_ptr(&enc);
       }
+      else {
+        u->fragment = Curl_memdup(fragment + 1, fraglen);
+        if(!u->fragment) {
+          result = CURLUE_OUT_OF_MEMORY;
+          goto fail;
+        }
 
-      if(junkscan(u->fragment, flags)) {
-        result = CURLUE_BAD_FRAGMENT;
-        goto fail;
+        if(junkscan(u->fragment, flags)) {
+          result = CURLUE_BAD_FRAGMENT;
+          goto fail;
+        }
       }
     }
   }
index 81f8f740c8508fd6acb90634b30b3c8b158b879d..a16d16adf5c2a4d573d29aeb70e4ff5023e0b8ed 100644 (file)
@@ -141,6 +141,9 @@ struct clearurlcase {
 };
 
 static const struct testcase get_parts_list[] ={
+  {"https://user@example.net?hello# space ",
+   "https | user | [12] | [13] | example.net | [15] | / | hello | %20space%20",
+   CURLU_ALLOW_SPACE|CURLU_URLENCODE, 0, CURLUE_OK},
   {"https://test%test", "", 0, 0, CURLUE_BAD_HOSTNAME},
   {"https://example.com%252f%40@example.net",
    "https | example.com%2f@ | [12] | [13] | example.net | [15] | / "