From c23bafd788d8bf1cc0cd8f94a7a8cf8f8ef531f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 30 Jan 2019 14:36:27 +0100 Subject: [PATCH] =?UTF-8?q?=E2=98=94=20Protect=20against=20callback=20erro?= =?UTF-8?q?rs=20in=20decorators=20on=20default=20view?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/octoprint/server/util/flask.py | 47 ++++++++++++++++++------------ 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/src/octoprint/server/util/flask.py b/src/octoprint/server/util/flask.py index 772538acd..9b14ffe62 100644 --- a/src/octoprint/server/util/flask.py +++ b/src/octoprint/server/util/flask.py @@ -992,11 +992,14 @@ def etagged(etag): def decorated_function(*args, **kwargs): rv = f(*args, **kwargs) if isinstance(rv, flask.Response): - result = etag - if callable(result): - result = result(rv) - if result: - rv.set_etag(result) + try: + result = etag + if callable(result): + result = result(rv) + if result: + rv.set_etag(result) + except: + logging.getLogger(__name__).exception(u"Error while calculating the etag value for response {!r}".format(rv)) return rv return decorated_function return decorator @@ -1008,16 +1011,19 @@ def lastmodified(date): def decorated_function(*args, **kwargs): rv = f(*args, **kwargs) if not "Last-Modified" in rv.headers: - result = date - if callable(result): - result = result(rv) + try: + result = date + if callable(result): + result = result(rv) - if not isinstance(result, basestring): - from werkzeug.http import http_date - result = http_date(result) + if not isinstance(result, basestring): + from werkzeug.http import http_date + result = http_date(result) - if result: - rv.headers["Last-Modified"] = result + if result: + rv.headers["Last-Modified"] = result + except: + logging.getLogger(__name__).exception(u"Error while calculating the lastmodified value for response {!r}".format(rv)) return rv return decorated_function return decorator @@ -1027,12 +1033,15 @@ def conditional(condition, met): def decorator(f): @functools.wraps(f) def decorated_function(*args, **kwargs): - if callable(condition) and condition(): - # condition has been met, return met-response - rv = met - if callable(met): - rv = met() - return rv + try: + if callable(condition) and condition(): + # condition has been met, return met-response + rv = met + if callable(met): + rv = met() + return rv + except: + logging.getLogger(__name__).exception(u"Error while evaluating conditional {!r} or met {!r}".format(condition, met)) # condition hasn't been met, call decorated function return f(*args, **kwargs)