summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.opendev.org>2023-04-03 13:30:06 +0000
committerGerrit Code Review <review@openstack.org>2023-04-03 13:30:06 +0000
commit0cb25700ee15586df337a8f7b4e552ba5d864b25 (patch)
treefe288108131822e194791f31e40d0d0115fad686
parent395e9e56594201ac06a5842eac32b0d65daafcf9 (diff)
parent328c0c84086b623cda07c543faefab41d2f6f44e (diff)
downloadkolla-0cb25700ee15586df337a8f7b4e552ba5d864b25.tar.gz
kolla-0cb25700ee15586df337a8f7b4e552ba5d864b25.zip
Merge "Fix test malicious tarball fail" into stable/zed
-rw-r--r--kolla/image/tasks.py2
-rw-r--r--kolla/tests/test_build.py72
2 files changed, 73 insertions, 1 deletions
diff --git a/kolla/image/tasks.py b/kolla/image/tasks.py
index 53ae4b705..64df3d386 100644
--- a/kolla/image/tasks.py
+++ b/kolla/image/tasks.py
@@ -265,7 +265,7 @@ class BuildTask(DockerTask):
def builder(self, image):
def _test_malicious_tarball(archive, path):
- tar_file = tarfile.open(archive, 'r|gz')
+ tar_file = tarfile.open(archive, 'r|*')
for n in tar_file.getnames():
if not os.path.abspath(os.path.join(path, n)).startswith(path):
tar_file.close()
diff --git a/kolla/tests/test_build.py b/kolla/tests/test_build.py
index 078714583..6b373ea2f 100644
--- a/kolla/tests/test_build.py
+++ b/kolla/tests/test_build.py
@@ -293,9 +293,81 @@ class TasksTest(base.TestCase):
@mock.patch.dict(os.environ, clear=True)
@mock.patch('docker.APIClient')
+ def test_local_directory(self, mock_client):
+ tmpdir = tempfile.mkdtemp()
+ file_name = 'test.txt'
+ file_path = os.path.join(tmpdir, file_name)
+ saved_umask = os.umask(0o077)
+
+ try:
+ with open(file_path, 'w') as f:
+ f.write('Hello')
+
+ self.dc = mock_client
+ self.image.plugins = [{
+ 'name': 'fake-image-base-plugin-test',
+ 'type': 'local',
+ 'enabled': True,
+ 'source': tmpdir}
+ ]
+ push_queue = mock.Mock()
+ builder = tasks.BuildTask(self.conf, self.image, push_queue)
+ builder.run()
+ self.assertTrue(builder.success)
+
+ except IOError:
+ print('IOError')
+ else:
+ os.remove(file_path)
+ finally:
+ os.umask(saved_umask)
+ os.rmdir(tmpdir)
+
+ @mock.patch.dict(os.environ, clear=True)
+ @mock.patch('docker.APIClient')
def test_malicious_tar(self, mock_client):
tmpdir = tempfile.mkdtemp()
file_name = 'test.txt'
+ archive_name = 'my_archive.tar'
+ file_path = os.path.join(tmpdir, file_name)
+ archive_path = os.path.join(tmpdir, archive_name)
+ # Ensure the file is read/write by the creator only
+ saved_umask = os.umask(0o077)
+
+ try:
+ with open(file_path, 'w') as f:
+ f.write('Hello')
+
+ with tarfile.open(archive_path, 'w') as tar:
+ tar.add(file_path, arcname='../test.txt')
+
+ self.dc = mock_client
+ self.image.plugins = [{
+ 'name': 'fake-image-base-plugin-test',
+ 'type': 'local',
+ 'enabled': True,
+ 'source': archive_path}
+ ]
+
+ push_queue = mock.Mock()
+ builder = tasks.BuildTask(self.conf, self.image, push_queue)
+ builder.run()
+ self.assertFalse(builder.success)
+
+ except IOError:
+ print('IOError')
+ else:
+ os.remove(file_path)
+ os.remove(archive_path)
+ finally:
+ os.umask(saved_umask)
+ os.rmdir(tmpdir)
+
+ @mock.patch.dict(os.environ, clear=True)
+ @mock.patch('docker.APIClient')
+ def test_malicious_tar_gz(self, mock_client):
+ tmpdir = tempfile.mkdtemp()
+ file_name = 'test.txt'
archive_name = 'my_archive.tar.gz'
file_path = os.path.join(tmpdir, file_name)
archive_path = os.path.join(tmpdir, archive_name)