From 995615d0a0c5ddd13f52cc4d694cf4e5c0490658 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Wed, 19 Feb 2020 16:19:02 +0100 Subject: [PATCH] policies/expression: Return False if Policy returns Undefined and log warning --- passbook/policies/expression/evaluator.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/passbook/policies/expression/evaluator.py b/passbook/policies/expression/evaluator.py index 1e5bc26d6..042a4d9db 100644 --- a/passbook/policies/expression/evaluator.py +++ b/passbook/policies/expression/evaluator.py @@ -3,6 +3,7 @@ import re from typing import TYPE_CHECKING, Any, Dict from django.core.exceptions import ValidationError +from jinja2 import Undefined from jinja2.exceptions import TemplateSyntaxError, UndefinedError from jinja2.nativetypes import NativeEnvironment from structlog import get_logger @@ -13,6 +14,8 @@ from passbook.policies.struct import PolicyRequest, PolicyResult if TYPE_CHECKING: from passbook.core.models import User +LOGGER = get_logger() + class Evaluator: """Validate and evaulate jinja2-based expressions""" @@ -67,6 +70,13 @@ class Evaluator: result = expression.render( request=request, **self._get_expression_context(request) ) + if isinstance(result, Undefined): + LOGGER.warning( + "Expression policy returned undefined", + src=expression_source, + req=request, + ) + return PolicyRequest(False) if isinstance(result, list) and len(result) == 2: return PolicyResult(*result) if result: