Skip to content

Commit 816a82e

Browse files
committed
fix(security): prevent code execution in skill frontmatter
1 parent 3c5ea13 commit 816a82e

6 files changed

Lines changed: 12 additions & 23 deletions

File tree

File renamed without changes.
File renamed without changes.
File renamed without changes.

docs/architecture/tla/CommandFrontmatter.tla renamed to docs/architecture/tla/Frontmatter.tla

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
---- MODULE CommandFrontmatter ----
2-
\* Models parseCommandFrontmatter (src/services/command/commands.ts).
3-
\* Both callers, scanCommandDirectory and tryLoadCommand, use it. The property test covers both.
4-
\* Source: the header language in a workspace command file. Sink: the gray-matter engine call.
5-
\* Failure: a command file header selects an executable engine and the extension runs its code.
1+
---- MODULE Frontmatter ----
2+
\* Models parseFrontmatter (src/utils/safeFrontmatter.ts).
3+
\* Callers: scanCommandDirectory, tryLoadCommand and SkillsManager (load, read, update). One guard serves all.
4+
\* Source: the header language in a workspace command or skill file. Sink: the gray-matter engine call.
5+
\* Failure: a file header selects an executable engine and the extension runs its code.
66
\* Invariant: NoExecutableEngine. gray-matter is an oracle. Only the header language is modeled.
77
\* Expect: Fixed holds. None fails at once. NoBomStrip fails with a BOM and an executable header.
88
\* Result: Fixed holds in 6 states. None fails in 1 state. NoBomStrip fails in 1 state, with a BOM.
99
\* Replay: gray-matter runs a js header in the real library. The guard in the code stops it.
10-
\* Not modeled: the YAML and JSON parsers. The command body.
10+
\* Not modeled: the YAML and JSON parsers. The file body.
1111
\* Cost: TLC runs in under 1 second. Agent time is not measured.
1212
\* Review: 1 covered (bot thread on double BOM, PR 2455), 0 added, 0 gap.
1313
\* Replay: two BOMs do not run code in gray-matter. It is a false alarm. A test case stays.
14+
\* Skills: the skill caller is the same input domain. The skill path was unguarded before this change.
15+
\* Property test: src/utils/__tests__/safeFrontmatter.spec.ts. Caller tests: SkillsManager.spec.ts, frontmatter-commands.spec.ts.
1416

1517
CONSTANT Variant \* "Fixed", "None" or "NoBomStrip"
1618

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ Deploy the app.`
516516
})
517517
})
518518

519-
// Invariant NoExecutableEngine (docs/architecture/tla/CommandFrontmatter.tla): no header runs code.
519+
// Invariant NoExecutableEngine (docs/architecture/tla/Frontmatter.tla): no header runs code.
520520
// Exercise the real parser through both public loading paths.
521521
describe.each(["discovery", "individual"])("%s frontmatter security", (entryPoint) => {
522522
async function load(content: string) {

‎src/services/command/commands.ts‎

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from "fs/promises"
22
import * as path from "path"
33
import { Dirent } from "fs"
4-
import matter from "gray-matter"
4+
import { parseFrontmatter } from "../../utils/safeFrontmatter"
55
import { getGlobalRooDirectory, getProjectRooDirectoryForCwd } from "../roo-config"
66
import { getBuiltInCommands, getBuiltInCommand } from "./built-in-commands"
77

@@ -10,19 +10,6 @@ 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-
2613
export interface Command {
2714
name: string
2815
content: string
@@ -234,7 +221,7 @@ async function tryLoadCommand(
234221

235222
try {
236223
// Try to parse frontmatter with gray-matter
237-
parsed = parseCommandFrontmatter(content)
224+
parsed = parseFrontmatter(content)
238225
description =
239226
typeof parsed.data.description === "string" && parsed.data.description.trim()
240227
? parsed.data.description.trim()
@@ -319,7 +306,7 @@ async function scanCommandDirectory(
319306

320307
try {
321308
// Try to parse frontmatter with gray-matter
322-
parsed = parseCommandFrontmatter(content)
309+
parsed = parseFrontmatter(content)
323310
description =
324311
typeof parsed.data.description === "string" && parsed.data.description.trim()
325312
? parsed.data.description.trim()

0 commit comments

Comments
 (0)