From e85a3b3d694336d6361ded7aaccd3f45e10fef0f Mon Sep 17 00:00:00 2001 From: Teodora Sechkova Date: Wed, 8 Apr 2020 15:26:23 +0300 Subject: [PATCH] Replace os.sep with '/' in _check_path() Use a hard-coded unix separator ('/') so that an exception is also raised for paths starting with '/' when executing on Windows systems. Update test_check_path to explicitly test invalid paths starting with Windows style separator. Signed-off-by: Teodora Sechkova --- tests/test_repository_tool.py | 13 +++++++------ tuf/repository_tool.py | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/test_repository_tool.py b/tests/test_repository_tool.py index 94d7723bcc..e4ac72bdf1 100755 --- a/tests/test_repository_tool.py +++ b/tests/test_repository_tool.py @@ -1677,10 +1677,6 @@ def test_add_paths(self): # paths, which it previously did. self.targets_object.add_paths(['non-existent'], 'tuf') - # add_paths() should not raise an exception for paths that - # are not located in the repository's targets directory - repository_directory = os.path.join('repository_data', 'repository') - self.targets_object.add_paths([repository_directory], 'tuf') @@ -1724,11 +1720,16 @@ def test_check_path(self): self.assertRaises(securesystemslib.exceptions.FormatError, self.targets_object._check_path, 3) - # Test invalid pathname - starting with os separator + # Test invalid pathname + # Starting with os separator self.assertRaises(tuf.exceptions.InvalidNameError, self.targets_object._check_path, '/file1.txt') - # Test invalid pathname - using '\' as os separator + # Starting with Windows-style separator + self.assertRaises(tuf.exceptions.InvalidNameError, + self.targets_object._check_path, '\\file1.txt') + + # Using Windows-style separator ('\') self.assertRaises(tuf.exceptions.InvalidNameError, self.targets_object._check_path, 'subdir\\non-existent') diff --git a/tuf/repository_tool.py b/tuf/repository_tool.py index 77134082dd..70480ed90c 100755 --- a/tuf/repository_tool.py +++ b/tuf/repository_tool.py @@ -2784,7 +2784,7 @@ def _check_path(self, pathname): raise tuf.exceptions.InvalidNameError('Path ' + repr(pathname) + ' does not use the forward slash (/) as directory separator.') - if pathname.startswith(os.sep): + if pathname.startswith('/'): raise tuf.exceptions.InvalidNameError('Path ' + repr(pathname) + ' starts with a directory separator. All paths should be relative' ' to targets directory.')