From 89428a40b3768caf58addd04d93bc6c86f2cf252 Mon Sep 17 00:00:00 2001 From: Ganesh Nalawade Date: Fri, 13 Oct 2017 21:26:24 +0530 Subject: [PATCH] Validate parse_cli filter inputs (#31293) * Validate parse_cli filter inputs Fixes #30517 * Add check to validate input is of type string * Add check to confirm template file exist * Update error message for parse_cli_textfsm invalid template path * Add input validation for parse_cli_textfsm filter --- lib/ansible/plugins/filter/network.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/ansible/plugins/filter/network.py b/lib/ansible/plugins/filter/network.py index fde29ef008..6a53cf5c07 100644 --- a/lib/ansible/plugins/filter/network.py +++ b/lib/ansible/plugins/filter/network.py @@ -27,7 +27,7 @@ import json from collections import Mapping from ansible.module_utils.network_common import Template -from ansible.module_utils.six import iteritems +from ansible.module_utils.six import iteritems, string_types from ansible.errors import AnsibleError try: @@ -76,6 +76,12 @@ def re_search(regex, value): def parse_cli(output, tmpl): + if not isinstance(output, string_types): + raise AnsibleError("parse_cli input should be a string, but was given a input of %s" % (type(output))) + + if not os.path.exists(tmpl): + raise AnsibleError('unable to locate parse_cli template: %s' % tmpl) + try: template = Template() except ImportError as exc: @@ -216,8 +222,11 @@ def parse_cli_textfsm(value, template): if not HAS_TEXTFSM: raise AnsibleError('parse_cli_textfsm filter requires TextFSM library to be installed') + if not isinstance(value, string_types): + raise AnsibleError("parse_cli_textfsm input should be a string, but was given a input of %s" % (type(value))) + if not os.path.exists(template): - raise AnsibleError('unable to locate parse_cli template: %s' % template) + raise AnsibleError('unable to locate parse_cli_textfsm template: %s' % template) try: template = open(template)