Skip to content

Commit 835bfd2

Browse files
authored
Skip the AST analysis when command-line input has any parsing errors (#3075)
1 parent 49d34de commit 835bfd2

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

PSReadLine/History.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,24 @@ public static AddToHistoryOption GetDefaultAddToHistoryOption(string line)
565565
return AddToHistoryOption.MemoryAndFile;
566566
}
567567

568+
// The input contains at least one match of some sensitive patterns, so now we need to further
569+
// analyze the input using the ASTs to see if it should actually be considered sensitive.
568570
bool isSensitive = false;
571+
ParseError[] parseErrors = _singleton._parseErrors;
572+
573+
// We need to compare the text here, instead of simply checking whether or not '_ast' is null.
574+
// This is because we may need to update from history file in the middle of editing an input,
575+
// and in that case, the '_ast' may be not-null, but it was not parsed from 'line'.
569576
Ast ast = string.Equals(_singleton._ast?.Extent.Text, line)
570577
? _singleton._ast
571-
: Parser.ParseInput(line, out _, out _);
578+
: Parser.ParseInput(line, out _, out parseErrors);
579+
580+
if (parseErrors != null && parseErrors.Length > 0)
581+
{
582+
// If the input has any parsing errors, we cannot reliably analyze the AST. We just consider
583+
// it sensitive in this case, given that it contains matches of our sensitive pattern.
584+
return AddToHistoryOption.MemoryOnly;
585+
}
572586

573587
do
574588
{

PSReadLine/ReadLine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@ private void Initialize(Runspace runspace, EngineIntrinsics engineIntrinsics)
721721
_mark = 0;
722722
_emphasisStart = -1;
723723
_emphasisLength = 0;
724+
_ast = null;
724725
_tokens = null;
725726
_parseErrors = null;
726727
_inputAccepted = false;

test/HistoryTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ public void SensitiveHistoryDefaultBehavior_Two()
201201
"Get-SecretInfo -Name mytoken; Get-SecretVault; Register-SecretVault; Remove-Secret apikey; Set-Secret", // 'Set-Secret' Not saved to file.
202202
"Set-SecretInfo -Name apikey; Set-SecretVaultDefault; Test-SecretVault; Unlock-SecretVault -password $pwd; Unregister-SecretVault -SecretVault vaultInfo",
203203
"Get-ResultFromTwo -Secret1 (Get-Secret -Name blah -AsPlainText) -Secret2 $secret2",
204-
"Get-ResultFromTwo -Secret1 (Get-Secret -Name blah -AsPlainText) -Secret2 sdv87ysdfayf798hfasd8f7ha" // '-Secret2' has expr-value argument. Not saved to file.
204+
"Get-ResultFromTwo -Secret1 (Get-Secret -Name blah -AsPlainText) -Secret2 sdv87ysdfayf798hfasd8f7ha", // '-Secret2' has expr-value argument. Not saved to file.
205+
"$environment -brand $brand -userBitWardenEmail $bwuser -userBitWardenPassword $bwpass" // '-userBitWardenPassword' matches sensitive pattern and it has parsing error. Not save to file.
205206
};
206207

207208
string[] expectedSavedItems = new[] {

0 commit comments

Comments
 (0)