| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1395,6 +1395,58 @@ internal static void ContinueCommand(RemoteRunspace remoteRunspace, Pipeline cmd | |||
| 1395 | 1395 | } | |
| 1396 | 1396 | } | |
| 1397 | 1397 | ||
| 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 | + | ||
| 1398 | 1450 | #endregion | |
| 1399 | 1451 | } | |
| 1400 | 1452 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments