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