An open source design system that's fully customizable and agent ready
TypeScript
fix(cli): report theme build output-path failures with registered codes (#6543)
Joeyc8447563cd30
diff --git a/.changeset/cli-spec-output-path-error-codes.md b/.changeset/cli-spec-output-path-error-codes.mdnew file mode 100644index 0000000000..22bbda0efb--- /dev/null+++ b/.changeset/cli-spec-output-path-error-codes.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[fix] `astryx theme build --out` now reports a path that leaves the working directory with `ERR_PATH_TRAVERSAL`, and an output directory it cannot create with `ERR_WRITE_FAILED`, instead of unregistered codes such as `PATH_TRAVERSAL` or `EEXIST`. The programmatic `themeBuild()` throws the same codes as an `AstryxError`.++@josephfarinadiff --git a/packages/cli/api/theme/build/build.mjs b/packages/cli/api/theme/build/build.mjsindex a0df9d5df5..1a2281bc52 100644--- a/packages/cli/api/theme/build/build.mjs+++ b/packages/cli/api/theme/build/build.mjs@@ -224,10 +224,10 @@ function staleBuildOutputs(writes, cwd) {/** @param {Array<{dest: string, content: string}>} writes */function writeBuildOutputs(writes) {if (writes.length === 0) return;- fs.mkdirSync(path.dirname(writes[0].dest), {recursive: true});/** @type {Array<{tmp: string, dest: string}>} */const staged = [];try {+ fs.mkdirSync(path.dirname(writes[0].dest), {recursive: true});for (const write of writes) {const tmp = `${write.dest}.${process.pid}.tmp`;fs.writeFileSync(tmp, write.content);@@ -2505,7 +2505,18 @@ async function themeBuildInternal(// Guard: relative paths must not escape cwd via `../`. Absolute paths are// trusted (the user explicitly controls where output goes, like gcc -o).if (!path.isAbsolute(options.out)) {- assertWithin(options.out, cwd, {label: 'output path'});+ try {+ assertWithin(options.out, cwd, {label: 'output path'});+ } catch (err) {+ if (err instanceof PathSafetyError) {+ throw new AstryxError(+ err.message,+ undefined,+ ERROR_CODES.ERR_PATH_TRAVERSAL,+ );+ }+ throw err;+ }}} else {outPath = path.join(path.dirname(filePath), `${baseName}.css`);diff --git a/packages/cli/api/theme/themeBuild.doc.mjs b/packages/cli/api/theme/themeBuild.doc.mjsindex 758f9b97ff..b294ebaac6 100644--- a/packages/cli/api/theme/themeBuild.doc.mjs+++ b/packages/cli/api/theme/themeBuild.doc.mjs@@ -93,7 +93,7 @@ export const doc = {},{code: 'ERR_PATH_TRAVERSAL',- when: 'the theme name contains a path separator or traversal marker',+ when: 'the theme name contains a path separator or traversal marker, or a relative `out` path resolves outside cwd (including through a symlink)',},{code: 'ERR_CORE_NOT_FOUND',@@ -105,7 +105,7 @@ export const doc = {},{code: 'ERR_WRITE_FAILED',- when: 'writing the outputs fails (staged temp files are rolled back)',+ when: 'creating the output directory or writing the outputs fails (staged temp files are rolled back)',},],examples: [diff --git a/packages/cli/clients/cli/commands/build-theme.path-safety.test.mjs b/packages/cli/clients/cli/commands/build-theme.path-safety.test.mjsindex e9ea367030..9b04ab2199 100644--- a/packages/cli/clients/cli/commands/build-theme.path-safety.test.mjs+++ b/packages/cli/clients/cli/commands/build-theme.path-safety.test.mjs@@ -7,15 +7,21 @@* - Theme name with `..` segments cannot escape the output directory.* - Theme name with `/` is rejected with a clear error (vs. ENOENT).* - Multi-file write is atomic: if one file fails, none are left behind.+ * - An `--out` escape, or an output directory that cannot be created, fails+ * with a registered error code.** Runs the CLI in-process (shared runCli harness) against a tiny synthetic theme file.*/-import {describe, it, expect, beforeEach, afterEach} from 'vitest';+import {describe, it, expect, beforeAll, beforeEach, afterEach} from 'vitest';import * as fs from 'node:fs';import * as path from 'node:path';import * as os from 'node:os';+import {ensureCoreBuilt} from './ensure-core-built.mjs';import {runCli} from '../../../test-utils/run-cli.mjs';+import {themeBuild} from '../../../api/theme/build/build.mjs';+import {AstryxError} from '../../../api/error.mjs';+import {ERROR_CODES} from '../../../foundation/response/error-codes.mjs';function writeTheme(dir, name) {fs.mkdirSync(dir, {recursive: true});@@ -93,3 +99,82 @@ describe('theme build path safety', () => {expect(entries).toEqual(['theme.mjs']);});});++describe('theme build output-path failures carry registered codes', () => {+ // These reach the output-path step, which runs after CSS generation.+ beforeAll(() => {+ ensureCoreBuilt();+ }, 200_000);++ const registered = new Set(Object.values(ERROR_CODES));++ /** @param {string[]} args @param {string} cwd */+ async function buildJson(args, cwd) {+ const result = await runCli(['--json', 'theme', 'build', ...args], cwd);+ return {result, envelope: JSON.parse(result.stdout)};+ }++ it('reports an --out path outside the project as ERR_PATH_TRAVERSAL', async () => {+ const project = path.join(tmpDir, 'project');+ const themeFile = writeTheme(path.join(project, 'themes'), 'ocean');++ const {result, envelope} = await buildJson(+ [path.relative(project, themeFile), '--out', '../escaped.css'],+ project,+ );++ expect(result.code).toBe(1);+ expect(envelope.code).toBe(ERROR_CODES.ERR_PATH_TRAVERSAL);+ expect(registered.has(envelope.code)).toBe(true);+ expect(fs.existsSync(path.join(tmpDir, 'escaped.css'))).toBe(false);+ });++ it('reports an --out path that escapes through a symlink as ERR_PATH_TRAVERSAL', async () => {+ const project = path.join(tmpDir, 'project');+ const outside = path.join(tmpDir, 'outside');+ fs.mkdirSync(outside, {recursive: true});+ const themeFile = writeTheme(path.join(project, 'themes'), 'ocean');+ fs.symlinkSync(outside, path.join(project, 'link'), 'dir');++ const {result, envelope} = await buildJson(+ [path.relative(project, themeFile), '--out', 'link/escaped.css'],+ project,+ );++ expect(result.code).toBe(1);+ expect(envelope.code).toBe(ERROR_CODES.ERR_PATH_TRAVERSAL);+ expect(fs.readdirSync(outside)).toEqual([]);+ });++ it('reports an output directory that cannot be created as ERR_WRITE_FAILED', async () => {+ const project = path.join(tmpDir, 'project');+ const themeFile = writeTheme(path.join(project, 'themes'), 'ocean');+ // A regular file where the output directory would have to be created.+ fs.writeFileSync(path.join(project, 'blocker'), '');++ const {result, envelope} = await buildJson(+ [path.relative(project, themeFile), '--out', 'blocker/ocean.css'],+ project,+ );++ expect(result.code).toBe(1);+ expect(envelope.code).toBe(ERROR_CODES.ERR_WRITE_FAILED);+ expect(registered.has(envelope.code)).toBe(true);+ });++ it('themeBuild() rejects an escaping out path with an AstryxError', async () => {+ const project = path.join(tmpDir, 'project');+ const themeFile = writeTheme(path.join(project, 'themes'), 'ocean');++ const build = themeBuild(+ path.relative(project, themeFile),+ {out: '../escaped.css'},+ {cwd: project},+ );++ await expect(build).rejects.toBeInstanceOf(AstryxError);+ await expect(build).rejects.toMatchObject({+ code: ERROR_CODES.ERR_PATH_TRAVERSAL,+ });+ });+});