Do not crash templating when filter/test name is not a valid Ansible plugin name (#78913)

* Do not crash templating when filter/test name is not a valid Ansible plugin name.
* Store and re-raise KeyError if there was one.

Co-authored-by: s-hertel <19572925+s-hertel@users.noreply.github.com>
pull/79019/head^2
Felix Fontein 2 years ago committed by GitHub
parent 367cdae3b2
commit 6d0aeac1e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- "Do not crash when templating an expression with a test or filter that is not a valid Ansible filter name (https://github.com/ansible/ansible/issues/78912, https://github.com/ansible/ansible/pull/78913)."

@ -423,12 +423,13 @@ class JinjaPluginIntercept(MutableMapping):
if not isinstance(key, string_types):
raise ValueError('key must be a string, got %s instead' % type(key))
original_exc = None
if key not in self._loaded_builtins:
plugin = None
try:
plugin = self._pluginloader.get(key)
except (AnsibleError, KeyError) as e:
raise TemplateSyntaxError('Could not load "%s": %s' % (key, to_native(e)), 0)
original_exc = e
except Exception as e:
display.vvvv('Unexpected plugin load (%s) exception: %s' % (key, to_native(e)))
raise e
@ -439,8 +440,11 @@ class JinjaPluginIntercept(MutableMapping):
self._delegatee[key] = plugin.j2_function
self._loaded_builtins.add(key)
# let it trigger keyerror if we could not find ours or jinja2 one
func = self._delegatee[key]
# raise template syntax error if we could not find ours or jinja2 one
try:
func = self._delegatee[key]
except KeyError as e:
raise TemplateSyntaxError('Could not load "%s": %s' % (key, to_native(original_exc or e)), 0)
# if i do have func and it is a filter, it nees wrapping
if self._pluginloader.type == 'filter':

@ -16,3 +16,20 @@
vars:
# Kind of hack to just send a JSON string through jinja, by templating out nothing
foo: '{{ "" }}{"null": null, "true": true, "false": false}'
- name: Make sure that test with name that isn't a valid Ansible plugin name does not result in a crash (1/2)
set_fact:
foo: '{{ [{"failed": false}] | selectattr("failed", "==", true) }}'
- name: Make sure that test with name that isn't a valid Ansible plugin name does not result in a crash (2/2)
template:
src: invalid_test_name.j2
dest: /tmp/foo
ignore_errors: true
register: result
- assert:
that:
- result is failed
- >-
"TemplateSyntaxError: Could not load \"asdf \": 'invalid plugin name: ansible.builtin.asdf '" in result.msg

@ -0,0 +1 @@
{{ [{"failed": false}] | selectattr("failed", "asdf ", true) }}
Loading…
Cancel
Save