@procedures/fs 0.54.0 → 0.55.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/dist/link-target.d.ts +35 -0
- package/dist/link-target.d.ts.map +1 -0
- package/dist/link-target.js +70 -0
- package/dist/link-target.js.map +1 -0
- package/dist/root.d.ts +2 -0
- package/dist/root.d.ts.map +1 -1
- package/dist/root.js +4 -6
- package/dist/root.js.map +1 -1
- package/package.json +2 -2
- package/src/link-target.ts +76 -0
- package/src/root.ts +4 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @procedures/fs
|
|
2
2
|
|
|
3
|
+
## 0.55.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 2918e97: Two security fixes:
|
|
8
|
+
|
|
9
|
+
- `@procedures/shell`: on Windows, a program name with no folder starts only from a folder of `PATH`, never from the working folder of the call. Before, a call such as "run `git` in this cloned repository" started a `git.exe` from the top folder of the repository. When no folder of `PATH` has the program, the start fails with `ENOENT`, as for a missing program, and the error names the program as before. `taskkill` starts by its full path in the system folder, and `shell.which` gives a match from `PATH` only.
|
|
10
|
+
- `@procedures/fs`: under a root, a link to a missing target is resolved against the real folder of the link. Before, the check resolved a relative target against the text of the path. When an earlier part of the path was a link to a folder, the check said "inside the root" for a write that went outside it. A `..` in a relative target follows the rule of the system: POSIX goes up from the real folder of the name before it, and Windows removes that name as text. The archive procedures and the other users of `@procedures/fs/root` get the same fix.
|
|
11
|
+
- Updated dependencies [fd9c6d4]
|
|
12
|
+
- @procedures/client@0.55.0
|
|
13
|
+
|
|
14
|
+
## 0.54.1
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- Updated dependencies [13c3a8b]
|
|
19
|
+
- @procedures/client@0.54.1
|
|
20
|
+
|
|
3
21
|
## 0.54.0
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The destination of a symlink: the path that a relative target of a link leads to, as the system
|
|
3
|
+
* resolves it. The root check (`root.ts`) uses it for a link whose target does not exist, because
|
|
4
|
+
* a write follows such a link and makes its target.
|
|
5
|
+
*
|
|
6
|
+
* The two systems use different rules for a `..` in a relative target:
|
|
7
|
+
* - POSIX follows the folders: `..` goes up from the real folder that the names before it lead to.
|
|
8
|
+
* So in the target `a/../x`, where `a` is a link to a folder, `..` is the parent of the target of
|
|
9
|
+
* `a`.
|
|
10
|
+
* - Windows reads the target as text: it joins the real folder of the link and the target, and a
|
|
11
|
+
* `..` removes the name before it. So `a\..\x` is `x` in the folder of the link.
|
|
12
|
+
*
|
|
13
|
+
* A test of `root.test.ts` writes through a table of links with `node:fs` on each system and
|
|
14
|
+
* compares the file that the system makes with the result of this function.
|
|
15
|
+
*/
|
|
16
|
+
/** True for the error of a path that does not exist. */
|
|
17
|
+
export declare function isMissing(error: unknown): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* How a system reads a relative target of a symlink: `folders` follows the real folders of the
|
|
20
|
+
* names (POSIX), `text` removes a name before `..` as text (Windows).
|
|
21
|
+
*/
|
|
22
|
+
export type LinkRule = "folders" | "text";
|
|
23
|
+
/** The rule of a system: `text` on Windows, `folders` on each other system. */
|
|
24
|
+
export declare function linkRuleOf(platform: NodeJS.Platform): LinkRule;
|
|
25
|
+
/** The rule of the system of this process. */
|
|
26
|
+
export declare const SYSTEM_LINK_RULE: LinkRule;
|
|
27
|
+
/**
|
|
28
|
+
* This function gives the path that the target of a symlink leads to. A relative target starts at
|
|
29
|
+
* the real folder of the link, not at the text of the path: an earlier link to a folder changes
|
|
30
|
+
* the folder of the link. With the rule `folders`, each name of the target before its last name
|
|
31
|
+
* goes to its real path, so a `..` after a link goes up from the target of that link. With the
|
|
32
|
+
* rule `text`, the target joins the real folder of the link as text.
|
|
33
|
+
*/
|
|
34
|
+
export declare function linkDestination(link: string, target: string, rule?: LinkRule): Promise<string>;
|
|
35
|
+
//# sourceMappingURL=link-target.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"link-target.d.ts","sourceRoot":"","sources":["../src/link-target.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAKH,wDAAwD;AACxD,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAGjD;AAYD;;;GAGG;AACH,MAAM,MAAM,QAAQ,GAAG,SAAS,GAAG,MAAM,CAAC;AAE1C,+EAA+E;AAC/E,wBAAgB,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC,QAAQ,GAAG,QAAQ,CAE9D;AAED,8CAA8C;AAC9C,eAAO,MAAM,gBAAgB,EAAE,QAAuC,CAAC;AAEvE;;;;;;GAMG;AACH,wBAAsB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,GAAE,QAA2B,GAAG,OAAO,CAAC,MAAM,CAAC,CAmBtH"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The destination of a symlink: the path that a relative target of a link leads to, as the system
|
|
3
|
+
* resolves it. The root check (`root.ts`) uses it for a link whose target does not exist, because
|
|
4
|
+
* a write follows such a link and makes its target.
|
|
5
|
+
*
|
|
6
|
+
* The two systems use different rules for a `..` in a relative target:
|
|
7
|
+
* - POSIX follows the folders: `..` goes up from the real folder that the names before it lead to.
|
|
8
|
+
* So in the target `a/../x`, where `a` is a link to a folder, `..` is the parent of the target of
|
|
9
|
+
* `a`.
|
|
10
|
+
* - Windows reads the target as text: it joins the real folder of the link and the target, and a
|
|
11
|
+
* `..` removes the name before it. So `a\..\x` is `x` in the folder of the link.
|
|
12
|
+
*
|
|
13
|
+
* A test of `root.test.ts` writes through a table of links with `node:fs` on each system and
|
|
14
|
+
* compares the file that the system makes with the result of this function.
|
|
15
|
+
*/
|
|
16
|
+
import { realpath } from "node:fs/promises";
|
|
17
|
+
import { dirname, join, parse, resolve, sep } from "node:path";
|
|
18
|
+
/** True for the error of a path that does not exist. */
|
|
19
|
+
export function isMissing(error) {
|
|
20
|
+
const code = error?.code;
|
|
21
|
+
return code === "ENOENT" || code === "ENOTDIR";
|
|
22
|
+
}
|
|
23
|
+
/** The real path of a path, or the path itself when it does not exist. */
|
|
24
|
+
async function realOrSame(path) {
|
|
25
|
+
try {
|
|
26
|
+
return await realpath(path);
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
if (isMissing(error))
|
|
30
|
+
return path;
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/** The rule of a system: `text` on Windows, `folders` on each other system. */
|
|
35
|
+
export function linkRuleOf(platform) {
|
|
36
|
+
return platform === "win32" ? "text" : "folders";
|
|
37
|
+
}
|
|
38
|
+
/** The rule of the system of this process. */
|
|
39
|
+
export const SYSTEM_LINK_RULE = linkRuleOf(process.platform);
|
|
40
|
+
/**
|
|
41
|
+
* This function gives the path that the target of a symlink leads to. A relative target starts at
|
|
42
|
+
* the real folder of the link, not at the text of the path: an earlier link to a folder changes
|
|
43
|
+
* the folder of the link. With the rule `folders`, each name of the target before its last name
|
|
44
|
+
* goes to its real path, so a `..` after a link goes up from the target of that link. With the
|
|
45
|
+
* rule `text`, the target joins the real folder of the link as text.
|
|
46
|
+
*/
|
|
47
|
+
export async function linkDestination(link, target, rule = SYSTEM_LINK_RULE) {
|
|
48
|
+
// The folder of the link exists: the system read the link in it
|
|
49
|
+
const folder = await realpath(dirname(link));
|
|
50
|
+
if (rule === "text")
|
|
51
|
+
return resolve(folder, target);
|
|
52
|
+
const { root } = parse(target);
|
|
53
|
+
let current = root === "" ? folder : resolve(folder, root);
|
|
54
|
+
const names = target
|
|
55
|
+
.slice(root.length)
|
|
56
|
+
.split(sep === "\\" ? /[\\/]/ : "/")
|
|
57
|
+
.filter((name) => name !== "" && name !== ".");
|
|
58
|
+
for (const [index, name] of names.entries()) {
|
|
59
|
+
if (name === "..") {
|
|
60
|
+
current = dirname(current);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
current = join(current, name);
|
|
64
|
+
if (index < names.length - 1)
|
|
65
|
+
current = await realOrSame(current);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return current;
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=link-target.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"link-target.js","sourceRoot":"","sources":["../src/link-target.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAE/D,wDAAwD;AACxD,MAAM,UAAU,SAAS,CAAC,KAAc;IACtC,MAAM,IAAI,GAAI,KAA2C,EAAE,IAAI,CAAC;IAChE,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,SAAS,CAAC;AACjD,CAAC;AAED,0EAA0E;AAC1E,KAAK,UAAU,UAAU,CAAC,IAAY;IACpC,IAAI,CAAC;QACH,OAAO,MAAM,QAAQ,CAAC,IAAI,CAAC,CAAC;IAC9B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,SAAS,CAAC,KAAK,CAAC;YAAE,OAAO,IAAI,CAAC;QAClC,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC;AAQD,+EAA+E;AAC/E,MAAM,UAAU,UAAU,CAAC,QAAyB;IAClD,OAAO,QAAQ,KAAK,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC;AACnD,CAAC;AAED,8CAA8C;AAC9C,MAAM,CAAC,MAAM,gBAAgB,GAAa,UAAU,CAAC,OAAO,CAAC,QAAQ,CAAC,C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
|
|
12
12
|
* - The check uses the real path (`fs.realpath`) of the path. For a path that does not exist, it
|
|
13
13
|
* uses the real path of the nearest existing ancestor. So a symlinked parent folder cannot
|
|
14
14
|
* lead a write or a `mkdir` out of the root.
|
|
15
|
+
* - A relative target of a symlink starts at the real folder of the link, not at the text of the
|
|
16
|
+
* path. A `..` in the target follows the rule of the system (`link-target.ts`).
|
|
15
17
|
* - The procedure gets the absolute path, so the paths of the output are absolute.
|
|
16
18
|
*
|
|
17
19
|
* On Windows, the comparison ignores the case of the letters, and `/` and `\` are the same.
|
package/dist/root.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"root.d.ts","sourceRoot":"","sources":["../src/root.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"root.d.ts","sourceRoot":"","sources":["../src/root.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAIH,OAAO,EAAE,eAAe,EAAE,KAAK,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAGzE,uEAAuE;AACvE,eAAO,MAAM,YAAY,iBAAiB,CAAC;AAK3C;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAG9D;AAcD;;;;GAIG;AACH,wBAAsB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,SAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAwBzE;AAED,uEAAuE;AACvE,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACd;AAED;;;GAGG;AACH,wBAAsB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,SAAwB,GAAG,OAAO,CAAC,YAAY,CAAC,CAUpG;AAED,yDAAyD;AACzD,wBAAgB,WAAW,CAAC,SAAS,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,GAAG,eAAe,CAEtF;AAED,iEAAiE;AACjE,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAErD;AAED;;;GAGG;AACH,wBAAsB,gBAAgB,CAAC,IAAI,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAE7F;AAED;;;;GAIG;AACH,wBAAsB,MAAM,CAAC,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,aAAa,EAAE,IAAI,GAAE,MAAoB,GAAG,OAAO,CAAC,MAAM,CAAC,CAYpI"}
|
package/dist/root.js
CHANGED
|
@@ -12,6 +12,8 @@
|
|
|
12
12
|
* - The check uses the real path (`fs.realpath`) of the path. For a path that does not exist, it
|
|
13
13
|
* uses the real path of the nearest existing ancestor. So a symlinked parent folder cannot
|
|
14
14
|
* lead a write or a `mkdir` out of the root.
|
|
15
|
+
* - A relative target of a symlink starts at the real folder of the link, not at the text of the
|
|
16
|
+
* path. A `..` in the target follows the rule of the system (`link-target.ts`).
|
|
15
17
|
* - The procedure gets the absolute path, so the paths of the output are absolute.
|
|
16
18
|
*
|
|
17
19
|
* On Windows, the comparison ignores the case of the letters, and `/` and `\` are the same.
|
|
@@ -19,6 +21,7 @@
|
|
|
19
21
|
import { readlink, realpath } from "node:fs/promises";
|
|
20
22
|
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
21
23
|
import { InvocationError } from "@procedures/client";
|
|
24
|
+
import { isMissing, linkDestination } from "./link-target.js";
|
|
22
25
|
/** The reason of the `FORBIDDEN` error for a path outside the root. */
|
|
23
26
|
export const OUTSIDE_ROOT = "outside-root";
|
|
24
27
|
/** The maximum number of symlinks that one check follows. After it, the check fails with `ELOOP`. */
|
|
@@ -31,11 +34,6 @@ export function isInside(folder, path) {
|
|
|
31
34
|
const rel = relative(folder, path);
|
|
32
35
|
return rel === "" || (!isAbsolute(rel) && rel !== ".." && !rel.startsWith(`..${sep}`));
|
|
33
36
|
}
|
|
34
|
-
/** True for the error of a path that does not exist. */
|
|
35
|
-
function isMissing(error) {
|
|
36
|
-
const code = error?.code;
|
|
37
|
-
return code === "ENOENT" || code === "ENOTDIR";
|
|
38
|
-
}
|
|
39
37
|
/** The target of a symlink, or undefined when the path is not a symlink or does not exist. */
|
|
40
38
|
async function linkTarget(path) {
|
|
41
39
|
try {
|
|
@@ -72,7 +70,7 @@ export async function realPathOf(path, links = 0) {
|
|
|
72
70
|
loop.code = "ELOOP";
|
|
73
71
|
throw loop;
|
|
74
72
|
}
|
|
75
|
-
return join(await realPathOf(
|
|
73
|
+
return join(await realPathOf(await linkDestination(current, target), links + 1), ...rest);
|
|
76
74
|
}
|
|
77
75
|
const parent = dirname(current);
|
|
78
76
|
// No ancestor exists, for example a drive that is not there
|
package/dist/root.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"root.js","sourceRoot":"","sources":["../src/root.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"root.js","sourceRoot":"","sources":["../src/root.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AACxF,OAAO,EAAE,eAAe,EAAsB,MAAM,oBAAoB,CAAC;AACzE,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAE9D,uEAAuE;AACvE,MAAM,CAAC,MAAM,YAAY,GAAG,cAAc,CAAC;AAE3C,qGAAqG;AACrG,MAAM,SAAS,GAAG,EAAE,CAAC;AAErB;;;GAGG;AACH,MAAM,UAAU,QAAQ,CAAC,MAAc,EAAE,IAAY;IACnD,MAAM,GAAG,GAAG,QAAQ,CAAC,MA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1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@procedures/fs",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.55.0",
|
|
5
5
|
"description": "File system procedures: read, write, copy, move, glob, exists, mkdir and JSON read and write, callable in process, over RPC or from Claude through MCP.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"filesystem",
|
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
"procedures": "./dist/register.js"
|
|
71
71
|
},
|
|
72
72
|
"dependencies": {
|
|
73
|
-
"@procedures/client": "0.
|
|
73
|
+
"@procedures/client": "0.55.0",
|
|
74
74
|
"zod": "^4.6.5"
|
|
75
75
|
},
|
|
76
76
|
"devDependencies": {
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The destination of a symlink: the path that a relative target of a link leads to, as the system
|
|
3
|
+
* resolves it. The root check (`root.ts`) uses it for a link whose target does not exist, because
|
|
4
|
+
* a write follows such a link and makes its target.
|
|
5
|
+
*
|
|
6
|
+
* The two systems use different rules for a `..` in a relative target:
|
|
7
|
+
* - POSIX follows the folders: `..` goes up from the real folder that the names before it lead to.
|
|
8
|
+
* So in the target `a/../x`, where `a` is a link to a folder, `..` is the parent of the target of
|
|
9
|
+
* `a`.
|
|
10
|
+
* - Windows reads the target as text: it joins the real folder of the link and the target, and a
|
|
11
|
+
* `..` removes the name before it. So `a\..\x` is `x` in the folder of the link.
|
|
12
|
+
*
|
|
13
|
+
* A test of `root.test.ts` writes through a table of links with `node:fs` on each system and
|
|
14
|
+
* compares the file that the system makes with the result of this function.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { realpath } from "node:fs/promises";
|
|
18
|
+
import { dirname, join, parse, resolve, sep } from "node:path";
|
|
19
|
+
|
|
20
|
+
/** True for the error of a path that does not exist. */
|
|
21
|
+
export function isMissing(error: unknown): boolean {
|
|
22
|
+
const code = (error as NodeJS.ErrnoException | undefined)?.code;
|
|
23
|
+
return code === "ENOENT" || code === "ENOTDIR";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The real path of a path, or the path itself when it does not exist. */
|
|
27
|
+
async function realOrSame(path: string): Promise<string> {
|
|
28
|
+
try {
|
|
29
|
+
return await realpath(path);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
if (isMissing(error)) return path;
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* How a system reads a relative target of a symlink: `folders` follows the real folders of the
|
|
38
|
+
* names (POSIX), `text` removes a name before `..` as text (Windows).
|
|
39
|
+
*/
|
|
40
|
+
export type LinkRule = "folders" | "text";
|
|
41
|
+
|
|
42
|
+
/** The rule of a system: `text` on Windows, `folders` on each other system. */
|
|
43
|
+
export function linkRuleOf(platform: NodeJS.Platform): LinkRule {
|
|
44
|
+
return platform === "win32" ? "text" : "folders";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The rule of the system of this process. */
|
|
48
|
+
export const SYSTEM_LINK_RULE: LinkRule = linkRuleOf(process.platform);
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* This function gives the path that the target of a symlink leads to. A relative target starts at
|
|
52
|
+
* the real folder of the link, not at the text of the path: an earlier link to a folder changes
|
|
53
|
+
* the folder of the link. With the rule `folders`, each name of the target before its last name
|
|
54
|
+
* goes to its real path, so a `..` after a link goes up from the target of that link. With the
|
|
55
|
+
* rule `text`, the target joins the real folder of the link as text.
|
|
56
|
+
*/
|
|
57
|
+
export async function linkDestination(link: string, target: string, rule: LinkRule = SYSTEM_LINK_RULE): Promise<string> {
|
|
58
|
+
// The folder of the link exists: the system read the link in it
|
|
59
|
+
const folder = await realpath(dirname(link));
|
|
60
|
+
if (rule === "text") return resolve(folder, target);
|
|
61
|
+
const { root } = parse(target);
|
|
62
|
+
let current = root === "" ? folder : resolve(folder, root);
|
|
63
|
+
const names = target
|
|
64
|
+
.slice(root.length)
|
|
65
|
+
.split(sep === "\\" ? /[\\/]/ : "/")
|
|
66
|
+
.filter((name) => name !== "" && name !== ".");
|
|
67
|
+
for (const [index, name] of names.entries()) {
|
|
68
|
+
if (name === "..") {
|
|
69
|
+
current = dirname(current);
|
|
70
|
+
} else {
|
|
71
|
+
current = join(current, name);
|
|
72
|
+
if (index < names.length - 1) current = await realOrSame(current);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return current;
|
|
76
|
+
}
|
package/src/root.ts
CHANGED
|
@@ -12,6 +12,8 @@
|
|
|
12
12
|
* - The check uses the real path (`fs.realpath`) of the path. For a path that does not exist, it
|
|
13
13
|
* uses the real path of the nearest existing ancestor. So a symlinked parent folder cannot
|
|
14
14
|
* lead a write or a `mkdir` out of the root.
|
|
15
|
+
* - A relative target of a symlink starts at the real folder of the link, not at the text of the
|
|
16
|
+
* path. A `..` in the target follows the rule of the system (`link-target.ts`).
|
|
15
17
|
* - The procedure gets the absolute path, so the paths of the output are absolute.
|
|
16
18
|
*
|
|
17
19
|
* On Windows, the comparison ignores the case of the letters, and `/` and `\` are the same.
|
|
@@ -20,6 +22,7 @@
|
|
|
20
22
|
import { readlink, realpath } from "node:fs/promises";
|
|
21
23
|
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
22
24
|
import { InvocationError, type ProcedurePath } from "@procedures/client";
|
|
25
|
+
import { isMissing, linkDestination } from "./link-target.js";
|
|
23
26
|
|
|
24
27
|
/** The reason of the `FORBIDDEN` error for a path outside the root. */
|
|
25
28
|
export const OUTSIDE_ROOT = "outside-root";
|
|
@@ -36,12 +39,6 @@ export function isInside(folder: string, path: string): boolean {
|
|
|
36
39
|
return rel === "" || (!isAbsolute(rel) && rel !== ".." && !rel.startsWith(`..${sep}`));
|
|
37
40
|
}
|
|
38
41
|
|
|
39
|
-
/** True for the error of a path that does not exist. */
|
|
40
|
-
function isMissing(error: unknown): boolean {
|
|
41
|
-
const code = (error as NodeJS.ErrnoException | undefined)?.code;
|
|
42
|
-
return code === "ENOENT" || code === "ENOTDIR";
|
|
43
|
-
}
|
|
44
|
-
|
|
45
42
|
/** The target of a symlink, or undefined when the path is not a symlink or does not exist. */
|
|
46
43
|
async function linkTarget(path: string): Promise<string | undefined> {
|
|
47
44
|
try {
|
|
@@ -75,7 +72,7 @@ export async function realPathOf(path: string, links = 0): Promise<string> {
|
|
|
75
72
|
loop.code = "ELOOP";
|
|
76
73
|
throw loop;
|
|
77
74
|
}
|
|
78
|
-
return join(await realPathOf(
|
|
75
|
+
return join(await realPathOf(await linkDestination(current, target), links + 1), ...rest);
|
|
79
76
|
}
|
|
80
77
|
const parent = dirname(current);
|
|
81
78
|
// No ancestor exists, for example a drive that is not there
|