T09 · Insecure Skill Coding Practices
- Location
workspace-hygiene.sh:34- Finding
Unsafe Line-Based Truncation of Agent Instruction and Memory Files
- Content
View full analysis
5 entries === if [ -f "$WORKSPACE/MEMORY.md" ]; then mem_lines=$(wc -l < "$WORKSPACE/MEMORY.md") if [ "$mem_lines" -gt 80 ]; then # Archive excess, keep first 80 lines tail -n +81 "$WORKSPACE/MEMORY.md" >> "$ARCHIVE/memory-overflow-$TODAY.md" 2>/dev/null head -n 80 "$WORKSPACE/MEMORY.md" > "$WORKSPACE/MEMORY.md.tmp" mv "$WORKSPACE/MEMORY.md.tmp" "$WORKSPACE/MEMORY.md" ((lines_removed += mem_lines - 80)) ((trim_count++)) fi fi # === 3. AGENTS.md: trim if over 50 lines === if [ -f "$WORKSPACE/AGENTS.md" ]; then agent_lines=$(wc -l < "$WORKSPACE/AGENTS.md") if [ "$agent_lines" -gt 50 ]; then tail -n +51 "$WORKSPACE/AGENTS.md" >> "$ARCHIVE/agents-overflow-$TODAY.md" 2>/dev/null head -n 50 "$WORKSPACE/AGENTS.md" > "$WORKSPACE/AGENTS.md.tmp" mv "$WORKSPACE/AGENTS.md.tmp" "$WORKSPACE/AGENTS.md" ((lines_removed += agent_lines - 50)) ((trim_count++)) fi fi ``` ### Technical Analysis The script truncates `MEMORY.md` and `AGENTS.md` at fixed line boundaries without understanding their semantic structure. It does not distinguish action logs from active instructions, verify that a Markdown section remains complete, preserve designated security directives, or validate the resulting file. This behavior also differs from the documented workflow in `SKILL.md`, which says that bloat should first be identified and old content moved while leaving a pointer. The implementation instead assumes that every line after the configured limit is disposable from the active file. Although overflow content is appended to an archive, the active files are immediately replaced. Consequently, important safety constraints, operating rules, references, or active memory l ...[truncated 1331 chars]- Remediation
View remediation
` and ``. - Maintain a protected list of sections that must never be removed, including safety constraints and active-state references. - Parse Markdown sections and archive complete logical units rather than arbitrary lines. - Create a timestamped backup before modifying either file. - Write the replacement to a secure temporary file in the same directory, validate its contents, synchronize it to disk, and atomically rename it. - Confirm that archival succeeded before replacing the active file. - Leave a valid archive pointer as required by `SKILL.md`. - Consider requiring interactive approval before modifying `AGENTS.md`, because it controls future Agent behavior. ]]>
