fix(cli): reject nonnumeric output slice bounds (#13344)

This commit is contained in:
RRFZ authored and GitHub committed 2026-11-22 03:54:12 -05:00
1 parent 6cc7de3005
commit 9a6ee71886
5 files changed
+64 -8

No files matched your search

+14 -3
View File
@@ -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);
}
+8 -4
View File
@@ -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
+23
View File
@@ -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 }) => {
@@ -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(
{
+18
View File
@@ -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" },
});
});