From 7046bb9f152e63ad3e381d45b004726b616306b8 Mon Sep 17 00:00:00 2001 From: Eyal <109809+eyal0@users.noreply.github.com> Date: Thu, 10 Dec 2020 21:30:10 -0700 Subject: [PATCH 1/3] Validate websocket JSON before sending The default json library in python will send infinity values as `Infinity` as described [here](https://docs.python.org/3/library/json.html#json.dump). Check that the JSON is strictly valid before sending it, otherwise throw an exception. This fixes #3875 --- src/octoprint/server/util/sockjs.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/octoprint/server/util/sockjs.py b/src/octoprint/server/util/sockjs.py index fdc883ad9..7e43f5ff8 100644 --- a/src/octoprint/server/util/sockjs.py +++ b/src/octoprint/server/util/sockjs.py @@ -589,6 +589,9 @@ class PrinterStateConnection( def _do_emit(self, type, payload): try: + import json + + _ = json.dumps(octoprint.util.thaw_frozendict({type: payload}), allow_nan=False) self.send({type: payload}) except Exception as e: if self._logger.isEnabledFor(logging.DEBUG): From cfaeaba39c02a0b1bdcf12a1201d37c16cd2cff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 11 Dec 2020 14:38:07 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=90=9B=20Better=20fix=20for=20#3875?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of encoding twice, encode only once with the right parameters --- src/octoprint/server/util/sockjs.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/octoprint/server/util/sockjs.py b/src/octoprint/server/util/sockjs.py index 7e43f5ff8..c0c7bf1f7 100644 --- a/src/octoprint/server/util/sockjs.py +++ b/src/octoprint/server/util/sockjs.py @@ -71,6 +71,7 @@ class JsonEncodingSessionWrapper(wrapt.ObjectProxy): octoprint.vendor.sockjs.tornado.util.bytes_to_str(msg), separators=(",", ":"), default=JsonEncoding.encode, + allow_nan=False, ), stats, ) @@ -589,9 +590,6 @@ class PrinterStateConnection( def _do_emit(self, type, payload): try: - import json - - _ = json.dumps(octoprint.util.thaw_frozendict({type: payload}), allow_nan=False) self.send({type: payload}) except Exception as e: if self._logger.isEnabledFor(logging.DEBUG): From dc269846d1c2eba245ec64527192a1815056fe34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 11 Dec 2020 15:01:44 +0100 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=A5=85=20Validate=20additional=20data?= =?UTF-8?q?=20from=20plugins=20at=20source?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit That way it won't even enter the state data if it's not JSON compliant, that's better than no longer being able to send any state reports just because one plugin is sending broken data. Closes #3875 --- src/octoprint/printer/standard.py | 7 +++++++ src/octoprint/server/util/sockjs.py | 10 ++-------- src/octoprint/util/json/__init__.py | 10 ++++++++++ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/octoprint/printer/standard.py b/src/octoprint/printer/standard.py index dac3d0a38..836edffa4 100644 --- a/src/octoprint/printer/standard.py +++ b/src/octoprint/printer/standard.py @@ -18,6 +18,7 @@ import time from frozendict import frozendict from past.builtins import basestring, long +import octoprint.util.json from octoprint import util as util from octoprint.events import Events, eventManager from octoprint.filemanager import FileDestinations, NoSuchStorage, valid_file_type @@ -272,7 +273,13 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): try: additional = hook(initial=initial) if additional and isinstance(additional, dict): + octoprint.util.json.dump({name: additional}) plugin_data[name] = additional + except ValueError: + self._logger.exception( + "Invalid additional data from plugin {}".format(name), + extra={"plugin": name}, + ) except Exception: self._logger.exception( "Error while retrieving additional data from plugin {}, blacklisting it for further loops".format( diff --git a/src/octoprint/server/util/sockjs.py b/src/octoprint/server/util/sockjs.py index c0c7bf1f7..5e476c86e 100644 --- a/src/octoprint/server/util/sockjs.py +++ b/src/octoprint/server/util/sockjs.py @@ -5,7 +5,6 @@ __author__ = "Gina Häußge " __license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html" __copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms of the AGPLv3 License" -import json import logging import threading import time @@ -27,7 +26,7 @@ from octoprint.access.permissions import Permissions from octoprint.access.users import LoginStatusListener from octoprint.events import Events from octoprint.settings import settings -from octoprint.util.json import JsonEncoding +from octoprint.util.json import dump as json_dump class ThreadSafeSession(octoprint.vendor.sockjs.tornado.session.Session): @@ -67,12 +66,7 @@ class ThreadSafeSession(octoprint.vendor.sockjs.tornado.session.Session): class JsonEncodingSessionWrapper(wrapt.ObjectProxy): def send_message(self, msg, stats=True, binary=False): self.send_jsonified( - json.dumps( - octoprint.vendor.sockjs.tornado.util.bytes_to_str(msg), - separators=(",", ":"), - default=JsonEncoding.encode, - allow_nan=False, - ), + json_dump(octoprint.vendor.sockjs.tornado.util.bytes_to_str(msg)), stats, ) diff --git a/src/octoprint/util/json/__init__.py b/src/octoprint/util/json/__init__.py index d9363ee70..8b572d719 100644 --- a/src/octoprint/util/json/__init__.py +++ b/src/octoprint/util/json/__init__.py @@ -6,12 +6,22 @@ __copyright__ = "Copyright (C) 2018 The OctoPrint Project - Released under terms import collections +import json import frozendict from octoprint.util import to_unicode +def dump(obj): + return json.dumps( + obj, + separators=(",", ":"), + default=JsonEncoding.encode, + allow_nan=False, + ) + + class JsonEncoding(object): encoders = collections.OrderedDict()