Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the sensitive history scrubbing to allow retrieving token from az, gcloud, and kubectl #3641

Merged
merged 1 commit into from
Apr 4, 2023
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
73 changes: 65 additions & 8 deletions PSReadLine/History.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ public class HistoryItem
"Set-SecretVaultDefault",
"Test-SecretVault",
"Unlock-SecretVault",
"Unregister-SecretVault"
"Unregister-SecretVault",
"Get-AzAccessToken",
};

private void ClearSavedCurrentLine()
Expand Down Expand Up @@ -511,15 +512,32 @@ private static bool IsOnLeftSideOfAnAssignment(Ast ast, out Ast rhs)
return result;
}

private static bool IsRightSideOfAnAssignmentSafe(Ast rhs)
{
if (rhs is PipelineAst)
{
// Right hand side is a pipeline.
return true;
}

if (rhs is CommandExpressionAst cmdExprAst && cmdExprAst.Expression is MemberExpressionAst or InvokeMemberExpressionAst)
{
// Right hand side is a member access, or method invocation.
return true;
}

return false;
}

private static bool IsSecretMgmtCommand(StringConstantExpressionAst strConst, out CommandAst command)
{
command = null;
bool result = false;
command = strConst.Parent as CommandAst;

if (command is not null)
if (strConst.Parent is CommandAst cmdAst && ReferenceEquals(cmdAst.CommandElements[0], strConst) && s_SecretMgmtCommands.Contains(strConst.Value))
{
result = ReferenceEquals(command.CommandElements[0], strConst)
&& s_SecretMgmtCommands.Contains(strConst.Value);
result = true;
command = cmdAst;
}

return result;
Expand Down Expand Up @@ -568,6 +586,45 @@ private static ExpressionAst GetArgumentForParameter(CommandParameterAst param)
return null;
}

private static bool IsCloudTokenOrSecretAccess(StringConstantExpressionAst arg2Ast, out CommandAst command)
{
bool result = false;
command = arg2Ast.Parent as CommandAst;

if (command is not null && command.CommandElements.Count >= 3
&& command.CommandElements[0] is StringConstantExpressionAst nameAst
&& command.CommandElements[1] is StringConstantExpressionAst arg1Ast
&& command.CommandElements[2] == arg2Ast)
{
string name = nameAst.Value;
string arg1 = arg1Ast.Value;
string arg2 = arg2Ast.Value;

if (string.Equals(name, "gcloud", StringComparison.OrdinalIgnoreCase))
{
result = string.Equals(arg1, "auth", StringComparison.OrdinalIgnoreCase) &&
string.Equals(arg2, "print-access-token", StringComparison.OrdinalIgnoreCase);
}
else if (string.Equals(name, "az", StringComparison.OrdinalIgnoreCase))
{
result = string.Equals(arg1, "account", StringComparison.OrdinalIgnoreCase) &&
string.Equals(arg2, "get-access-token", StringComparison.OrdinalIgnoreCase);
}
else if (string.Equals(name, "kubectl", StringComparison.OrdinalIgnoreCase))
{
result = (string.Equals(arg1, "get", StringComparison.OrdinalIgnoreCase) || string.Equals(arg1, "describe", StringComparison.OrdinalIgnoreCase))
&& (string.Equals(arg2, "secrets", StringComparison.OrdinalIgnoreCase) || string.Equals(arg2, "secret", StringComparison.OrdinalIgnoreCase));
}
}

if (!result)
{
command = null;
}

return result;
}

public static AddToHistoryOption GetDefaultAddToHistoryOption(string line)
{
if (string.IsNullOrEmpty(line))
Expand Down Expand Up @@ -618,8 +675,7 @@ public static AddToHistoryOption GetDefaultAddToHistoryOption(string line)
// If it appears on the left-hand-side of an assignment, and the right-hand-side is
// not a command invocation, we consider it sensitive.
// e.g. `$token = Get-Secret` vs. `$token = 'token-text'` or `$token, $url = ...`
isSensitive = IsOnLeftSideOfAnAssignment(innerAst, out Ast rhs)
&& rhs is not PipelineAst;
isSensitive = IsOnLeftSideOfAnAssignment(innerAst, out Ast rhs) && !IsRightSideOfAnAssignmentSafe(rhs);

if (!isSensitive)
{
Expand All @@ -629,7 +685,8 @@ public static AddToHistoryOption GetDefaultAddToHistoryOption(string line)

case StringConstantExpressionAst strConst:
isSensitive = true;
if (IsSecretMgmtCommand(strConst, out CommandAst command))
if (IsSecretMgmtCommand(strConst, out CommandAst command)
|| IsCloudTokenOrSecretAccess(strConst, out command))
{
// If it's one of the secret management commands that we can ignore, we consider it safe.
isSensitive = false;
Expand Down
16 changes: 16 additions & 0 deletions test/HistoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ public void SensitiveHistoryDefaultBehavior_Two()
"$a.Password.Secret | Set-Value",
"Write-Host $a.Password.Secret",
"($a.Password, $b) = ('aa', 'bb')", // setting the 'Password' property with string value. Not saved to file.
"kubectl get secrets",
"kubectl get secret db-user-pass -o jsonpath='{.data.password}' | base64 --decode",
"kubectl describe secret db-user-pass",
"(Get-AzAccessToken -ResourceUrl 'https://abc.com').Token",
"$token = (Get-AzAccessToken -ResourceUrl 'abc').Token",
"az account get-access-token --resource=https://abc.com --query accessToken --output tsv",
"curl -X GET --header \"Authorization: Bearer $token\" https://abc.com",
"$env:PGPASS = gcloud auth print-access-token",
};

string[] expectedSavedItems = new[] {
Expand All @@ -232,6 +240,14 @@ public void SensitiveHistoryDefaultBehavior_Two()
"$a.Secret = $secret",
"$a.Password.Secret | Set-Value",
"Write-Host $a.Password.Secret",
"kubectl get secrets",
"kubectl get secret db-user-pass -o jsonpath='{.data.password}' | base64 --decode",
"kubectl describe secret db-user-pass",
"(Get-AzAccessToken -ResourceUrl 'https://abc.com').Token",
"$token = (Get-AzAccessToken -ResourceUrl 'abc').Token",
"az account get-access-token --resource=https://abc.com --query accessToken --output tsv",
"curl -X GET --header \"Authorization: Bearer $token\" https://abc.com",
"$env:PGPASS = gcloud auth print-access-token",
};

try
Expand Down