T07 · Tool Hijacking and Spoofing
- Location
scripts/pdf2md.py:48- Finding
Automatic Import and Execution of Untrusted Python OCR Adapters
- Content
View full analysis
Vulnerability Details
File Location:
scripts/pdf2md.py:48-58, 102-119
Vulnerability Type: Untrusted local module loading and arbitrary code execution
Risk Level: MediumVulnerable Code
python def find_bailian_adapter(): """定位百炼 CLI adapter。 优先级: 1. 环境变量 BAILIAN_ADAPTER 指定的路径 2. 命令行 --bailian-adapter 参数(在 main 中处理) 3. 默认路径(若存在) """ env_path = os.environ.get("BAILIAN_ADAPTER") if env_path and os.path.exists(env_path): return env_path candidates = [ os.path.expanduser("~/bailian_cli_adapter.py"), os.path.join(os.path.dirname(os.path.abspath(__file__)), "bailian_cli_adapter.py"), ] for c in candidates: if os.path.exists(c): return c return Nonepython adapter = find_bailian_adapter() if not adapter: return None sys.path.insert(0, os.path.dirname(adapter)) try: from bailian_cli_adapter import vision_describe except ImportError: try: mod_name = os.path.splitext(os.path.basename(adapter))[0] mod = __import__(mod_name) vision_describe = mod.vision_describe except Exception: return NoneTechnical Analysis
The OCR implementation discovers Python adapter files through an environment variable, a command-line override, the user's home directory, or the Skill directory. It then adds the adapter directory to
sys.pathand imports a module from that directory.Python executes module-level statements during import. Consequently, loading an adapter is equivalent to executing the adapter as arbitrary Python code. The implementation performs no integrity, ownership, permission, signature, trusted-directory, or canonical-path validation before importing it.
The fixed import of
bailian_cli_adapterafter modifyingsys.pathalso creates module-resolution ambiguity. A malicious file with that name in the selected ...[truncated 1489 chars]- Remediation
View remediation
Remediation Suggestions
- Remove implicit adapter discovery from the user's home directory.
- Require an explicit adapter configuration and clearly warn that adapters are executable code.
- Resolve the adapter with
os.path.realpath()and ensure it remains within an approved directory. - Verify file ownership and reject group-writable or world-writable adapter files and parent directories.
- Authenticate adapters using an allowlisted cryptographic hash or signed package.
- Avoid inserting arbitrary directories at the beginning of the global
sys.path. - Prefer a narrowly defined adapter protocol executed in an isolated subprocess instead of importing arbitrary Python into the main process.
- Run adapters with reduced privileges, filesystem restrictions, network restrictions, resource limits, and a sanitized environment.
- Require explicit user consent before sending rendered document pages to any remote OCR provider.
