Skip to content

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

TypeScript

fix(cli): confine theme add staging writes (#6535)

Joey5f0588c59fd1
diff --git a/.changeset/cli-spec-theme-add-staging.md b/.changeset/cli-spec-theme-add-staging.md
new file mode 100644
index 0000000000..87349eea0c
--- /dev/null
+++ b/.changeset/cli-spec-theme-add-staging.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] `astryx theme add` no longer writes through a symlink that already sits at the temporary name it stages each file under. A link that leads outside the project now fails with `ERR_PATH_TRAVERSAL`, and any other entry at that name fails the copy instead of being overwritten.
+
+@josephfarina
diff --git a/packages/cli/api/theme/add/add.mjs b/packages/cli/api/theme/add/add.mjs
index a04e6d17be..cec140e480 100644
--- a/packages/cli/api/theme/add/add.mjs
+++ b/packages/cli/api/theme/add/add.mjs
@@ -148,8 +148,12 @@ export async function themeAdd(slug, options = {}) {
label: `theme destination for ${w.name}`,
});
fs.mkdirSync(path.dirname(dest), {recursive: true});
- const tmp = `${dest}.${process.pid}.tmp`;
- fs.writeFileSync(tmp, scaffoldContents(fs.readFileSync(w.src)));
+ // The staging file is an output path too: confine it, and never write
+ // through an entry that already exists under its name.
+ const tmp = assertWithin(`${w.name}.${process.pid}.tmp`, resolvedDir, {
+ label: `theme staging file for ${w.name}`,
+ });
+ fs.writeFileSync(tmp, scaffoldContents(fs.readFileSync(w.src)), {flag: 'wx'});
staged.push({tmp, dest});
}
for (const s of staged) {
diff --git a/packages/cli/api/theme/add/add.staging.test.mjs b/packages/cli/api/theme/add/add.staging.test.mjs
new file mode 100644
index 0000000000..aab774b4ae
--- /dev/null
+++ b/packages/cli/api/theme/add/add.staging.test.mjs
@@ -0,0 +1,66 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+import {afterEach, beforeEach, describe, expect, it} from 'vitest';
+import * as fs from 'node:fs';
+import * as os from 'node:os';
+import * as path from 'node:path';
+import {themeAdd} from './add.mjs';
+import {listThemes} from '../_adapter.mjs';
+import {isErrorCode} from '../../../foundation/response/error-codes.mjs';
+
+let tmpDir;
+let outsideDir;
+
+beforeEach(() => {
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-themeadd-staging-'));
+ outsideDir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-themeadd-outside-'));
+});
+
+afterEach(() => {
+ fs.rmSync(tmpDir, {recursive: true, force: true});
+ fs.rmSync(outsideDir, {recursive: true, force: true});
+});
+
+/**
+ * Put a symlink where `theme add` stages the first file of `slug`.
+ * @param {string} slug
+ * @param {string} target
+ */
+function plantStagingLink(slug, target) {
+ const theme = listThemes().find(entry => entry.slug === slug);
+ if (!theme) throw new Error(`missing bundled theme ${slug}`);
+ const first = theme.files[0];
+ const dest = path.join(tmpDir, 'src', 'themes', slug, first);
+ fs.mkdirSync(path.dirname(dest), {recursive: true});
+ fs.symlinkSync(target, `${dest}.${process.pid}.tmp`);
+ return dest;
+}
+
+describe('themeAdd staging writes stay inside the project', () => {
+ it('refuses a staging path that links outside the project', async () => {
+ const victim = path.join(outsideDir, 'victim.txt');
+ fs.writeFileSync(victim, 'outside\n');
+ const dest = plantStagingLink('stone', victim);
+
+ await expect(themeAdd('stone', {cwd: tmpDir})).rejects.toMatchObject({
+ code: 'ERR_PATH_TRAVERSAL',
+ });
+ expect(fs.readFileSync(victim, 'utf-8')).toBe('outside\n');
+ expect(fs.existsSync(dest)).toBe(false);
+ });
+
+ it('never creates a file through a dangling staging link', async () => {
+ const victim = path.join(outsideDir, 'created.txt');
+ plantStagingLink('stone', victim);
+
+ let error;
+ try {
+ await themeAdd('stone', {cwd: tmpDir});
+ } catch (caught) {
+ error = caught;
+ }
+
+ expect(isErrorCode(error?.code)).toBe(true);
+ expect(fs.existsSync(victim)).toBe(false);
+ });
+});