aboutsummaryrefslogtreecommitdiff
path: root/src/util/paths.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/paths.ts')
-rw-r--r--src/util/paths.ts12
1 files changed, 11 insertions, 1 deletions
diff --git a/src/util/paths.ts b/src/util/paths.ts
index 30a1bbd..3aa1237 100644
--- a/src/util/paths.ts
+++ b/src/util/paths.ts
@@ -1,6 +1,16 @@
import { randomUUID } from "node:crypto";
import { tmpdir } from "node:os";
-import { join } from "node:path";
+import { isAbsolute, join, relative, resolve } from "node:path";
+
+// `relative` rather than a string prefix: it is separator-agnostic and carries each platform's
+// own case rules, so a Windows path is not rejected for its backslashes or its drive-letter case.
+export const isPathWithin = (root: string, target: string): boolean => {
+ const absRoot = resolve(root);
+ const absTarget = resolve(target);
+ if (absTarget === absRoot) return true;
+ const rel = relative(absRoot, absTarget);
+ return rel !== "" && !rel.startsWith("..") && !isAbsolute(rel);
+};
const NAMESPACE_RE = /^[a-zA-Z0-9_-]+$/;