-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We also update the patch with commit actually merged as part of libkeepass/pykeepass#388.
- Loading branch information
Showing
2 changed files
with
61 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,79 @@ | ||
From fca73313b77d2c8ea32db8b21858745500c334ec Mon Sep 17 00:00:00 2001 | ||
From: Jan-Michael Brummer <jan[email protected]> | ||
Date: Tue, 26 Mar 2024 10:27:48 +0100 | ||
Subject: [PATCH] Fix composite key computation for BytesIO | ||
From 6e591b43d60cc6e1552cf2ef8cce6ac6c59a0265 Mon Sep 17 00:00:00 2001 | ||
From: Jan-Michael Brummer <jan[email protected]> | ||
Date: Tue, 9 Apr 2024 21:58:09 +0200 | ||
Subject: [PATCH] Fix composite key computation for BytesIO (#388) | ||
|
||
In case a keyfile as BytesIO has been read before, the | ||
next read will be empty. We need to ensure that we | ||
are reading the data from the beginning. | ||
|
||
Add seek to start to fix it. | ||
|
||
Co-authored-by: Jan-Michael Brummer <[email protected]> | ||
--- | ||
pykeepass/kdbx_parsing/common.py | 9 ++++++++- | ||
1 file changed, 8 insertions(+), 1 deletion(-) | ||
pykeepass/kdbx_parsing/common.py | 6 ++++-- | ||
tests/tests.py | 14 ++++++++++++++ | ||
2 files changed, 18 insertions(+), 2 deletions(-) | ||
|
||
diff --git a/pykeepass/kdbx_parsing/common.py b/pykeepass/kdbx_parsing/common.py | ||
index 3973f61..1865bb4 100644 | ||
index 5542a0b..ac42d8c 100644 | ||
--- a/pykeepass/kdbx_parsing/common.py | ||
+++ b/pykeepass/kdbx_parsing/common.py | ||
@@ -13,6 +13,7 @@ import unicodedata | ||
import zlib | ||
import re | ||
@@ -1,13 +1,13 @@ | ||
import base64 | ||
import codecs | ||
import hashlib | ||
+import io | ||
from io import BytesIO | ||
from collections import OrderedDict | ||
import logging | ||
@@ -116,7 +117,13 @@ def compute_key_composite(password=None, keyfile=None): | ||
password_composite = b'' | ||
import re | ||
import zlib | ||
from binascii import Error as BinasciiError | ||
from collections import OrderedDict | ||
from copy import deepcopy | ||
-from io import BytesIO | ||
|
||
from construct import ( | ||
Adapter, | ||
@@ -128,6 +128,8 @@ def compute_key_composite(password=None, keyfile=None): | ||
# hash the keyfile | ||
if keyfile: | ||
- if hasattr(keyfile, "read"): | ||
+ if ( | ||
+ isinstance(keyfile, io.BufferedIOBase) | ||
+ or isinstance(keyfile, io.TextIOBase) | ||
+ or isinstance(keyfile, io.RawIOBase) | ||
+ ): | ||
+ if keyfile.seekable(): | ||
if hasattr(keyfile, "read"): | ||
+ if hasattr(keyfile, "seekable") and keyfile.seekable(): | ||
+ keyfile.seek(0) | ||
keyfile_bytes = keyfile.read() | ||
else: | ||
with open(keyfile, 'rb') as f: | ||
-- | ||
2.44.0 | ||
|
||
@@ -194,7 +196,7 @@ class XML(Adapter): | ||
|
||
def _decode(self, data, con, path): | ||
parser = etree.XMLParser(remove_blank_text=True) | ||
- return etree.parse(BytesIO(data), parser) | ||
+ return etree.parse(io.BytesIO(data), parser) | ||
|
||
def _encode(self, tree, con, path): | ||
return etree.tostring(tree) | ||
diff --git a/tests/tests.py b/tests/tests.py | ||
index 7584219..5bc02b6 100644 | ||
--- a/tests/tests.py | ||
+++ b/tests/tests.py | ||
@@ -875,6 +875,20 @@ def test_fields(self): | ||
|
||
|
||
class PyKeePassTests3(KDBX3Tests): | ||
+ def test_consecutives_saves_with_stream(self): | ||
+ # https://github.com/libkeepass/pykeepass/pull/388 | ||
+ self.setUp() | ||
+ | ||
+ with open(base_dir / self.keyfile_tmp, 'rb') as f: | ||
+ keyfile = BytesIO(f.read()) | ||
+ | ||
+ for _i in range(5): | ||
+ with PyKeePass( | ||
+ base_dir / self.database_tmp, | ||
+ password=self.password, | ||
+ keyfile=keyfile, | ||
+ ) as kp: | ||
+ kp.save() | ||
|
||
def test_set_credentials(self): | ||
self.kp_tmp.password = 'f00bar' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters