File tree Expand file tree Collapse file tree 1 file changed +5
-6
lines changed Expand file tree Collapse file tree 1 file changed +5
-6
lines changed Original file line number Diff line number Diff 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-
157156def check_hash (filepath : PathLike , val : str | None = None , hash_type : str = "md5" ) -> bool :
158157 """
159158 Verify hash signature of specified file.
You can’t perform that action at this time.
0 commit comments