Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3102,6 +3102,30 @@ internal static object ForEach(IEnumerator enumerator, object expression, object
else
{
var property = member as PSPropertyInfo;
if (languageMode == PSLanguageMode.ConstrainedLanguage
&& numArgs != 0
&& !CoreTypes.Contains(basedCurrent.GetType()))
{
if (SystemPolicy.GetSystemLockdownPolicy() != SystemEnforcementMode.Audit)
{
throw InterpreterError.NewInterpreterException(
current,
typeof(PSInvalidOperationException),
null,
"PropertySetterNotSupportedInConstrainedLanguage",
ParserStrings.PropertySetConstrainedLanguage);
}

SystemPolicy.LogWDACAuditMessage(
context: context,
title: ParameterBinderStrings.WDACBinderInvocationLogTitle,
message: StringUtil.Format(
ParameterBinderStrings.WDACBinderInvocationLogMessage,
name,
basedCurrent.GetType().FullName ?? string.Empty),
fqid: "MethodOrPropertyInvocationNotAllowed",
dropIntoDebugger: true);
}

switch (numArgs)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,47 @@ try

$expectedError.FullyQualifiedErrorId | Should -BeExactly "MethodInvocationNotSupportedInConstrainedLanguage"
}

It "Verify ForEach engine intrinsic method cannot set property values for non-core types when in constrained language mode" {

try
{
$ExecutionContext.SessionState.LanguageMode = 'ConstrainedLanguage'
& {
@($ExecutionContext.SessionState).ForEach('LanguageMode', 'FullLanguage')
}

throw "No Exception!"
}
catch
{
$expectedError = $_
}
finally
{
Invoke-LanguageModeTestingSupportCmdlet -EnableFullLanguageMode
}

$expectedError.FullyQualifiedErrorId | Should -BeExactly "PropertySetterNotSupportedInConstrainedLanguage"
}

It "Verify ForEach engine intrinsic method correctly allows setting property values for core types in constrained language mode" {

$validateSet = [ValidateSet]::new('test')
try
{
$ExecutionContext.SessionState.LanguageMode = 'ConstrainedLanguage'
& {
@($validateSet).ForEach('ErrorMessage', 'NewMessage')
}
}
finally
{
Invoke-LanguageModeTestingSupportCmdlet -EnableFullLanguageMode
}

$validateSet.ErrorMessage | Should -BeExactly 'NewMessage'
}
}

Describe "ThreadJob Constrained Language Tests" -Tags 'Feature','RequireAdminOnWindows' {
Expand Down
Loading