Fix the dot-sourcing behavior of pwsh -file for advanced-function scripts#27727
Open
daxian-dbw wants to merge 1 commit into
Open
Fix the dot-sourcing behavior of pwsh -file for advanced-function scripts#27727daxian-dbw wants to merge 1 commit into
pwsh -file for advanced-function scripts#27727daxian-dbw wants to merge 1 commit into
Conversation
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aligns the language-mode compatibility validation behavior between the “simple script” and “advanced script” execution paths when scripts are invoked via pwsh -File, addressing inconsistent failures under WDAC/system lockdown scenarios.
Changes:
- Exposes whether a
PSScriptCmdletinstance was created withScriptCommandProcessorShouldRethrowExitenabled (the-Fileconsole-host scenario). - Updates the advanced-script
CommandProcessorpath to skipValidateCompatibleLanguageMode(...)when the script is being run viapwsh -File, matching the existing behavior of the simple-script path.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/System.Management.Automation/engine/runtime/CompiledScriptBlock.cs | Adds an internal accessor to surface the script-cmdlet “rethrow exit” flag to other engine components. |
| src/System.Management.Automation/engine/CommandProcessor.cs | Updates the dot-sourcing language-mode validation guard to also consider the -File (“rethrow exit”) case. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Summary
Context
The behavior between signed advanced script and signed simple script is inconsistent in a WDAC enforced environment -- signed advanced script is rejected when running with
pwsh.exe -fwhile the signed simple script works fine.It turns out advanced script and simple script take different code paths to calling
ValidateCompatibleLanguageMode, and they use different condition checks to decide whether they will actually call into that method.For simple script, PowerShell uses the
DlrScriptCommandProcessor, which callsValidateCompatibleLanguageModeat this location with the following check:For advanced script, PowerShell uses
CommandProcessor, which callsValidateCompatibleLanguageModeat this location, with the following check:As you can see, the check for simple script code path has && !this._rethrowExitException, but that is missing from the advance script code path.
_rethrowExitExceptiongets set at startup in ConsoleHost, as follows:The value of
_rethrowExitExceptionwill betrueif it's justpwsh -file xxx.ps1. In this case, even though the global scope is CLM, there is no harm to dot-source the FLM script file, because the process will be gone right after the script finishes execution, so cross boundary is fine in this case. We believe that's why theDlrScriptCommandProcessorcode path has that check.So, for a simple script, the check returns
falseforpwsh -file xxx.ps1, and thus it never calls toValidateCompatibleLanguageMode. While for an advanced script, it always calls toValidateCompatibleLanguageMode. This is why in a WDAC enforced environment,pwsh -fileallows a signed simple script to work but errors out for a signed advanced script.Fix
Given the above, the fix should be adding the
&& !this._rethrowExitExceptioncheck to the CommandProcessor code path too (the advance script code path). With such a fix,pwsh -file xxx.ps1will work for both signed advanced script and simple script.pwsh -noexit -file xxx.ps1will continue to fail for signed scripts as it is today.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header