juergbi commented on code in PR #37:
URL: 
https://github.com/apache/buildstream-plugins/pull/37#discussion_r1676032326


##########
src/buildstream_plugins/sources/cargo.py:
##########
@@ -148,7 +148,25 @@ def stage(self, directory):
         try:
             mirror_file = self._get_mirror_file()
             with tarfile.open(mirror_file) as tar:
-                tar.extractall(path=directory)
+
+                def is_within_directory(directory, target):
+                    abs_directory = os.path.abspath(directory)
+                    abs_target = os.path.abspath(target)
+
+                    prefix = os.path.commonprefix([abs_directory, abs_target])
+
+                    return prefix == abs_directory
+
+                def safe_extract(tar, path=".", members=None, *, 
numeric_owner=False):
+
+                    for member in tar.getmembers():
+                        member_path = os.path.join(path, member.name)
+                        if not is_within_directory(path, member_path):
+                            raise Exception("Attempted Path Traversal in Tar 
File")
+
+                    tar.extractall(path, members, numeric_owner=numeric_owner)

Review Comment:
   @BenjaminSchubert `TarFile.extractall()` internally does the equivalent to 
`TarFile.getmembers()` anyway if no `members` argument is passed. However, it 
caches the member list including the offsets.
   
   @nanonyme While I'd like to see fully sandboxed source plugins, that would 
be a major undertaking, clearly out of scope of this fix.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to