Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Additional checks for tar file extraction, closes #965

davidmezzetti303a2576155d
diff --git a/src/python/txtai/archive/tar.py b/src/python/txtai/archive/tar.py
index 676c0cdc..81dce221 100644
--- a/src/python/txtai/archive/tar.py
+++ b/src/python/txtai/archive/tar.py
@@ -28,10 +28,13 @@ class Tar(Compress):
# Validate paths
for member in tar.getmembers():
fullpath = os.path.join(path, member.name)
- if not self.validate(path, fullpath):
- raise IOError(f"Invalid tar entry: {member.name}")
- tar.extractall(output)
+ # Reject paths outside of base directory and links
+ if not self.validate(path, fullpath) or member.issym() or member.islnk():
+ raise IOError(f"Invalid tar entry: {member.name}{'->' + member.linkname if member.linkname else ''}")
+
+ # Unpack data. Apply default data filter to only allow basic TAR features.
+ tar.extractall(output, filter="data")
def compression(self, path):
"""
diff --git a/test/python/testarchive.py b/test/python/testarchive.py
index dd1466e7..3d64b4f7 100644
--- a/test/python/testarchive.py
+++ b/test/python/testarchive.py
@@ -49,6 +49,32 @@ class TestArchive(unittest.TestCase):
path = os.path.join(archive.path(), "dir", "test")
self.assertTrue(os.path.exists(path))
+ def testInvalidTarLink(self):
+ """
+ Test invalid tar file with symlinks
+ """
+
+ symlink = os.path.join(tempfile.gettempdir(), "link")
+
+ # Remove symlink if it already exists
+ try:
+ os.remove(symlink)
+ except OSError:
+ pass
+
+ # Create symlink and add to TAR file
+ os.symlink(os.path.join(tempfile.gettempdir(), "noexist"), symlink)
+
+ path = os.path.join(tempfile.gettempdir(), "badtarlink")
+ with tarfile.open(path, "w") as tar:
+ tar.add(symlink, arcname="l")
+
+ archive = ArchiveFactory.create()
+
+ # Validate error is thrown for file
+ with self.assertRaises(IOError):
+ archive.load(path, "tar")
+
def testInvalidTar(self):
"""
Test invalid tar file