An open source design system that's fully customizable and agent ready
TypeScript
fix(cli): confine the init template scaffold to the project directory (#6538)
Joey24efa05aab04
diff --git a/.changeset/cli-spec-init-template-confinement.md b/.changeset/cli-spec-init-template-confinement.mdnew file mode 100644index 0000000000..720e2ad6c2--- /dev/null+++ b/.changeset/cli-spec-init-template-confinement.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[fix] Programmatic `init()` now confines the starter template it scaffolds with `templateName` to the project directory. A `src` symlink that points outside the project is rejected with `ERR_PATH_TRAVERSAL` before anything is written.++@josephfarinadiff --git a/packages/cli/api/init/init.doc.mjs b/packages/cli/api/init/init.doc.mjsindex c89f686ddc..03bef4f265 100644--- a/packages/cli/api/init/init.doc.mjs+++ b/packages/cli/api/init/init.doc.mjs@@ -90,6 +90,10 @@ export const doc = {code: 'ERR_FILE_EXISTS',when: 'scaffolding a template would overwrite an existing page.tsx',},+ {+ code: 'ERR_PATH_TRAVERSAL',+ when: 'the template output path resolves outside cwd, for example through a symlinked src directory',+ },],examples: [{label: 'Default setup', code: 'const r = await init();'},diff --git a/packages/cli/api/init/init.test.mjs b/packages/cli/api/init/init.test.mjsindex 38426d5716..f4ddbcd2df 100644--- a/packages/cli/api/init/init.test.mjs+++ b/packages/cli/api/init/init.test.mjs@@ -287,4 +287,17 @@ describe('init() — write-path safety', () => {init({features: 'template', templateName: '../../etc/evil'}, {cwd: tmpDir}),).rejects.toMatchObject({code: ERROR_CODES.ERR_UNKNOWN_TEMPLATE});});++ it('refuses a template write that a symlinked src would carry outside cwd (ERR_PATH_TRAVERSAL)', async () => {+ const outside = fs.mkdtempSync(path.join(process.cwd(), '.astryx-init-outside-'));+ try {+ fs.symlinkSync(outside, path.join(tmpDir, 'src'), 'dir');+ await expect(+ init({features: 'template', templateName: 'blank'}, {cwd: tmpDir}),+ ).rejects.toMatchObject({code: ERROR_CODES.ERR_PATH_TRAVERSAL});+ expect(fs.readdirSync(outside)).toEqual([]);+ } finally {+ fs.rmSync(outside, {recursive: true, force: true});+ }+ });});diff --git a/packages/cli/api/init/run/run.mjs b/packages/cli/api/init/run/run.mjsindex f9005793f3..5edb770439 100644--- a/packages/cli/api/init/run/run.mjs+++ b/packages/cli/api/init/run/run.mjs@@ -17,7 +17,7 @@import * as path from 'node:path';import * as fs from 'node:fs';import {CLI_ROOT} from '../../../foundation/fs/paths.mjs';-import {PathSafetyError} from '../../../foundation/fs/path-safety.mjs';+import {PathSafetyError, assertWithin} from '../../../foundation/fs/path-safety.mjs';import {getCliInvocation} from '../../../foundation/env/package-manager.mjs';import {installAgentDocs,@@ -190,9 +190,19 @@ function applyTemplate(cwd, {templateName}, invocation, data) {);}- const outputDir = path.resolve(cwd, `./src/pages/${templateName}`);+ let destFile;+ try {+ destFile = assertWithin(path.join('src', 'pages', templateName, 'page.tsx'), cwd, {+ label: 'template output path',+ });+ } catch (err) {+ if (err instanceof PathSafetyError) {+ throw new AstryxError(err.message, undefined, ERROR_CODES.ERR_PATH_TRAVERSAL);+ }+ throw err;+ }+ const outputDir = path.dirname(destFile);const srcPath = path.join(CLI_ROOT, 'assets', 'templates', 'pages', templateName, 'page.tsx');- const destFile = path.join(outputDir, 'page.tsx');// Don't clobber a user's existing page — same guard the peer template/copy and// theme/add write-leaves apply (init is a public API surface too).if (fs.existsSync(destFile)) {