Skip to content

Commit

Permalink
Fixes file/directory collisions
Browse files Browse the repository at this point in the history
A carefully crafted file could collide with a directory; this is no
longer possible.
  • Loading branch information
remram44 committed Oct 2, 2013
1 parent 4464965 commit 12fbe0c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 3 additions & 1 deletion file_archive/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@


def hash_file(f):
"""Hashes a file to its 40 hex character SHA1.
"""Hashes a file to a 40 hex character SHA1.
"""
h = hashlib.sha1()
h.update('file\n')
chunk = f.read(CHUNKSIZE)
while chunk:
h.update(chunk)
Expand Down Expand Up @@ -45,6 +46,7 @@ def hash_directory(path, visited=None):
raise ValueError("Can't hash directory structure: loop detected at "
"%s" % path)
visited.add(os.path.realpath(path))
h.update('dir\n')
for f in sorted(os.listdir(path)):
pf = os.path.join(path, f)
if os.path.isdir(pf):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ def test_req_empty(self):

def test_putfile(self):
h1 = self.store.add_file(self.t('file1.bin'), {'a': 'b'})
self.assertEqual(h1, '6edc650f52e26ce867b3765e0563dc3e445cdaa9')
self.assertEqual(h1, 'fce92fa2647153f7d696a3c1884d732290273102')
self.assertTrue(os.path.isfile(os.path.join(
self.path,
'objects',
'6e',
'dc650f52e26ce867b3765e0563dc3e445cdaa9')))
'fc',
'e92fa2647153f7d696a3c1884d732290273102')))
self.assertEqual(
self.store.get(h1).metadata,
{'hash': h1, 'a': 'b'})
Expand Down

0 comments on commit 12fbe0c

Please sign in to comment.