From b3c54308b78cc15f7e486dd08867935a4d2a2e9a Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Wed, 20 May 2020 16:13:36 +0200 Subject: [PATCH] policies/expression: validate syntax on form clean --- passbook/policies/expression/evaluator.py | 4 +--- passbook/policies/expression/forms.py | 7 +++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/passbook/policies/expression/evaluator.py b/passbook/policies/expression/evaluator.py index afecd9dc0..2b31f4671 100644 --- a/passbook/policies/expression/evaluator.py +++ b/passbook/policies/expression/evaluator.py @@ -56,10 +56,8 @@ class Evaluator: kwargs["pb_is_group_member"] = Evaluator.jinja2_func_is_group_member kwargs["pb_logger"] = get_logger() kwargs["requests"] = Session() + kwargs["pb_is_sso_flow"] = request.context.get(PLAN_CONTEXT_SSO, False) if request.http_request: - kwargs["pb_is_sso_flow"] = request.http_request.session.get( - PLAN_CONTEXT_SSO, False - ) kwargs["pb_client_ip"] = ( get_client_ip(request.http_request) or "255.255.255.255" ) diff --git a/passbook/policies/expression/forms.py b/passbook/policies/expression/forms.py index 54117958d..edc7a3545 100644 --- a/passbook/policies/expression/forms.py +++ b/passbook/policies/expression/forms.py @@ -3,6 +3,7 @@ from django import forms from passbook.admin.fields import CodeMirrorWidget +from passbook.policies.expression.evaluator import Evaluator from passbook.policies.expression.models import ExpressionPolicy from passbook.policies.forms import GENERAL_FIELDS @@ -12,6 +13,12 @@ class ExpressionPolicyForm(forms.ModelForm): template_name = "policy/expression/form.html" + def clean_expression(self): + """Test Jinja2 Syntax""" + expression = self.cleaned_data.get("expression") + Evaluator().validate(expression) + return expression + class Meta: model = ExpressionPolicy