An open source design system that's fully customizable and agent ready
TypeScript
fix(cli): cap the layout expression read from stdin (#6572)
Joey8a34e0646490
diff --git a/.changeset/cli-spec-layout-stdin-cap.md b/.changeset/cli-spec-layout-stdin-cap.mdnew file mode 100644index 0000000000..1beb491d2f--- /dev/null+++ b/.changeset/cli-spec-layout-stdin-cap.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[fix] `astryx layout check -` and `astryx layout expand -` now stop reading stdin at 5 MB and fail with `ERR_INVALID_ARGUMENT`, the same size cap `--file` already had. Before, an endless or oversized pipe was buffered whole until memory ran out.++@josephfarinadiff --git a/packages/cli/clients/cli/commands/layout-check.doc.mjs b/packages/cli/clients/cli/commands/layout-check.doc.mjsindex 30e7072e19..600b7790bf 100644--- a/packages/cli/clients/cli/commands/layout-check.doc.mjs+++ b/packages/cli/clients/cli/commands/layout-check.doc.mjs@@ -58,7 +58,7 @@ export const doc = {{code: 0, when: 'the expression is valid'},{code: 1,- when: 'the expression is invalid or empty, has a syntax error, or a bad --form',+ when: 'the expression is invalid, empty or over 5 MB (from stdin or --file), has a syntax error, or a bad --form',},],related: ['layout expand', 'layout grammar'],diff --git a/packages/cli/clients/cli/commands/layout-expand.doc.mjs b/packages/cli/clients/cli/commands/layout-expand.doc.mjsindex 9d9ec1d409..d01035e929 100644--- a/packages/cli/clients/cli/commands/layout-expand.doc.mjs+++ b/packages/cli/clients/cli/commands/layout-expand.doc.mjs@@ -76,7 +76,7 @@ export const doc = {{code: 0, when: 'success'},{code: 1,- when: 'a missing/empty expression, a bad --name or --form, a parse/validation error, or a path escape',+ when: 'a missing, empty or over-5 MB expression (from stdin or --file), a bad --name or --form, a parse/validation error, or a path escape',},],related: ['layout check', 'layout grammar'],diff --git a/packages/cli/clients/cli/commands/layout.mjs b/packages/cli/clients/cli/commands/layout.mjsindex 6740846eb2..1c316cfd99 100644--- a/packages/cli/clients/cli/commands/layout.mjs+++ b/packages/cli/clients/cli/commands/layout.mjs@@ -59,6 +59,9 @@ import {NO_RESULT_SET, resultSet} from '../../../foundation/debug/index.mjs';* @property {boolean} [loose]*/+/** The largest layout expression read from --file or stdin. */+const MAX_EXPRESSION_BYTES = 5 * 1024 * 1024; // 5 MB+/*** Resolve the expression from arg, --file, or stdin ('-').* @param {string} [expr]@@ -78,8 +81,7 @@ async function readExpression(expr, options = {}) {code: ERROR_CODES.ERR_FILE_NOT_FOUND,});}- const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5 MB- if (stat.size > MAX_FILE_SIZE) {+ if (stat.size > MAX_EXPRESSION_BYTES) {cliError(`File "${options.file}" is too large (${(stat.size / 1024 / 1024).toFixed(1)} MB, max 5 MB)`,{code: ERROR_CODES.ERR_FILE_NOT_FOUND},@@ -100,9 +102,19 @@ async function readExpression(expr, options = {}) {}}if (expr === '-') {+ // Capped like --file: an endless stream must not be buffered whole./** @type {Buffer[]} */const chunks = [];- for await (const chunk of process.stdin) chunks.push(/** @type {Buffer} */ (chunk));+ let size = 0;+ for await (const chunk of process.stdin) {+ size += /** @type {Buffer} */ (chunk).length;+ if (size > MAX_EXPRESSION_BYTES) {+ cliError('The layout expression on stdin is too large (max 5 MB)', {+ code: ERROR_CODES.ERR_INVALID_ARGUMENT,+ });+ }+ chunks.push(/** @type {Buffer} */ (chunk));+ }return Buffer.concat(chunks).toString('utf-8');}return expr ?? '';diff --git a/packages/cli/clients/cli/commands/layout.stdin-cap.test.mjs b/packages/cli/clients/cli/commands/layout.stdin-cap.test.mjsnew file mode 100644index 0000000000..a432645659--- /dev/null+++ b/packages/cli/clients/cli/commands/layout.stdin-cap.test.mjs@@ -0,0 +1,47 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file `layout check -` reads the expression from stdin under the same 5 MB+ * cap as --file, instead of buffering whatever the pipe delivers. Spawns the+ * real bin, since the in-process harness has no stdin.+ */++import {describe, it, expect} from 'vitest';+import {spawnSync} from 'node:child_process';+import * as path from 'node:path';+import {fileURLToPath} from 'node:url';++const HERE = path.dirname(fileURLToPath(import.meta.url));+const BIN = path.resolve(HERE, '../bin/astryx.mjs');+// Run against the monorepo root so @astryxdesign/core is discoverable.+const REPO = path.resolve(HERE, '../../../..');+const SLOW = 60_000;++/** @param {string} input @param {string[]} [extra] */+function checkFromStdin(input, extra = []) {+ return spawnSync(process.execPath, [BIN, 'layout', 'check', '-', ...extra], {+ cwd: REPO,+ input,+ encoding: 'utf-8',+ maxBuffer: 16 * 1024 * 1024,+ });+}++describe('layout check - (stdin)', () => {+ it('refuses more than 5 MB with ERR_INVALID_ARGUMENT, in both modes', () => {+ // Valid once trimmed, so only the cap can reject it.+ const big = ' '.repeat(5 * 1024 * 1024 + 1) + 'V';+ const json = checkFromStdin(big, ['--json']);+ expect(json.status).toBe(1);+ expect(JSON.parse(json.stdout).code).toBe('ERR_INVALID_ARGUMENT');+ const human = checkFromStdin(big);+ expect(human.status).toBe(1);+ expect(human.stderr + human.stdout).toMatch(/too large/);+ }, SLOW);++ it('still reads a normal expression', () => {+ const r = checkFromStdin('V > B', ['--json']);+ expect(r.status).toBe(0);+ expect(JSON.parse(r.stdout).data.valid).toBe(true);+ }, SLOW);+});