commit 7294613447e9f2857630c4eeb6547c68aad365fc parent 176a1162b4564e780f0eb19142b12e965777562b Author: SomberNight <somber.night@protonmail.com> Date: Wed, 27 Jan 2021 20:04:08 +0100 util.is_hex_str: forbid whitespaces Diffstat:
M | electrum/tests/test_util.py | | | 10 | ++++++++++ |
M | electrum/util.py | | | 5 | ++++- |
2 files changed, 14 insertions(+), 1 deletion(-)
diff --git a/electrum/tests/test_util.py b/electrum/tests/test_util.py @@ -125,13 +125,23 @@ class TestUtil(ElectrumTestCase): def test_is_hex_str(self): self.assertTrue(is_hex_str('09a4')) + self.assertTrue(is_hex_str('abCD')) self.assertTrue(is_hex_str('2A5C3F4062E4F2FCCE7A1C7B4310CB647B327409F580F4ED72CB8FC0B1804DFA')) self.assertTrue(is_hex_str('00' * 33)) + self.assertFalse(is_hex_str('0x09a4')) + self.assertFalse(is_hex_str('2A 5C3F')) + self.assertFalse(is_hex_str(' 2A5C3F')) + self.assertFalse(is_hex_str('2A5C3F ')) self.assertFalse(is_hex_str('000')) + self.assertFalse(is_hex_str('123')) + self.assertFalse(is_hex_str('0x123')) self.assertFalse(is_hex_str('qweqwe')) + self.assertFalse(is_hex_str(b'09a4')) + self.assertFalse(is_hex_str(b'\x09\xa4')) self.assertFalse(is_hex_str(None)) self.assertFalse(is_hex_str(7)) + self.assertFalse(is_hex_str(7.2)) def test_is_integer(self): self.assertTrue(is_integer(7)) diff --git a/electrum/util.py b/electrum/util.py @@ -584,9 +584,12 @@ def is_hash256_str(text: Any) -> bool: def is_hex_str(text: Any) -> bool: if not isinstance(text, str): return False try: - bytes.fromhex(text) + b = bytes.fromhex(text) except: return False + # forbid whitespaces in text: + if len(text) != 2 * len(b): + return False return True