Skip to content

Commit 3929d66

Browse files
committed
Merge pull request #353 from PowerShell/AddScriptBugFixBranch
Updated Engine to use AddCommand to prevent Script Based injection attacks
2 parents 7ef6ce7 + 4454618 commit 3929d66

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

Engine/ScriptAnalyzer.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using System.Globalization;
2727
using System.Collections.Concurrent;
2828
using System.Threading.Tasks;
29+
using System.Collections.ObjectModel;
2930

3031
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
3132
{
@@ -545,18 +546,19 @@ private List<ExternalRule> GetExternalRule(string[] moduleNames)
545546
using (System.Management.Automation.PowerShell posh =
546547
System.Management.Automation.PowerShell.Create(state))
547548
{
548-
string script = string.Format(CultureInfo.CurrentCulture, "Get-Module -Name '{0}' -ListAvailable", moduleName);
549-
shortModuleName = posh.AddScript(script).Invoke<PSModuleInfo>().First().Name;
549+
posh.AddCommand("Get-Module").AddParameter("Name", moduleName).AddParameter("ListAvailable");
550+
shortModuleName = posh.Invoke<PSModuleInfo>().First().Name;
550551

551552
// Invokes Update-Help for this module
552553
// Required since when invoking Get-Help later on, the cmdlet prompts for Update-Help interactively
553554
// By invoking Update-Help first, Get-Help will not prompt for downloading help later
554-
script = string.Format(CultureInfo.CurrentCulture, "Update-Help -Module '{0}' -Force", shortModuleName);
555-
posh.AddScript(script).Invoke();
556-
555+
posh.AddCommand("Update-Help").AddParameter("Module", shortModuleName).AddParameter("Force");
556+
posh.Invoke();
557+
557558
// Invokes Get-Command and Get-Help for each functions in the module.
558-
script = string.Format(CultureInfo.CurrentCulture, "Get-Command -Module '{0}'", shortModuleName);
559-
var psobjects = posh.AddScript(script).Invoke();
559+
posh.Commands.Clear();
560+
posh.AddCommand("Get-Command").AddParameter("Module", shortModuleName);
561+
var psobjects = posh.Invoke();
560562

561563
foreach (PSObject psobject in psobjects)
562564
{
@@ -570,10 +572,22 @@ private List<ExternalRule> GetExternalRule(string[] moduleNames)
570572
//Only add functions that are defined as rules.
571573
if (param != null)
572574
{
573-
script = string.Format(CultureInfo.CurrentCulture, "(Get-Help -Name {0}).Description | Out-String", funcInfo.Name);
574-
string desc = posh.AddScript(script).Invoke()[0].ImmediateBaseObject.ToString()
575-
.Replace("\r\n", " ").Trim();
575+
posh.AddCommand("Get-Help").AddParameter("Name", funcInfo.Name);
576+
Collection<PSObject> helpContent = posh.Invoke();
577+
578+
// Retrieve "Description" field in the help content
579+
string desc = String.Empty;
580+
581+
if ((null != helpContent) && ( 1 == helpContent.Count))
582+
{
583+
dynamic description = helpContent[0].Properties["Description"];
576584

585+
if (null != description)
586+
{
587+
desc = description.Value[0].Text;
588+
}
589+
}
590+
577591
rules.Add(new ExternalRule(funcInfo.Name, funcInfo.Name, desc, param.Name, param.ParameterType.FullName,
578592
funcInfo.ModuleName, funcInfo.Module.Path));
579593
}
@@ -784,8 +798,8 @@ public Dictionary<string, List<string>> CheckRuleExtension(string[] path, PathIn
784798
using (System.Management.Automation.PowerShell posh =
785799
System.Management.Automation.PowerShell.Create())
786800
{
787-
string script = string.Format(CultureInfo.CurrentCulture, "Get-Module -Name '{0}' -ListAvailable", resolvedPath);
788-
PSModuleInfo moduleInfo = posh.AddScript(script).Invoke<PSModuleInfo>().First();
801+
posh.AddCommand("Get-Module").AddParameter("Name", resolvedPath).AddParameter("ListAvailable");
802+
PSModuleInfo moduleInfo = posh.Invoke<PSModuleInfo>().First();
789803

790804
// Adds original path, otherwise path.Except<string>(validModPaths) will fail.
791805
// It's possible that user can provide something like this:

0 commit comments

Comments
 (0)