]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
urlapi: fix an issue parsing file URLs
authortiymat <138939221+tiymat@users.noreply.github.com>
Wed, 27 May 2026 03:14:31 +0000 (00:44 -0230)
committerDaniel Stenberg <daniel@haxx.se>
Thu, 28 May 2026 06:49:56 +0000 (08:49 +0200)
Fixes #21743
Closes #21764

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

index 589a400834b1ef31fba2bc714fe0957638c3ee5a..a5ec95032b4623aa1fc623885dcc35d490ed794e 100644 (file)
@@ -876,13 +876,19 @@ UNITTEST CURLUcode parse_file(const char *url, size_t urllen, CURLU *u,
   path = &url[5];
   pathlen = urllen - 5;
 
+  /* RFC 8089: file-hier-part = ( "//" auth-path ) / local-path, where
+     local-path also starts with a "/". So reject anything that doesn't
+     start with at least one "/" */
+  if(path[0] != '/')
+    return CURLUE_BAD_FILE_URL;
+
   /* Extra handling URLs with an authority component (i.e. that start with
    * "file://")
    *
    * We allow omitted hostname (e.g. file:/<path>) -- valid according to
    * RFC 8089, but not the (current) WHAT-WG URL spec.
    */
-  if(path[0] == '/' && path[1] == '/') {
+  if(path[1] == '/') {
     /* swallow the two slashes */
     const char *ptr = &path[2];
 
index cd2cbec589c8be723dff1fcb3a6c9ce25c6450e5..bdf8b56cad5b9370c40a6233524ca2c18e14faf2 100644 (file)
@@ -886,7 +886,11 @@ static const struct urltestcase get_url_list[] = {
   {"file:///.", "file:///", 0, 0, CURLUE_OK},
   {"file:///./", "file:///", 0, 0, CURLUE_OK},
   {"file:///a", "file:///a", 0, 0, CURLUE_OK},
-  {"file:./", "file://", 0, 0, CURLUE_OK},
+  {"file:./", "", 0, 0, CURLUE_BAD_FILE_URL},
+  {"file:foo", "", 0, 0, CURLUE_BAD_FILE_URL},
+  {"file:foo/bar", "", 0, 0, CURLUE_BAD_FILE_URL},
+  {"file:?q", "", 0, 0, CURLUE_BAD_FILE_URL},
+  {"file:#f", "", 0, 0, CURLUE_BAD_FILE_URL},
   {"http://example.com/hello/../here",
    "http://example.com/hello/../here",
    CURLU_PATH_AS_IS, 0, CURLUE_OK},
index 024c7ff4002844b762f68a86855adb0df241ee47..b5b372336af3c77ba6921b737f0ae0a27ba4fb0a 100644 (file)
@@ -267,6 +267,10 @@ static CURLcode test_unit1675(const char *arg)
       {"file:///etc/hosts", "/etc/hosts", TRUE},
       {"file://localhost/etc/hosts", "/etc/hosts", TRUE},
       {"file://apple/etc/hosts", "/etc/hosts", FALSE},
+      {"file:foo", NULL, FALSE},
+      {"file:./", NULL, FALSE},
+      {"file:?q", NULL, FALSE},
+      {"file:#f", NULL, FALSE},
 #ifdef _WIN32
       {"file:///c:/windows/system32", "c:/windows/system32", TRUE},
       {"file://localhost/c:/windows/system32", "c:/windows/system32", TRUE},