T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/baidu-memory-tools.sh:38
- Finding
- Arbitrary Python Code Execution Through Unsafely Interpolated Memory Input<![CDATA[ ## Vulnerability Details **File Location**: - `scripts/baidu-memory-tools.sh:38-51` - `scripts/baidu-memory-tools.sh:74-96` - `scripts/triple-integration.sh:44-61` **Vulnerability Type**: Python source-code injection through shell variable interpolation **Risk Level**: Critical ### Vulnerable Code `scripts/baidu-memory-tools.sh:38-51`: ```bash python3 -c " import sys sys.path.append('$SKILL_DIR/skills/memory-baidu-embedding-db') from memory_baidu_embedding_db import MemoryBaiduEmbeddingDB try: db = MemoryBaiduEmbeddingDB() result = db.add_memory(content='$TEXT', tags=['conversation'], metadata={'source': 'triple-memory'}) print('✅ 成功存储记忆') print(f'ID: {result.get(\"id\", \"unknown\")}') except Exception as e: print(f'❌ 存储失败: {str(e)}') " ``` `scripts/baidu-memory-tools.sh:74-96`: ```bash python3 -c " import sys import os # 使用固定的workspace路径 workspace = '/root/clawd' sys.path.insert(0, os.path.join(workspace, 'skills', 'memory-baidu-embedding-db')) from memory_baidu_embedding_db import MemoryBaiduEmbeddingDB try: db = MemoryBaiduEmbeddingDB() results = db.search_memories('$QUERY', limit=$LIMIT) if results: print(f'找到 {len(results)} 条相关记忆:') for i, res in enumerate(results, 1): similarity = res.get('similarity', 0) content_preview = res['content'][:80] + '...' if len(res['content']) > 80 else res['content'] print(f' {i}. 相似度: {similarity:.3f} - {content_preview}') else: print('未找到相关记忆') except Exception as e: print(f'搜索失败: {str(e)}') " ``` `scripts/triple-integration.sh:44-61`: ```bash python3 -c " import sys sys.path.append('$SKILL_DIR/skills/memory-baidu-embedding-db') from memory_baidu_embedding_db import MemoryBaiduEmbeddingDB try: db = MemoryBaiduEmbeddingDB() result = db.add_memory( content='$TEXT', tags=['$TAGS', 'semantic'], metadata={'importance': '$IMPORTANCE', 'source': 'triple-memory'} ) print(' ...[truncated 2422 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Remove all dynamic generation of Python source code. 2. Put the Python logic in a fixed `.py` module and pass data through `sys.argv`, stdin, or a serialized JSON document. 3. If stdin is used, parse it as data rather than evaluating it: ```bash printf '%s' "$TEXT" | python3 safe_memory_store.py ``` ```python import sys content = sys.stdin.read() db.add_memory( content=content, tags=["conversation"], metadata={"source": "triple-memory"}, ) ``` 4. Pass multiple fields through JSON: ```bash python3 safe_memory_store.py <<EOF {"content": $(printf '%s' "$TEXT" | jq -Rs .)} EOF ``` 5. Validate `LIMIT` before use and enforce a reasonable range: ```bash case "$LIMIT" in ''|*[!0-9]*) echo "Invalid limit" >&2; exit 1 ;; esac if [ "$LIMIT" -lt 1 ] || [ "$LIMIT" -gt 100 ]; then echo "Limit must be between 1 and 100" >&2 exit 1 fi ``` 6. Restrict importance to the documented values and validate tag length and character sets. 7. Add regression tests containing quotes, newlines, backslashes, command substitutions, and known Python-injection payloads. 8. Run the memory component under a dedicated, unprivileged account with narrowly scoped filesystem and network access. ]]>
