policies/expression: validate syntax on form clean

This commit is contained in:
Jens Langhammer 2020-05-20 16:13:36 +02:00
parent c1a72a0474
commit b3c54308b7
2 changed files with 8 additions and 3 deletions

View File

@ -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"
)

View File

@ -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