| 1 |
import contextlib |
| 2 |
import copy |
| 3 |
import traceback |
| 4 |
import json |
| 5 |
from typing import Any |
| 6 |
|
| 7 |
import jsonpatch |
| 8 |
from jsonpatch import JsonPatchException |
| 9 |
from jsonpointer import JsonPointerException |
| 10 |
|
| 11 |
from . import ElectrumTestCase |
| 12 |
|
| 13 |
from electrum.json_db import JsonDB |
| 14 |
from electrum.util import WalletFileException |
| 15 |
|
| 16 |
class TestJsonpatch(ElectrumTestCase): |
| 17 |
|
| 18 |
async def test_op_replace(self): |
| 19 |
data1 = {'foo': 'bar', 'numbers': [1, 3, 4, 8], 'dictlevelA1': {'secret1': 2, 'secret2': 4, 'secret3': 6}} |
| 20 |
patches = [{"op": "replace", "path": "/dictlevelA1/secret2", "value": 2222}] |
| 21 |
jpatch = jsonpatch.JsonPatch(patches) |
| 22 |
data2 = jpatch.apply(data1) |
| 23 |
self.assertEqual( |
| 24 |
{'foo': 'bar', 'numbers': [1, 3, 4, 8], 'dictlevelA1': {'secret1': 2, 'secret2': 2222, 'secret3': 6}}, |
| 25 |
data2 |
| 26 |
) |
| 27 |
|
| 28 |
@contextlib.contextmanager |
| 29 |
def _customAssertRaises(self, *args, **kwargs): |
| 30 |
with self.assertRaises(*args, **kwargs) as ctx: |
| 31 |
try: |
| 32 |
yield ctx |
| 33 |
except Exception as e: |
| 34 |
|
| 35 |
ctx._customctx_original_tb = "".join(traceback.format_exception(e)) |
| 36 |
raise |
| 37 |
|
| 38 |
async def test_patch_does_not_leak_privatekeys(self): |
| 39 |
data1 = { |
| 40 |
'dictlevelB1': 'secret77', |
| 41 |
'dictlevelC1': [1, "secret99", 4, 8], |
| 42 |
'dictlevelA1': {"dictlevelA2_aa": "secret11", "dictlevelA2_bb": "secret12", "dictlevelA2_cc": "secret13"}} |
| 43 |
def fail_if_leaking_secret(ctx) -> None: |
| 44 |
self.assertNotIn("secret", str(ctx.exception)) |
| 45 |
self.assertNotIn("secret", repr(ctx.exception)) |
| 46 |
self.assertNotIn("secret", ctx._customctx_original_tb) |
| 47 |
self.assertNotIn("dictlevel", str(ctx.exception)) |
| 48 |
self.assertNotIn("dictlevel", repr(ctx.exception)) |
| 49 |
self.assertNotIn("dictlevel", ctx._customctx_original_tb) |
| 50 |
self.assertIn("redacted", str(ctx.exception)) |
| 51 |
self.assertIn("redacted", repr(ctx.exception)) |
| 52 |
|
| 53 |
with self.subTest(msg="replace_dict_inner_key_missing"): |
| 54 |
patches = [{"op": "replace", "path": "/dictlevelA1/dictlevelX2", "value": "nakamoto_secret"}] |
| 55 |
jpatch = jsonpatch.JsonPatch(patches) |
| 56 |
with self._customAssertRaises(JsonPatchException) as ctx: |
| 57 |
data2 = jpatch.apply(data1) |
| 58 |
fail_if_leaking_secret(ctx) |
| 59 |
with self.subTest(msg="replace_dict_outer_key_missing"): |
| 60 |
patches = [{"op": "replace", "path": "/dictlevelX1/dictlevelX2", "value": "nakamoto_secret"}] |
| 61 |
jpatch = jsonpatch.JsonPatch(patches) |
| 62 |
with self._customAssertRaises(JsonPointerException) as ctx: |
| 63 |
data2 = jpatch.apply(data1) |
| 64 |
fail_if_leaking_secret(ctx) |
| 65 |
|
| 66 |
with self.subTest(msg="remove_dict_inner_key_missing"): |
| 67 |
patches = [{"op": "remove", "path": "/dictlevelA1/dictlevelX2"}] |
| 68 |
jpatch = jsonpatch.JsonPatch(patches) |
| 69 |
with self._customAssertRaises(JsonPatchException) as ctx: |
| 70 |
data2 = jpatch.apply(data1) |
| 71 |
fail_if_leaking_secret(ctx) |
| 72 |
with self.subTest(msg="remove_dict_outer_key_missing"): |
| 73 |
patches = [{"op": "remove", "path": "/dictlevelX1/dictlevelX2"}] |
| 74 |
jpatch = jsonpatch.JsonPatch(patches) |
| 75 |
with self._customAssertRaises(JsonPointerException) as ctx: |
| 76 |
data2 = jpatch.apply(data1) |
| 77 |
fail_if_leaking_secret(ctx) |
| 78 |
|
| 79 |
with self.subTest(msg="add_dict_inner_key_missing"): |
| 80 |
patches = [{"op": "add", "path": "/dictlevelA1/dictlevelX2/dictlevelX3/dictlevelX4", "value": "nakamoto_secret"}] |
| 81 |
jpatch = jsonpatch.JsonPatch(patches) |
| 82 |
with self._customAssertRaises(JsonPointerException) as ctx: |
| 83 |
data2 = jpatch.apply(data1) |
| 84 |
fail_if_leaking_secret(ctx) |
| 85 |
with self.subTest(msg="add_dict_outer_key_missing"): |
| 86 |
patches = [{"op": "add", "path": "/dictlevelX1/dictlevelX2/dictlevelX3/dictlevelX4", "value": "nakamoto_secret"}] |
| 87 |
jpatch = jsonpatch.JsonPatch(patches) |
| 88 |
with self._customAssertRaises(JsonPointerException) as ctx: |
| 89 |
data2 = jpatch.apply(data1) |
| 90 |
fail_if_leaking_secret(ctx) |
| 91 |
|
| 92 |
|
| 93 |
def pop1_from_dict(d: dict, key: str) -> Any: |
| 94 |
return d.pop(key) |
| 95 |
|
| 96 |
|
| 97 |
def pop2_from_dict(d: dict, key: str) -> Any: |
| 98 |
val = d[key] |
| 99 |
del d[key] |
| 100 |
return val |
| 101 |
|
| 102 |
|
| 103 |
class TestJsonDB(ElectrumTestCase): |
| 104 |
|
| 105 |
async def test_jsonpatch_replace_after_remove(self): |
| 106 |
data = { 'a':{} } |
| 107 |
|
| 108 |
patches = [{"op": "add", "path": "/a/b", "value": "42"}] |
| 109 |
jpatch = jsonpatch.JsonPatch(patches) |
| 110 |
data = jpatch.apply(data) |
| 111 |
self.assertEqual(data, {'a': {"b": "42"}}) |
| 112 |
|
| 113 |
patches = [{"op": "remove", "path": "/a/b"}] |
| 114 |
jpatch = jsonpatch.JsonPatch(patches) |
| 115 |
data = jpatch.apply(data) |
| 116 |
self.assertEqual(data, {'a': {}}) |
| 117 |
|
| 118 |
patches = [{"op": "replace", "path": "/a/b", "value": "43"}] |
| 119 |
jpatch = jsonpatch.JsonPatch(patches) |
| 120 |
with self.assertRaises(JsonPatchException): |
| 121 |
data = jpatch.apply(data) |
| 122 |
|
| 123 |
async def test_jsondb_replace_after_remove(self): |
| 124 |
for pop_from_dict in [pop1_from_dict, pop2_from_dict]: |
| 125 |
with self.subTest(pop_from_dict): |
| 126 |
data = { 'a': {'b': {'c': 0}}, 'd': 3} |
| 127 |
db = JsonDB(repr(data)) |
| 128 |
a = db.get_dict('a') |
| 129 |
|
| 130 |
b = pop_from_dict(a, 'b') |
| 131 |
self.assertEqual(len(db.pending_changes), 1) |
| 132 |
|
| 133 |
b['c'] = 42 |
| 134 |
self.assertEqual(len(db.pending_changes), 1) |
| 135 |
patches = json.loads('[' + ','.join(db.pending_changes) + ']') |
| 136 |
jpatch = jsonpatch.JsonPatch(patches) |
| 137 |
data = jpatch.apply(data) |
| 138 |
self.assertEqual(data, {'a': {}, 'd': 3}) |
| 139 |
|
| 140 |
async def test_jsondb_replace_after_remove_nested(self): |
| 141 |
for pop_from_dict in [pop1_from_dict, pop2_from_dict]: |
| 142 |
with self.subTest(pop_from_dict): |
| 143 |
data = { 'a': {'b': {'c': 0}}, 'd': 3} |
| 144 |
db = JsonDB(repr(data)) |
| 145 |
|
| 146 |
a = pop_from_dict(db.data, "a") |
| 147 |
self.assertEqual(len(db.pending_changes), 1) |
| 148 |
b = a['b'] |
| 149 |
|
| 150 |
b['c'] = 42 |
| 151 |
self.assertEqual(len(db.pending_changes), 1) |
| 152 |
patches = json.loads('[' + ','.join(db.pending_changes) + ']') |
| 153 |
jpatch = jsonpatch.JsonPatch(patches) |
| 154 |
data = jpatch.apply(data) |
| 155 |
self.assertEqual(data, {'d': 3}) |
| 156 |
|
| 157 |
async def test_json_db_maybe_load_incomplete_data_with_control_characters(self): |
| 158 |
|
| 159 |
raw_json_db = '{"a": {"b": "c1"}, "d": "e"},\n{"op": "replace", "path": "/a/b", "value": "user_supp}}}lied_text"}' |
| 160 |
raw_json_db = raw_json_db[:-4] |
| 161 |
db = JsonDB(raw_json_db) |
| 162 |
self.assertEqual({"a": {"b": "c1"}, "d": "e"}, dict(db.data)) |
| 163 |
|
| 164 |
async def test_json_db_load_arbitrarily_truncated_data(self): |
| 165 |
|
| 166 |
|
| 167 |
base = {'labels': {'tx1': 'nasty } { label'}, 'nested': {'a': [1, {'b': '}}}'}]}} |
| 168 |
patches = [ |
| 169 |
{'op': 'add', 'path': '/labels/tx2', 'value': 'user_supp}}}lied {{{ text'}, |
| 170 |
{'op': 'replace', 'path': '/labels/tx1', 'value': 'fake ,\\n{ separator'}, |
| 171 |
] |
| 172 |
|
| 173 |
states = [base] |
| 174 |
for p in patches: |
| 175 |
states.append(jsonpatch.JsonPatch([p]).apply(states[-1])) |
| 176 |
s = json.dumps(base) |
| 177 |
for p in patches: |
| 178 |
s += ',\n' + json.dumps(p) |
| 179 |
self.assertEqual(states[-1], json.loads(json.dumps(JsonDB(s).data))) |
| 180 |
main_len = len(json.dumps(base)) |
| 181 |
for i in range(1, len(s)): |
| 182 |
if i < main_len: |
| 183 |
with self.assertRaises(WalletFileException): |
| 184 |
JsonDB(s[:i]) |
| 185 |
else: |
| 186 |
db = JsonDB(s[:i]) |
| 187 |
self.assertIn(json.loads(json.dumps(db.data)), states) |
| 188 |
|
| 189 |
async def test_jsondb_pointer_escaping(self): |
| 190 |
|
| 191 |
data = {'labels': {'some/label~key': 'hello'}, 'x1/': {'type': 'bip32'}} |
| 192 |
db = JsonDB(json.dumps(data)) |
| 193 |
labels = db.get_dict('labels') |
| 194 |
labels['some/label~key'] = 'world' |
| 195 |
db.data.pop('x1/') |
| 196 |
patches = json.loads('[' + ','.join(db.pending_changes) + ']') |
| 197 |
self.assertEqual({'/labels/some~1label~0key', '/x1~1'}, {p['path'] for p in patches}) |
| 198 |
data = jsonpatch.JsonPatch(patches).apply(data) |
| 199 |
self.assertEqual({'labels': {'some/label~key': 'world'}}, data) |