@astryxdesign/cli 0.6.3-canary.7482949 → 0.6.3-canary.7cd3516
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.
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*
|
|
8
8
|
* - Absolute paths (e.g. `/tmp/x`) — re-rooted by `path.join` is silent;
|
|
9
9
|
* better to fail loudly. Pass `{allowAbsolute: true}` to opt in.
|
|
10
|
-
* - Paths that escape via `..` segments.
|
|
10
|
+
* - Paths that escape via `..` segments or a symlink, dangling or not.
|
|
11
11
|
*
|
|
12
12
|
* Returns the resolved absolute path on success.
|
|
13
13
|
*
|
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
* @param {string} [options.label='path'] - Human-readable name of the arg
|
|
20
20
|
* being checked (used in error messages, e.g. 'output directory').
|
|
21
21
|
* @returns {string} Absolute resolved path inside `rootDir`.
|
|
22
|
-
* @throws {PathSafetyError}
|
|
22
|
+
* @throws {PathSafetyError} An escape carries the registered
|
|
23
|
+
* `ERR_PATH_TRAVERSAL`, so it reaches an envelope correctly even uncaught.
|
|
23
24
|
*/
|
|
24
25
|
export function assertWithin(targetPath: string, rootDir: string, options?: {
|
|
25
26
|
allowAbsolute?: boolean | undefined;
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
import * as path from 'node:path';
|
|
16
16
|
import * as fs from 'node:fs';
|
|
17
|
+
import {ERROR_CODES} from '../response/error-codes.mjs';
|
|
17
18
|
|
|
18
19
|
/**
|
|
19
20
|
* Error thrown by path-safety guards. Carries a stable `code`
|
|
@@ -38,7 +39,7 @@ export class PathSafetyError extends Error {
|
|
|
38
39
|
*
|
|
39
40
|
* - Absolute paths (e.g. `/tmp/x`) — re-rooted by `path.join` is silent;
|
|
40
41
|
* better to fail loudly. Pass `{allowAbsolute: true}` to opt in.
|
|
41
|
-
* - Paths that escape via `..` segments.
|
|
42
|
+
* - Paths that escape via `..` segments or a symlink, dangling or not.
|
|
42
43
|
*
|
|
43
44
|
* Returns the resolved absolute path on success.
|
|
44
45
|
*
|
|
@@ -50,7 +51,8 @@ export class PathSafetyError extends Error {
|
|
|
50
51
|
* @param {string} [options.label='path'] - Human-readable name of the arg
|
|
51
52
|
* being checked (used in error messages, e.g. 'output directory').
|
|
52
53
|
* @returns {string} Absolute resolved path inside `rootDir`.
|
|
53
|
-
* @throws {PathSafetyError}
|
|
54
|
+
* @throws {PathSafetyError} An escape carries the registered
|
|
55
|
+
* `ERR_PATH_TRAVERSAL`, so it reaches an envelope correctly even uncaught.
|
|
54
56
|
*/
|
|
55
57
|
export function assertWithin(targetPath, rootDir, options = {}) {
|
|
56
58
|
const {allowAbsolute = false, label = 'path'} = options;
|
|
@@ -83,42 +85,70 @@ export function assertWithin(targetPath, rootDir, options = {}) {
|
|
|
83
85
|
throw new PathSafetyError(
|
|
84
86
|
`Invalid ${label} "${targetPath}": resolves outside the project root ` +
|
|
85
87
|
`(${absRoot}). Path traversal is not allowed.`,
|
|
86
|
-
|
|
88
|
+
ERROR_CODES.ERR_PATH_TRAVERSAL,
|
|
87
89
|
);
|
|
88
90
|
}
|
|
89
91
|
|
|
90
92
|
// path.resolve is purely lexical — it does NOT follow symlinks, so a symlink
|
|
91
93
|
// INSIDE the root that points outside would pass the check above while the
|
|
92
|
-
// real write lands outside root. Canonicalize
|
|
93
|
-
//
|
|
94
|
-
//
|
|
95
|
-
// writes through this guard.
|
|
94
|
+
// real write lands outside root. Canonicalize both paths through every
|
|
95
|
+
// symlink, dangling ones included (a write through a dangling link creates
|
|
96
|
+
// its target), and re-check.
|
|
96
97
|
try {
|
|
97
|
-
const realRoot =
|
|
98
|
-
|
|
99
|
-
while (!fs.existsSync(existing)) {
|
|
100
|
-
const parent = path.dirname(existing);
|
|
101
|
-
if (parent === existing) break;
|
|
102
|
-
existing = parent;
|
|
103
|
-
}
|
|
104
|
-
const realExisting = fs.realpathSync(existing);
|
|
98
|
+
const realRoot = canonicalPath(absRoot);
|
|
99
|
+
const realTarget = canonicalPath(resolved);
|
|
105
100
|
const realRootWithSep = realRoot.endsWith(path.sep) ? realRoot : realRoot + path.sep;
|
|
106
|
-
if (
|
|
101
|
+
if (realTarget !== realRoot && !realTarget.startsWith(realRootWithSep)) {
|
|
107
102
|
throw new PathSafetyError(
|
|
108
103
|
`Invalid ${label} "${targetPath}": resolves outside the project root ` +
|
|
109
104
|
`(${absRoot}) via a symlink. Path traversal is not allowed.`,
|
|
110
|
-
|
|
105
|
+
ERROR_CODES.ERR_PATH_TRAVERSAL,
|
|
111
106
|
);
|
|
112
107
|
}
|
|
113
108
|
} catch (err) {
|
|
114
109
|
if (err instanceof PathSafetyError) throw err;
|
|
115
|
-
//
|
|
116
|
-
//
|
|
110
|
+
// A symlink loop, a permission error, or a race; fall back to the lexical
|
|
111
|
+
// result already validated above rather than crash.
|
|
117
112
|
}
|
|
118
113
|
|
|
119
114
|
return resolved;
|
|
120
115
|
}
|
|
121
116
|
|
|
117
|
+
/** Linux's MAXSYMLINKS; a longer chain is treated as a loop. */
|
|
118
|
+
const MAX_SYMLINK_HOPS = 40;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* `target` with every symlink resolved, including a dangling one, which
|
|
122
|
+
* `fs.realpathSync` refuses. Components below the deepest existing entry are
|
|
123
|
+
* appended as given.
|
|
124
|
+
*
|
|
125
|
+
* @param {string} target absolute path
|
|
126
|
+
* @param {number} [hops] symlinks followed so far
|
|
127
|
+
* @returns {string}
|
|
128
|
+
* @throws when the chain exceeds {@link MAX_SYMLINK_HOPS} or `lstat` fails
|
|
129
|
+
* for a reason other than a missing entry
|
|
130
|
+
*/
|
|
131
|
+
function canonicalPath(target, hops = 0) {
|
|
132
|
+
if (hops > MAX_SYMLINK_HOPS) throw new Error(`Too many symlinks: ${target}`);
|
|
133
|
+
let stat;
|
|
134
|
+
try {
|
|
135
|
+
stat = fs.lstatSync(target);
|
|
136
|
+
} catch (err) {
|
|
137
|
+
const code = /** @type {NodeJS.ErrnoException} */ (err).code;
|
|
138
|
+
if (code !== 'ENOENT' && code !== 'ENOTDIR') throw err;
|
|
139
|
+
const parent = path.dirname(target);
|
|
140
|
+
if (parent === target) return target;
|
|
141
|
+
return path.join(canonicalPath(parent, hops), path.basename(target));
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
return fs.realpathSync(target);
|
|
145
|
+
} catch (err) {
|
|
146
|
+
if (!stat.isSymbolicLink()) throw err;
|
|
147
|
+
}
|
|
148
|
+
const link = fs.readlinkSync(target);
|
|
149
|
+
return canonicalPath(path.resolve(fs.realpathSync(path.dirname(target)), link), hops + 1);
|
|
150
|
+
}
|
|
151
|
+
|
|
122
152
|
/**
|
|
123
153
|
* Validate a "name" argument (e.g. theme name, component name) that will
|
|
124
154
|
* be embedded in a generated filename. Rejects names containing path
|
|
@@ -19,6 +19,19 @@ afterEach(() => {
|
|
|
19
19
|
fs.rmSync(tmpDir, {recursive: true, force: true});
|
|
20
20
|
});
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* The code a guard rejected with, or 'accepted'.
|
|
24
|
+
* @param {() => unknown} fn
|
|
25
|
+
*/
|
|
26
|
+
function rejectionCode(fn) {
|
|
27
|
+
try {
|
|
28
|
+
fn();
|
|
29
|
+
} catch (err) {
|
|
30
|
+
return err instanceof PathSafetyError ? err.code : err;
|
|
31
|
+
}
|
|
32
|
+
return 'accepted';
|
|
33
|
+
}
|
|
34
|
+
|
|
22
35
|
describe('assertWithin', () => {
|
|
23
36
|
it('accepts a relative path that stays inside root', () => {
|
|
24
37
|
const result = assertWithin('subdir/file.txt', tmpDir);
|
|
@@ -38,6 +51,14 @@ describe('assertWithin', () => {
|
|
|
38
51
|
expect(() => assertWithin('a/b/../../../c', tmpDir)).toThrow(PathSafetyError);
|
|
39
52
|
});
|
|
40
53
|
|
|
54
|
+
it('reports an escape with the registered ERR_PATH_TRAVERSAL code', () => {
|
|
55
|
+
// An uncaught guard error reaches the JSON envelope with this code as-is.
|
|
56
|
+
expect(rejectionCode(() => assertWithin('../escaped', tmpDir))).toBe('ERR_PATH_TRAVERSAL');
|
|
57
|
+
expect(
|
|
58
|
+
rejectionCode(() => assertWithin('/etc/passwd', tmpDir, {allowAbsolute: true})),
|
|
59
|
+
).toBe('ERR_PATH_TRAVERSAL');
|
|
60
|
+
});
|
|
61
|
+
|
|
41
62
|
it('rejects absolute paths by default', () => {
|
|
42
63
|
expect(() => assertWithin('/tmp/elsewhere', tmpDir)).toThrow(/absolute paths are not allowed/i);
|
|
43
64
|
});
|
|
@@ -160,6 +181,7 @@ describe('assertWithin — symlink escape (realpath guard)', () => {
|
|
|
160
181
|
// path.resolve is lexical and would pass this; the realpath check must catch it.
|
|
161
182
|
expect(() => assertWithin('link/evil.txt', root)).toThrow(PathSafetyError);
|
|
162
183
|
expect(() => assertWithin('link/evil.txt', root)).toThrow(/outside|symlink|traversal/i);
|
|
184
|
+
expect(rejectionCode(() => assertWithin('link/evil.txt', root))).toBe('ERR_PATH_TRAVERSAL');
|
|
163
185
|
});
|
|
164
186
|
|
|
165
187
|
it('still accepts a legit non-existent nested path inside root', () => {
|
|
@@ -169,4 +191,32 @@ describe('assertWithin — symlink escape (realpath guard)', () => {
|
|
|
169
191
|
it('rejects a NUL byte in the path', () => {
|
|
170
192
|
expect(() => assertWithin('a\u0000b', root)).toThrow(PathSafetyError);
|
|
171
193
|
});
|
|
194
|
+
|
|
195
|
+
it('rejects a dangling symlink whose target is outside root', () => {
|
|
196
|
+
// A write through the link would create the missing target outside root.
|
|
197
|
+
fs.symlinkSync(path.join(outside, 'created.txt'), path.join(root, 'leaf'));
|
|
198
|
+
expect(rejectionCode(() => assertWithin('leaf', root))).toBe('ERR_PATH_TRAVERSAL');
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('rejects a path through a dangling directory symlink that points outside root', () => {
|
|
202
|
+
fs.symlinkSync(path.join(outside, 'missing-dir'), path.join(root, 'dir'));
|
|
203
|
+
expect(rejectionCode(() => assertWithin('dir/evil.txt', root))).toBe('ERR_PATH_TRAVERSAL');
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('follows a chain of links that ends in a dangling target outside root', () => {
|
|
207
|
+
fs.symlinkSync(path.join(outside, 'missing.txt'), path.join(root, 'second'));
|
|
208
|
+
fs.symlinkSync('second', path.join(root, 'first'));
|
|
209
|
+
expect(rejectionCode(() => assertWithin('first', root))).toBe('ERR_PATH_TRAVERSAL');
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it('accepts a dangling symlink whose target stays inside root', () => {
|
|
213
|
+
fs.symlinkSync(path.join('sub', 'new.txt'), path.join(root, 'leaf'));
|
|
214
|
+
expect(assertWithin('leaf', root)).toBe(path.join(root, 'leaf'));
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('terminates on a symlink loop', () => {
|
|
218
|
+
fs.symlinkSync('loop', path.join(root, 'loop'));
|
|
219
|
+
const result = rejectionCode(() => assertWithin('loop', root));
|
|
220
|
+
expect(['accepted', 'ERR_PATH_TRAVERSAL']).toContain(result);
|
|
221
|
+
});
|
|
172
222
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astryxdesign/cli",
|
|
3
|
-
"version": "0.6.3-canary.
|
|
3
|
+
"version": "0.6.3-canary.7cd3516",
|
|
4
4
|
"displayName": "CLI",
|
|
5
5
|
"description": "Scaffold projects, browse templates, generate themes, and get agent-ready docs from the command line.",
|
|
6
6
|
"author": "Meta Open Source",
|
|
@@ -100,10 +100,10 @@
|
|
|
100
100
|
"zod": "^4.4.3"
|
|
101
101
|
},
|
|
102
102
|
"peerDependencies": {
|
|
103
|
-
"@astryxdesign/charts": "0.6.3-canary.
|
|
104
|
-
"@astryxdesign/core": "0.6.3-canary.
|
|
105
|
-
"@astryxdesign/lab": "0.6.3-canary.
|
|
106
|
-
"@astryxdesign/theme-neutral": "0.6.3-canary.
|
|
103
|
+
"@astryxdesign/charts": "0.6.3-canary.7cd3516",
|
|
104
|
+
"@astryxdesign/core": "0.6.3-canary.7cd3516",
|
|
105
|
+
"@astryxdesign/lab": "0.6.3-canary.7cd3516",
|
|
106
|
+
"@astryxdesign/theme-neutral": "0.6.3-canary.7cd3516"
|
|
107
107
|
},
|
|
108
108
|
"peerDependenciesMeta": {
|
|
109
109
|
"@astryxdesign/charts": {
|
|
@@ -120,10 +120,10 @@
|
|
|
120
120
|
}
|
|
121
121
|
},
|
|
122
122
|
"devDependencies": {
|
|
123
|
-
"@astryxdesign/charts": "0.6.3-canary.
|
|
124
|
-
"@astryxdesign/core": "0.6.3-canary.
|
|
125
|
-
"@astryxdesign/lab": "0.6.3-canary.
|
|
126
|
-
"@astryxdesign/theme-neutral": "0.6.3-canary.
|
|
123
|
+
"@astryxdesign/charts": "0.6.3-canary.7cd3516",
|
|
124
|
+
"@astryxdesign/core": "0.6.3-canary.7cd3516",
|
|
125
|
+
"@astryxdesign/lab": "0.6.3-canary.7cd3516",
|
|
126
|
+
"@astryxdesign/theme-neutral": "0.6.3-canary.7cd3516",
|
|
127
127
|
"@heroicons/react": "^2.2.0",
|
|
128
128
|
"@stylexjs/stylex": "^0.19.0",
|
|
129
129
|
"@types/babel__core": "^7.20.5",
|