From 9a6ee7188697c5e99616dbb75154ce2cdbc79af7 Mon Sep 17 00:00:00 2001 From: RRFZ <247457381+Plastic-time@users.noreply.github.com> Date: Sun, 22 Nov 2026 03:54:12 -0500 Subject: [PATCH] fix(cli): reject nonnumeric output slice bounds (#13344) --- src/cli/outputFilter.ts | 17 +++++++++++--- src/cliOutput.ts | 12 ++++++---- tests/boundary/cli/cliOutput.test.ts | 23 +++++++++++++++++++ .../boundary/cli/filteredArrayOutput.test.ts | 2 +- tests/boundary/cli/outputFilter.test.ts | 18 +++++++++++++++ 5 files changed, 64 insertions(+), 8 deletions(-) diff --git a/src/cli/outputFilter.ts b/src/cli/outputFilter.ts index eb1eabd351..990130520e 100644 --- a/src/cli/outputFilter.ts +++ b/src/cli/outputFilter.ts @@ -15,7 +15,10 @@ export type OutputFilterSegment = export type OutputFilterPath = readonly OutputFilterSegment[]; export interface OutputFilterError { - readonly kind: "unclosed_slice" | "extra_slice_bounds"; + readonly kind: + | "unclosed_slice" + | "extra_slice_bounds" + | "invalid_slice_bound"; } /** Split on commas outside `[...]`; an empty last path is dropped. */ @@ -57,8 +60,16 @@ const parsePath = ( segments.push(...keySegments(remaining.slice(0, open))); const bounds = remaining.slice(open + 1, close).split(","); if (bounds.length > 2) return err({ kind: "extra_slice_bounds" }); - const [start, end] = bounds; - segments.push({ start: Number(start), end: Number(end) }); + const [rawStart = "", rawEnd] = bounds; + const start = Number(rawStart); + const end = Number(rawEnd); + // Keep doubled openers with numeric or empty bounds on the legacy path. + const legacyStart = + rawStart.startsWith("[") && !Number.isNaN(Number(rawStart.slice(1))); + const invalidStart = Number.isNaN(start) && !legacyStart; + const invalidEnd = rawEnd !== undefined && Number.isNaN(end); + if (invalidStart || invalidEnd) return err({ kind: "invalid_slice_bound" }); + segments.push({ start, end }); remaining = remaining.slice(close + 1); if (remaining.startsWith(".")) remaining = remaining.slice(1); } diff --git a/src/cliOutput.ts b/src/cliOutput.ts index a5fc4a858b..740ee477ca 100644 --- a/src/cliOutput.ts +++ b/src/cliOutput.ts @@ -278,10 +278,14 @@ const validateCliOutputControls = ( ok: false, format: format === "yaml" || format === "jsonl" ? format : "json", code: "INVALID_OUTPUT_FILTER", - message: - filterPaths.error.kind === "unclosed_slice" - ? "The --filter-output expression has a slice without a closing ]. Close the slice or remove --filter-output." - : "The --filter-output expression has a slice with more than two bounds. Use [start,end].", + message: { + unclosed_slice: + "The --filter-output expression has a slice without a closing ]. Close the slice or remove --filter-output.", + extra_slice_bounds: + "The --filter-output expression has a slice with more than two bounds. Use [start,end].", + invalid_slice_bound: + "The --filter-output expression has a nonnumeric slice bound. Use a number or leave the bound empty.", + }[filterPaths.error.kind], }; const delimited = format === "csv" || format === "tsv"; // Name missing delimited controls before their combination checks; preserve diff --git a/tests/boundary/cli/cliOutput.test.ts b/tests/boundary/cli/cliOutput.test.ts index ae8ff28ae3..60add6da7f 100644 --- a/tests/boundary/cli/cliOutput.test.ts +++ b/tests/boundary/cli/cliOutput.test.ts @@ -382,6 +382,29 @@ describe("CLI output filter slice validation", () => { }, ); + workspaceCliTest( + "rejects a nonnumeric slice bound instead of returning no rows", + async ({ cli }) => { + const result = await cli.run({ + freshProcess: true, + arguments: [ + "providers", + "--json", + "--filter-output", + "capabilities[0,2x].provider.id", + ], + }); + expect(result.exitCode).toBe(1); + expect(result.json).toMatchObject({ + ok: false, + error: { + code: "INVALID_OUTPUT_FILTER", + message: expect.stringContaining("nonnumeric slice bound"), + }, + }); + }, + ); + workspaceCliTest( "retains closed slices and multiple filter paths", async ({ cli }) => { diff --git a/tests/boundary/cli/filteredArrayOutput.test.ts b/tests/boundary/cli/filteredArrayOutput.test.ts index d6f59fb717..576fde2a54 100644 --- a/tests/boundary/cli/filteredArrayOutput.test.ts +++ b/tests/boundary/cli/filteredArrayOutput.test.ts @@ -494,7 +494,7 @@ it("projects consecutive slices through nested root arrays and merges cell field it.each([ ["rows[0,2][-2,-1]", { rows: [[1], [3]] }], ["rows[0,2][1,1]", { rows: [[], []] }], - ["rows[0,2][invalid,1]", { rows: [[1], [3]] }], + ["rows[0,2][,1]", { rows: [[1], [3]] }], ] as const)("retains parsed slice bounds for %s", async (filter, expected) => { const output = await runFilteredCli( { diff --git a/tests/boundary/cli/outputFilter.test.ts b/tests/boundary/cli/outputFilter.test.ts index 51ef1b5ccb..452bdef48e 100644 --- a/tests/boundary/cli/outputFilter.test.ts +++ b/tests/boundary/cli/outputFilter.test.ts @@ -10,6 +10,10 @@ it.each([ [[{ key: "rows" }, { start: 0, end: 3 }, { key: "name" }]], ], ["a[1,2],b", [[{ key: "a" }, { start: 1, end: 2 }], [{ key: "b" }]]], + ["a[,2]", [[{ key: "a" }, { start: 0, end: 2 }]]], + ["a[-1,]", [[{ key: "a" }, { start: -1, end: 0 }]]], + ["a[1]", [[{ key: "a" }, { start: 1, end: NaN }]]], + ["a[[0,1]", [[{ key: "a" }, { start: NaN, end: 1 }]]], ["a..b.", [[{ key: "a" }, { key: "b" }]]], ["a,,b,", [[{ key: "a" }], [], [{ key: "b" }]]], ])("parses %j", (expression, paths) => { @@ -35,3 +39,17 @@ it.each(["rows[0,1,2].name", "name,rows[-1,2,]"])( }); }, ); + +it.each([ + "rows[x,2]", + "rows[0,2x]", + "rows[x]", + "rows[[x,1]", + "rows[[x]", + "rows[0,2][invalid,1]", +])("refuses a nonnumeric slice bound in %j", (expression) => { + expect(parseOutputFilter(expression)).toEqual({ + ok: false, + error: { kind: "invalid_slice_bound" }, + }); +});