T09 · Insecure Skill Coding Practices
- Location
scripts/script.sh:10- Finding
Unescaped Input Allows JSONL Record Injection and Data Corruption
- Content
View full analysis
> "$DATA_DIR/data.jsonl" } ``` The vulnerable function is reached through the `add` command at `scripts/script.sh:48-52`: ```bash cmd_add() { local value="${1:?Usage: rectifier 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` parameter contains user-controlled command-line input and is interpolated directly into a JSON object. Shell quoting prevents this value from being interpreted as a shell command, but it does not make the value safe for JSON. Quotation marks, backslashes, carriage returns, newlines, and other JSON control characters are not escaped. An attacker can consequently terminate the intended `val` field, corrupt the record, or insert additional lines that resemble independent JSONL records. The application's readers use line-based processing and `grep`/`cut` rather than a strict JSON parser. They therefore may display or export injected lines as legitimate stored records. ### Attack Path 1. An attacker or untrusted caller invokes `scripts/script.sh add` with an entry containing JSON delimiters and a newline. 2. `cmd_add` forwards the input to `_save_entry`. 3. `_save_entry` writes the input verbatim into `data.jsonl`. 4. The newline creates an additional physical record, while quotation marks can alter the apparent JSON structure. 5. Subsequent `list`, `search`, and `export` operations process the forged or malformed content. 6. Users may rely on misleading output or distribute an export containing atta ...[truncated 690 chars]- Remediation
View remediation
> "$DATA_DIR/data.jsonl" ``` Additional hardening should include: 1. Parse stored records with a strict JSON parser rather than `grep` and `cut`. 2. Reject malformed existing records and report their line numbers. 3. Consider limiting the accepted entry size to prevent excessive disk consumption. 4. Write records to a securely created temporary file and atomically replace the destination where multi-step updates are required. 5. Add tests covering quotation marks, backslashes, tabs, carriage returns, newlines, and Unicode input. ]]>
