libkeepass / pykeepass

Python library to interact with keepass databases (supports KDBX3 and KDBX4)
https://pypi.org/project/pykeepass/
GNU General Public License v3.0
403 stars 96 forks source link

Can't modify attachments after saving! #358

Closed afg137 closed 4 months ago

afg137 commented 10 months ago

If I (1) create a new attachment, (2) save the database, and (3) modify the same attachment, and (4) save the database again, the second change is not reflected in the database! (Not that it should matter, but I am using KeePassXC.)

To reproduce:

kp = PyKeePass([my DB], [my pasword])
e = add_entry(kp.root_group, "foo", "", "")
binary_id = kp.add_binary("bar".encode())
e.add_attachment(binary_id, "baz.txt")
kp.save()

kp.delete_binary(binary_id)
kp.save()

After running the following, the KeePassXC GUI still shows document "baz.txt" under the entry "foo", with "bar" as its contents! Also, this is not an issue with KeePassXC specifically, but rather the API itself; if I then run:

kp = PyKeePass([my DB], [my pasword])
print(kp.binaries)

I see [b'bar'], instead of an empty list.

How can I fix this? Thanks!

afg137 commented 10 months ago

I finally figured this out by looking at the test cases here.

Turns out you have to call kp.reload() after the first kp.save() if you want to keep modifying binaries after a save.

Can this be made explicit in the documentation? In particular because other database actions (e.g., adding entries) do not require a reload if there is a save between them, so this behavior is non-standard and unintuitive.

(Even though the original problem has been fixed, I'll keep this issue open to flag the suggested documentation update.)

Evidlo commented 10 months ago

I'm not able to reproduce this with your example. Here's my slightly modified code

from pykeepass import PyKeePass

db, password, key = 'test4.kdbx', 'password', 'test4.key'

kp = PyKeePass(db, password, key)
print('before:', kp.binaries)

e = kp.add_entry(kp.root_group, "foo", "", "")
binary_id = kp.add_binary("bar".encode())
e.add_attachment(binary_id, "baz.txt")
kp.save()

kp.delete_binary(binary_id)
kp.save()

kp2 = PyKeePass(db, password, key)
print('after:', kp2.binaries)

I even deleted the self.kp_tmp.reload() you linked to in tests.py and the tests still pass.

Evidlo commented 4 months ago

I'll assume this is resolved. Reopen if not.