FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

add internal RedactIfPowerShellCode method · PowerShell/PowerShell@f229dc0 · GitHub

Repository navigation

Commit f229dc0

Browse files
committed
add internal RedactIfPowerShellCode method
1 parent 1aac7f5 commit f229dc0

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

‎src/System.Management.Automation/engine/remoting/commands/PushRunspaceCommand.cs‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,58 @@ internal static void ContinueCommand(RemoteRunspace remoteRunspace, Pipeline cmd
13951395
}
13961396
}
13971397

1398+
internal static string RedactIfPowerShellCode(string input)
1399+
{
1400+
if (string.IsNullOrEmpty(input))
1401+
{
1402+
return input;
1403+
}
1404+
1405+
bool startsWithDangerousToken =
1406+
input.StartsWith('$') ||
1407+
input.StartsWith("@{") ||
1408+
input.StartsWith('(') ||
1409+
input.StartsWith('"') ||
1410+
input.StartsWith("'") ||
1411+
input.StartsWith('@') ||
1412+
input.StartsWith('\'');
1413+
1414+
if (startsWithDangerousToken)
1415+
{
1416+
// TODO add a debug message
1417+
return "REDACTED";
1418+
}
1419+
1420+
var ast = Parser.ParseInput(
1421+
input.ToString(),
1422+
out Token[] _,
1423+
out ParseError[] errors);
1424+
1425+
if (errors.Length > 0)
1426+
{
1427+
return input;
1428+
}
1429+
1430+
bool containsCode = false;
1431+
// we leave out n is CommandAst from here as to allow VMNames like svr19 to pass through without being redacted.
1432+
// Also could have used .Any() from Linq but I couldn't get that working.
1433+
foreach (var node in ast.FindAll(
1434+
n =>
1435+
n is VariableExpressionAst ||
1436+
n is ExpandableStringExpressionAst ||
1437+
n is SubExpressionAst,
1438+
true))
1439+
{
1440+
containsCode = true;
1441+
// TODO add a debug message
1442+
break;
1443+
}
1444+
1445+
return containsCode
1446+
? "REDACTED"
1447+
: input;
1448+
}
1449+
13981450
#endregion
13991451
}
14001452
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL