Skip to content

Commit

Permalink
Replace os.sep with '/' in _check_path()
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
sechkova committed Apr 8, 2020
1 parent 87e1e11 commit e85a3b3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
13 changes: 7 additions & 6 deletions tests/test_repository_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')



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

Expand Down
2 changes: 1 addition & 1 deletion tuf/repository_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down

0 comments on commit e85a3b3

Please sign in to comment.