Skip to content

Commit 1d1c8b7

Browse files
PaulHigindaxian-dbw
authored andcommitted
Merge TFS 8193011: Constrained language mode doesn't allow CIM cmdlets (#2158)
* Merge TFS 8193011: Constrained language mode doesn't allow argument transformation * Update CommandProcessorBase.cs Added comment per review comment.
1 parent c695d41 commit 1d1c8b7

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

src/System.Management.Automation/engine/CommandProcessorBase.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,24 +169,40 @@ protected static void ValidateCompatibleLanguageMode(ScriptBlock scriptBlock,
169169
InvocationInfo invocationInfo)
170170
{
171171
// If we are in a constrained language mode (Core or Restricted), block it.
172-
// This goes both ways:
172+
// We are currently restricting in one direction:
173173
// - Can't dot something from a more permissive mode, since that would probably expose
174174
// functions that were never designed to handle untrusted data.
175-
// - Can't dot something from a less permissive mode, since that might introduce tainted
176-
// data into the current scope.
175+
// This function won't be called for NoLanguage mode so the only direction checked is trusted
176+
// (FullLanguage mode) script running in a constrained/restricted session.
177177
if ((scriptBlock.LanguageMode.HasValue) &&
178178
(scriptBlock.LanguageMode != languageMode) &&
179179
((languageMode == PSLanguageMode.RestrictedLanguage) ||
180180
(languageMode == PSLanguageMode.ConstrainedLanguage)))
181181
{
182-
ErrorRecord errorRecord = new ErrorRecord(
182+
// Finally check if script block is really just PowerShell commands plus parameters.
183+
// If so then it is safe to dot source across language mode boundaries.
184+
bool isSafeToDotSource = false;
185+
try
186+
{
187+
scriptBlock.GetPowerShell();
188+
isSafeToDotSource = true;
189+
}
190+
catch (Exception e)
191+
{
192+
CheckForSevereException(e);
193+
}
194+
195+
if (!isSafeToDotSource)
196+
{
197+
ErrorRecord errorRecord = new ErrorRecord(
183198
new NotSupportedException(
184199
DiscoveryExceptions.DotSourceNotSupported),
185200
"DotSourceNotSupported",
186201
ErrorCategory.InvalidOperation,
187202
null);
188-
errorRecord.SetInvocationInfo(invocationInfo);
189-
throw new CmdletInvocationException(errorRecord);
203+
errorRecord.SetInvocationInfo(invocationInfo);
204+
throw new CmdletInvocationException(errorRecord);
205+
}
190206
}
191207
}
192208

src/System.Management.Automation/engine/parser/TypeResolver.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
using System.Management.Automation.Language;
1313
using System.Management.Automation.Runspaces;
1414
using System.Net;
15+
using System.Net.NetworkInformation;
1516
using System.Numerics;
1617
using System.Reflection;
1718
using System.Security;
19+
using System.Security.AccessControl;
20+
using System.Security.Cryptography.X509Certificates;
1821
using System.Text.RegularExpressions;
1922
using System.Xml;
2023
using Microsoft.Management.Infrastructure;
@@ -677,10 +680,13 @@ internal static class CoreTypes
677680
{ typeof(Microsoft.Management.Infrastructure.CimType), new[] { "cimtype" } },
678681
{ typeof(CimConverter), new[] { "cimconverter" } },
679682
{ typeof(ModuleSpecification), null },
683+
{ typeof(IPEndPoint), new[] { "IPEndpoint" } },
680684
{ typeof(NullString), new[] { "NullString" } },
681685
{ typeof(OutputTypeAttribute), new[] { "OutputType" } },
682686
{ typeof(Object[]), null },
687+
{ typeof(ObjectSecurity), new[] { "ObjectSecurity" } },
683688
{ typeof(ParameterAttribute), new[] { "Parameter" } },
689+
{ typeof(PhysicalAddress), new[] { "PhysicalAddress" } },
684690
{ typeof(PSCredential), new[] { "pscredential" } },
685691
{ typeof(PSDefaultValueAttribute), new[] { "PSDefaultValue" } },
686692
{ typeof(PSListModifier), new[] { "pslistmodifier" } },
@@ -716,9 +722,13 @@ internal static class CoreTypes
716722
{ typeof(void), new[] { "void" } },
717723
{ typeof(IPAddress), new[] { "ipaddress" } },
718724
{ typeof(DscLocalConfigurationManagerAttribute), new[] {"DscLocalConfigurationManager"}},
725+
{ typeof(WildcardPattern), new[] { "WildcardPattern" } },
726+
{ typeof(X509Certificate), new[] { "X509Certificate" } },
727+
{ typeof(X500DistinguishedName), new[] { "X500DistinguishedName" } },
719728
{ typeof(XmlDocument), new[] { "xml" } },
729+
{ typeof(CimSession), new[] { "CimSession" } },
720730
#if !CORECLR
721-
// Following types not int CoreCLR
731+
// Following types not in CoreCLR
722732
{ typeof(DirectoryEntry), new[] { "adsi" } },
723733
{ typeof(DirectorySearcher), new[] { "adsisearcher" } },
724734
{ typeof(ManagementClass), new[] { "wmiclass" } },

0 commit comments

Comments
 (0)