From 1262e5fdca0bf4bef68a22c03f1afed80127ddfc Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Wed, 15 Feb 2017 08:53:58 -0800 Subject: Fix hash filter for non-ascii strings and Python3 hashlib hashes operate on byte strings. When given a text string on Python3, hashlib backtraces. When given a text string on Python2, hashlib will backtrace if the string contains non-ascii characters. Encode the text string to utf-8 prior to hashing to avoid this problem. Fixes #21452 (cherry picked from commit 99fd2328af598e35cb29f72694c8ea28c8469707) --- lib/ansible/plugins/filter/core.py | 4 ++-- test/integration/roles/test_filters/tasks/main.yml | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index 4cf7c658ac..ca3a033cbd 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -48,7 +48,7 @@ except: from ansible import errors from ansible.compat.six import iteritems, string_types from ansible.compat.six.moves import reduce -from ansible.module_utils._text import to_text +from ansible.module_utils._text import to_bytes, to_text from ansible.parsing.yaml.dumper import AnsibleDumper from ansible.utils.hashing import md5s, checksum_s from ansible.utils.unicode import unicode_wrap @@ -229,7 +229,7 @@ def get_hash(data, hashtype='sha1'): except: return None - h.update(data) + h.update(to_bytes(data, errors='surrogate_or_strict')) return h.hexdigest() def get_encrypted_password(password, hashtype='sha512', salt=None): diff --git a/test/integration/roles/test_filters/tasks/main.yml b/test/integration/roles/test_filters/tasks/main.yml index 1f6f0682e9..ec81d7f3fc 100644 --- a/test/integration/roles/test_filters/tasks/main.yml +++ b/test/integration/roles/test_filters/tasks/main.yml @@ -113,3 +113,8 @@ that: - "users | json_query('[*].hosts[].host') == ['host_a', 'host_b', 'host_c', 'host_d']" +- name: Test hash filter + assert: + that: + - '"{{ "hash" | hash("sha1") }}" == "2346ad27d7568ba9896f1b7da6b5991251debdf2"' + - '"{{ "café" | hash("sha1") }}" == "f424452a9673918c6f09b0cdd35b20be8e6ae7d7"' -- cgit v1.2.1