From 05e5698472bfc7015dfa05603b0ae630b1bb8fd8 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Mon, 10 Jul 2017 19:45:16 +0200 Subject: [PATCH] powershell.ps1: Validate Windows paths (#26488) During the writing of Windows path integration tests we discovered that incorrect paths (including escape sequences) cause very cryptic error messages. This fix ensures that invalid paths cause a proper error message. We also had to fix the following modules: - win_shortcut: `src` can be a URL --- lib/ansible/module_utils/powershell.ps1 | 4 ++++ lib/ansible/modules/windows/win_shortcut.ps1 | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/ansible/module_utils/powershell.ps1 b/lib/ansible/module_utils/powershell.ps1 index 62cf3476fe..6bf0c27734 100644 --- a/lib/ansible/module_utils/powershell.ps1 +++ b/lib/ansible/module_utils/powershell.ps1 @@ -205,6 +205,10 @@ Function Get-AnsibleParam($obj, $name, $default = $null, $resultobj = @{}, $fail if ($type -eq "path") { # Expand environment variables on path-type $value = Expand-Environment($value) + # Test if a valid path is provided + if (-not (Test-Path -IsValid $value)) { + Fail-Json -obj $resultobj -message "Get-AnsibleParam: Parameter '$name' has an invalid path '$value' specified." + } } elseif ($type -eq "str") { # Convert str types to real Powershell strings $value = $value.ToString() diff --git a/lib/ansible/modules/windows/win_shortcut.ps1 b/lib/ansible/modules/windows/win_shortcut.ps1 index 2a97bf1fef..891fcc89d8 100644 --- a/lib/ansible/modules/windows/win_shortcut.ps1 +++ b/lib/ansible/modules/windows/win_shortcut.ps1 @@ -26,7 +26,7 @@ $ErrorActionPreference = "Stop" $params = Parse-Args $args -supports_check_mode $true $check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false -$src = Get-AnsibleParam -obj $params -name "src" -type "path" +$src = Get-AnsibleParam -obj $params -name "src" $dest = Get-AnsibleParam -obj $params -name "dest" -type "path" -failifempty $true $state = Get-AnsibleParam -obj $params -name "state" -type "string" -default "present" -validateset "present","absent" $orig_args = Get-AnsibleParam -obj $params -name "args" -type "string"