From c62c4b97a709a9fe4c3c41dcaf0e6291ac450050 Mon Sep 17 00:00:00 2001 From: agyoungs Date: Fri, 6 Oct 2023 12:50:34 -0500 Subject: [PATCH] Create directory structure when writing files (#244) * Create directory structure when writing files * Add test for directory structure * Added in additional handling for paths outside the directory Co-authored-by: Alex Youngs --- src/rocker/core.py | 14 ++++++++++---- test/test_file_writing.py | 12 ++++++++++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/rocker/core.py b/src/rocker/core.py index 733d31e1..cb667da5 100644 --- a/src/rocker/core.py +++ b/src/rocker/core.py @@ -30,6 +30,7 @@ import pexpect import fcntl +from pathlib import Path import signal import struct import termios @@ -410,12 +411,17 @@ def run(self, command='', **kwargs): def write_files(extensions, args_dict, target_directory): all_files = {} for active_extension in extensions: - for file_name, contents in active_extension.get_files(args_dict).items(): - if os.path.isabs(file_name): + for file_path, contents in active_extension.get_files(args_dict).items(): + if os.path.isabs(file_path): print('WARNING!! Path %s from extension %s is absolute' - 'and cannot be written out, skipping' % (file_name, active_extension.get_name())) + 'and cannot be written out, skipping' % (file_path, active_extension.get_name())) continue - full_path = os.path.join(target_directory, file_name) + full_path = os.path.join(target_directory, file_path) + if Path(target_directory).resolve() not in Path(full_path).resolve().parents: + print('WARNING!! Path %s from extension %s is outside target directory' + 'and cannot be written out, skipping' % (file_path, active_extension.get_name())) + continue + Path(os.path.dirname(full_path)).mkdir(exist_ok=True, parents=True) with open(full_path, 'w') as fh: print('Writing to file %s' % full_path) fh.write(contents) diff --git a/test/test_file_writing.py b/test/test_file_writing.py index 03194812..ceca6908 100644 --- a/test/test_file_writing.py +++ b/test/test_file_writing.py @@ -48,8 +48,9 @@ def get_name(cls): def get_files(self, cliargs): all_files = {} - all_files['test_file.txt'] = """The quick brown fox jumped over the lazy dog. -%s""" % cliargs + all_files['test_file.txt'] = """The quick brown fox jumped over the lazy dog. %s""" % cliargs + all_files['path/to/test_file.txt'] = """The quick brown fox jumped over the lazy dog. %s""" % cliargs + all_files['../outside/path/to/test_file.txt'] = """Path outside directory should be skipped""" all_files['/absolute.txt'] = """Absolute file path should be skipped""" return all_files @@ -89,4 +90,11 @@ def test_file_injection(self): self.assertIn('test_key', content) self.assertIn('test_value', content) + with open(os.path.join(td, 'path/to/test_file.txt'), 'r') as fh: + content = fh.read() + self.assertIn('quick brown', content) + self.assertIn('test_key', content) + self.assertIn('test_value', content) + + self.assertFalse(os.path.exists('../outside/path/to/test_file.txt')) self.assertFalse(os.path.exists('/absolute.txt'))