Skip to content

Commit

Permalink
Fix creation of InitialSessionState to use CreateDefault2() (#1547)
Browse files Browse the repository at this point in the history
We erroneously changed our code to use `CreateDefault()` instead of
`CreateDefault2()`. There existed old but defunct code for testing the
use of `CreateDefault()` instead, but it was never intentionally moved
into production. The latter function loads only
`Microsoft.PowerShell.Core`, leaving us to load the exact modules we
intend to load ourselves, which is the safer behavior (and the previous
behavior).
  • Loading branch information
andyleejordan authored Aug 18, 2021
1 parent 3d0fb29 commit 69a0b04
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 27 deletions.
20 changes: 2 additions & 18 deletions PowerShellEditorServices.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,6 @@ if (Get-Command git -ErrorAction SilentlyContinue) {
git update-index --assume-unchanged "$PSScriptRoot/src/PowerShellEditorServices.Hosting/BuildInfo.cs"
}

function Invoke-WithCreateDefaultHook {
param([scriptblock]$ScriptBlock)

try
{
$env:PSES_TEST_USE_CREATE_DEFAULT = 1
& $ScriptBlock
} finally {
Remove-Item env:PSES_TEST_USE_CREATE_DEFAULT
}
}

function Install-Dotnet {
param (
[string[]]$Channel
Expand Down Expand Up @@ -246,16 +234,12 @@ task TestServerWinPS -If (-not $script:IsNix) {

task TestServerPS7 -If (-not $script:IsRosetta) {
Set-Location .\test\PowerShellEditorServices.Test\
Invoke-WithCreateDefaultHook -NewModulePath $script:PSCoreModulePath {
exec { & $script:dotnetExe $script:dotnetTestArgs $script:NetRuntime.PS7 }
}
exec { & $script:dotnetExe $script:dotnetTestArgs $script:NetRuntime.PS7 }
}

task TestServerPS72 {
Set-Location .\test\PowerShellEditorServices.Test\
Invoke-WithCreateDefaultHook -NewModulePath $script:PSCoreModulePath {
exec { & $script:dotnetExe $script:dotnetTestArgs $script:NetRuntime.PS72 }
}
exec { & $script:dotnetExe $script:dotnetTestArgs $script:NetRuntime.PS72 }
}

task TestE2E {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,15 +468,12 @@ private static RunspacePool CreatePssaRunspacePool(out PSModuleInfo pssaModuleIn
throw new FileNotFoundException("Unable to find PSScriptAnalyzer module on the module path");
}

// Now that we know where the PSScriptAnalyzer we want to use is,
// create a base session state with PSScriptAnalyzer loaded
#if DEBUG
InitialSessionState sessionState = Environment.GetEnvironmentVariable("PSES_TEST_USE_CREATE_DEFAULT") == "1"
? InitialSessionState.CreateDefault()
: InitialSessionState.CreateDefault2();
#else
// Now that we know where the PSScriptAnalyzer we want to use is, create a base
// session state with PSScriptAnalyzer loaded
//
// We intentionally use `CreateDefault2()` as it loads `Microsoft.PowerShell.Core`
// only, which is a more minimal and therefore safer state.
InitialSessionState sessionState = InitialSessionState.CreateDefault2();
#endif

sessionState.ImportPSModule(new [] { pssaModuleInfo.ModuleBase });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ internal static class PowerShellContextFactory
public static PowerShellContextService Create(ILogger logger)
{
PowerShellContextService powerShellContext = new PowerShellContextService(logger, null, isPSReadLineEnabled: false);
var initialSessionState = InitialSessionState.CreateDefault();

// We intentionally use `CreateDefault2()` as it loads `Microsoft.PowerShell.Core` only,
// which is a more minimal and therefore safer state.
var initialSessionState = InitialSessionState.CreateDefault2();

// We set the process scope's execution policy (which is really the runspace's scope) to
// `Bypass` so we can import our bundled modules. This is equivalent in scope to the CLI
// argument `-ExecutionPolicy Bypass`, which (for instance) the extension passes. Thus
Expand Down

0 comments on commit 69a0b04

Please sign in to comment.