An open source design system that's fully customizable and agent ready
TypeScript
fix(cli): confine every swizzle write, not just the output base (#6573)
Joey63d3eda200bc
diff --git a/.changeset/cli-spec-swizzle-confine-writes.md b/.changeset/cli-spec-swizzle-confine-writes.mdnew file mode 100644index 0000000000..aec3d3a23a--- /dev/null+++ b/.changeset/cli-spec-swizzle-confine-writes.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[fix] `astryx swizzle` no longer writes outside the project through a symlink in the output folder. When the component folder or one of its files is a symlink that points outside the project, the command now fails with `ERR_PATH_TRAVERSAL` before it writes anything.++@josephfarinadiff --git a/packages/cli/api/swizzle/copy/copy.mjs b/packages/cli/api/swizzle/copy/copy.mjsindex 96d34ff3b3..f3b7178814 100644--- a/packages/cli/api/swizzle/copy/copy.mjs+++ b/packages/cli/api/swizzle/copy/copy.mjs@@ -159,6 +159,24 @@ function resolveOwners(coreDir, loadedIntegrations, name, coreIssuesUrl) {return owners;}+/**+ * `assertWithin`, with an escape reported as ERR_PATH_TRAVERSAL.+ * @param {string} target+ * @param {string} cwd+ * @param {{allowAbsolute?: boolean, label: string}} options+ * @returns {string}+ */+function confineWrite(target, cwd, options) {+ try {+ return assertWithin(target, cwd, options);+ } catch (err) {+ if (err instanceof PathSafetyError) {+ throw new AstryxError(err.message, [], ERROR_CODES.ERR_PATH_TRAVERSAL);+ }+ throw err;+ }+}+/** @param {string} file */function isExcludedFromCopy(file) {return (@@ -243,23 +261,22 @@ export async function swizzleCopy(component, options = {}) {const componentDir = owner.sourceDir;- // Path-safety: --output must resolve inside cwd.- let outputBase;- try {- outputBase = assertWithin(output, cwd, {label: 'output directory'});- } catch (err) {- if (err instanceof PathSafetyError) {- throw new AstryxError(err.message, [], ERROR_CODES.ERR_PATH_TRAVERSAL);- }- throw err;- }+ // Path-safety: every write target resolves inside cwd, not just --output. An+ // existing component directory or file may be a symlink out of cwd.+ const outputBase = confineWrite(output, cwd, {label: 'output directory'});const outputDir = path.join(outputBase, dirName);- // Pre-flight overwrite check before any mkdir/writeFile.+ // Pre-flight path and overwrite checks before any mkdir/writeFile.const sourceFiles = fs.readdirSync(componentDir).filter(file => {if (isExcludedFromCopy(file)) return false;return fs.statSync(path.join(componentDir, file)).isFile();});+ for (const file of sourceFiles) {+ confineWrite(path.join(outputDir, file), cwd, {+ allowAbsolute: true,+ label: 'output file',+ });+ }const existingFiles = sourceFiles.filter(f =>fs.existsSync(path.join(outputDir, f)),);diff --git a/packages/cli/api/swizzle/swizzle.doc.mjs b/packages/cli/api/swizzle/swizzle.doc.mjsindex 1558b5518d..ffb4a5f5cf 100644--- a/packages/cli/api/swizzle/swizzle.doc.mjs+++ b/packages/cli/api/swizzle/swizzle.doc.mjs@@ -80,7 +80,7 @@ export const doc = {},{code: 'ERR_PATH_TRAVERSAL',- when: 'the component name contains a path separator or traversal, or output resolves outside cwd',+ when: 'the component name contains a path separator or traversal, output resolves outside cwd, or an existing output file or directory is a symlink that resolves outside cwd',},{code: 'ERR_UNKNOWN_COMPONENT',diff --git a/packages/cli/clients/cli/commands/swizzle.path-safety.test.mjs b/packages/cli/clients/cli/commands/swizzle.path-safety.test.mjsindex f3de4f5667..91e723cf90 100644--- a/packages/cli/clients/cli/commands/swizzle.path-safety.test.mjs+++ b/packages/cli/clients/cli/commands/swizzle.path-safety.test.mjs@@ -74,6 +74,37 @@ describe('swizzle path safety', () => {expect(fs.existsSync(absTarget)).toBe(false);});+ it('rejects a component directory that is a symlink out of the project', async () => {+ const {project, outside} = buildFakeRepo(tmpDir);+ const base = path.join(project, 'components', 'astryx');+ fs.mkdirSync(base, {recursive: true});+ fs.symlinkSync(outside, path.join(base, 'Button'));++ const result = await runCli(['--json', 'swizzle', 'Button'], project);++ expect(result.code).not.toBe(0);+ expect(JSON.parse(result.stdout).code).toBe('ERR_PATH_TRAVERSAL');+ expect(fs.readdirSync(outside)).toEqual([]);+ });++ it('rejects an existing file that is a symlink out of the project, even with --overwrite', async () => {+ const {project, outside} = buildFakeRepo(tmpDir);+ const outDir = path.join(project, 'components', 'astryx', 'Button');+ fs.mkdirSync(outDir, {recursive: true});+ const target = path.join(outside, 'kept.txt');+ fs.writeFileSync(target, 'kept\n');+ fs.symlinkSync(target, path.join(outDir, 'Button.tsx'));++ const result = await runCli(+ ['--json', 'swizzle', 'Button', '--overwrite'],+ project,+ );++ expect(result.code).not.toBe(0);+ expect(JSON.parse(result.stdout).code).toBe('ERR_PATH_TRAVERSAL');+ expect(fs.readFileSync(target, 'utf-8')).toBe('kept\n');+ });+it('requires --overwrite in non-interactive mode when files already exist', async () => {const {project} = buildFakeRepo(tmpDir);const outDir = path.join(project, 'components', 'astryx', 'Button');