Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(cli): confine the file template copy writes, not just its folder (#6563)

Joeya8c3461dea4f
diff --git a/.changeset/cli-spec-template-copy-confine-file.md b/.changeset/cli-spec-template-copy-confine-file.md
new file mode 100644
index 0000000000..b06c939ff1
--- /dev/null
+++ b/.changeset/cli-spec-template-copy-confine-file.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] `astryx template <name> <dir>` now refuses the write, with `ERR_PATH_TRAVERSAL`, when the file it would create in that directory is a symlink to an existing file outside the project. Before, only the directory was checked, so `--overwrite` followed the link and replaced the file it pointed at.
+
+@josephfarina
diff --git a/packages/cli/api/template/copy/copy.mjs b/packages/cli/api/template/copy/copy.mjs
index 82113d08e2..e7cc0646eb 100644
--- a/packages/cli/api/template/copy/copy.mjs
+++ b/packages/cli/api/template/copy/copy.mjs
@@ -36,13 +36,22 @@ export function templateCopy(match, {targetPath, cwd, overwrite = false}) {
);
}
- // Path-safety: resolve the user-supplied targetPath relative to cwd,
- // rejecting absolute paths and any traversal that escapes the project
- // root. This guard runs BEFORE any mkdir/copyFile so we never create
- // directories outside the root just to fail on the file write.
- let resolvedTarget;
+ // If targetPath looks like a file (e.g. `./foo.tsx`), write directly to
+ // it. Previously this path was treated as a directory and the file was
+ // written as `./foo.tsx/page.tsx`, which is wrong and surprising.
+ const fileTarget = isFilePathArg(targetPath)
+ ? targetPath
+ : path.join(
+ targetPath,
+ match.type === 'block' ? path.basename(match.filePath) : 'page.tsx',
+ );
+
+ // Path-safety: the guard sees the file that will be written, not only its
+ // directory — a symlink at that name would otherwise carry the write
+ // outside the project root. Runs BEFORE any mkdir/write.
+ let outputFilePath;
try {
- resolvedTarget = assertWithin(targetPath, cwd, {
+ outputFilePath = assertWithin(fileTarget, cwd, {
label: 'template target path',
});
} catch (err) {
@@ -55,23 +64,8 @@ export function templateCopy(match, {targetPath, cwd, overwrite = false}) {
}
throw err;
}
-
- // If targetPath looks like a file (e.g. `./foo.tsx`), write directly to
- // it. Previously this path was treated as a directory and the file was
- // written as `./foo.tsx/page.tsx`, which is wrong and surprising.
- let outputDir;
- let outputFileName;
- let outputFilePath;
- if (isFilePathArg(targetPath)) {
- outputDir = path.dirname(resolvedTarget);
- outputFileName = path.basename(resolvedTarget);
- outputFilePath = resolvedTarget;
- } else {
- outputDir = resolvedTarget;
- outputFileName =
- match.type === 'block' ? path.basename(match.filePath) : 'page.tsx';
- outputFilePath = path.join(outputDir, outputFileName);
- }
+ const outputDir = path.dirname(outputFilePath);
+ const outputFileName = path.basename(outputFilePath);
// Refuse to clobber an existing file unless the caller opts in. The CLI has
// its own pre-flight collision message, but the API is a public surface
diff --git a/packages/cli/api/template/copy/copy.test.mjs b/packages/cli/api/template/copy/copy.test.mjs
index 4a637a6eed..d0189f1d83 100644
--- a/packages/cli/api/template/copy/copy.test.mjs
+++ b/packages/cli/api/template/copy/copy.test.mjs
@@ -48,4 +48,21 @@ describe('template.copy — overwrite + path safety', () => {
).rejects.toMatchObject({code: 'ERR_PATH_TRAVERSAL'});
expect(fs.existsSync(path.join(dir, '..', 'escape.tsx'))).toBe(false);
}, SLOW);
+
+ it('rejects a directory target whose page.tsx is a symlink leading outside cwd', async () => {
+ const outside = fs.mkdtempSync(path.join(os.tmpdir(), 'tmpl-copy-outside-'));
+ try {
+ const victim = path.join(outside, 'victim.tsx');
+ fs.writeFileSync(victim, 'OUTSIDE');
+ fs.mkdirSync(path.join(dir, 'dest'));
+ fs.symlinkSync(victim, path.join(dir, 'dest', 'page.tsx'));
+
+ await expect(
+ template('blank', {targetPath: './dest', overwrite: true, cwd: dir}),
+ ).rejects.toMatchObject({code: 'ERR_PATH_TRAVERSAL'});
+ expect(fs.readFileSync(victim, 'utf-8')).toBe('OUTSIDE');
+ } finally {
+ fs.rmSync(outside, {recursive: true, force: true});
+ }
+ }, SLOW);
});