Ensure assemblies listed FileList are not loaded (#12968)

Co-authored-by: Aditya Patwardhan <Aditya Patwardhan>
This commit is contained in:
Aditya Patwardhan
2020-06-30 10:16:13 -07:00
committed by GitHub
co-authored by Aditya Patwardhan
parent 0c8aa53a90
commit 808bf22d2f
2 changed files with 39 additions and 5 deletions
@@ -4379,7 +4379,7 @@ namespace Microsoft.PowerShell.Commands
{
try
{
string fixedFileName = FixupFileName(moduleBase, s, extension, importingModule);
string fixedFileName = FixupFileName(moduleBase, s, extension, importingModule, skipLoading: true);
var dir = Path.GetDirectoryName(fixedFileName);
if (string.Equals(psHome, dir, StringComparison.OrdinalIgnoreCase) ||
@@ -4537,9 +4537,9 @@ namespace Microsoft.PowerShell.Commands
/// <summary>
/// A utility routine to fix up a file name so it's rooted and has an extension.
/// </summary>
internal string FixupFileName(string moduleBase, string name, string extension, bool isImportingModule)
internal string FixupFileName(string moduleBase, string name, string extension, bool isImportingModule, bool skipLoading = false)
{
return FixupFileName(moduleBase, name, extension, isImportingModule, pathIsResolved: out _);
return FixupFileName(moduleBase, name, extension, isImportingModule, pathIsResolved: out _, skipLoading);
}
/// <summary>
@@ -4554,10 +4554,11 @@ namespace Microsoft.PowerShell.Commands
/// <param name="extension">The extension to use in case the given name has no extension.</param>
/// <param name="isImportingModule">Indicate if we are loading a module.</param>
/// <param name="pathIsResolved">Indicate if the returned path is fully resolved.</param>
/// <param name="skipLoading">Indicate if the resolved module should be loaded.</param>
/// <returns>
/// The resolved file path. Or, the combined path of <paramref name="moduleBase"/> and <paramref name="name"/> when the file path cannot be resolved.
/// </returns>
internal string FixupFileName(string moduleBase, string name, string extension, bool isImportingModule, out bool pathIsResolved)
internal string FixupFileName(string moduleBase, string name, string extension, bool isImportingModule, out bool pathIsResolved, bool skipLoading = false)
{
pathIsResolved = false;
string originalName = name;
@@ -4585,7 +4586,7 @@ namespace Microsoft.PowerShell.Commands
// Return the path if successfully resolved.
if (resolvedPath != null)
{
if (isImportingModule && resolvedPath.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
if (isImportingModule && resolvedPath.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) && !skipLoading)
{
// If we are fixing up an assembly file path and we are actually loading the module, then we load the resolved assembly file here.
// This is because we process type/format ps1xml files before 'RootModule' and 'NestedModules' entries during the module loading.
@@ -361,3 +361,36 @@ Describe "Import-Module -Force behaviour" -Tag "CI" {
{ Test-Two } | Should -Throw -ErrorId 'CommandNotFoundException'
}
}
Describe "Module with FileList" -Tag "CI" {
BeforeAll {
$src = @"
using System;
namespace ModuleCmdlets
{
public class FileListLoad
{
}
}
"@
$originalPSModulePath = $env:PSModulePath
New-Item -ItemType Directory -Path "$testdrive\Modules\FileListLoadTest\" -Force > $null
$asmPath = "$TESTDRIVE\Modules\FileListLoadTest\FileListLoadTest.dll"
Add-Type -TypeDefinition $src -OutputAssembly $asmPath
$env:PSModulePath += [System.IO.Path]::PathSeparator + "$testdrive\Modules"
New-ModuleManifest -Path "$testdrive\Modules\FileListLoadTest\FileListLoadTest.psd1" -FileList @("FileListLoadTest.dll")
}
AfterAll {
$env:PSModulePath = $originalPSModulePath
}
It "Assemblies in FileList are not loaded" {
Import-Module FileListLoadTest
$asms = [System.AppDomain]::CurrentDomain.GetAssemblies().Location
$asmPath | Should -Not -BeIn $asms
}
}