Skip to content

Commit f015dae

Browse files
fix(commands): prevent code execution in command frontmatter (#2420)
<!-- coderabbit-ai-generated:v1 --> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent b140824 commit f015dae

2 files changed

Lines changed: 58 additions & 2 deletions

File tree

‎src/services/command/__tests__/frontmatter-commands.spec.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,49 @@ describe("Command loading with frontmatter", () => {
2222
vi.clearAllMocks()
2323
})
2424

25+
describe.each(["lookup", "discovery"] as const)("%s engine restrictions", (entryPoint) => {
26+
async function load(content: string) {
27+
mockFs.stat = vi.fn().mockResolvedValue({ isDirectory: () => true })
28+
mockFs.readFile = vi.fn().mockResolvedValue(content)
29+
mockFs.readdir = vi.fn().mockResolvedValue([{ name: "test.md", isFile: () => true }])
30+
return entryPoint === "lookup"
31+
? getCommand("/test/cwd", "test")
32+
: (await getCommands("/test/cwd")).find((command) => command.name === "test")
33+
}
34+
35+
it.each(["js", "javascript", "JavaScript", " js", "javascript\r", "python"])(
36+
"treats %s frontmatter as text without executing it",
37+
async (language) => {
38+
const executed = vi.fn()
39+
vi.stubGlobal("__commandFrontmatterExecuted", executed)
40+
try {
41+
// Include a BOM to exercise gray-matter's input normalization.
42+
const content = `\uFEFF---${language}\n(globalThis.__commandFrontmatterExecuted(), {description: "unsafe"})\n---\nBody`
43+
const command = await load(content)
44+
expect(executed).not.toHaveBeenCalled()
45+
expect(command).toMatchObject({ content: content.trim() })
46+
expect(command?.description).toBeUndefined()
47+
} finally {
48+
vi.unstubAllGlobals()
49+
}
50+
},
51+
)
52+
53+
it.each(["", "yaml", "yml", "json"])("accepts data-only %s frontmatter", async (language) => {
54+
const metadata =
55+
language === "json"
56+
? '{"description":"Safe", "argument-hint":"input", "mode":"code"}'
57+
: "description: Safe\nargument-hint: input\nmode: code"
58+
const command = await load(`---${language}\n${metadata}\n---\nBody`)
59+
expect(command).toMatchObject({
60+
content: "Body",
61+
description: "Safe",
62+
argumentHint: "input",
63+
mode: "code",
64+
})
65+
})
66+
})
67+
2568
describe("getCommand with frontmatter", () => {
2669
it("should load command with description from frontmatter", async () => {
2770
const commandContent = `---

‎src/services/command/commands.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ import { getBuiltInCommands, getBuiltInCommand } from "./built-in-commands"
1010
*/
1111
const MAX_DEPTH = 5
1212

13+
/** Parse command metadata only after rejecting document-selected executable engines. */
14+
function parseCommandFrontmatter(content: string) {
15+
// gray-matter strips a leading BOM before detecting the opening delimiter.
16+
const input = content.replace(/^\uFEFF/, "")
17+
if (matter.test(input)) {
18+
const { name: language } = matter.language(input)
19+
if (language && !["yaml", "yml", "json"].includes(language.toLowerCase())) {
20+
throw new Error(`Unsupported command frontmatter language: ${language}`)
21+
}
22+
}
23+
return matter(content)
24+
}
25+
1326
export interface Command {
1427
name: string
1528
content: string
@@ -221,7 +234,7 @@ async function tryLoadCommand(
221234

222235
try {
223236
// Try to parse frontmatter with gray-matter
224-
parsed = matter(content)
237+
parsed = parseCommandFrontmatter(content)
225238
description =
226239
typeof parsed.data.description === "string" && parsed.data.description.trim()
227240
? parsed.data.description.trim()
@@ -306,7 +319,7 @@ async function scanCommandDirectory(
306319

307320
try {
308321
// Try to parse frontmatter with gray-matter
309-
parsed = matter(content)
322+
parsed = parseCommandFrontmatter(content)
310323
description =
311324
typeof parsed.data.description === "string" && parsed.data.description.trim()
312325
? parsed.data.description.trim()

0 commit comments

Comments
 (0)