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 @@ -144,7 +144,7 @@ protected override void ProcessRecord()

// Prevent script injection attack by disallowing ExportModuleMemberCommand to export module members across
// language boundaries. This will prevent injected untrusted script from exporting private trusted module functions.
if (Context.EngineSessionState.Module != null &&
if (Context.EngineSessionState.Module?.LanguageMode != null &&
Context.LanguageMode != Context.EngineSessionState.Module.LanguageMode)
{
var se = new PSSecurityException(Modules.CannotExportMembersAccrossLanguageBoundaries);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,54 @@ try
$module.ExportedCommands.Values[0].Name | Should -BeExactly 'PublicFn'
}
}

Describe "Export-ModuleMember should succeed in FullLanguage mode with scriptblock created without context" -Tag 'Feature' {

BeforeAll {

$typeDef = @'
using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

public class TestScriptBlockCreate
{
private ScriptBlock _scriptBlock;

public ScriptBlock CreateScriptBlock()
{
var thread = new System.Threading.Thread(ThreadProc);
thread.Start(null);
thread.Join();

return _scriptBlock;
}

private void ThreadProc(object state)
{
// Create script block on thread with no PowerShell context
_scriptBlock = ScriptBlock.Create(@"function Do-Nothing {}; Export-ModuleMember -Function Do-Nothing");
}
}
'@

try
{
Add-Type -TypeDefinition $typeDef
}
catch { }
}

It "Verfies that Export-ModuleMember does not throw error with context-less scriptblock" {

$scriptBlockCreator = [TestScriptBlockCreate]::new()
$testScriptBlock = $scriptBlockCreator.CreateScriptBlock()

$testScriptBlock | Should -Not -BeNullOrEmpty

{ New-Module -ScriptBlock $testScriptBlock -ErrorAction Stop } | Should -Not -Throw -Because "Scriptblock without execution context is allowed in Full Language"
}
}
}
finally
{
Expand Down