Skip to content

Commit 3c5ea13

Browse files
coderabbitai[bot]edelauna
authored andcommitted
fix(security): prevent code execution in skill and command frontmatter
<!-- coderabbit-ai-generated:v1 -->
1 parent 6843b60 commit 3c5ea13

4 files changed

Lines changed: 108 additions & 4 deletions

File tree

‎src/services/skills/SkillsManager.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as fs from "fs/promises"
22
import * as path from "path"
33
import * as vscode from "vscode"
44
import matter from "gray-matter"
5+
import { parseFrontmatter } from "../../utils/safeFrontmatter"
56

67
import type { ClineProvider } from "../../core/webview/ClineProvider"
78
import { getGlobalRooDirectory, getGlobalAgentsDirectory, getProjectAgentsDirectoryForCwd } from "../roo-config"
@@ -316,7 +317,7 @@ export class SkillsManager {
316317

317318
try {
318319
// Use gray-matter to parse frontmatter
319-
const { data: frontmatter } = matter(fileContent)
320+
const { data: frontmatter } = parseFrontmatter(fileContent)
320321

321322
// Validate required fields (only name and description for now)
322323
if (!frontmatter.name || typeof frontmatter.name !== "string") {
@@ -490,7 +491,7 @@ export class SkillsManager {
490491

491492
// Read skill content from disk
492493
const fileContent = await fs.readFile(skill.path, "utf-8")
493-
const { content: body } = matter(fileContent)
494+
const { content: body } = parseFrontmatter(fileContent)
494495

495496
return {
496497
...skill,
@@ -945,7 +946,7 @@ Add your skill instructions here.
945946

946947
// Read the current SKILL.md file
947948
const fileContent = await fs.readFile(skill.path, "utf-8")
948-
const { data: frontmatter, content: body } = matter(fileContent)
949+
const { data: frontmatter, content: body } = parseFrontmatter(fileContent)
949950

950951
// Update the frontmatter with new modeSlugs
951952
if (newModeSlugs && newModeSlugs.length > 0) {
@@ -959,7 +960,7 @@ Add your skill instructions here.
959960
}
960961

961962
// Serialize back to SKILL.md format
962-
const newContent = matter.stringify(body, frontmatter)
963+
const newContent = matter.stringify({ content: body }, frontmatter)
963964
await fs.writeFile(skill.path, newContent, "utf-8")
964965

965966
// Refresh skills list

‎src/services/skills/__tests__/SkillsManager.spec.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,47 @@ describe("SkillsManager", () => {
177177
await skillsManager.dispose()
178178
})
179179

180+
it.each(["javascript", "js", "JS"])("blocks executable %s metadata in skill operations", async (language) => {
181+
const skillDir = p(projectSkillsDir, "example")
182+
mockDirectoryExists.mockImplementation(async (dir: string) => dir === projectSkillsDir)
183+
mockRealpath.mockImplementation(async (value: string) => value)
184+
mockReaddir.mockImplementation(async (dir: string) => (dir === projectSkillsDir ? ["example"] : []))
185+
mockStat.mockResolvedValue({ isDirectory: () => true })
186+
mockFileExists.mockResolvedValue(true)
187+
const instructions = `---${language}\n(globalThis.frontmatterExecuted(), {})\n---\nInstructions`
188+
const valid = `---\nname: example\ndescription: Example skill\n---\n${instructions}`
189+
mockReadFile.mockResolvedValue(valid)
190+
await skillsManager.discoverSkills()
191+
expect((await skillsManager.getSkillContent("example"))?.instructions).toBe(instructions)
192+
193+
const executed = vi.fn()
194+
vi.stubGlobal("frontmatterExecuted", executed)
195+
try {
196+
await skillsManager.updateSkillModes("example", "project", ["code"])
197+
expect(mockWriteFile).toHaveBeenCalledWith(
198+
p(skillDir, "SKILL.md"),
199+
expect.stringContaining(instructions),
200+
"utf-8",
201+
)
202+
mockWriteFile.mockClear()
203+
mockReadFile.mockResolvedValue(
204+
`---${language}\n(globalThis.frontmatterExecuted(), {name: "example", description: "Example"})\n---\nBody`,
205+
)
206+
await expect(skillsManager.getSkillContent("example")).rejects.toThrow("Unsupported frontmatter language")
207+
await expect(skillsManager.updateSkillModes("example", "project", ["code"])).rejects.toThrow(
208+
"Unsupported frontmatter language",
209+
)
210+
expect(mockWriteFile).not.toHaveBeenCalled()
211+
await skillsManager.discoverSkills()
212+
expect(skillsManager.getSkillsMetadata().some((skill) => skill.path === p(skillDir, "SKILL.md"))).toBe(
213+
false,
214+
)
215+
expect(executed).not.toHaveBeenCalled()
216+
} finally {
217+
vi.unstubAllGlobals()
218+
}
219+
})
220+
180221
describe("discoverSkills", () => {
181222
it("should discover skills from global directory", async () => {
182223
const pdfSkillDir = p(globalSkillsDir, "pdf-processing")
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { parseFrontmatter } from "../safeFrontmatter"
2+
3+
describe("parseFrontmatter", () => {
4+
it.each(["", "yaml", "yml", "json"])("accepts data-only %s metadata", (language) => {
5+
const metadata = language === "json" ? '{"name":"example"}' : "name: example"
6+
expect(parseFrontmatter(`---${language}\n${metadata}\n---\nBody`)).toMatchObject({
7+
data: { name: "example" },
8+
content: "Body",
9+
})
10+
})
11+
12+
it.each(["javascript", "js", "JavaScript", "JS", " javascript ", "toml"])(
13+
"rejects %s before executing metadata, including with a BOM",
14+
(language) => {
15+
for (const prefix of ["", "\uFEFF"]) {
16+
const executed = vi.fn()
17+
vi.stubGlobal("frontmatterExecuted", executed)
18+
try {
19+
expect(() =>
20+
parseFrontmatter(
21+
`${prefix}---${language}\r\n(globalThis.frontmatterExecuted(), {})\r\n---\r\nBody`,
22+
),
23+
).toThrow("Unsupported frontmatter language")
24+
expect(executed).not.toHaveBeenCalled()
25+
} finally {
26+
vi.unstubAllGlobals()
27+
}
28+
}
29+
},
30+
)
31+
32+
it("does not strip a second BOM and expose an unchecked selector", () => {
33+
const executed = vi.fn()
34+
vi.stubGlobal("frontmatterExecuted", executed)
35+
try {
36+
parseFrontmatter("\uFEFF\uFEFF---js\n(globalThis.frontmatterExecuted(), {})\n---\nBody")
37+
expect(executed).not.toHaveBeenCalled()
38+
} finally {
39+
vi.unstubAllGlobals()
40+
}
41+
})
42+
43+
it("preserves Markdown without metadata", () => {
44+
expect(parseFrontmatter("# Heading\nBody").content).toBe("# Heading\nBody")
45+
})
46+
})

‎src/utils/safeFrontmatter.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import matter from "gray-matter"
2+
3+
/** Parse Markdown metadata without allowing document-selected executable engines. */
4+
export function parseFrontmatter(content: string) {
5+
// gray-matter strips a leading BOM before detecting the opening delimiter.
6+
const normalized = content.replace(/^\uFEFF/, "")
7+
if (normalized.startsWith("---") && normalized[3] !== "-") {
8+
const { name } = matter.language(normalized)
9+
if (name && !["yaml", "yml", "json"].includes(name)) {
10+
throw new Error(`Unsupported frontmatter language: ${name}`)
11+
}
12+
}
13+
14+
// Passing options also bypasses gray-matter's shared, input-only parse cache.
15+
return matter(content, { language: "yaml" })
16+
}

0 commit comments

Comments
 (0)