-
Notifications
You must be signed in to change notification settings - Fork 410
Add new AvoidSecretDisclosure rule #2183
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
Open
iRon7
wants to merge
3
commits into
PowerShell:main
Choose a base branch
from
iRon7:AvoidSecureStringDisclosure
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| #if !CORECLR | ||
| using System.ComponentModel.Composition; | ||
| #endif | ||
| using System.Globalization; | ||
| using System.Management.Automation.Language; | ||
| using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; | ||
|
|
||
| namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules | ||
| { | ||
| /// <summary> | ||
| /// AvoidSecretDisclosure: Checks whether a plaintext secret is being retrieved which can lead to | ||
| /// security vulnerabilities such as memory trails or logging trails. | ||
| /// The general approach of dealing with credentials is to avoid them and instead rely on other means | ||
| /// to authenticate, such as certificates or Windows authentication. | ||
| /// </summary> | ||
| #if !CORECLR | ||
| [Export(typeof(IScriptRule))] | ||
| #endif | ||
| public class AvoidSecretDisclosure : ConfigurableRule | ||
| { | ||
|
|
||
| /// <summary> | ||
| /// Construct an object of AvoidSecretDisclosure type. | ||
| /// </summary> | ||
| public AvoidSecretDisclosure() { | ||
| Enable = true; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Analyzes the given ast to find the [violation] | ||
| /// </summary> | ||
| /// <param name="ast">AST to be analyzed. This should be non-null</param> | ||
| /// <param name="fileName">Name of file that corresponds to the input AST.</param> | ||
| /// <returns>A an enumerable type containing the violations</returns> | ||
| public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) | ||
| { | ||
| if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage); | ||
|
|
||
| // Check for ConvertFrom-SecureString with -AsPlainText parameter | ||
| IEnumerable<Ast> convertFromSecureStringAsts = ast.FindAll(testAst => | ||
| testAst is CommandAst cmdAst && | ||
| cmdAst.GetCommandName() != null && | ||
| cmdAst.GetCommandName().Equals("ConvertFrom-SecureString", StringComparison.OrdinalIgnoreCase), | ||
| true | ||
| ); | ||
|
|
||
| foreach (CommandAst cmdAst in convertFromSecureStringAsts) | ||
| { | ||
| // Use StaticParameterBinder to reliably get parameter values | ||
| var bindingResult = StaticParameterBinder.BindCommand(cmdAst, true); | ||
|
|
||
| // Check for -AsPlainText parameter | ||
| // The constantValue appears true even the value is a variable | ||
| // This is ok because the rule should still trigger in that case since the value of the | ||
| // variable could be true at runtime, and we want to catch all potential violations | ||
| if ( | ||
| bindingResult.BoundParameters.ContainsKey("AsPlainText") && | ||
| bindingResult.BoundParameters["AsPlainText"].ConstantValue is bool constantValue && | ||
| constantValue == true | ||
| ) { | ||
| yield return GetDiagnosticRecord(cmdAst.Extent, fileName, "AsPlainText"); | ||
| } | ||
| } | ||
|
|
||
| // Check for any invocation of a method that starts with "SecureStringTo" | ||
| // (e.g. SecureStringToBSTR, SecureStringToCoTaskMemAnsi, etc.) | ||
| IEnumerable<Ast> secureStringToAsts = ast.FindAll(testAst => | ||
| testAst is InvokeMemberExpressionAst invokeAst && | ||
| invokeAst.Member != null && | ||
| invokeAst.Member.ToString().StartsWith("SecureStringTo", StringComparison.OrdinalIgnoreCase), | ||
| true | ||
| ); | ||
|
|
||
| foreach (InvokeMemberExpressionAst secureStringToAst in secureStringToAsts) { | ||
| yield return GetDiagnosticRecord(secureStringToAst.Extent, fileName, secureStringToAst.Member.ToString()); | ||
| } | ||
|
|
||
| // Check for any member access of a property named "Password". | ||
| // Note that this is a heuristic and may lead to false positives, | ||
| // as not all properties named "Password" necessarily contain secrets, | ||
| // and there may be secrets stored in properties with other names. | ||
| // However, it is too complex to reliably determine whether a Password | ||
| // property is a result of e.g. a PSCredential.GetNetworkCredential() call. | ||
| // Anyways, this is still a useful common check to have. | ||
| IEnumerable<Ast> passwordAsts = ast.FindAll(testAst => | ||
| testAst is MemberExpressionAst memberAst && | ||
| memberAst.Member != null && | ||
| string.Equals(memberAst.Member.ToString(), "Password", StringComparison.OrdinalIgnoreCase), | ||
| true | ||
| ); | ||
|
|
||
| foreach (MemberExpressionAst passwordAst in passwordAsts) { | ||
| yield return GetDiagnosticRecord(passwordAst.Extent, fileName, passwordAst.Member.ToString()); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Helper function to create a DiagnosticRecord for a given violation | ||
| /// </summary> | ||
| private DiagnosticRecord GetDiagnosticRecord(IScriptExtent Extent, string fileName, string suppressionId) | ||
| { | ||
| return new DiagnosticRecord( | ||
| Strings.AvoidSecretDisclosureError, | ||
| Extent, | ||
| GetName(), | ||
| DiagnosticSeverity.Warning, | ||
| fileName, | ||
| suppressionId | ||
| ); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Retrieves the common name of this rule. | ||
| /// </summary> | ||
| public override string GetCommonName() | ||
| { | ||
| return string.Format(CultureInfo.CurrentCulture, Strings.AvoidSecretDisclosureCommonName); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Retrieves the description of this rule. | ||
| /// </summary> | ||
| public override string GetDescription() | ||
| { | ||
| return string.Format(CultureInfo.CurrentCulture, Strings.AvoidSecretDisclosureDescription); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Retrieves the name of this rule. | ||
| /// </summary> | ||
| public override string GetName() | ||
| { | ||
| return string.Format( | ||
| CultureInfo.CurrentCulture, | ||
| Strings.NameSpaceFormat, | ||
| GetSourceName(), | ||
| Strings.AvoidSecretDisclosureName); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Retrieves the severity of the rule: error, warning or information. | ||
| /// </summary> | ||
| public override RuleSeverity GetSeverity() | ||
| { | ||
| return RuleSeverity.Warning; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the severity of the returned diagnostic record: error, warning, or information. | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| public DiagnosticSeverity GetDiagnosticSeverity() | ||
| { | ||
| return DiagnosticSeverity.Warning; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Retrieves the name of the module/assembly the rule is from. | ||
| /// </summary> | ||
| public override string GetSourceName() | ||
| { | ||
| return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Retrieves the type of the rule, Builtin, Managed or Module. | ||
| /// </summary> | ||
| public override SourceType GetSourceType() | ||
| { | ||
| return SourceType.Builtin; | ||
| } | ||
| } | ||
| } | ||
|
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.