Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

fix(text): stop the search term being parsed as a ripgrep flag (#564)

KageBinary36dcc76ff926
diff --git a/ix-cli/src/cli/__tests__/text-arg-injection.test.ts b/ix-cli/src/cli/__tests__/text-arg-injection.test.ts
new file mode 100644
index 00000000..687ca9ca
--- /dev/null
+++ b/ix-cli/src/cli/__tests__/text-arg-injection.test.ts
@@ -0,0 +1,61 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { Command } from "commander";
+
+const execFile = vi.hoisted(() =>
+ vi.fn((_cmd: string, _args: string[], _opts: unknown, cb: (e: unknown, r: unknown) => void) =>
+ cb(null, { stdout: "", stderr: "" }),
+ ),
+);
+
+// Spread the real module: `config.ts`, reached through this command, imports
+// `execSync` from it.
+vi.mock("node:child_process", async (orig) => ({
+ ...(await orig<Record<string, unknown>>()),
+ execFile,
+}));
+
+import { registerTextCommand } from "../commands/text.js";
+
+/**
+ * `ix text` hands its search term straight to ripgrep's argv, and through
+ * `ix mcp` that term is a string a model chose. Without a `--` separator rg
+ * parses a leading `-` as one of its own flags, which is how `--files` ended up
+ * running instead of a search, and how `--pre=<cmd>`/`--file=<path>` could take
+ * the path argument away and leave rg reading stdin for ever.
+ */
+describe("ix text does not let the search term become an rg flag", () => {
+ beforeEach(() => {
+ execFile.mockClear();
+ });
+
+ async function argvFor(term: string, extra: string[] = []): Promise<string[]> {
+ const program = new Command();
+ registerTextCommand(program);
+ await program.parseAsync(["text", ...extra, "--", term], { from: "user" });
+ return execFile.mock.calls[0]?.[1] as string[];
+ }
+
+ it.each(["--files", "--pre=/bin/sh", "--file=/etc/passwd", "-l", "--no-ignore"])(
+ "passes %s as a pattern, after the separator",
+ async (term) => {
+ const args = await argvFor(term);
+
+ const sep = args.indexOf("--");
+ expect(sep).toBeGreaterThanOrEqual(0);
+ expect(args[sep + 1]).toBe(term);
+ // Nothing before the separator came from the caller.
+ expect(args.slice(0, sep)).not.toContain(term);
+ // The path is still the last positional, so rg always has one and never
+ // falls back to reading stdin.
+ expect(args).toHaveLength(sep + 3);
+ },
+ );
+
+ it("still places rg's own flags before the separator", async () => {
+ const args = await argvFor("needle", ["--limit", "5", "--language", "python"]);
+
+ expect(args.slice(0, args.indexOf("--"))).toEqual([
+ "--json", "--max-count", "5", "--glob", "*.py",
+ ]);
+ });
+});
diff --git a/ix-cli/src/cli/commands/text.ts b/ix-cli/src/cli/commands/text.ts
index b0e7491f..281dcbc5 100644
--- a/ix-cli/src/cli/commands/text.ts
+++ b/ix-cli/src/cli/commands/text.ts
@@ -32,7 +32,16 @@ export function registerTextCommand(program: Command): void {
}
}
- rgArgs.push(term, searchPath);
+ // `--` first. `term` reaches this argv verbatim from the caller, and
+ // through `ix mcp` the caller is a model choosing the string, so
+ // without the separator ripgrep parses any term beginning with `-` as
+ // one of its own flags. `ix text --path=. --format=json -- --files`
+ // ran rg's `--files` instead of searching for that literal, and a flag
+ // that swallows the path argument -- `--pre=<cmd>`, `--file=<path>` --
+ // leaves rg with no path to search, so it reads stdin and never
+ // returns. `ix mcp` serialises its runs, so one such call blocks every
+ // other tool until the timeout and leaves the child process behind.
+ rgArgs.push("--", term, searchPath);
const { stdout } = await execFileAsync("rg", rgArgs, { maxBuffer: 10 * 1024 * 1024 });