Skip to content

Commit 8f37c92

Browse files
authored
Blacklist System.Windows.Forms form loaded to prevent a crash (#6822)
Blacklist `System.Windows.Forms` (`WinForms`) from being loaded to prevent a crash
1 parent e0ee13c commit 8f37c92

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ private PowerShellAssemblyLoadContext(string basePaths)
108108
// - Value: strong name of the TPA that contains the type represented by Key.
109109
private readonly Dictionary<string, string> _coreClrTypeCatalog;
110110
private readonly Lazy<HashSet<string>> _availableDotNetAssemblyNames;
111+
private readonly HashSet<string> _blackListedAssemblies = new HashSet<string>(StringComparer.OrdinalIgnoreCase){
112+
"System.Windows.Forms"
113+
};
111114

112115
#if !UNIX
113116
private string _winDir;
@@ -295,6 +298,13 @@ private Assembly Resolve(AssemblyLoadContext loadContext, AssemblyName assemblyN
295298
private bool TryFindInGAC(AssemblyName assemblyName, out string assemblyFilePath)
296299
{
297300
assemblyFilePath = null;
301+
if (_blackListedAssemblies.Contains(assemblyName.Name))
302+
{
303+
// DotNet catches and throws a new exception with no inner exception
304+
// We cannot change the message DotNet returns.
305+
return false;
306+
}
307+
298308
if (Internal.InternalTestHooks.DisableGACLoading)
299309
{
300310
return false;

test/powershell/engine/Basic/Assembly.LoadFrom.ps1 renamed to test/powershell/engine/Basic/Assembly.LoadFrom.Tests.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
3+
34
Describe "Assembly.LoadFrom Validation Test" -Tags "CI" {
45
BeforeAll {
56
$ConsumerCode = @'
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
Describe "Assembly::LoadWithPartialName Validation Test" -Tags "CI" {
5+
6+
$defaultErrorId = 'FileLoadException'
7+
$testcases = @(
8+
# verify winforms is blocked
9+
@{
10+
Name = 'system.windows.forms'
11+
ErrorId = $defaultErrorId
12+
}
13+
# Verify alternative casing is blocked
14+
@{
15+
Name = 'System.Windows.Forms'
16+
ErrorId = $defaultErrorId
17+
}
18+
)
19+
20+
# All existing cases should fail on all platforms either because it doesn't exist or
21+
# because the assembly is blacklisted
22+
It "Assembly::LoadWithPartialName should fail to load blacklisted assembly: <Name>" -TestCases $testcases {
23+
param(
24+
[Parameter(Mandatory)]
25+
[string]
26+
$Name,
27+
[Parameter(Mandatory)]
28+
[string]
29+
$ErrorId
30+
)
31+
32+
{[System.Reflection.Assembly]::LoadWithPartialName($Name)} | Should -Throw -ErrorId $ErrorId
33+
}
34+
}

0 commit comments

Comments
 (0)