Skip to content

armgit / rembg

Public Imported from GitHub
0

Rembg is a tool to remove images background

Fix path traversal vulnerability in custom model sessions (GHSA-3wqj-33cg-xc48)

Daniel Gatis7c76d3cdc575
diff --git a/rembg/sessions/ben_custom.py b/rembg/sessions/ben_custom.py
index 2295c4d..4823631 100644
--- a/rembg/sessions/ben_custom.py
+++ b/rembg/sessions/ben_custom.py
@@ -75,7 +75,13 @@ class BenCustomSession(BaseSession):
if model_path is None:
raise ValueError("model_path is required")
- return os.path.abspath(os.path.expanduser(model_path))
+ abs_path = os.path.abspath(os.path.expanduser(model_path))
+ allowed_dir = os.path.abspath(os.path.expanduser(cls.u2net_home()))
+ if not abs_path.startswith(allowed_dir + os.sep) and abs_path != allowed_dir:
+ raise ValueError(
+ f"model_path must be within the models directory: {allowed_dir}"
+ )
+ return abs_path
@classmethod
def name(cls, *args, **kwargs):
diff --git a/rembg/sessions/dis_custom.py b/rembg/sessions/dis_custom.py
index c9d946d..fb1365e 100644
--- a/rembg/sessions/dis_custom.py
+++ b/rembg/sessions/dis_custom.py
@@ -74,7 +74,13 @@ class DisCustomSession(BaseSession):
if model_path is None:
raise ValueError("model_path is required")
- return os.path.abspath(os.path.expanduser(model_path))
+ abs_path = os.path.abspath(os.path.expanduser(model_path))
+ allowed_dir = os.path.abspath(os.path.expanduser(cls.u2net_home()))
+ if not abs_path.startswith(allowed_dir + os.sep) and abs_path != allowed_dir:
+ raise ValueError(
+ f"model_path must be within the models directory: {allowed_dir}"
+ )
+ return abs_path
@classmethod
def name(cls, *args, **kwargs):
diff --git a/rembg/sessions/u2net_custom.py b/rembg/sessions/u2net_custom.py
index 6ad49fc..2e8f12b 100644
--- a/rembg/sessions/u2net_custom.py
+++ b/rembg/sessions/u2net_custom.py
@@ -80,7 +80,13 @@ class U2netCustomSession(BaseSession):
if model_path is None:
raise ValueError("model_path is required")
- return os.path.abspath(os.path.expanduser(model_path))
+ abs_path = os.path.abspath(os.path.expanduser(model_path))
+ allowed_dir = os.path.abspath(os.path.expanduser(cls.u2net_home()))
+ if not abs_path.startswith(allowed_dir + os.sep) and abs_path != allowed_dir:
+ raise ValueError(
+ f"model_path must be within the models directory: {allowed_dir}"
+ )
+ return abs_path
@classmethod
def name(cls, *args, **kwargs):