Skip to content

Commit 6843b60

Browse files
Secure command parsing; add task titles, cancellation and observation tracking; fix profiles, recovery, i18n and CI (#2455)
* fix(commands): block executable command frontmatter <!-- coderabbit-ai-generated:v1 --> * test(commands): add frontmatter engine tests and TLA+ spec --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Elliott de Launay <edelauna@gmail.com>
1 parent 0cfed55 commit 6843b60

5 files changed

Lines changed: 100 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SPECIFICATION Spec
2+
CONSTANTS
3+
Variant = "NoBomStrip"
4+
INVARIANTS TypeOK NoExecutableEngine
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SPECIFICATION Spec
2+
CONSTANTS
3+
Variant = "None"
4+
INVARIANTS TypeOK NoExecutableEngine
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SPECIFICATION Spec
2+
CONSTANTS
3+
Variant = "Fixed"
4+
INVARIANTS TypeOK NoExecutableEngine
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.
6+
\* Invariant: NoExecutableEngine. gray-matter is an oracle. Only the header language is modeled.
7+
\* Expect: Fixed holds. None fails at once. NoBomStrip fails with a BOM and an executable header.
8+
\* Result: Fixed holds in 6 states. None fails in 1 state. NoBomStrip fails in 1 state, with a BOM.
9+
\* 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.
11+
\* Cost: TLC runs in under 1 second. Agent time is not measured.
12+
\* Review: 1 covered (bot thread on double BOM, PR 2455), 0 added, 0 gap.
13+
\* Replay: two BOMs do not run code in gray-matter. It is a false alarm. A test case stays.
14+
15+
CONSTANT Variant \* "Fixed", "None" or "NoBomStrip"
16+
17+
VARIABLE in
18+
19+
\* "exec" = js or javascript in any case. "data" = yaml, yml, json or no language.
20+
\* "other" = any other name. gray-matter finds no engine for it.
21+
Inputs == [bom : BOOLEAN, lang : {"data", "exec", "other"}]
22+
23+
\* The guard reads the header language. Without the BOM strip it misses the header.
24+
GuardSeesHeader(i) == Variant = "Fixed" \/ (Variant = "NoBomStrip" /\ ~i.bom)
25+
Rejected(i) == GuardSeesHeader(i) /\ i.lang # "data"
26+
Executed(i) == i.lang = "exec" /\ ~Rejected(i)
27+
28+
TypeOK == in \in Inputs
29+
30+
Init == in \in Inputs
31+
Next == UNCHANGED in
32+
Spec == Init /\ [][Next]_in
33+
34+
\* Safety: no input reaches an executable engine.
35+
NoExecutableEngine == ~Executed(in)
36+
37+
====

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,3 +515,54 @@ Deploy the app.`
515515
})
516516
})
517517
})
518+
519+
// Invariant NoExecutableEngine (docs/architecture/tla/CommandFrontmatter.tla): no header runs code.
520+
// Exercise the real parser through both public loading paths.
521+
describe.each(["discovery", "individual"])("%s frontmatter security", (entryPoint) => {
522+
async function load(content: string) {
523+
mockFs.stat = vi.fn().mockResolvedValue({ isDirectory: () => true })
524+
mockFs.readdir = vi.fn().mockResolvedValue([{ name: "security-probe.md", isFile: () => true }])
525+
mockFs.readFile = vi.fn().mockResolvedValue(content)
526+
return entryPoint === "discovery"
527+
? (await getCommands("/test/cwd")).find((command) => command.name === "security-probe")
528+
: await getCommand("/test/cwd", "security-probe")
529+
}
530+
531+
it.each(["javascript", "js", "JS", "JavaScript", " javascript ", "\uFEFF---js", "\uFEFF\uFEFF---javascript"])(
532+
"does not execute %s frontmatter",
533+
async (header) => {
534+
const execute = vi.fn(() => ({ description: "Executed" }))
535+
vi.stubGlobal("__commandFrontmatterProbe", execute)
536+
try {
537+
const opening = header.startsWith("\uFEFF") ? header : `---${header}`
538+
const content = `${opening}\n(globalThis.__commandFrontmatterProbe())\n---\nCommand body`
539+
const result = await load(content)
540+
expect(execute).not.toHaveBeenCalled()
541+
expect(result?.description).toBeUndefined()
542+
expect(result?.content).toBe(content.trim())
543+
} finally {
544+
vi.unstubAllGlobals()
545+
}
546+
},
547+
)
548+
549+
it.each(["toml", "coffee", "constructor", "__proto__"])("keeps unsupported %s as text", async (header) => {
550+
const content = `---${header}\ndescription: Test\n---\nCommand body`
551+
const result = await load(content)
552+
expect(result?.content).toBe(content)
553+
expect(result?.description).toBeUndefined()
554+
})
555+
556+
it.each(["", "yaml", "yml", "json"])("accepts data frontmatter %s", async (header) => {
557+
const data = { description: "Test", "argument-hint": "value", mode: "code" }
558+
const metadata =
559+
header === "json" ? JSON.stringify(data) : "description: Test\nargument-hint: value\nmode: code"
560+
const result = await load(`---${header}\r\n${metadata}\r\n---\r\nCommand body`)
561+
expect(result).toMatchObject({
562+
description: "Test",
563+
argumentHint: "value",
564+
mode: "code",
565+
content: "Command body",
566+
})
567+
})
568+
})

0 commit comments

Comments
 (0)