Merge pull request #11094 from devdavidejesus/fix-10760-storage-not-utf8
What changed, and why it matters
This commit improves Electrum's error handling when a user tries to open a file that is not a valid Electrum wallet. Previously, if the file contained bytes that were not valid UTF-8 text, the program would crash with a confusing technical error. Now it shows a clearer message suggesting the file may not be an Electrum wallet. This is a minor user-experience and robustness improvement, not a fix for a serious security vulnerability.
No urgent action required. Treat as a routine quality fix. If reviewing a larger set of changes, confirm that WalletFileException is handled safely upstream and does not leak sensitive path information to untrusted UIs.
Security signals we found
Error-handling hardening
User-facing exception message improved
No cryptographic or parsing logic changed
Evidence from the diff
The change wraps the UTF-8 decoding of wallet files in a try/except for UnicodeDecodeError and re-raises it as a WalletFileException with a user-friendly message. A unit test verifies that a binary file starting with the SQLite magic header raises WalletFileException. This prevents raw UnicodeDecodeError stack traces when non-wallet files are loaded, but does not change how wallet contents are parsed, validated, or decrypted.
Changed components
electrum/storage.py:WalletStorage.__init__tests/test_wallet.pyInspect captured patch +10 / −1
### electrum/storage.py
@@ -77,7 +77,10 @@ def __init__(
raise StorageReadWriteError(e) from e
if self.file_exists():
with open(self.path, "rb") as f:
- self.raw = f.read().decode("utf-8")
+ try:
+ self.raw = f.read().decode("utf-8")
+ except UnicodeDecodeError as e:
+ raise WalletFileException("Cannot read wallet file. (not an Electrum wallet file?)") from e
self.pos = f.seek(0, os.SEEK_END)
self.init_pos = self.pos
self._encryption_version = self._init_encryption_version()
### tests/test_wallet.py
@@ -71,6 +71,12 @@ def test_read_dictionary_from_file(self):
self.assertEqual("b", db.get("a"))
self.assertEqual("d", db.get("c"))
+ def test_read_file_that_is_not_utf8(self):
+ with open(self.wallet_path, "wb") as f:
+ f.write(b"SQLite format 3\x00\xd8")
+ with self.assertRaises(util.WalletFileException):
+ WalletStorage(self.wallet_path)
+
def test_write_dictionary_to_file(self):
storage = WalletStorage(self.wallet_path)Why this scored 23/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.