Skip to content

Commit 4431852

Browse files
Update monai/apps/utils.py
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Signed-off-by: h3rrr <81402797+h3rrr@users.noreply.github.com>
1 parent e204524 commit 4431852

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

monai/apps/utils.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,14 @@ def safe_extract_member(member, extract_to):
145145
full_path = os.path.join(extract_to, member_path)
146146
full_path = os.path.normpath(full_path)
147147

148-
extract_to_abs = os.path.abspath(extract_to)
149-
full_path_abs = os.path.abspath(full_path)
150-
151-
if not (full_path_abs == extract_to_abs or full_path_abs.startswith(extract_to_abs + os.sep)):
152-
raise ValueError(f"Path traversal attack detected: {member_path}")
148+
extract_root = os.path.realpath(extract_to)
149+
target_real = os.path.realpath(full_path)
150+
# Ensure the resolved path stays within the extraction root
151+
if os.path.commonpath([extract_root, target_real]) != extract_root:
152+
raise ValueError(f"Unsafe path: path traversal {member_path}") # noqa: TRY003
153153

154154
return full_path
155155

156-
157156
def check_hash(filepath: PathLike, val: str | None = None, hash_type: str = "md5") -> bool:
158157
"""
159158
Verify hash signature of specified file.

0 commit comments

Comments
 (0)