mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-28 21:31:26 -07:00
Validate variable names when loading 'vars:' blocks
TODO: add this to VariableManager to validate vars loaded from files too Fixes #12022
This commit is contained in:
parent
266a069a73
commit
5a5b9f211b
3 changed files with 47 additions and 34 deletions
|
@ -14,47 +14,16 @@
|
|||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
import ast
|
||||
|
||||
from six import string_types
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.plugins.action import ActionBase
|
||||
from ansible.utils.boolean import boolean
|
||||
from ansible.utils.vars import isidentifier
|
||||
|
||||
def isidentifier(ident):
|
||||
"""
|
||||
Determines, if string is valid Python identifier using the ast module.
|
||||
Orignally posted at: http://stackoverflow.com/a/29586366
|
||||
"""
|
||||
|
||||
if not isinstance(ident, string_types):
|
||||
return False
|
||||
|
||||
try:
|
||||
root = ast.parse(ident)
|
||||
except SyntaxError:
|
||||
return False
|
||||
|
||||
if not isinstance(root, ast.Module):
|
||||
return False
|
||||
|
||||
if len(root.body) != 1:
|
||||
return False
|
||||
|
||||
if not isinstance(root.body[0], ast.Expr):
|
||||
return False
|
||||
|
||||
if not isinstance(root.body[0].value, ast.Name):
|
||||
return False
|
||||
|
||||
if root.body[0].value.id != ident:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
class ActionModule(ActionBase):
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue