T09 · Insecure Skill Coding Practices
Warning
- Location
- upload.py:148
- Finding
- Dry-run mode can create a remote Pinecone index## Vulnerability Details **File Location**: `upload.py:148-151`, `upload.py:174-207`, and `pinecone_tool.py:390-412` **Vulnerability Type**: Unexpected remote state modification caused by eager service initialization **Risk Level**: Medium ### Vulnerable Code ```python # upload.py:148-151 # 初始化知识库 kb = KnowledgeBase(config) # 收集所有要处理的文件 ``` ```python # upload.py:174-207 # 预览模式 if args.dry_run: print("预览模式(不实际上传)\n") all_files = [] # 收集单个文件 for file_path in files_to_process: doc = kb.loader.load(file_path) if doc: all_files.append(doc) # 收集目录文件 for dir_path in directories_to_process: docs = kb.loader.load_directory(dir_path, args.recursive) all_files.extend(docs) print(f"找到 {len(all_files)} 个文件:\n") total_chunks = 0 for doc in all_files: chunks = kb.splitter.split(doc) total_chunks += len(chunks) print(f" {doc.filename}") print(f" 路径: {doc.source}") print(f" 类型: {doc.file_type}") print(f" 大小: {format_size(doc.metadata.get('size_bytes', 0))}") print(f" 预计块数: {len(chunks)}") print() print(f"\n预计总块数: {total_chunks}") print("\n预览完成,移除 --dry-run 参数执行实际上传") return ``` ```python # pinecone_tool.py:390-412 class PineconeManager: """Pinecone 管理器 - 封装所有 Pinecone 操作""" MAX_BATCH_SIZE_MB = 2 # Pinecone 单批次限制 2MB def __init__(self, config: Config): self.config = config self.pc = Pinecone(api_key=config.PINECONE_API_KEY) self.index = self._get_or_create_index() def _get_or_create_index(self): """获取或创建索引""" index_name = self.config.INDEX_NAME # 检查索引是否存在 if index_name not in self.pc.list_indexes().names(): print(f"📝 创建索引: {index_name}") self.pc.create_index( name=ind ...[truncated 2268 chars]
- Remediation
- ## Remediation Suggestions 1. Evaluate `args.dry_run` before constructing any object that initializes a remote service. 2. In dry-run mode, instantiate only `DocumentLoader` and `TextSplitter`, which are the components required for local preview processing. 3. Refactor `KnowledgeBase` to initialize `PineconeManager` lazily when an upload or search operation first requires it. 4. Separate index lookup from index creation. Index creation should require an explicit upload operation or a dedicated `--create-index` option. 5. Add an automated test that runs dry-run with a mocked Pinecone client and asserts that `list_indexes`, `create_index`, `upsert`, and `query` are never called. 6. Document that dry-run performs no embedding requests, Pinecone requests, resource creation, or vector uploads.
