diff --git a/private/Invoke-PackageCommand.ps1 b/private/Invoke-PackageCommand.ps1 index b938a7d..eb166c2 100644 --- a/private/Invoke-PackageCommand.ps1 +++ b/private/Invoke-PackageCommand.ps1 @@ -115,11 +115,13 @@ $process.StartInfo.WorkingDirectory = $WorkingDirectory $process.StartInfo.FileName = $Executable $process.StartInfo.Arguments = $Arguments + $process.StartInfo.RedirectStandardInput = $true $process.StartInfo.RedirectStandardOutput = $true $process.StartInfo.RedirectStandardError = $true if ($FallbackToShellExecute) { $process.StartInfo.UseShellExecute = $true + $process.StartInfo.RedirectStandardInput = $false $process.StartInfo.RedirectStandardOutput = $false $process.StartInfo.RedirectStandardError = $false } @@ -154,6 +156,8 @@ # See issue #25 and https://stackoverflow.com/questions/11531068/powershell-capturing-standard-out-and-error-with-process-object $StdOutAsync = $process.StandardOutput.ReadToEndAsync() $StdErrAsync = $process.StandardError.ReadToEndAsync() + # https://github.com/jantari/LSUClient/issues/103 + $process.StandardInput.Close() } $process.WaitForExit() diff --git a/private/Resolve-XMLDependencies.ps1 b/private/Resolve-XMLDependencies.ps1 index dd3d4ce..9f847db 100644 --- a/private/Resolve-XMLDependencies.ps1 +++ b/private/Resolve-XMLDependencies.ps1 @@ -6,13 +6,16 @@ [Parameter( Mandatory = $true )] [string]$PackagePath, [switch]$TreatUnsupportedAsPassed, - [switch]$FailInboxDrivers + [switch]$FailInboxDrivers, + [switch]$ParentNodeIsAnd ) $XMLTreeDepth++ [DependencyParserState]$ParserState = 0 + $i = 0 foreach ($XMLTREE in $XMLIN) { + $i++ Write-Debug "$('- ' * $XMLTreeDepth )|> Node: $($XMLTREE.SchemaInfo.Name)" if ($XMLTREE.SchemaInfo.Name -eq 'Not') { @@ -20,36 +23,45 @@ Write-Debug "$('- ' * $XMLTreeDepth)Switched state to: $ParserState" } - $Result = if ($XMLTREE.SchemaInfo.Name -like "_*") { - switch (Test-MachineSatisfiesDependency -Dependency $XMLTREE -PackagePath $PackagePath -DebugIndent $XMLTreeDepth -FailInboxDrivers:$FailInboxDrivers) { - 0 { - $true - } - -1 { - $false - } - -2 { - Write-Debug "$('- ' * $XMLTreeDepth)Something unsupported encountered in: $($XMLTREE.SchemaInfo.Name)" - if ($TreatUnsupportedAsPassed) { $true } else { $false } + $Result = switch -Wildcard ($XMLTREE.SchemaInfo.Name) { + '_*' { + switch (Test-MachineSatisfiesDependency -Dependency $XMLTREE -PackagePath $PackagePath -DebugIndent $XMLTreeDepth -FailInboxDrivers:$FailInboxDrivers) { + 0 { + $true + } + -1 { + $false + } + -2 { + Write-Debug "$('- ' * $XMLTreeDepth)Something unsupported encountered in: $($XMLTREE.SchemaInfo.Name)" + if ($TreatUnsupportedAsPassed) { $true } else { $false } + } } } - } else { - $SubtreeResults = Resolve-XMLDependencies -XMLIN $XMLTREE.ChildNodes -PackagePath $PackagePath -TreatUnsupportedAsPassed:$TreatUnsupportedAsPassed -FailInboxDrivers:$FailInboxDrivers - switch ($XMLTREE.SchemaInfo.Name) { - 'And' { - Write-Debug "$('- ' * $XMLTreeDepth)Tree was AND: Results: $subtreeresults" - if ($subtreeresults -contains $false) { $false } else { $true } - } - default { - Write-Debug "$('- ' * $XMLTreeDepth)Tree was OR: Results: $subtreeresults" - if ($subtreeresults -contains $true ) { $true } else { $false } - } + 'And' { + $SubtreeResults = Resolve-XMLDependencies -XMLIN $XMLTREE.ChildNodes -PackagePath $PackagePath -TreatUnsupportedAsPassed:$TreatUnsupportedAsPassed -FailInboxDrivers:$FailInboxDrivers -ParentNodeIsAnd + Write-Debug "$('- ' * $XMLTreeDepth)Tree was AND: Results: $SubtreeResults" + if ($SubtreeResults -contains $false) { $false } else { $true } + } + default { + $SubtreeResults = Resolve-XMLDependencies -XMLIN $XMLTREE.ChildNodes -PackagePath $PackagePath -TreatUnsupportedAsPassed:$TreatUnsupportedAsPassed -FailInboxDrivers:$FailInboxDrivers + Write-Debug "$('- ' * $XMLTreeDepth)Tree was OR: Results: $SubtreeResults" + if ($SubtreeResults -contains $true ) { $true } else { $false } } } Write-Debug "$('- ' * $XMLTreeDepth)< Returning $($Result -bxor $ParserState) from node $($XMLTREE.SchemaInfo.Name)" $Result -bxor $ParserState + + # If we're evaluating the children of an And-node, and we get a negative result before the last child-element, + # we can stop and don't have to process the remaining children anymore as the And-result will always be false. + # This speeds things up but it can also avoid even running problematic tests, e.g. some ExternalDetections. + if ($ParentNodeIsAnd -and $i -ne $XMLIN.Count -and -not ($Result -bxor $ParserState)) { + Write-Debug "$('- ' * $XMLTreeDepth)Stopping AND evaluation early" + $ParserState = 0 # DO_HAVE + break; + } $ParserState = 0 # DO_HAVE }