Skip to content

Commit

Permalink
Create directory structure when writing files (#244)
Browse files Browse the repository at this point in the history
* 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 <alexyoungs@hatchbed.com>
  • Loading branch information
agyoungs and agyoungs authored Oct 6, 2023
1 parent 4401ae6 commit c62c4b9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
14 changes: 10 additions & 4 deletions src/rocker/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import pexpect

import fcntl
from pathlib import Path
import signal
import struct
import termios
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions test/test_file_writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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'))

0 comments on commit c62c4b9

Please sign in to comment.