From 1b0d466bc4e7b5c4e57139086f01df66dd7d0a90 Mon Sep 17 00:00:00 2001 From: Piotr Popieluch Date: Tue, 24 Nov 2020 14:49:22 +0100 Subject: [PATCH 1/4] utf-8 fix in unpickle --- webapp/graphite/util.py | 3 +++ webapp/tests/test_util.py | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/webapp/graphite/util.py b/webapp/graphite/util.py index b5dc1e1d3..9690b6a10 100644 --- a/webapp/graphite/util.py +++ b/webapp/graphite/util.py @@ -200,6 +200,9 @@ class SafeUnpickler(pickle.Unpickler): 'graphite.intervals': set(['Interval', 'IntervalSet']), } + def __init__(self, file): + super().__init__(file, encoding='bytes') + def find_class(self, module, name): if module not in self.PICKLE_SAFE: raise pickle.UnpicklingError('Attempting to unpickle unsafe module %s' % module) diff --git a/webapp/tests/test_util.py b/webapp/tests/test_util.py index ca52a78ba..12940eee0 100644 --- a/webapp/tests/test_util.py +++ b/webapp/tests/test_util.py @@ -4,6 +4,7 @@ from datetime import datetime from mock import patch +from io import BytesIO from .base import TestCase @@ -119,3 +120,13 @@ def test_logtime(ok, custom=None, timer=None): test_logtime(False) self.assertEqual(log.info.call_count, 3) self.assertRegexpMatches(log.info.call_args[0][0], r'test :: failed in [-.e0-9]+s') + +class SafeUnpicklerTest(TestCase): + + def test_load(self): + unpickler = util.unpickle() + p = b'\x80\x04\x95\r\x00\x00\x00\x00\x00\x00\x00\x8c\ttest.d\xc3\xb8d\x94.' + u = unpickler.load(BytesIO(p)) + x = 'test.død' + + self.assertEqual(u, x) From 67dd35aae6eaec937385fd16034c615ef381dd40 Mon Sep 17 00:00:00 2001 From: Piotr Popieluch Date: Fri, 11 Dec 2020 10:51:40 +0100 Subject: [PATCH 2/4] Update webapp/graphite/util.py Co-authored-by: Adam Stephens <2071575+adamcstephens@users.noreply.github.com> --- webapp/graphite/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webapp/graphite/util.py b/webapp/graphite/util.py index 9690b6a10..1b6203317 100644 --- a/webapp/graphite/util.py +++ b/webapp/graphite/util.py @@ -201,7 +201,7 @@ class SafeUnpickler(pickle.Unpickler): } def __init__(self, file): - super().__init__(file, encoding='bytes') + super().__init__(file, encoding='utf8') def find_class(self, module, name): if module not in self.PICKLE_SAFE: From d5340c0074de4a6a748bc3fa9d5d6d0b53db1f3e Mon Sep 17 00:00:00 2001 From: Piotr Date: Mon, 14 Dec 2020 18:23:45 +0100 Subject: [PATCH 3/4] fix linting erros --- webapp/tests/test_util.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/webapp/tests/test_util.py b/webapp/tests/test_util.py index 12940eee0..085042829 100644 --- a/webapp/tests/test_util.py +++ b/webapp/tests/test_util.py @@ -1,3 +1,4 @@ +# -*- encoding: utf-8 -*- import os import socket import pytz @@ -121,6 +122,7 @@ def test_logtime(ok, custom=None, timer=None): self.assertEqual(log.info.call_count, 3) self.assertRegexpMatches(log.info.call_args[0][0], r'test :: failed in [-.e0-9]+s') + class SafeUnpicklerTest(TestCase): def test_load(self): From c7751f294c38dbbba7b74e82716d31bd39bb663d Mon Sep 17 00:00:00 2001 From: deniszh Date: Wed, 23 Dec 2020 22:11:27 +0100 Subject: [PATCH 4/4] Fixing test as @ploxlin suggests --- webapp/tests/test_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webapp/tests/test_util.py b/webapp/tests/test_util.py index 085042829..8e1dfc1ea 100644 --- a/webapp/tests/test_util.py +++ b/webapp/tests/test_util.py @@ -127,7 +127,7 @@ class SafeUnpicklerTest(TestCase): def test_load(self): unpickler = util.unpickle() - p = b'\x80\x04\x95\r\x00\x00\x00\x00\x00\x00\x00\x8c\ttest.d\xc3\xb8d\x94.' + p = b"S'test.d\\xc3\\xb8d'\np0\n." u = unpickler.load(BytesIO(p)) x = 'test.død'