forked from Txio-labs/txio-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity.js
More file actions
27 lines (25 loc) · 933 Bytes
/
Copy pathsecurity.js
File metadata and controls
27 lines (25 loc) · 933 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import path from "path"
import { fileURLToPath, pathToFileURL } from "url"
export function isPathInside(childPath, parentPath) {
const relative = path.relative(parentPath, childPath)
return !relative.startsWith("..") && !path.isAbsolute(relative)
}
export function isUrlInsideRoot(targetUrl, rootUrl) {
try {
const target = new URL(targetUrl)
const root = new URL(rootUrl)
if (target.protocol !== root.protocol) return false
if (target.protocol === "file:") {
const targetPath = path.resolve(fileURLToPath(target.href))
const rootPath = path.resolve(fileURLToPath(root.href))
return targetPath === rootPath || isPathInside(targetPath, rootPath)
}
return (
target.origin === root.origin &&
(target.pathname === root.pathname ||
target.pathname.startsWith(root.pathname.endsWith("/") ? root.pathname : root.pathname + "/"))
)
} catch {
return false
}
}