|
15 | 15 | import os |
16 | 16 | import requests |
17 | 17 | import sys |
| 18 | +import tarfile |
| 19 | +import tempfile |
18 | 20 | from unittest import mock |
19 | 21 |
|
20 | 22 | from kolla.cmd import build as build_cmd |
@@ -299,6 +301,47 @@ def test_process_source(self, mock_get, mock_client, |
299 | 301 | else: |
300 | 302 | self.assertIsNotNone(get_result) |
301 | 303 |
|
| 304 | + @mock.patch.dict(os.environ, clear=True) |
| 305 | + @mock.patch('docker.APIClient') |
| 306 | + def test_malicious_tar(self, mock_client): |
| 307 | + self.conf.set_override('install_type', 'source') |
| 308 | + tmpdir = tempfile.mkdtemp() |
| 309 | + file_name = 'test.txt' |
| 310 | + archive_name = 'my_archive.tar.gz' |
| 311 | + file_path = os.path.join(tmpdir, file_name) |
| 312 | + archive_path = os.path.join(tmpdir, archive_name) |
| 313 | + # Ensure the file is read/write by the creator only |
| 314 | + saved_umask = os.umask(0o077) |
| 315 | + |
| 316 | + try: |
| 317 | + with open(file_path, 'w') as f: |
| 318 | + f.write('Hello') |
| 319 | + |
| 320 | + with tarfile.open(archive_path, 'w:gz') as tar: |
| 321 | + tar.add(file_path, arcname='../test.txt') |
| 322 | + |
| 323 | + self.dc = mock_client |
| 324 | + self.image.plugins = [{ |
| 325 | + 'name': 'fake-image-base-plugin-test', |
| 326 | + 'type': 'local', |
| 327 | + 'enabled': True, |
| 328 | + 'source': archive_path} |
| 329 | + ] |
| 330 | + |
| 331 | + push_queue = mock.Mock() |
| 332 | + builder = build.BuildTask(self.conf, self.image, push_queue) |
| 333 | + builder.run() |
| 334 | + self.assertFalse(builder.success) |
| 335 | + |
| 336 | + except IOError: |
| 337 | + print('IOError') |
| 338 | + else: |
| 339 | + os.remove(file_path) |
| 340 | + os.remove(archive_path) |
| 341 | + finally: |
| 342 | + os.umask(saved_umask) |
| 343 | + os.rmdir(tmpdir) |
| 344 | + |
302 | 345 | @mock.patch('os.path.exists') |
303 | 346 | @mock.patch('os.utime') |
304 | 347 | @mock.patch('shutil.rmtree') |
|
0 commit comments