An open source design system that's fully customizable and agent ready
TypeScript
fix(cli): confine nested theme copy destinations (#6270)
Joey513f549ba42e
diff --git a/.changeset/theme-copy-and-codemod-proof.md b/.changeset/theme-copy-and-codemod-proof.mdnew file mode 100644index 0000000000..6fb57f0b85--- /dev/null+++ b/.changeset/theme-copy-and-codemod-proof.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[fix] Keep copied integration theme files inside the target project.++@josephfarinadiff --git a/packages/cli/api/theme/add/add.mjs b/packages/cli/api/theme/add/add.mjsindex 2bc4116e43..cfc1028927 100644--- a/packages/cli/api/theme/add/add.mjs+++ b/packages/cli/api/theme/add/add.mjs@@ -78,11 +78,25 @@ export async function themeAdd(slug, options = {}) {throw err;}- const writes = match.files.map(name => ({- name,- src: path.join(themeSrcDir, name),- dest: path.join(resolvedDir, name),- }));+ let writes;+ try {+ writes = match.files.map(name => ({+ name,+ src: path.join(themeSrcDir, name),+ dest: assertWithin(name, resolvedDir, {+ label: `theme destination for ${name}`,+ }),+ }));+ } catch (err) {+ if (err instanceof PathSafetyError) {+ throw new AstryxError(+ err.message,+ undefined,+ ERROR_CODES.ERR_PATH_TRAVERSAL,+ );+ }+ throw err;+ }for (const w of writes) {if (!fs.existsSync(w.src)) {throw new AstryxError(@@ -116,11 +130,14 @@ export async function themeAdd(slug, options = {}) {try {fs.mkdirSync(resolvedDir, {recursive: true});for (const w of writes) {- fs.mkdirSync(path.dirname(w.dest), {recursive: true});- const tmp = `${w.dest}.${process.pid}.tmp`;+ const dest = assertWithin(w.name, resolvedDir, {+ label: `theme destination for ${w.name}`,+ });+ fs.mkdirSync(path.dirname(dest), {recursive: true});+ const tmp = `${dest}.${process.pid}.tmp`;const contents = stripCopyrightHeader(fs.readFileSync(w.src, 'utf-8'));fs.writeFileSync(tmp, contents);- staged.push({tmp, dest: w.dest});+ staged.push({tmp, dest});}for (const s of staged) {fs.renameSync(s.tmp, s.dest);@@ -133,6 +150,13 @@ export async function themeAdd(slug, options = {}) {/* best-effort */}}+ if (err instanceof PathSafetyError) {+ throw new AstryxError(+ err.message,+ undefined,+ ERROR_CODES.ERR_PATH_TRAVERSAL,+ );+ }throw new AstryxError(`Failed to write theme files: ${/** @type {any} */ (err).message}`,undefined,diff --git a/packages/cli/api/theme/integration-themes.test.mjs b/packages/cli/api/theme/integration-themes.test.mjsindex 3606c97f9e..9320177a97 100644--- a/packages/cli/api/theme/integration-themes.test.mjs+++ b/packages/cli/api/theme/integration-themes.test.mjs@@ -104,6 +104,35 @@ describe('themeAdd with integration themes', () => {).toContain('oceanBlue');});+ it('rejects a nested destination symlink that escapes the project', async () => {+ installThemeIntegration(+ '@acme/themes',+ 'ocean',+ "import {oceanBlue} from './tokens/colors';\nexport const oceanTheme = {oceanBlue};\n",+ {'tokens/colors.ts': "export const oceanBlue = '#0064e0';\n"},+ );+ const outsideDir = fs.mkdtempSync(+ path.join(process.cwd(), '.astryx-theme-outside-'),+ );+ try {+ const targetDir = path.join(tmpDir, 'src', 'themes', 'ocean');+ fs.mkdirSync(targetDir, {recursive: true});+ fs.symlinkSync(+ outsideDir,+ path.join(targetDir, 'tokens'),+ process.platform === 'win32' ? 'junction' : 'dir',+ );++ await expect(themeAdd('ocean', {cwd: tmpDir})).rejects.toMatchObject({+ code: 'ERR_PATH_TRAVERSAL',+ });+ expect(fs.existsSync(path.join(outsideDir, 'colors.ts'))).toBe(false);+ expect(fs.existsSync(path.join(targetDir, 'oceanTheme.ts'))).toBe(false);+ } finally {+ fs.rmSync(outsideDir, {recursive: true, force: true});+ }+ });+it('fails closed on a duplicate slug and resolves it with package scope', async () => {installThemeIntegration('@acme/themes', 'neutral');