Skip to content

Commit

Permalink
Merge pull request ansible#4434 from ryanpetrello/jinja-injector-sand…
Browse files Browse the repository at this point in the history
…box-validation

prevent unsafe jinja from being saved in the first place for cred types
  • Loading branch information
ryanpetrello authored Jul 7, 2020
2 parents f943277 + 61d3a76 commit bc14e99
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions awx/main/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import re
import urllib.parse

from jinja2 import Environment, StrictUndefined
from jinja2.exceptions import UndefinedError, TemplateSyntaxError
from jinja2 import sandbox, StrictUndefined
from jinja2.exceptions import UndefinedError, TemplateSyntaxError, SecurityError

# Django
from django.contrib.postgres.fields import JSONField as upstream_JSONBField
Expand Down Expand Up @@ -932,7 +932,7 @@ def __str__(self):
self.validate_env_var_allowed(key)
for key, tmpl in injector.items():
try:
Environment(
sandbox.ImmutableSandboxedEnvironment(
undefined=StrictUndefined
).from_string(tmpl).render(valid_namespace)
except UndefinedError as e:
Expand All @@ -942,6 +942,10 @@ def __str__(self):
code='invalid',
params={'value': value},
)
except SecurityError as e:
raise django_exceptions.ValidationError(
_('Encountered unsafe code execution: {}').format(e)
)
except TemplateSyntaxError as e:
raise django_exceptions.ValidationError(
_('Syntax error rendering template for {sub_key} inside of {type} ({error_msg})').format(
Expand Down

0 comments on commit bc14e99

Please sign in to comment.