From 95fd0c5977432bb3ff2c960ebe9664c68f61a5ca Mon Sep 17 00:00:00 2001 From: "Ron B. Yeh" Date: Wed, 26 Jan 2022 00:18:35 -0800 Subject: [PATCH] Use regex to check that the port is a series of digits 0-9. Fixes https://github.com/IonicaBizau/parse-path/issues/31 --- lib/index.js | 9 +++++---- test/index.js | 12 ++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/index.js b/lib/index.js index 8965313..a68212f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -90,11 +90,12 @@ function parsePath(url) { splits = output.resource.split(":"); if (splits.length === 2) { output.resource = splits[0]; - if (splits[1]) { - output.port = Number(splits[1]); - if (isNaN(output.port)) { + const port = splits[1]; + if (port) { + output.port = Number(port); + if (isNaN(output.port) || port.match(/^\d+$/) === null) { output.port = null; - parts.unshift(splits[1]); + parts.unshift(port); } } else { output.port = null diff --git a/test/index.js b/test/index.js index 90a8627..794ea09 100644 --- a/test/index.js +++ b/test/index.js @@ -159,6 +159,18 @@ const INPUTS = [ , hash: "" , search: "" } + ], [ + "git@github.com:0xABC/git-stats.git" + , { + protocols: [] + , protocol: "ssh" + , port: null + , resource: "github.com" + , user: "git" + , pathname: "/0xABC/git-stats.git" + , hash: "" + , search: "" + } ], [ "https://attacker.com\\@example.com" , {