summaryrefslogtreecommitdiff
path: root/test/units/template/test_templar.py
diff options
context:
space:
mode:
authorMartin Krizek <martin.krizek@gmail.com>2022-05-09 22:46:42 +0200
committerGitHub <noreply@github.com>2022-05-09 15:46:42 -0500
commitd5e5bd34d6249b3b0c66c3423fa09a9f3b032467 (patch)
tree555ad5e02c374d1ec81b22174b44b9bcee7fa414 /test/units/template/test_templar.py
parent19415e12384cad75115e0dda2c4df7b71c0bc6f6 (diff)
downloadansible-d5e5bd34d6249b3b0c66c3423fa09a9f3b032467.tar.gz
Prevent losing unsafe from lookups (#77609) (#77652)
This patch fixes a bug which under certain conditions results in data returned from lookups not being marked as unsafe. Each time Templar.do_template is invoked a new AnsibleContext is created and stored effectively at two places: 1) as an instance variable in templar_obj.cur_context 2) as a local variable called new_context in do_template method of Templar Due to custom functionality in Ansible's Context that allows for nested templating it is possible that during resolving variable's value template/do_template method is called recursively again, again creating a new context. At that point the problem manifests itself because as mentioned in 1) above the context is overwriten on the templar object which means that any subsequent calls to _lookup will use the new context to mark it as unsafe which is now different to the local new_context which is used for testing for unsafe property. The solution to the problem appears to be to restore the original context inside do_template and also to eliminate the local variable new_context to prevent problems in the future. It appears that we don't have a better way of storing the context other than as some form of global variable and so this appears to be the "best" solution possible at this point. Hopefully data tagging will be the solution here. For more examples see unit and integration tests included in this patch. Fixes #77535 (cherry picked from commit 3980eb8c09d170a861351f8aff4a1aa1a8cbb626)
Diffstat (limited to 'test/units/template/test_templar.py')
-rw-r--r--test/units/template/test_templar.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/test/units/template/test_templar.py b/test/units/template/test_templar.py
index dd6985ce3b..181bb31e9f 100644
--- a/test/units/template/test_templar.py
+++ b/test/units/template/test_templar.py
@@ -444,3 +444,28 @@ class TestAnsibleContext(BaseTemplar, unittest.TestCase):
def test_is_unsafe(self):
context = self._context()
self.assertFalse(context._is_unsafe(AnsibleUndefined()))
+
+
+def test_unsafe_lookup():
+ res = Templar(
+ None,
+ variables={
+ 'var0': '{{ var1 }}',
+ 'var1': ['unsafe'],
+ }
+ ).template('{{ lookup("list", var0) }}')
+ assert getattr(res[0], '__UNSAFE__', False)
+
+
+def test_unsafe_lookup_no_conversion():
+ res = Templar(
+ None,
+ variables={
+ 'var0': '{{ var1 }}',
+ 'var1': ['unsafe'],
+ }
+ ).template(
+ '{{ lookup("list", var0) }}',
+ convert_data=False,
+ )
+ assert getattr(res, '__UNSAFE__', False)