-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(common): prevent injection during jinja templating #1850
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Would be great if we could avoid the global var
toucan_connectors/common.py
Outdated
jinja_env = ImmutableSandboxedEnvironment() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jinja_env = ImmutableSandboxedEnvironment() |
toucan_connectors/common.py
Outdated
else: | ||
env = Environment() # noqa: S701 | ||
env = jinja_env # noqa: S701 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
env = jinja_env # noqa: S701 | |
env = ImmutableSandboxedEnvironment() |
toucan_connectors/common.py
Outdated
@@ -237,7 +243,7 @@ def _flatten_dict(p, parent_key=""): | |||
parameters.update(p_keep_type) | |||
|
|||
logging.getLogger(__name__).debug(f"Render query: {query} with parameters {parameters}") | |||
return Template(query).render(parameters) | |||
return jinja_env.from_string(query).render(parameters) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, I'm in favor of avoiding globals when possible
toucan_connectors/common.py
Outdated
@@ -476,7 +482,7 @@ def pandas_read_sql( | |||
if convert_to_printf: | |||
query = convert_to_printf_templating_style(query) | |||
if render_user: | |||
query = Template(query).render({"user": params.get("user", {})}) | |||
query = jinja_env.from_string(query).render({"user": params.get("user", {})}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
Instanciating an env doesn't cost a lot, so it's best to avoid any globals
Indeed, instantiating an env is cheap, so no need for a global: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Forward port of #1849