Skip to content

Commit

Permalink
entry: Do not set protected=True on setters
Browse files Browse the repository at this point in the history
Setting a property would call _set_string_field which by default would
set the protected status of the attribute as "True".

In order to verify in tests if a property is protected we add a private
method _is_property_protected which shares the code with
is_custom_property_protected.

Fixes: #376
  • Loading branch information
A6GibKm committed Mar 6, 2024
1 parent 769ee25 commit ed311e7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pykeepass/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _get_string_field(self, key):
if field is not None:
return field.text

def _set_string_field(self, key, value, protected=True):
def _set_string_field(self, key, value, protected=False):
"""Create or overwrite a string field in an Entry
Args:
Expand Down Expand Up @@ -183,7 +183,7 @@ def password(self):

@password.setter
def password(self, value):
return self._set_string_field('Password', value)
return self._set_string_field('Password', value, protected=True)

@property
def url(self):
Expand Down Expand Up @@ -231,7 +231,7 @@ def otp(self):

@otp.setter
def otp(self, value):
return self._set_string_field('otp', value)
return self._set_string_field('otp', value, protected=True)

@property
def history(self):
Expand Down Expand Up @@ -338,6 +338,10 @@ def is_custom_property_protected(self, key):
"""
assert key not in reserved_keys, '{} is a reserved key'.format(key)
return self._is_property_protected(key)

def _is_property_protected(self, key):
"""Whether a property is protected."""
field = self._xpath('String/Key[text()="{}"]/../Value'.format(key), first=True)
if field is not None:
return field.attrib.get("Protected", "False") == "True"
Expand Down
19 changes: 19 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,25 @@ def test_issue344(self):
e._element.xpath('Times/ExpiryTime')[0].text = None
self.assertEqual(e.expiry_time, None)

def test_issue376(self):
# Setting the properties of an entry should not change the Protected
# property
subgroup = self.kp.root_group
e = self.kp.add_entry(subgroup, 'banana_entry', 'user', 'pass')

self.assertEqual(e._is_property_protected('Password'), True)
self.assertEqual(e._is_property_protected('Title'), False)
self.assertEqual(e.otp, None)
self.assertEqual(e._is_property_protected('otp'), False)

e.title = 'pineapple'
e.password = 'pizza'
e.otp = 'aa'

self.assertEqual(e._is_property_protected('Password'), True)
self.assertEqual(e._is_property_protected('Title'), False)
self.assertEqual(e._is_property_protected('otp'), True)

class EntryFindTests4(KDBX4Tests, EntryFindTests3):
pass

Expand Down

0 comments on commit ed311e7

Please sign in to comment.