diff --git a/src/utils/forbiddenPaths.js b/src/utils/forbiddenPaths.js index b91a4f7..d2d4007 100644 --- a/src/utils/forbiddenPaths.js +++ b/src/utils/forbiddenPaths.js @@ -22,57 +22,50 @@ function getInitializePath(to) { } } -// Function to check if URL path name is a forbidden path. function isForbiddenPath(decodedHref, match) { const forbiddenPath = forbiddenPaths[match]; if (!forbiddenPath) return false; + + const isWin32 = os.platform() === "win32"; + const decodedHrefLower = isWin32 ? decodedHref.toLowerCase() : null; + if (typeof forbiddenPath === "string") { - return ( - decodedHref === forbiddenPath || - (os.platform() === "win32" && - decodedHref.toLowerCase() === forbiddenPath.toLowerCase()) - ); + return isWin32 + ? decodedHrefLower === forbiddenPath.toLowerCase() + : decodedHref === forbiddenPath; } + if (typeof forbiddenPath === "object") { - return forbiddenPath.some((forbiddenPathSingle) => { - return ( - decodedHref === forbiddenPathSingle || - (os.platform() === "win32" && - decodedHref.toLowerCase() === forbiddenPathSingle.toLowerCase()) - ); - }); + return isWin32 + ? forbiddenPath.some((path) => decodedHrefLower === path.toLowerCase()) + : forbiddenPath.includes(decodedHref); } + return false; } -// Function to check if URL path name is index of one of defined forbidden paths. function isIndexOfForbiddenPath(decodedHref, match) { const forbiddenPath = forbiddenPaths[match]; if (!forbiddenPath) return false; + + const isWin32 = os.platform() === "win32"; + const decodedHrefLower = isWin32 ? decodedHref.toLowerCase() : null; + if (typeof forbiddenPath === "string") { - return ( - decodedHref === forbiddenPath || - decodedHref.indexOf(forbiddenPath + "/") === 0 || - (os.platform() === "win32" && - (decodedHref.toLowerCase() === forbiddenPath.toLowerCase() || - decodedHref - .toLowerCase() - .indexOf(forbiddenPath.toLowerCase() + "/") === 0)) - ); + const forbiddenPathLower = isWin32 ? forbiddenPath.toLowerCase() : null; + return isWin32 + ? decodedHrefLower.indexOf(forbiddenPathLower) == 0 + : decodedHref.indexOf(forbiddenPath) == 0; } + if (typeof forbiddenPath === "object") { - return forbiddenPath.some((forbiddenPathSingle) => { - return ( - decodedHref === forbiddenPathSingle || - decodedHref.indexOf(forbiddenPathSingle + "/") === 0 || - (os.platform() === "win32" && - (decodedHref.toLowerCase() === forbiddenPathSingle.toLowerCase() || - decodedHref - .toLowerCase() - .indexOf(forbiddenPathSingle.toLowerCase() + "/") === 0)) - ); - }); + return isWin32 + ? forbiddenPath.some( + (path) => decodedHrefLower.indexOf(path.toLowerCase()) == 0, + ) + : forbiddenPath.some((path) => decodedHref.indexOf(path) == 0); } + return false; } diff --git a/src/utils/matchHostname.js b/src/utils/matchHostname.js index b2c8c46..36b3ce4 100644 --- a/src/utils/matchHostname.js +++ b/src/utils/matchHostname.js @@ -1,17 +1,17 @@ function matchHostname(hostname, reqHostname) { - if (typeof hostname == "undefined" || hostname == "*") { + if (typeof hostname === "undefined" || hostname === "*") { return true; - } else if (reqHostname && hostname.indexOf("*.") == 0 && hostname != "*.") { + } else if (reqHostname && hostname.indexOf("*.") == 0 && hostname !== "*.") { const hostnamesRoot = hostname.substring(2); if ( - reqHostname == hostnamesRoot || + reqHostname === hostnamesRoot || (reqHostname.length > hostnamesRoot.length && - reqHostname.indexOf("." + hostnamesRoot) == + reqHostname.indexOf("." + hostnamesRoot) === reqHostname.length - hostnamesRoot.length - 1) ) { return true; } - } else if (reqHostname && reqHostname == hostname) { + } else if (reqHostname && reqHostname === hostname) { return true; } return false;