Skip to content

Commit

Permalink
Chore: use jinja sandbox for templates
Browse files Browse the repository at this point in the history
  • Loading branch information
m1n0 committed Nov 13, 2024
1 parent 0ecbec4 commit 982c10f
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions soda/core/soda/common/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from jinja2 import Environment
from jinja2.runtime import Context
from jinja2.sandbox import SandboxedEnvironment


class OsContext(Context):
Expand All @@ -18,7 +19,7 @@ def resolve_or_missing(self, key):


def create_os_environment():
environment = Environment(variable_start_string="${", variable_end_string="}")
environment = SandboxedEnvironment(variable_start_string="${", variable_end_string="}", autoescape=True)
environment.context_class = OsContext
return environment

Expand All @@ -27,14 +28,16 @@ class Jinja:
environment = create_os_environment()

@staticmethod
def resolve(template: str, variables: dict = None) -> str:
def resolve(template: str, variables: dict = None, environment: Environment = None) -> str:
"""
Convenience method that funnels Jinja exceptions into parselog errors.
This method throws no exceptions. Returns None in case of Jinja exceptions.
"""
if environment is None:
environment = create_os_environment()
if not isinstance(variables, dict):
variables = {}
jinja_template = Jinja.environment.from_string(template)
jinja_template = environment.from_string(template)
rendered_value = jinja_template.render(variables)
return rendered_value

Expand Down

0 comments on commit 982c10f

Please sign in to comment.