1
0
Fork 0
forked from svrjs/svrjs

Optimize function related to forbidden path checking and hostname matching

This commit is contained in:
Dorian Niemiec 2024-08-28 11:29:54 +02:00
parent 32d29be09f
commit 0318047078
2 changed files with 32 additions and 39 deletions

View file

@ -22,57 +22,50 @@ function getInitializePath(to) {
} }
} }
// Function to check if URL path name is a forbidden path.
function isForbiddenPath(decodedHref, match) { function isForbiddenPath(decodedHref, match) {
const forbiddenPath = forbiddenPaths[match]; const forbiddenPath = forbiddenPaths[match];
if (!forbiddenPath) return false; if (!forbiddenPath) return false;
const isWin32 = os.platform() === "win32";
const decodedHrefLower = isWin32 ? decodedHref.toLowerCase() : null;
if (typeof forbiddenPath === "string") { if (typeof forbiddenPath === "string") {
return ( return isWin32
decodedHref === forbiddenPath || ? decodedHrefLower === forbiddenPath.toLowerCase()
(os.platform() === "win32" && : decodedHref === forbiddenPath;
decodedHref.toLowerCase() === forbiddenPath.toLowerCase())
);
} }
if (typeof forbiddenPath === "object") { if (typeof forbiddenPath === "object") {
return forbiddenPath.some((forbiddenPathSingle) => { return isWin32
return ( ? forbiddenPath.some((path) => decodedHrefLower === path.toLowerCase())
decodedHref === forbiddenPathSingle || : forbiddenPath.includes(decodedHref);
(os.platform() === "win32" &&
decodedHref.toLowerCase() === forbiddenPathSingle.toLowerCase())
);
});
} }
return false; return false;
} }
// Function to check if URL path name is index of one of defined forbidden paths.
function isIndexOfForbiddenPath(decodedHref, match) { function isIndexOfForbiddenPath(decodedHref, match) {
const forbiddenPath = forbiddenPaths[match]; const forbiddenPath = forbiddenPaths[match];
if (!forbiddenPath) return false; if (!forbiddenPath) return false;
const isWin32 = os.platform() === "win32";
const decodedHrefLower = isWin32 ? decodedHref.toLowerCase() : null;
if (typeof forbiddenPath === "string") { if (typeof forbiddenPath === "string") {
return ( const forbiddenPathLower = isWin32 ? forbiddenPath.toLowerCase() : null;
decodedHref === forbiddenPath || return isWin32
decodedHref.indexOf(forbiddenPath + "/") === 0 || ? decodedHrefLower.indexOf(forbiddenPathLower) == 0
(os.platform() === "win32" && : decodedHref.indexOf(forbiddenPath) == 0;
(decodedHref.toLowerCase() === forbiddenPath.toLowerCase() ||
decodedHref
.toLowerCase()
.indexOf(forbiddenPath.toLowerCase() + "/") === 0))
);
} }
if (typeof forbiddenPath === "object") { if (typeof forbiddenPath === "object") {
return forbiddenPath.some((forbiddenPathSingle) => { return isWin32
return ( ? forbiddenPath.some(
decodedHref === forbiddenPathSingle || (path) => decodedHrefLower.indexOf(path.toLowerCase()) == 0,
decodedHref.indexOf(forbiddenPathSingle + "/") === 0 || )
(os.platform() === "win32" && : forbiddenPath.some((path) => decodedHref.indexOf(path) == 0);
(decodedHref.toLowerCase() === forbiddenPathSingle.toLowerCase() ||
decodedHref
.toLowerCase()
.indexOf(forbiddenPathSingle.toLowerCase() + "/") === 0))
);
});
} }
return false; return false;
} }

View file

@ -1,17 +1,17 @@
function matchHostname(hostname, reqHostname) { function matchHostname(hostname, reqHostname) {
if (typeof hostname == "undefined" || hostname == "*") { if (typeof hostname === "undefined" || hostname === "*") {
return true; return true;
} else if (reqHostname && hostname.indexOf("*.") == 0 && hostname != "*.") { } else if (reqHostname && hostname.indexOf("*.") == 0 && hostname !== "*.") {
const hostnamesRoot = hostname.substring(2); const hostnamesRoot = hostname.substring(2);
if ( if (
reqHostname == hostnamesRoot || reqHostname === hostnamesRoot ||
(reqHostname.length > hostnamesRoot.length && (reqHostname.length > hostnamesRoot.length &&
reqHostname.indexOf("." + hostnamesRoot) == reqHostname.indexOf("." + hostnamesRoot) ===
reqHostname.length - hostnamesRoot.length - 1) reqHostname.length - hostnamesRoot.length - 1)
) { ) {
return true; return true;
} }
} else if (reqHostname && reqHostname == hostname) { } else if (reqHostname && reqHostname === hostname) {
return true; return true;
} }
return false; return false;