tx: replace whitespace chars in raw tx string
What changed, and why it matters
This commit changes how Electrum cleans up raw Bitcoin transaction text that users paste into the wallet. Previously it only trimmed whitespace from the beginning and end. Now it removes all whitespace characters (including newlines and spaces) anywhere inside the string. This is a usability fix for copy-pasting transactions from documents such as a timelock recovery PDF. It is not a security patch, but it slightly reduces the chance that a malformed or trick pasted transaction could behave unexpectedly.
No urgent action. Treat as a routine robustness/usability improvement. Reviewers may want to confirm that removing all whitespace does not interfere with base64-encoded transaction inputs, since base64 alphabet does not include whitespace, but stripping embedded whitespace before base64 decoding is generally safe.
Security signals we found
Input sanitization change for externally supplied raw transaction strings
Previously embedded whitespace could cause parsing paths to differ or fail
No explicit security claim in commit message or diff
Evidence from the diff
In electrum/transaction.py, convert_raw_tx_to_hex() now uses re.sub(r’\s’, ‘’, raw) on str inputs and re.sub(rb’\s’, b’‘, raw) on bytes inputs, instead of only str.strip()/bytes.strip(). The change lets internally embedded whitespace (newlines, tabs, spaces) be stripped before hex/base64 decoding. Tests were updated to inject whitespace in the middle of sample transactions, not just at the edges. The commit message frames this as a convenience improvement, not a vulnerability fix.
Changed components
electrum/transaction.py:convert_raw_tx_to_hex()tests/test_transaction.pyInspect captured patch +9 / −3
diff --git a/electrum/transaction.py b/electrum/transaction.py
index 7f621af..5bbbdf8 100644
--- a/electrum/transaction.py
+++ b/electrum/transaction.py
@@ -36,6 +36,7 @@ from enum import IntEnum
import itertools
import binascii
import copy
+import re
import electrum_ecc as ecc
from electrum_ecc.util import bip340_tagged_hash
@@ -1482,7 +1483,11 @@ def convert_raw_tx_to_hex(raw: Union[str, bytes]) -> str:
if not raw:
raise ValueError("empty string")
raw_unstripped = raw
- raw = raw.strip()
+ # remove all whitespace characters
+ if isinstance(raw, str):
+ raw = re.sub(r'\s', '', raw)
+ else:
+ raw = re.sub(rb'\s', b'', raw)
# try hex
try:
return binascii.unhexlify(raw).hex()
diff --git a/tests/test_transaction.py b/tests/test_transaction.py
index 9b0402a..ed74461 100644
--- a/tests/test_transaction.py
+++ b/tests/test_transaction.py
@@ -276,10 +276,11 @@ class TestTransaction(ElectrumTestCase):
data = raw_tx.data
tx_from_any(data) # test if raises (should not)
else:
+ mid = len(raw_tx.data) // 2
if isinstance(raw_tx.data, str):
- data = whitespace_str + raw_tx.data + whitespace_str
+ data = whitespace_str + raw_tx.data[:mid] + whitespace_str + raw_tx.data[mid:] + whitespace_str
else:
- data = whitespace_bytes + raw_tx.data + whitespace_bytes
+ data = whitespace_bytes + raw_tx.data[:mid] + whitespace_bytes + raw_tx.data[mid:] + whitespace_bytes
if raw_tx.is_whitespace_allowed:
tx_from_any(data) # test if raises (should not)
else:
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.