Details
# In s_command.py, filter out custom models
ALLOWED_HTTP_MODELS = [
name for name in sessions_names
if not name.endswith('_custom')
]
# Use ALLOWED_HTTP_MODELS in the model parameter regex
model: str = Query(
regex=r"(" + "|".join(ALLOWED_HTTP_MODELS) + ")",
default="u2net",
)
Option 2: Validate model_path Against Allowlist
If custom models must be supported via HTTP:
import os
ALLOWED_MODEL_DIRS = [
os.path.expanduser("~/.u2net"),
"/app/models", # or your designated model directory
]
def validate_model_path(path: str) -> str:
"""Validate model path is within allowed directories."""
abs_path = os.path.abspath(os.path.expanduser(path))
for allowed_dir in ALLOWED_MODEL_DIRS:
allowed_abs = os.path.abspath(allowed_dir)
if abs_path.startswith(allowed_abs + os.sep):
return abs_path
raise ValueError(f"model_path must be within allowed directories")
Option 3: Document Security Considerations
At minimum, add security warnings to the documentation:
⚠️ **Security Warning**: When running `rembg s` in production:
- Do NOT expose the server directly to the internet
- Use a reverse proxy with authentication
- Consider disabling custom model support
---
References
- CWE-22: Improper Limitation of a Pathname to a Restricted Directory
- CWE-73: External Control of File Name or Path
- OWASP Path Traversal: Path Traversal Attack
---