T09 · Insecure Skill Coding Practices
- Location
SKILL.md:39- Finding
Unescaped Markdown Metadata Allows JSON Injection and Index Corruption
- Content
View full analysis
/dev/null || echo 0) lines=$(wc -l < "$f") frontmatter=$(head -20 "$f" | grep -c '^---$' || echo 0) echo "{\"file\":\"$f\",\"title\":\"$title\",\"headings_count\":$headings,\"lines\":$lines,\"has_frontmatter\":$frontmatter}" done | jq -s '.' > index.json ``` ### Technical Analysis The Skill constructs JSON by directly interpolating the Markdown-derived title and filesystem-derived path into an `echo` string. Neither value is encoded as a JSON string before being inserted. An attacker who controls a Markdown heading or filename can include quotation marks, backslashes, control characters, or JSON syntax. This can produce either: - Valid JSON containing attacker-injected properties. - Invalid JSON that causes `jq` to fail and prevents creation of the index. - Misleading metadata that is subsequently trusted by search, publication, or automation systems. For example, a heading resembling the following can terminate the intended `title` value and introduce additional properties: ```markdown # Example","trusted":true,"injected":"value ``` Because `jq` receives the already assembled text, it parses the injected structure rather than treating the entire heading as an inert string. This is JSON injection rather than shell command injection: the quoted shell expansions prevent heading contents from being directly interpreted as shell commands. No path to arbitrary code execution, privilege escalation, credential access, or network exfiltration was identified. ### Attack Path 1. An attacker creates or supplies a Markdown file within the directory selected for indexing. 2. The attacker places crafted JSON syntax in th ...[truncated 1405 chars]- Remediation
View remediation
/dev/null) || headings=0 lines=$(wc -l < "$f") frontmatter=$(head -20 "$f" | grep -c '^---$') || frontmatter=0 jq -n \ --arg file "$f" \ --arg title "$title" \ --argjson headings_count "$headings" \ --argjson lines "$lines" \ --argjson has_frontmatter "$frontmatter" \ '{ file: $file, title: $title, headings_count: $headings_count, lines: $lines, has_frontmatter: $has_frontmatter }' done | jq -s '.' > index.json ``` Additional hardening measures: 1. Validate that numeric values match a strict integer format before passing them to `--argjson`. 2. Treat filenames and all document content as untrusted input. 3. Write output to a temporary file and atomically rename it only after `jq` succeeds, preventing a failed scan from replacing a valid index. 4. Enable explicit pipeline error handling in a script, such as `set -euo pipefail`, while handling expected non-match statuses from `grep`. 5. Add regression tests using headings and filenames containing quotes, backslashes, tabs, Unicode characters, and embedded JSON syntax. 6. Ensure downstream consumers accept only the documented schema and reject unexpected properties. ]]>
