Skip to content

Commit ed311e7

Browse files
committed
entry: Do not set protected=True on setters
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
1 parent 769ee25 commit ed311e7

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

pykeepass/entry.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def _get_string_field(self, key):
9292
if field is not None:
9393
return field.text
9494

95-
def _set_string_field(self, key, value, protected=True):
95+
def _set_string_field(self, key, value, protected=False):
9696
"""Create or overwrite a string field in an Entry
9797
9898
Args:
@@ -183,7 +183,7 @@ def password(self):
183183

184184
@password.setter
185185
def password(self, value):
186-
return self._set_string_field('Password', value)
186+
return self._set_string_field('Password', value, protected=True)
187187

188188
@property
189189
def url(self):
@@ -231,7 +231,7 @@ def otp(self):
231231

232232
@otp.setter
233233
def otp(self, value):
234-
return self._set_string_field('otp', value)
234+
return self._set_string_field('otp', value, protected=True)
235235

236236
@property
237237
def history(self):
@@ -338,6 +338,10 @@ def is_custom_property_protected(self, key):
338338
339339
"""
340340
assert key not in reserved_keys, '{} is a reserved key'.format(key)
341+
return self._is_property_protected(key)
342+
343+
def _is_property_protected(self, key):
344+
"""Whether a property is protected."""
341345
field = self._xpath('String/Key[text()="{}"]/../Value'.format(key), first=True)
342346
if field is not None:
343347
return field.attrib.get("Protected", "False") == "True"

tests/tests.py

+19
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,25 @@ def test_issue344(self):
10231023
e._element.xpath('Times/ExpiryTime')[0].text = None
10241024
self.assertEqual(e.expiry_time, None)
10251025

1026+
def test_issue376(self):
1027+
# Setting the properties of an entry should not change the Protected
1028+
# property
1029+
subgroup = self.kp.root_group
1030+
e = self.kp.add_entry(subgroup, 'banana_entry', 'user', 'pass')
1031+
1032+
self.assertEqual(e._is_property_protected('Password'), True)
1033+
self.assertEqual(e._is_property_protected('Title'), False)
1034+
self.assertEqual(e.otp, None)
1035+
self.assertEqual(e._is_property_protected('otp'), False)
1036+
1037+
e.title = 'pineapple'
1038+
e.password = 'pizza'
1039+
e.otp = 'aa'
1040+
1041+
self.assertEqual(e._is_property_protected('Password'), True)
1042+
self.assertEqual(e._is_property_protected('Title'), False)
1043+
self.assertEqual(e._is_property_protected('otp'), True)
1044+
10261045
class EntryFindTests4(KDBX4Tests, EntryFindTests3):
10271046
pass
10281047

0 commit comments

Comments
 (0)