T09 · Insecure Skill Coding Practices
- Location
scripts/remove_bg.py:140- Finding
Arbitrary Python Code Execution Through Unsafely Interpolated File Paths
- Content
View full analysis
- Remediation
View remediation
Security audit
Security checks for vulnerabilities and agentic risk
This appears to be a legitimate background-removal skill, but unsafe script behavior and persistent environment changes make it require careful review before installation.
Install only if you trust the publisher and are comfortable with a home-directory virtualenv, model downloads, and persistent PATH edits. Avoid processing filenames or output paths from untrusted sources until the dynamic `python -c` path handling is fixed, upgrade the rembg dependency, and prefer an explicit setup step over automatic installation during image processing.
scripts/remove_bg.py:140Arbitrary Python Code Execution Through Unsafely Interpolated File Paths
setup/install.py:62PowerShell Command Injection Through Unescaped PATH Content
The dependency is pinned to rembg[cpu,cli]==2.0.72, and the provided advisories indicate this version is affected by known vulnerabilities, including path traversal in custom model loading and SSRF-related issues in the server component. Even though this skill is for background removal, the package extras include CLI functionality and the vulnerable version is explicitly installed, so using it in setup increases real supply-chain and runtime risk if untrusted inputs or server features are exposed.
The skill instructs users to run local Python setup and processing scripts that create a virtual environment under the user's home directory, install dependencies, download models, and perform file operations, yet it declares no explicit tool scope or permissions. This creates a trust and review gap: an agent or user may execute shell, filesystem, and environment-affecting actions without clear upfront authorization boundaries.
The skill metadata and main documentation are written entirely in Chinese, which imposes a specific language on users without any opt-in or alternative language option. The policy explicitly calls for flagging language or locale constraints when the skill does not offer user choice.
The manifest description is written only in Chinese ("使用 rembg AI 模型去除图片背景") with no indication that the skill is region-specific or that users can choose another language. This can violate language/locale policy when a skill implicitly enforces a specific language without opt-in or justification.
The script's stated purpose is batch background removal, but it auto-installs and executes a setup script as part of normal operation. In this context, that behavior is more dangerous because it violates user expectations and causes additional code execution not strictly necessary for processing images.
This code automatically executes setup/install.py when the expected virtual environment is missing. In a skill context, that means a routine image-processing command can unexpectedly run additional installation logic from the repository, increasing the trust boundary and enabling arbitrary code execution if the project files are tampered with or unreviewed.
print("=" * 50)
install_script = os.path.join(PROJECT_ROOT, "setup", "install.py")
result = subprocess.run([sys.executable, install_script], capture_output=True, text=True)
if result.returncode != 0:
print(f"✗ 环境初始化失败 (Environment initialization failed): {result.stderr}")
As with the earlier installer path, the script automatically runs setup/install.py if rembg import fails. This expands a simple batch-processing operation into arbitrary local code execution from the repository, which is risky in an agent skill because users may not expect setup code to run during normal processing.
print("=" * 50)
install_script = os.path.join(PROJECT_ROOT, "setup", "install.py")
result = subprocess.run([sys.executable, install_script], capture_output=True, text=True)
if result.returncode != 0:
print(f"✗ 环境初始化失败 (Environment initialization failed): {result.stderr}")
The script builds a Python code string for python -c by interpolating input_file and output_file directly into quoted source code. A filename containing quotes or Python syntax can break out of the string literal and execute arbitrary Python when the batch job processes attacker-controlled files or paths.
"""
]
result = subprocess.run(cmd, capture_output=True, text=True)
return result.returncode == 0
The script is described as a single-image background remover, but it also bootstraps and installs an execution environment. This mismatch increases the risk of users running code that performs broader system changes than expected, which is a security-relevant transparency issue.
subprocess module calls execute external commands. Without careful input validation, this enables command injection.
for cmd in python_cmds:
try:
result = subprocess.run([cmd, "--version"],
capture_output=True, text=True, timeout=5)
if result.returncode == 0:
system_python = cmd
subprocess module calls execute external commands. Without careful input validation, this enables command injection.
for cmd in python_cmds:
try:
result = subprocess.run([cmd, "--version"],
capture_output=True, text=True, timeout=5)
if result.returncode == 0:
system_python = cmd
Commands invoke sudo or root privileges. Verify this elevated access is necessary and justified.
print()
print("请先安装 Python (Please install Python first):")
print(" - macOS: brew install python3")
print(" - Linux: sudo apt install python3 / sudo yum install python3")
print(" - Windows: https://www.python.org/downloads/")
print()
sys.exit(1)
Commands invoke sudo or root privileges. Verify this elevated access is necessary and justified.
print()
print("请先安装 Python (Please install Python first):")
print(" - macOS: brew install python3")
print(" - Linux: sudo apt install python3 / sudo yum install python3")
print(" - Windows: https://www.python.org/downloads/")
print()
sys.exit(1)
The skill has capabilities beyond its core function, including installer execution and subprocess orchestration. While not inherently malicious, these extra capabilities enlarge the attack surface and create an unexpected code-execution path in a tool that users may assume only manipulates images.
The script automatically initializes components under the user's home directory and runs subprocesses without an explicit warning or confirmation before making those changes. This is risky because it can surprise users and provides a covert execution/modification path under a benign-seeming command.
This script automatically executes setup/install.py on first run without a clear prior opt-in, causing code execution and environment modification outside the stated core task of background removal. If the package contents are tampered with, this becomes a straightforward execution path for arbitrary code.
print("=" * 50)
install_script = os.path.join(PROJECT_ROOT, "setup", "install.py")
result = subprocess.run([sys.executable, install_script], capture_output=True, text=True)
if result.returncode != 0:
print(f"✗ 环境初始化失败 (Environment initialization failed): {result.stderr}")
subprocess module calls execute external commands. Without careful input validation, this enables command injection.
else:
# 检查 rembg 是否安装
try:
subprocess.run([venv_python, "-c", "import rembg"],
capture_output=True, check=True)
except:
print("=" * 50)
subprocess module calls execute external commands. Without careful input validation, this enables command injection.
else:
# 检查 rembg 是否安装
try:
subprocess.run([venv_python, "-c", "import rembg"],
capture_output=True, check=True)
except:
print("=" * 50)
This is a second code path that automatically runs setup/install.py when rembg is missing, again executing additional code and modifying the user's home directory without explicit consent at the point of action. The skill context makes this more concerning because a seemingly simple image-processing command silently performs installation behavior.
print("=" * 50)
install_script = os.path.join(PROJECT_ROOT, "setup", "install.py")
result = subprocess.run([sys.executable, install_script], capture_output=True, text=True)
if result.returncode != 0:
print(f"✗ 环境初始化失败 (Environment initialization failed): {result.stderr}")
The script builds a Python code string with user-controlled input_file and output_file interpolated directly into a python -c command. A filename containing quotes or Python syntax can break out of the string literal and achieve arbitrary code execution in the spawned interpreter, making this more dangerous than a normal subprocess invocation.
"""
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
print(f"✗ 处理失败 (Processing failed): {result.stderr}")
subprocess module calls execute external commands. Without careful input validation, this enables command injection.
venv_python = os.path.join(VENV_DIR, "bin", "python")
try:
result = subprocess.run(
[venv_python, "-c", "import rembg; print(rembg.__version__)"],
capture_output=True,
text=True,
subprocess module calls execute external commands. Without careful input validation, this enables command injection.
# 尝试检查 u2net 模型
try:
result = subprocess.run(
[venv_python, "-c",
"from rembg import new_session; s = new_session('u2net'); print('OK')"],
capture_output=True,
The PowerShell command string includes data derived from the current PATH environment variable and is passed to powershell -Command. If PATH contains quotes or PowerShell metacharacters, this can break out of the intended string and cause unintended PowerShell command execution during installation; because this script modifies persistent user PATH, the installer context makes this more sensitive than ordinary local tooling.
# 使用 PowerShell 设置环境变量
ps_cmd = f'[Environment]::SetEnvironmentVariable("PATH", "{new_path}", "User")'
result = subprocess.run(
["powershell", "-Command", ps_cmd],
capture_output=True,
text=True
This is a real tainted-data flow: os.environ['PATH'] is incorporated into ps_cmd and then executed by PowerShell. An attacker who can influence PATH in the process environment before the installer runs could inject additional PowerShell syntax, leading to arbitrary command execution in the user's context.
# 使用 PowerShell 设置环境变量
ps_cmd = f'[Environment]::SetEnvironmentVariable("PATH", "{new_path}", "User")'
result = subprocess.run(
["powershell", "-Command", ps_cmd],
capture_output=True,
text=True
subprocess module calls execute external commands. Without careful input validation, this enables command injection.
try:
# Windows 下使用 --copies 避免符号链接问题
if system == "Windows":
subprocess.run([sys.executable, "-m", "venv", "--copies", VENV_DIR], check=True)
else:
subprocess.run([sys.executable, "-m", "venv", VENV_DIR], check=True)
print("✓ 虚拟环境创建成功 (Virtual environment created)")
No suspicious patterns detected.