Skip to content

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

TypeScript

fix(cli): confine the file layout expand writes, not just its folder (#6564)

Joeycb697769ca24
diff --git a/.changeset/cli-spec-layout-expand-confine-file.md b/.changeset/cli-spec-layout-expand-confine-file.md
new file mode 100644
index 0000000000..836f034a64
--- /dev/null
+++ b/.changeset/cli-spec-layout-expand-confine-file.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] `astryx layout expand <expr> <dir>` now refuses the write, with `ERR_PATH_TRAVERSAL`, when `<dir>/<Name>.tsx` is a symlink to an existing file outside the project. Before, only the directory was checked, so the generated TSX replaced the file the link pointed at.
+
+@josephfarina
diff --git a/packages/cli/api/layout/expand/expand.mjs b/packages/cli/api/layout/expand/expand.mjs
index 4cd295e6fd..3363ed69aa 100644
--- a/packages/cli/api/layout/expand/expand.mjs
+++ b/packages/cli/api/layout/expand/expand.mjs
@@ -120,18 +120,20 @@ export async function layoutExpand(expression, options = {}) {
let written = null;
if (targetPath) {
- let resolved;
+ // 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 root.
+ const fileTarget = isFilePathArg(targetPath)
+ ? targetPath
+ : path.join(targetPath, `${componentName}.tsx`);
+ let filePath;
try {
- resolved = assertWithin(targetPath, cwd, {label: 'layout target path'});
+ filePath = assertWithin(fileTarget, cwd, {label: 'layout target path'});
} catch (err) {
if (err instanceof PathSafetyError) {
throw new AstryxError(err.message, undefined, ERROR_CODES.ERR_PATH_TRAVERSAL);
}
throw err;
}
- const filePath = isFilePathArg(targetPath)
- ? resolved
- : path.join(resolved, `${componentName}.tsx`);
fs.mkdirSync(path.dirname(filePath), {recursive: true});
fs.writeFileSync(filePath, result.code);
written = path.relative(cwd, filePath);
diff --git a/packages/cli/api/layout/expand/expand.path-safety.test.mjs b/packages/cli/api/layout/expand/expand.path-safety.test.mjs
new file mode 100644
index 0000000000..62d53f1ace
--- /dev/null
+++ b/packages/cli/api/layout/expand/expand.path-safety.test.mjs
@@ -0,0 +1,53 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file Path-safety tests for the `layout.expand` write. The file written, not
+ * only the directory it lands in, must stay inside the project root.
+ */
+
+import {describe, it, expect, beforeAll} from 'vitest';
+import * as fs from 'node:fs';
+import * as os from 'node:os';
+import * as path from 'node:path';
+import {layoutExpand} from '../layout.mjs';
+import {buildRegistry} from '../../../foundation/xle/registry.mjs';
+
+const SLOW = 30_000;
+
+// The registry imports every component doc on first use; warm it once.
+beforeAll(async () => {
+ await buildRegistry();
+}, 120_000);
+
+describe('layout.expand — write confinement', () => {
+ it('rejects a directory target whose generated file is a symlink leading outside cwd', async () => {
+ // Inside the workspace so @astryxdesign/core resolves.
+ const cwd = fs.mkdtempSync(path.join(process.cwd(), '.xle-confine-test-'));
+ const outside = fs.mkdtempSync(path.join(os.tmpdir(), 'xle-confine-outside-'));
+ try {
+ const victim = path.join(outside, 'victim.tsx');
+ fs.writeFileSync(victim, 'OUTSIDE');
+ fs.mkdirSync(path.join(cwd, 'out'));
+ fs.symlinkSync(victim, path.join(cwd, 'out', 'GeneratedLayout.tsx'));
+
+ await expect(
+ layoutExpand('V > B', {targetPath: './out', cwd}),
+ ).rejects.toMatchObject({code: 'ERR_PATH_TRAVERSAL'});
+ expect(fs.readFileSync(victim, 'utf-8')).toBe('OUTSIDE');
+ } finally {
+ fs.rmSync(cwd, {recursive: true, force: true});
+ fs.rmSync(outside, {recursive: true, force: true});
+ }
+ }, SLOW);
+
+ it('still writes <Name>.tsx into a plain directory target', async () => {
+ const cwd = fs.mkdtempSync(path.join(process.cwd(), '.xle-confine-test-'));
+ try {
+ const result = await layoutExpand('V > B', {targetPath: './out', name: 'Demo', cwd});
+ expect(result.data.written).toBe(path.join('out', 'Demo.tsx'));
+ expect(fs.existsSync(path.join(cwd, 'out', 'Demo.tsx'))).toBe(true);
+ } finally {
+ fs.rmSync(cwd, {recursive: true, force: true});
+ }
+ }, SLOW);
+});