Skip to content

Commit

Permalink
use Sandbox Environment to render templates
Browse files Browse the repository at this point in the history
  • Loading branch information
vabene1111 committed Nov 26, 2024
1 parent 4f9bff2 commit e6087d5
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions cookbook/helper/template_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import bleach
import markdown as md
from jinja2 import Template, TemplateSyntaxError, UndefinedError
from jinja2.exceptions import SecurityError
from jinja2.sandbox import SandboxedEnvironment
from markdown.extensions.tables import TableExtension

from cookbook.helper.mdx_attributes import MarkdownFormatExtension
Expand Down Expand Up @@ -89,11 +91,13 @@ def scale(number):
return f"<scalable-number v-bind:number='{bleach.clean(str(number))}' v-bind:factor='ingredient_factor'></scalable-number>"

try:
template = Template(instructions)
instructions = template.render(ingredients=ingredients, scale=scale)
env = SandboxedEnvironment()
instructions = env.from_string(instructions).render(ingredients=ingredients, scale=scale)
except TemplateSyntaxError:
return _('Could not parse template code.') + ' Error: Template Syntax broken'
except UndefinedError:
return _('Could not parse template code.') + ' Error: Undefined Error'
except SecurityError:
return _('Could not parse template code.') + ' Error: Security Error'

return instructions

0 comments on commit e6087d5

Please sign in to comment.