diff --git a/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs b/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs index 2d73ba752fb..f03bd0bf3e0 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs @@ -108,6 +108,9 @@ private PowerShellAssemblyLoadContext(string basePaths) // - Value: strong name of the TPA that contains the type represented by Key. private readonly Dictionary _coreClrTypeCatalog; private readonly Lazy> _availableDotNetAssemblyNames; + private readonly HashSet _blackListedAssemblies = new HashSet(StringComparer.OrdinalIgnoreCase){ + "System.Windows.Forms" + }; #if !UNIX private string _winDir; @@ -295,6 +298,13 @@ private Assembly Resolve(AssemblyLoadContext loadContext, AssemblyName assemblyN private bool TryFindInGAC(AssemblyName assemblyName, out string assemblyFilePath) { assemblyFilePath = null; + if (_blackListedAssemblies.Contains(assemblyName.Name)) + { + // DotNet catches and throws a new exception with no inner exception + // We cannot change the message DotNet returns. + return false; + } + if (Internal.InternalTestHooks.DisableGACLoading) { return false; diff --git a/test/powershell/engine/Basic/Assembly.LoadFrom.ps1 b/test/powershell/engine/Basic/Assembly.LoadFrom.Tests.ps1 similarity index 99% rename from test/powershell/engine/Basic/Assembly.LoadFrom.ps1 rename to test/powershell/engine/Basic/Assembly.LoadFrom.Tests.ps1 index 78cd468b434..4b4555a2715 100644 --- a/test/powershell/engine/Basic/Assembly.LoadFrom.ps1 +++ b/test/powershell/engine/Basic/Assembly.LoadFrom.Tests.ps1 @@ -1,5 +1,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. + Describe "Assembly.LoadFrom Validation Test" -Tags "CI" { BeforeAll { $ConsumerCode = @' diff --git a/test/powershell/engine/Basic/Assembly.LoadWithPartialName.Tests.ps1 b/test/powershell/engine/Basic/Assembly.LoadWithPartialName.Tests.ps1 new file mode 100644 index 00000000000..edf54b703dc --- /dev/null +++ b/test/powershell/engine/Basic/Assembly.LoadWithPartialName.Tests.ps1 @@ -0,0 +1,34 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +Describe "Assembly::LoadWithPartialName Validation Test" -Tags "CI" { + + $defaultErrorId = 'FileLoadException' + $testcases = @( + # verify winforms is blocked + @{ + Name = 'system.windows.forms' + ErrorId = $defaultErrorId + } + # Verify alternative casing is blocked + @{ + Name = 'System.Windows.Forms' + ErrorId = $defaultErrorId + } + ) + + # All existing cases should fail on all platforms either because it doesn't exist or + # because the assembly is blacklisted + It "Assembly::LoadWithPartialName should fail to load blacklisted assembly: " -TestCases $testcases { + param( + [Parameter(Mandatory)] + [string] + $Name, + [Parameter(Mandatory)] + [string] + $ErrorId + ) + + {[System.Reflection.Assembly]::LoadWithPartialName($Name)} | Should -Throw -ErrorId $ErrorId + } +}