CVE-2022-0624 (published 2022-06-28T09:15Z)

Authorization Bypass Through User-Controlled Key in GitHub repository ionicabizau/parse-path prior to 5.0.0.

Severity: HIGH
CVSS v2 base score: 7.5 (AV:N/AC:L/Au:N/C:P/I:P/A:P)
CVSS v3 base score: 7.3, HIGH (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:L/A:L)

Weakness information:
cwe_id: CWE-639
cwe_name: Authorization Bypass Through User-Controlled Key
description: The system's authorization functionality does not prevent one user from gaining access to another user's data or record by modifying the key value identifying the data.
extended_description: 
            Retrieval of a user record occurs in the system based on some key value that is under user control. The key would typically identify a user-related record stored in the system and would be used to lookup that record for presentation to the user. It is likely that an attacker would have to be an authenticated user in the system. However, the authorization process would not properly check the data access operation to ensure that the authenticated user performing the operation has sufficient entitlements to perform the requested data access, hence bypassing any other authorization checks present in the system.
            For example, attackers can look at places where user specific data is retrieved (e.g. search screens) and determine whether the key for the item being looked up is controllable externally. The key may be a hidden field in the HTML form field, might be passed as a URL parameter or as an unencrypted cookie variable, then in each of these cases it will be possible to tamper with the key value.
            One manifestation of this weakness is when a system uses sequential or otherwise easily-guessable session IDs that would allow one user to easily switch to another user's session and read/modify their data.
         
         

Fix information:
repo_url: https://github.com/ionicabizau/parse-path
commit_details: repo_url: https://github.com/ionicabizau/parse-path
author: Ionică Bizău
author_date: 2022-06-27 11:35:20+03:00
msg: Refactor the codebase to make it more secure
repository: repo_url: https://github.com/ionicabizau/parse-path
repo_name: IonicaBizau/parse-path
description: :crystal_ball: Parse paths (local paths, urls: ssh/git/etc)
repo_language: JavaScript
owner: ionicabizau
file_changes: filename: index.js
change_type: ModificationType.MODIFY
diff: @@ -1,8 +1,4 @@
-// Dependencies
 const protocols = require("protocols")
-    , isSsh = require("is-ssh")
-    , qs = require("query-string")
-    ;
 
 /**
  * parsePath
@@ -25,110 +21,50 @@ const protocols = require("protocols")
  *  - `query` (Object): The url querystring, parsed as object.
  */
 function parsePath(url) {
-    url = (url || "").trim().replace(/\r?\n|\r/gm, "")
-    var output = {
-            protocols: protocols(url)
-          , protocol: null
-          , port: null
-          , resource: ""
-          , user: ""
-          , pathname: ""
-          , hash: ""
-          , search: ""
-          , href: url
-          , query: Object.create(null)
-        }
-      , protocolIndex = url.indexOf("://")
-      , resourceIndex = -1
-      , splits = null
-      , parts = null
-      ;
 
-    if (url.startsWith(".")) {
-        if (url.startsWith("./")) {
-            url = url.substring(2);
-        }
-        output.pathname = url;
-        output.protocol = "file";
+    const output = {
+        protocols: []
+      , protocol: null
+      , port: null
+      , resource: ""
+      , user: ""
+      , password: ""
+      , pathname: ""
+      , hash: ""
+      , search: ""
+      , href: url
+      , query: {}
     }
 
-    const firstChar = url.charAt(1)
-    if (!output.protocol) {
+    try {
+        const parsed = new URL(url)
+        output.protocols = protocols(parsed)
         output.protocol = output.protocols[0]
-        if (!output.protocol) {
-            if (isSsh(url)) {
-                output.protocol = "ssh"
-            } else if (firstChar === "/" || firstChar === "~") {
-                url = url.substring(2)
-                output.protocol = "file"
-            } else {
-                output.protocol = "file"
-            }
-        }
-    }
-
-    if (protocolIndex !== -1) {
-        url = url.substring(protocolIndex + 3);
-    }
-
-    parts = url.split(/\/|\\/);
-    if (output.protocol !== "file") {
-        output.resource = parts.shift();
-    } else {
-        output.resource = "";
-    }
-
-    // user@domain
-    splits = output.resource.split("@");
-    if (splits.length === 2) {
-        output.user = splits[0];
-        output.resource = splits[1];
-    }
-
-
-    // domain.com:port
-    splits = output.resource.split(":");
-    if (splits.length === 2) {
-        output.resource = splits[0];
-        const port = splits[1];
-        if (port) {
-            output.port = Number(port);
-            if (isNaN(output.port) || port.match(/^\d+$/) === null) {
-                output.port = null;
-                parts.unshift(port);
-            }
-        } else {
-            output.port = null
-        }
-    }
-
-    // Remove empty elements
-    parts = parts.filter(Boolean);
-
-    // Stringify the pathname
-    if (output.protocol === "file") {
-        output.pathname = output.href
-    } else {
-      output.pathname = output.pathname || ((output.protocol !== "file" || output.href[0] === "/" ? "/" : "") + parts.join("/"));
-    }
-
-    // #some-hash
-    splits = output.pathname.split("#");
-    if (splits.length === 2) {
-        output.pathname = splits[0];
-        output.hash = splits[1];
-    }
-
-    // ?foo=bar
-    splits = output.pathname.split("?");
-    if (splits.length === 2) {
-        output.pathname = splits[0];
-        output.search = splits[1];
+        output.port = parsed.port
+        output.resource =  parsed.host
+        output.user = parsed.username || ""
+        output.password = parsed.password || ""
+        output.pathname = parsed.pathname
+        output.hash = parsed.hash.slice(1)
+        output.search = parsed.search.slice(1)
+        output.href = parsed.href
+        output.query = Object.fromEntries(parsed.searchParams)
+    } catch (e) {
+        // TODO Maybe check if it is a valid local file path
+        //      In any case, these will be parsed by higher
+        //      level parsers such as parse-url, git-url-parse, git-up
+        output.protocols = ["file"]
+        output.protocol = output.protocols[0]
+        output.port = ""
+        output.resource =  ""
+        output.user = ""
+        output.pathname = ""
+        output.hash = ""
+        output.search = ""
+        output.href = url
+        output.query = {}
     }
 
-    output.query = qs.parse(output.search);
-    output.href = output.href.replace(/\/$/, "")
-    output.pathname = output.pathname.replace(/\/$/, "")
     return output;
 }
 

programming_language: Scala
filename: package.json
change_type: ModificationType.MODIFY
diff: @@ -52,4 +52,4 @@
     "bloggify.json",
     "bloggify/"
   ]
-}
\ No newline at end of file
+}

programming_language: JavaScript
filename: index.js
change_type: ModificationType.MODIFY
diff: @@ -1,7 +1,6 @@
 // Dependencies
 const parseUrl = require("../lib")
     , tester = require("tester")
-    , qs = require("querystring")
     ;
 
 const INPUTS = [
@@ -10,12 +9,14 @@ const INPUTS = [
       , {
             protocols: [ "http" ]
           , protocol: "http"
-          , port: null
+          , port: ""
           , resource: "ionicabizau.net"
           , user: ""
           , pathname: "/blog"
           , hash: ""
           , search: ""
+          , href: "http://ionicabizau.net/blog"
+          , query: {}
         }
     ]
   , [
@@ -23,12 +24,14 @@ const INPUTS = [
       , {
             protocols: [ "http" ]
           , protocol: "http"
-          , port: null
+          , port: ""
           , resource: "ionicabizau.net"
           , user: ""
           , pathname: "/blog"
           , hash: ""
           , search: ""
+          , href: "http://ionicabizau.net/blog"
+          , query: {}
         }
     ]
   , [
@@ -36,12 +39,14 @@ const INPUTS = [
       , {
             protocols: ["http"]
           , protocol: "http"
-          , port: null
+          , port: ""
           , resource: "domain.com"
           , user: ""
           , pathname: "/path/name"
           , hash: "some-hash"
           , search: "foo=bar&bar=42"
+          , query: { foo: "bar", bar: "42" }
+          , href: "http://domain.com/path/name?foo=bar&bar=42#some-hash"
         }
     ]
   , [
@@ -49,12 +54,14 @@ const INPUTS = [
       , {
             protocols: ["http"]
           , protocol: "http"
-          , port: null
+          , port: ""
           , resource: "domain.com"
           , user: ""
           , pathname: "/path/name"
           , hash: "some-hash?foo=bar&bar=42"
           , search: ""
+          , query: {}
+          , href: "http://domain.com/path/name#some-hash?foo=bar&bar=42"
         }
     ]
   , [
@@ -62,137 +69,161 @@ const INPUTS = [
       , {
             protocols: ["git", "ssh"]
           , protocol: "git"
-          , port: null
+          , port: ""
           , resource: "host.xz"
           , user: "git"
           , pathname: "/path/name.git"
           , hash: ""
           , search: ""
+          , query: {}
+          , href: "git+ssh://git@host.xz/path/name.git"
         }
     ]
   , [
+        // NOTE:parse-path will look at this as a local path
+        //      For parsing it as url, please use parse-url
         "git@github.com:IonicaBizau/git-stats.git"
       , {
-            protocols: []
-          , protocol: "ssh"
-          , port: null
-          , resource: "github.com"
-          , user: "git"
-          , pathname: "/IonicaBizau/git-stats.git"
+            protocols: ["file"]
+          , protocol: "file"
+          , port: ""
+          , resource: ""
+          , user: ""
+          , pathname: ""
           , hash: ""
           , search: ""
+          , query: {}
+          , href: "git@github.com:IonicaBizau/git-stats.git"
         }
     ]
   , [
         "/path/to/file.png"
       , {
-            protocols: []
+            protocols: ["file"]
           , protocol: "file"
-          , port: null
+          , port: ""
           , resource: ""
           , user: ""
-          , pathname: "/path/to/file.png"
+          , pathname: ""
           , hash: ""
           , search: ""
+          , query: {}
+          , href: "/path/to/file.png"
         }
     ]
   , [
         "./path/to/file.png"
       , {
-            protocols: []
+            protocols: ["file"]
           , protocol: "file"
-          , port: null
+          , port: ""
           , resource: ""
           , user: ""
-          , pathname: "./path/to/file.png"
+          , pathname: ""
           , hash: ""
           , search: ""
+          , query: {}
+          , href: "./path/to/file.png"
         }
     ]
   , [
         "./.path/to/file.png"
       , {
-            protocols: []
+            protocols: ["file"]
           , protocol: "file"
-          , port: null
+          , port: ""
           , resource: ""
           , user: ""
-          , pathname: "./.path/to/file.png"
+          , pathname: ""
           , hash: ""
           , search: ""
+          , query: {}
+          , href: "./.path/to/file.png"
         }
     ]
   , [
         ".path/to/file.png"
       , {
-            protocols: []
+            protocols: ["file"]
           , protocol: "file"
-          , port: null
+          , port: ""
           , resource: ""
           , user: ""
-          , pathname: ".path/to/file.png"
+          , pathname: ""
           , hash: ""
           , search: ""
+          , query: {}
+          , href: ".path/to/file.png"
         }
     ]
   , [
         "path/to/file.png"
       , {
-            protocols: []
+            protocols: ["file"]
           , protocol: "file"
-          , port: null
+          , port: ""
           , resource: ""
           , user: ""
-          , pathname: "path/to/file.png"
+          , pathname: ""
           , hash: ""
           , search: ""
+          , query: {}
+          , href: "path/to/file.png"
         }
     ], [
       "git@github.com:9IonicaBizau/git-stats.git"
     , {
-          protocols: []
-        , protocol: "ssh"
-        , port: null
-        , resource: "github.com"
-        , user: "git"
-        , pathname: "/9IonicaBizau/git-stats.git"
+          protocols: ["file"]
+        , protocol: "file"
+        , port: ""
+        , resource: ""
+        , user: ""
+        , pathname: ""
         , hash: ""
         , search: ""
+        , query: {}
+        , href: "git@github.com:9IonicaBizau/git-stats.git"
       }
     ], [
       "git@github.com:0xABC/git-stats.git"
     , {
-          protocols: []
-        , protocol: "ssh"
-        , port: null
-        , resource: "github.com"
-        , user: "git"
-        , pathname: "/0xABC/git-stats.git"
+          protocols: ["file"]
+        , protocol: "file"
+        , port: ""
+        , resource: ""
+        , user: ""
+        , pathname: ""
         , hash: ""
         , search: ""
+        , query: {}
+        , href: "git@github.com:0xABC/git-stats.git"
       }
     ], [
       "https://attacker.com\\@example.com"
     , {
           protocols: ["https"]
         , protocol: "https"
-        , port: null
+        , port: ""
         , resource: "attacker.com"
         , user: ""
         , pathname: "/@example.com"
         , hash: ""
         , search: ""
+        , href: "https://attacker.com/@example.com"
+        , query: {}
       }
   ], [
       "jav\r\nascript://%0aalert(1)"
     , {
           protocols: ["javascript"]
         , protocol: "javascript"
-        , port: null
+        , port: ""
         , resource: "%0aalert(1)"
         , user: ""
         , pathname: ""
         , hash: ""
+        , href: "javascript://%0aalert(1)"
+        , query: {}
         , search: ""
       }
   ]
@@ -201,9 +232,9 @@ const INPUTS = [
 tester.describe("check urls", test => {
     INPUTS.forEach(function (c) {
         test.should("support " + c[0], () => {
-            c[1].href = c[0].trim().replace(/\r?\n|\r/gm, "");
-            c[1].query = qs.parse(c[1].search)
-            test.expect(parseUrl(c[0])).toEqual(c[1]);
+            const cParsed = parseUrl(c[0])
+            debugger
+            test.expect(cParsed).toEqual(c[1]);
         });
     });
 });

programming_language: CoffeeScript