Skip to content

Commit

Permalink
Merge pull request #202 from peopledoc/sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
Joachim Jablon authored Dec 16, 2021
2 parents 3ba3955 + 4c223c5 commit 220989a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
17 changes: 17 additions & 0 deletions tests/unit/test_client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,23 @@ def test_vault_client_base_render_template(vault):
assert vault.render_template("Hello {{ vault('a/b').value }}") == "Hello c"


def test_vault_client_base_render_template_error(vault):

with pytest.raises(exceptions.VaultRenderTemplateError):
assert vault.render_template("Hello {{ vault(") == "Hello c"


def test_vault_client_base_render_template_security_error(vault):

with pytest.raises(exceptions.VaultRenderTemplateError):
assert (
vault.render_template(
"Hello {{ joiner.__init__.__globals__.os.popen('date') }}"
)
== "Hello c"
)


@pytest.mark.parametrize("template", ["Hello {{ vault('a/b') }}", "Hello {{"])
def test_vault_client_base_render_template_path_not_found(vault, template):
with pytest.raises(exceptions.VaultRenderTemplateError):
Expand Down
7 changes: 6 additions & 1 deletion vault_cli/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import hvac # type: ignore
import jinja2
import jinja2.sandbox
import requests.packages.urllib3

from vault_cli import exceptions, sessions, settings, types, utils
Expand Down Expand Up @@ -515,12 +516,16 @@ def vault(path):
"Error while rendering template"
) from exc

env = jinja2.Environment(
env = jinja2.sandbox.SandboxedEnvironment(
loader=jinja2.FileSystemLoader(search_path.as_posix()),
keep_trailing_newline=True,
)
try:
return env.from_string(template).render(vault=vault)
except jinja2.exceptions.SecurityError as exc:
raise exceptions.VaultRenderTemplateError(
"Jinja2 template security error"
) from exc
except jinja2.exceptions.TemplateSyntaxError as exc:
raise exceptions.VaultRenderTemplateError(
"Jinja2 template syntax error"
Expand Down

0 comments on commit 220989a

Please sign in to comment.