T09 · Insecure Skill Coding Practices
- Location
scripts/script.sh:9- Finding
Unescaped JSONL Serialization Permits Record Injection and Data Corruption
- Content
View full analysis
> "$DATA_DIR/data.jsonl" } ``` The vulnerable function is reached from `cmd_add` at lines 47-52: ```bash cmd_add() { local value="${1:?Usage: relay add }" shift || true _save_entry "add" "$value $*" local count=$(wc -l < "$DATA_DIR/data.jsonl" 2>/dev/null || echo 0) echo "Added: $value (entry #$count)" } ``` ### Technical Analysis The `val` variable contains user-controlled command-line input and is inserted directly into a purported JSON object. No JSON encoding is applied to quotation marks, backslashes, control characters, or newline characters. For example, an entry containing a quotation mark can terminate the `val` string and introduce attacker-controlled JSON fields. An entry containing a newline can inject additional physical records into the JSONL file. Subsequent commands parse records using regular expressions rather than a JSON parser, making forged or malformed records more likely to be treated as legitimate data. ### Attack Path 1. An attacker or untrusted automation supplies an entry containing JSON syntax or newline characters. 2. `cmd_add` combines the arguments and passes them to `_save_entry`. 3. `_save_entry` interpolates the text directly into a JSON template. 4. The malformed text is appended to `data.jsonl`. 5. Later `list`, `search`, `stats`, `remove`, or `export` operations process the injected or corrupted records. ### Impact Assessment Exploitation does not grant additional operating-system privileges. It affects the integrity and availability of the application's local data within the configured `RELAY ...[truncated 223 chars]- Remediation
View remediation
> "$DATA_DIR/data.jsonl" ``` Additional hardening should include: 1. Validate or reject control characters if multiline entries are not required. 2. Parse stored records with a real JSON parser instead of `grep` and `cut`. 3. Write records to a securely created temporary file and atomically rename it when modifying the complete data file. 4. Detect and report malformed existing records rather than silently processing them. 5. Add tests covering quotes, backslashes, commas, Unicode, tabs, and newlines. ]]>
