diff --git a/pykeepass/pykeepass.py b/pykeepass/pykeepass.py index 0e4b37ee..ac43ae7b 100644 --- a/pykeepass/pykeepass.py +++ b/pykeepass/pykeepass.py @@ -687,7 +687,7 @@ def deref(self, value): ref (str): KeePass reference string to another field Returns: - str or uuid.UUID + str, uuid.UUID or None if no match found [fieldref]: https://keepass.info/help/base/fieldrefs.html """ @@ -710,6 +710,8 @@ def deref(self, value): if search_in == 'uuid': search_value = uuid.UUID(search_value) ref_entry = self.find_entries(first=True, **{search_in: search_value}) + if ref_entry is None: + return None value = value.replace(ref, getattr(ref_entry, wanted_field)) return self.deref(value) diff --git a/tests/tests.py b/tests/tests.py index 75842192..554b035c 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -528,6 +528,20 @@ def test_references(self): self.assertNotEqual(original_entry, clone1) self.assertNotEqual(clone1, clone2) + def test_broken_reference(self): + # TODO: move the entry into test databases + broken_entry_title = 'broken reference' + self.kp.add_entry( + self.kp.root_group, + title=broken_entry_title, + username='{REF:U@I:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA}', + password='{REF:P@I:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA}', + ) + broken_entry = self.kp.find_entries(title=broken_entry_title, first=True) + self.assertEqual(broken_entry.deref('username'), None) + self.assertEqual(broken_entry.deref('password'), None) + self.kp.delete_entry(broken_entry) + def test_set_and_get_fields(self): time = datetime.now(timezone.utc).replace(microsecond=0) changed_time = time + timedelta(hours=9)