T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/auto_evolve.py:95
- Finding
- Destructive Memory Rewriting Without Backup or Transactional Recovery<![CDATA[ ## Vulnerability Details **File Location**: `scripts/auto_evolve.py`, lines 95-147 **Vulnerability Type**: Destructive data handling and misleading archival behavior **Risk Level**: High ### Vulnerable Code ```python def compress_memory_if_needed(): """L1: 若MEMORY.md超过阈值,压缩并总结""" config = load_config() threshold_bytes = config.get("memory_threshold_mb", 2) * 1024 * 1024 memory_file = WORKSPACE / "MEMORY.md" if not memory_file.exists(): return ["ℹ️ MEMORY.md 不存在,跳过压缩"] size = memory_file.stat().st_size if size < threshold_bytes: return [f"ℹ️ MEMORY.md 大小 {size/1024/1024:.2f}MB,未超过阈值,跳过压缩"] # 超过阈值,读取内容进行总结 try: with open(memory_file, "r", encoding="utf-8") as f: content = f.read() # 保留前20%和最后80%的分界线 # 实际应该调用大模型API总结,但这里先做简单分割演示 lines = content.split("\n") total_lines = len(lines) # 保留最近80%的内容 keep_from = int(total_lines * 0.2) recent_content = "\n".join(lines[keep_from:]) # 生成摘要头 summary = f"""# MEMORY.md — 压缩摘要 > 由 robot-evolve 自动压缩生成 | {datetime.now().strftime('%Y-%m-%d')} ## 原始大小 - 压缩前: {size/1024/1024:.2f}MB - 行数: {total_lines} 行 ## 早期内容摘要 (详细历史请查看 `memory/evolution/` 中的归档日志) --- """ # 写入新内容 with open(memory_file, "w", encoding="utf-8") as f: f.write(summary + recent_content) new_size = memory_file.stat().st_size result = [ f"✅ MEMORY.md 已压缩(从 {size/1024/1024:.2f}MB 减少至 {new_size/1024/1024:.2f}MB)", f"✅ 保留最近 {int(total_lines * 0.8)} 行内容" ] log_action("记忆压缩", f"MEMORY.md从{size/1024/1024:.2f}MB压缩至{new_size/1024/1024:.2f}MB", "L1") return result ``` ### Technical Analysis The operation is described as compression and summarization, but it does not summarize or archive the removed material. It calculates a position at 20% of the file, retains only the rem ...[truncated 1493 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Require explicit user confirmation before modifying `MEMORY.md`. 2. Write the transformed content to a temporary file in the same directory. 3. Flush and synchronize the temporary file, validate its encoding and expected content, and then use an atomic replacement operation. 4. Create a timestamped, read-only backup before replacement. 5. Archive the removed content under a dedicated memory archive, not merely an operational log directory. 6. Replace the line-deletion behavior with genuine summarization, or clearly describe it as retention-based truncation. 7. Verify that the archive exists and is readable before deleting anything from the active file. 8. Restore the backup automatically if transformation or validation fails. 9. Record hashes, byte counts, backup paths, and restoration status in the audit log. ]]>
