Remove invalid dict entries from hashes.pkl

If the data in a hashes.pkl is corrupted but still de-serialized without
errors, it will mess up the replication and gets never fixed. This
happens for example if one of the keys is a NULL byte.

This patch checks if the dict keys in hashes.pkl are valid strings and
invalidates it if not.

Closes-Bug: 1830881
Change-Id: I84b062d062ff49935feed0aee3e1963bb72eb5ea
This commit is contained in:
Christian Schwede 2019-05-29 11:37:54 +02:00
parent 32d1b3246f
commit c9e78d15e1
2 changed files with 11 additions and 0 deletions

View File

@ -300,6 +300,12 @@ def read_hashes(partition_dir):
# given invalid input depending on the way in which the
# input is invalid.
pass
# Check for corrupted data that could break os.listdir()
for suffix in hashes.keys():
if not suffix.isalnum():
return {'valid': False}
# hashes.pkl w/o valid updated key is "valid" but "forever old"
hashes.setdefault('valid', True)
hashes.setdefault('updated', -1)

View File

@ -8104,6 +8104,11 @@ class TestHashesHelpers(unittest.TestCase):
# with the exactly the same value mutation from write_hashes
self.assertEqual(hashes, result)
def test_ignore_corrupted_hashes(self):
corrupted_hashes = {u'\x00\x00\x00': False, 'valid': True}
diskfile.write_hashes(self.testdir, corrupted_hashes)
result = diskfile.read_hashes(self.testdir)
self.assertFalse(result['valid'])
if __name__ == '__main__':
unittest.main()