From d75950e039d9dba8970ac36ffbd3334d3044cd1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20J=C3=BCrgens?= Date: Sat, 15 Oct 2022 14:35:41 +0200 Subject: [PATCH 1/3] Sanitize json --- vk_api/vk_api.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/vk_api/vk_api.py b/vk_api/vk_api.py index e4344e4..d2677e8 100644 --- a/vk_api/vk_api.py +++ b/vk_api/vk_api.py @@ -665,7 +665,11 @@ def method(self, method, values=None, captcha_sid=None, captcha_key=None, self.last_request = time.time() if response.ok: - response = response.json() + try: + response = response.json() + except json.decoder.JSONDecodeError: + sanitized = rx.sub(r'\p{C}', '', response.content) + response = json.loads(sanitized) else: error = ApiHttpError(self, method, values, raw, response) response = self.http_handler(error) From 03f45ccbe2dcc717f3cca450a6588578cf2e98fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20J=C3=BCrgens?= Date: Sat, 15 Oct 2022 16:10:46 +0200 Subject: [PATCH 2/3] Fix namespace typo and switch to robust json parsing --- vk_api/vk_api.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/vk_api/vk_api.py b/vk_api/vk_api.py index d2677e8..c733b4b 100644 --- a/vk_api/vk_api.py +++ b/vk_api/vk_api.py @@ -668,8 +668,12 @@ def method(self, method, values=None, captcha_sid=None, captcha_key=None, try: response = response.json() except json.decoder.JSONDecodeError: - sanitized = rx.sub(r'\p{C}', '', response.content) - response = json.loads(sanitized) + # This allows control characters + response = response.json(strict=False) + # Alternative: replace control characters with + # empty string + # sanitized = re.sub(r'\p{C}', '', response.content) + # response = json.loads(sanitized) else: error = ApiHttpError(self, method, values, raw, response) response = self.http_handler(error) From cff791d3b63aac69cdb95194f7cd5ea1f693ab04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20J=C3=BCrgens?= Date: Sat, 15 Oct 2022 16:18:49 +0200 Subject: [PATCH 3/3] Use working regex for control character range --- vk_api/vk_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vk_api/vk_api.py b/vk_api/vk_api.py index c733b4b..8d4333b 100644 --- a/vk_api/vk_api.py +++ b/vk_api/vk_api.py @@ -672,7 +672,7 @@ def method(self, method, values=None, captcha_sid=None, captcha_key=None, response = response.json(strict=False) # Alternative: replace control characters with # empty string - # sanitized = re.sub(r'\p{C}', '', response.content) + # sanitized = re.sub(r'[\x00-\x1f\x7f-\x9f]', '', response.content) # response = json.loads(sanitized) else: error = ApiHttpError(self, method, values, raw, response)