From a7972c2c76a9068b951abe849791a7ab6d349367 Mon Sep 17 00:00:00 2001 From: "coderabbitai[bot]" <136622811+coderabbitai[bot]@users.noreply.github.com> Date: Tue, 10 Nov 2026 23:48:04 +0000 Subject: [PATCH] fix(commands): prevent code execution in command frontmatter --- .../__tests__/frontmatter-commands.spec.ts | 43 +++++++++++++++++++ src/services/command/commands.ts | 17 +++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/services/command/__tests__/frontmatter-commands.spec.ts b/src/services/command/__tests__/frontmatter-commands.spec.ts index 3f93b55f94..ecf1c5ffff 100644 --- a/src/services/command/__tests__/frontmatter-commands.spec.ts +++ b/src/services/command/__tests__/frontmatter-commands.spec.ts @@ -22,6 +22,49 @@ describe("Command loading with frontmatter", () => { vi.clearAllMocks() }) + describe.each(["lookup", "discovery"] as const)("%s engine restrictions", (entryPoint) => { + async function load(content: string) { + mockFs.stat = vi.fn().mockResolvedValue({ isDirectory: () => true }) + mockFs.readFile = vi.fn().mockResolvedValue(content) + mockFs.readdir = vi.fn().mockResolvedValue([{ name: "test.md", isFile: () => true }]) + return entryPoint === "lookup" + ? getCommand("/test/cwd", "test") + : (await getCommands("/test/cwd")).find((command) => command.name === "test") + } + + it.each(["js", "javascript", "JavaScript", " js", "javascript\r", "python"])( + "treats %s frontmatter as text without executing it", + async (language) => { + const executed = vi.fn() + vi.stubGlobal("__commandFrontmatterExecuted", executed) + try { + // Include a BOM to exercise gray-matter's input normalization. + const content = `\uFEFF---${language}\n(globalThis.__commandFrontmatterExecuted(), {description: "unsafe"})\n---\nBody` + const command = await load(content) + expect(executed).not.toHaveBeenCalled() + expect(command).toMatchObject({ content: content.trim() }) + expect(command?.description).toBeUndefined() + } finally { + vi.unstubAllGlobals() + } + }, + ) + + it.each(["", "yaml", "yml", "json"])("accepts data-only %s frontmatter", async (language) => { + const metadata = + language === "json" + ? '{"description":"Safe", "argument-hint":"input", "mode":"code"}' + : "description: Safe\nargument-hint: input\nmode: code" + const command = await load(`---${language}\n${metadata}\n---\nBody`) + expect(command).toMatchObject({ + content: "Body", + description: "Safe", + argumentHint: "input", + mode: "code", + }) + }) + }) + describe("getCommand with frontmatter", () => { it("should load command with description from frontmatter", async () => { const commandContent = `--- diff --git a/src/services/command/commands.ts b/src/services/command/commands.ts index 4e69558dc1..7294e19eb5 100644 --- a/src/services/command/commands.ts +++ b/src/services/command/commands.ts @@ -10,6 +10,19 @@ import { getBuiltInCommands, getBuiltInCommand } from "./built-in-commands" */ const MAX_DEPTH = 5 +/** Parse command metadata only after rejecting document-selected executable engines. */ +function parseCommandFrontmatter(content: string) { + // gray-matter strips a leading BOM before detecting the opening delimiter. + const input = content.replace(/^\uFEFF/, "") + if (matter.test(input)) { + const { name: language } = matter.language(input) + if (language && !["yaml", "yml", "json"].includes(language.toLowerCase())) { + throw new Error(`Unsupported command frontmatter language: ${language}`) + } + } + return matter(content) +} + export interface Command { name: string content: string @@ -221,7 +234,7 @@ async function tryLoadCommand( try { // Try to parse frontmatter with gray-matter - parsed = matter(content) + parsed = parseCommandFrontmatter(content) description = typeof parsed.data.description === "string" && parsed.data.description.trim() ? parsed.data.description.trim() @@ -306,7 +319,7 @@ async function scanCommandDirectory( try { // Try to parse frontmatter with gray-matter - parsed = matter(content) + parsed = parseCommandFrontmatter(content) description = typeof parsed.data.description === "string" && parsed.data.description.trim() ? parsed.data.description.trim()