tstorage: deepcopy and sanitize json - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 65d0560475242e600ebd14cca0474ddc98e49485 DIR parent 592a403fa71775e413b4f3199b81636bad37e928 HTML Author: ThomasV <thomasv@gitorious> Date: Fri, 5 Sep 2014 12:04:03 +0200 storage: deepcopy and sanitize json Diffstat: M lib/wallet.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) --- DIR diff --git a/lib/wallet.py b/lib/wallet.py t@@ -25,6 +25,7 @@ import random import time import math import json +import copy from util import print_msg, print_error t@@ -92,13 +93,20 @@ class WalletStorage(object): except IOError: return try: - d = json.loads(data) + self.data = json.loads(data) except: try: d = ast.literal_eval(data) #parse raw data from reading wallet file except Exception: raise IOError("Cannot read wallet file.") - self.data = d + self.data = {} + for k, v in d.items(): + try: + json.dumps(key) + json.dumps(value) + except: + continue + self.data[key] = value self.file_exists = True def get(self, key, default=None): t@@ -106,13 +114,20 @@ class WalletStorage(object): v = self.data.get(key) if v is None: v = default + else: + v = copy.deepcopy(v) return v def put(self, key, value, save = True): - + try: + json.dumps(key) + json.dumps(value) + except: + print_error("json error: cannot save", key) + return with self.lock: if value is not None: - self.data[key] = value + self.data[key] = copy.deepcopy(value) elif key in self.data: self.data.pop(key) if save: