T03 · Remote Payload Retrieval and Execution
Error
- Location
- scripts/segment.py:39
- Finding
- Runtime Retrieval and Execution of an Unpinned Remote Dependency<![CDATA[ ## Vulnerability Details **File Location**: `scripts/segment.py`, lines 39-44 **Vulnerability Type**: Runtime installation of mutable remote code **Risk Level**: High ### Vulnerable Code ```python try: from segment_anything import SamPredictor, sam_model_registry except ImportError: print("正在安装 segment_anything...") os.system("pip install git+https://github.com/facebookresearch/segment-anything.git -q") from segment_anything import SamPredictor, sam_model_registry ``` ### Technical Analysis If `segment_anything` cannot be imported, the script invokes `pip` through a shell and installs the current contents of a remote Git repository. The dependency is not pinned to an immutable commit or verified against a cryptographic digest. Consequently, the code executed by the Skill can change after the Skill itself has been reviewed. Installation may execute package build logic, and the newly installed package is immediately imported in the same process. Although the repository is the documented upstream project, relying on its mutable default revision creates a remote payload execution and supply-chain boundary. The command is currently static, so no direct shell injection through user-controlled arguments was identified. However, `os.system` also ignores the installation command's exit status and relies on the environment's `pip` executable resolution. ### Attack Path 1. The Skill runs in an environment where `segment_anything` is not installed or cannot be imported. 2. The `ImportError` handler executes the `pip install` command. 3. `pip` retrieves the then-current source from the remote Git repository. 4. If the upstream repository, a maintainer account, the dependency chain, or the environment's tool resolution has been compromised, attacker-controlled package installation logic executes. 5. The script immediately imports the installed package, executing its module initialization code. 6. The payload runs with the same operating-sys ...[truncated 675 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Remove automatic package installation from application runtime. 2. Declare `segment-anything` as an installation-time dependency and require the environment to be prepared before the script starts. 3. Pin the package to an audited release or immutable Git commit rather than a mutable repository revision. 4. Use a lock file and cryptographic hashes for all transitive dependencies where supported. 5. On import failure, terminate with a clear error message instead of downloading and executing code. 6. If installation automation is unavoidable, invoke the interpreter explicitly without a shell, validate the exit status, and use an immutable source: ```python import subprocess import sys subprocess.run( [ sys.executable, "-m", "pip", "install", "git+https://github.com/facebookresearch/segment-anything.git@<audited-commit>", ], check=True, ) ``` This still carries installation-time risk and should be combined with commit pinning, dependency locking, and controlled deployment. ]]>
