From 73d46f4e17ad68863e10d9ab35485bee35264df9 Mon Sep 17 00:00:00 2001 From: Nicole Valenzuela Date: Thu, 31 Dec 2020 10:32:29 -0300 Subject: [PATCH 1/3] FIX: added a new function to remove or replace the simple quotes from the command #70 --- contents/common.py | 43 ++++++++++++++++++++++++++++++++++++------ contents/winrm-exec.py | 14 ++++++++++++-- plugin.yaml | 11 +++++++++++ 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/contents/common.py b/contents/common.py index b0e7d7e..ad98af3 100644 --- a/contents/common.py +++ b/contents/common.py @@ -25,11 +25,42 @@ def get_file(destination): return filename +def removeSimpleQuotes(command): + result = cleanSimpleQuoteCommand(command) -def replace_single_quotes_format(command): - command = command + " " - command = re.sub(r'\s(\')',' \"', command) - command = re.sub(r'(\')\s','\" ', command) - command = re.sub(r'\'("\'")\'', "\'", command) + return result - return command +def isAPathThatRequiresDoubleQuotes(candidate): + #first check that this is not multiple paths, e.g. 'C:\windows C:\tmp...' + regexpMultipleAbsolutePath = re.compile('\'[a-zA-Z]:\\\\.*\s[a-zA-Z]:\\\\.*') #at least two absolute paths + if regexpMultipleAbsolutePath.match(candidate): return False + + #verify if this is a path with no options after, windows style. e.g. 'C:\Windows /w...' + regexpPathAndOption = re.compile('\'[a-zA-Z]:\\\\.*\s/.+') + if regexpPathAndOption.match(candidate): return False + + #verify if this is a path with no options after, unix style. e.g. 'C:\Windows -v' + regexpPathAndOptionUnix = re.compile('\'[a-zA-Z]:\\\\.*\s-.+') + if regexpPathAndOptionUnix.match(candidate): return False + + #finally, check if this is a single path, with single quotes, and requires to be put between double quotes.e.g. 'C:\Program Files' + regexPathRequireQuotes = re.compile('\'[a-zA-Z]:\\\\.*\s') + if regexPathRequireQuotes.match(candidate): + return True + else: + return False + +def cleanSimpleQuoteCommand(command): + result = re.sub(r'(\'.+?\')\s', conditionalReplace, ' '+command+' ' ) + return result + +def conditionalReplace( aMatch ) : + result = '' + capturedGroup = aMatch.group(1) + capturedGroup = capturedGroup.strip() + + result = capturedGroup[1:(len(capturedGroup)-1)] + if isAPathThatRequiresDoubleQuotes(capturedGroup): + result = '"' + result + '"' + + return result+' ' diff --git a/contents/winrm-exec.py b/contents/winrm-exec.py index f871894..c5eb4df 100644 --- a/contents/winrm-exec.py +++ b/contents/winrm-exec.py @@ -118,6 +118,7 @@ def filter(self, record): operationtimeout = None forcefail = False exitBehaviour = "console" +cleanescapingflg = False if "RD_CONFIG_AUTHTYPE" in os.environ: authentication = os.getenv("RD_CONFIG_AUTHTYPE") @@ -158,10 +159,18 @@ def filter(self, record): if "RD_CONFIG_OPERATIONTIMEOUT" in os.environ: operationtimeout = os.getenv("RD_CONFIG_OPERATIONTIMEOUT") +if "RD_CONFIG_CLEANUNNECESSARYESCAPING" in os.environ: + if os.getenv("RD_CONFIG_CLEANUNNECESSARYESCAPING") == "true": + cleanescapingflg = True + else: + cleanescapingflg = False + exec_command = os.getenv("RD_EXEC_COMMAND") +log.debug("Command will be executed: " + exec_command) -if "cmd" in shell: - exec_command = common.replace_single_quotes_format(exec_command) +if cleanescapingflg: + exec_command = common.removeSimpleQuotes(exec_command) + log.debug("Command escaped will be executed: " + exec_command) endpoint=transport+'://'+args.hostname+':'+port @@ -214,6 +223,7 @@ def filter(self, record): log.debug("readtimeout:" + str(readtimeout)) log.debug("operationtimeout:" + str(operationtimeout)) log.debug("exit Behaviour:" + exitBehaviour) +log.debug("cleanescapingflg: " + str(cleanescapingflg)) log.debug("------------------------------------------") if not URLLIB_INSTALLED: diff --git a/plugin.yaml b/plugin.yaml index c8bd1e8..7cb2b91 100644 --- a/plugin.yaml +++ b/plugin.yaml @@ -173,6 +173,17 @@ providers: required: false renderingOptions: groupName: Kerberos + - name: cleanunnecessaryescaping + title: Clean Unnecessary Escaping + description: "Cleans unnecessarily Escaped characters on commands" + type: Select + values: "true, false" + default: "false" + required: true + scope: Instance + renderingOptions: + groupName: Misc + instance-scope-node-attribute: "clean-unnecessary-escaping" - name: WinRMcpPython title: WinRM Python File Copier description: Copying files to remote Windows computer From 9b12076bd7a1ed94d6099b7b96df3d1bc31f48c7 Mon Sep 17 00:00:00 2001 From: Nicole Valenzuela Date: Mon, 4 Jan 2021 11:03:30 -0300 Subject: [PATCH 2/3] Change 'clean-unnecessary-escaping' to 'clean-escaping' variable --- contents/winrm-exec.py | 4 ++-- plugin.yaml | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/contents/winrm-exec.py b/contents/winrm-exec.py index c5eb4df..8365f96 100644 --- a/contents/winrm-exec.py +++ b/contents/winrm-exec.py @@ -159,8 +159,8 @@ def filter(self, record): if "RD_CONFIG_OPERATIONTIMEOUT" in os.environ: operationtimeout = os.getenv("RD_CONFIG_OPERATIONTIMEOUT") -if "RD_CONFIG_CLEANUNNECESSARYESCAPING" in os.environ: - if os.getenv("RD_CONFIG_CLEANUNNECESSARYESCAPING") == "true": +if "RD_CONFIG_CLEANESCAPING" in os.environ: + if os.getenv("RD_CONFIG_CLEANESCAPING") == "true": cleanescapingflg = True else: cleanescapingflg = False diff --git a/plugin.yaml b/plugin.yaml index 7cb2b91..83efe09 100644 --- a/plugin.yaml +++ b/plugin.yaml @@ -173,8 +173,8 @@ providers: required: false renderingOptions: groupName: Kerberos - - name: cleanunnecessaryescaping - title: Clean Unnecessary Escaping + - name: cleanescaping + title: Clean Escaping description: "Cleans unnecessarily Escaped characters on commands" type: Select values: "true, false" @@ -183,7 +183,7 @@ providers: scope: Instance renderingOptions: groupName: Misc - instance-scope-node-attribute: "clean-unnecessary-escaping" + instance-scope-node-attribute: "clean-escaping" - name: WinRMcpPython title: WinRM Python File Copier description: Copying files to remote Windows computer From 48f72f4e4808d778000ada735cf6ba4e3e33f2de Mon Sep 17 00:00:00 2001 From: Nicole Valenzuela Date: Wed, 6 Jan 2021 15:00:00 -0300 Subject: [PATCH 3/3] Change 'Type: Select' to 'Type: Boolean' for the Clean Escaping variable --- plugin.yaml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/plugin.yaml b/plugin.yaml index 83efe09..6dc15c4 100644 --- a/plugin.yaml +++ b/plugin.yaml @@ -176,11 +176,9 @@ providers: - name: cleanescaping title: Clean Escaping description: "Cleans unnecessarily Escaped characters on commands" - type: Select - values: "true, false" + type: Boolean default: "false" - required: true - scope: Instance + required: false renderingOptions: groupName: Misc instance-scope-node-attribute: "clean-escaping"