T02 · Agent Memory Poisoning
Error
- Location
- scripts/memory_store.py:35
- Finding
- Persistent Agent Memory Injection Through Unsanitized Stock Data<![CDATA[ ## Vulnerability Details **File Location**: `scripts/memory_store.py:35-44, 52-85, 143-163, 165-203`; data originates from `scripts/analyze_stock_pro.py:204-228` **Vulnerability Type**: Persistent prompt and state injection **Risk Level**: High ### Vulnerable Code ```python def __init__(self, workspace_path: str = None): # 获取工作区路径 if workspace_path is None: workspace_path = os.path.expanduser("~/.openclaw/workspace") self.workspace_path = workspace_path self.memory_dir = os.path.join(workspace_path, "memory") self.a_share_memory_dir = os.path.join(self.memory_dir, "a-share") self.session_state_path = os.path.join(workspace_path, "SESSION-STATE.md") self.memory_md_path = os.path.join(workspace_path, "MEMORY.md") # 确保目录存在 os.makedirs(self.a_share_memory_dir, exist_ok=True) ``` ```python def store_analysis(self, analysis_data: Dict) -> str: """存储一次分析记录""" stock_code = analysis_data.get('stock_code', 'unknown') stock_name = analysis_data.get('stock_name', '未知股票') timestamp = datetime.now() record = { "timestamp": timestamp.isoformat(), "date": timestamp.strftime("%Y-%m-%d"), "time": timestamp.strftime("%H:%M:%S"), "stock_code": stock_code, "stock_name": stock_name, "price": analysis_data.get('price'), "change_percent": analysis_data.get('change_percent'), "technical_signal": analysis_data.get('technical', {}).get('signal', 'unknown'), "sentiment": analysis_data.get('sentiment', {}).get('overall_sentiment', 'unknown'), "recommendation": analysis_data.get('recommendation', 'unknown'), "key_points": analysis_data.get('key_points', []) } today_file = self._get_today_file() self._append_to_daily_log(today_file, record) stock_memory_file = os.path.join(self.a_share_memory_dir, f"{stock_code}.json") self._append_to_stock_memory(stock_memory_file, record) self._update_session_state( ...[truncated 2534 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Store stock-analysis history in an application-specific directory that is not automatically loaded as Agent context. 2. Do not write application records into `SESSION-STATE.md` or `MEMORY.md` by default. 3. Require explicit user consent before enabling persistent history. 4. Validate stock codes against a strict allowlist such as `^[0-9]{6}$`. 5. Restrict stock names to a single line and a reasonable length. 6. Escape Markdown control characters and remove carriage returns, line feeds, and instruction-like blocks before rendering. 7. Keep authoritative data in structured JSON or a database and generate display-only Markdown from validated fields. 8. Mark imported or user-controlled text as untrusted data when it is presented to an Agent. ]]>
