-
Notifications
You must be signed in to change notification settings - Fork 386
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
Add AvoidUsingAllowUnencryptedAuthentication #1857
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4ae2407
Add AvoidUsingAllowUnencryptedAuthentication rule
MJVL 827c2cf
Add AvoidUsingAllowUnencryptedAuthentication docs and tests
MJVL 4ebdcb9
Update docs/Rules/AvoidUsingAllowUnencryptedAuthentication.md
MJVL c489ba4
Fix code review suggestions
MJVL 8ee0d15
Fix md code styling
MJVL e1eca74
Merge branch 'master' of https://github.com/PowerShell/PSScriptAnalyz…
da58e92
bump rule count in tests again
4e7449a
Update docs/Rules/AvoidUsingAllowUnencryptedAuthentication.md
bergmeister 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 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,117 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Management.Automation.Language; | ||
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; | ||
#if !CORECLR | ||
using System.ComponentModel.Composition; | ||
#endif | ||
using System.Globalization; | ||
|
||
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules | ||
{ | ||
/// <summary> | ||
/// AvoidUsingAllowUnencryptedAuthentication: Avoid sending credentials and secrets over unencrypted connections. | ||
/// </summary> | ||
#if !CORECLR | ||
[Export(typeof(IScriptRule))] | ||
#endif | ||
public class AvoidUsingAllowUnencryptedAuthentication : AvoidParameterGeneric | ||
{ | ||
/// <summary> | ||
/// Condition on the cmdlet that must be satisfied for the error to be raised | ||
/// </summary> | ||
/// <param name="CmdAst"></param> | ||
/// <returns></returns> | ||
public override bool CommandCondition(CommandAst CmdAst) | ||
{ | ||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// Condition on the parameter that must be satisfied for the error to be raised. | ||
/// </summary> | ||
/// <param name="CmdAst"></param> | ||
/// <param name="CeAst"></param> | ||
/// <returns></returns> | ||
public override bool ParameterCondition(CommandAst CmdAst, CommandElementAst CeAst) | ||
{ | ||
return CeAst is CommandParameterAst && String.Equals((CeAst as CommandParameterAst).ParameterName, "AllowUnencryptedAuthentication", StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
/// <summary> | ||
/// Retrieves the error message | ||
/// </summary> | ||
/// <param name="FileName"></param> | ||
/// <param name="CmdAst"></param> | ||
/// <returns></returns> | ||
public override string GetError(string fileName, CommandAst cmdAst) | ||
{ | ||
return String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingAllowUnencryptedAuthenticationError); | ||
} | ||
|
||
/// <summary> | ||
/// GetName: Retrieves the name of this rule. | ||
/// </summary> | ||
/// <returns>The name of this rule</returns> | ||
public override string GetName() | ||
{ | ||
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingAllowUnencryptedAuthenticationName); | ||
} | ||
|
||
/// <summary> | ||
/// GetCommonName: Retrieves the common name of this rule. | ||
/// </summary> | ||
/// <returns>The common name of this rule</returns> | ||
public override string GetCommonName() | ||
{ | ||
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingAllowUnencryptedAuthenticationCommonName); | ||
} | ||
|
||
/// <summary> | ||
/// GetDescription: Retrieves the description of this rule. | ||
/// </summary> | ||
/// <returns>The description of this rule</returns> | ||
public override string GetDescription() | ||
{ | ||
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingAllowUnencryptedAuthenticationDescription); | ||
} | ||
|
||
/// <summary> | ||
/// GetSourceType: Retrieves the type of the rule: builtin, managed or module. | ||
/// </summary> | ||
public override SourceType GetSourceType() | ||
{ | ||
return SourceType.Builtin; | ||
} | ||
|
||
/// <summary> | ||
/// GetSeverity: Retrieves the severity of the rule: error, warning or information. | ||
/// </summary> | ||
/// <returns></returns> | ||
public override RuleSeverity GetSeverity() | ||
{ | ||
return RuleSeverity.Warning; | ||
} | ||
|
||
/// <summary> | ||
/// DiagnosticSeverity: Retrieves the severity of the rule of type DiagnosticSeverity: error, warning or information. | ||
/// </summary> | ||
/// <returns></returns> | ||
public override DiagnosticSeverity GetDiagnosticSeverity() | ||
{ | ||
return DiagnosticSeverity.Warning; | ||
} | ||
|
||
/// <summary> | ||
/// GetSourceName: Retrieves the module/assembly name the rule is from. | ||
/// </summary> | ||
public override string GetSourceName() | ||
{ | ||
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
44 changes: 44 additions & 0 deletions
44
Tests/Rules/AvoidUsingAllowUnencryptedAuthentication.tests.ps1
This file contains 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,44 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
BeforeAll { | ||
$settings = @{ | ||
IncludeRules = @('PSAvoidUsingAllowUnencryptedAuthentication') | ||
Rules = @{ | ||
PSAvoidUsingAllowUnencryptedAuthentication = @{ | ||
Enable = $true | ||
} | ||
} | ||
} | ||
|
||
$violationMessage = [regex]::Escape("The insecure AllowUsingUnencryptedAuthentication switch was used. This should be avoided except for compatability with legacy systems.") | ||
} | ||
|
||
Describe "AvoidUsingAllowUnencryptedAuthentication" { | ||
Context "When there are violations" { | ||
It "detects unencrypted authentication violations" { | ||
(Invoke-ScriptAnalyzer -ScriptDefinition 'Invoke-WebRequest foo -AllowUnencryptedAuthentication' -Settings $settings).Count | Should -Be 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'd be happier if these were separate test cases, but this is ok |
||
(Invoke-ScriptAnalyzer -ScriptDefinition 'Invoke-RestMethod foo -AllowUnencryptedAuthentication' -Settings $settings).Count | Should -Be 1 | ||
(Invoke-ScriptAnalyzer -ScriptDefinition 'iwr foo -AllowUnencryptedAuthentication' -Settings $settings).Count | Should -Be 1 | ||
} | ||
|
||
It "has the correct description message" { | ||
MJVL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(Invoke-ScriptAnalyzer -ScriptDefinition 'Invoke-WebRequest foo -AllowUnencryptedAuthentication' -Settings $settings).Message | Should -Match $violationMessage | ||
} | ||
|
||
It "detects arbitrary cmdlets" { | ||
(Invoke-ScriptAnalyzer -ScriptDefinition 'Invoke-CustomWebRequest foo -AllowUnencryptedAuthentication' -Settings $settings).Count | Should -Be 1 | ||
} | ||
|
||
} | ||
|
||
Context "When there are no violations" { | ||
It "does not flag safe usage" { | ||
(Invoke-ScriptAnalyzer -ScriptDefinition 'Invoke-WebRequest foo' -Settings $settings).Count | Should -Be 0 | ||
} | ||
|
||
It "does not flag cases with unrelated parameters" { | ||
(Invoke-ScriptAnalyzer -ScriptDefinition 'Invoke-WebRequest foo -Method Get' -Settings $settings).Count | Should -Be 0 | ||
} | ||
} | ||
} |
This file contains 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,33 @@ | ||
--- | ||
description: Avoid sending credentials and secrets over unencrypted connections | ||
ms.custom: PSSA v1.22.0 | ||
ms.date: 11/06/2022 | ||
ms.topic: reference | ||
title: AvoidUsingAllowUnencryptedAuthentication | ||
--- | ||
# AvoidUsingAllowUnencryptedAuthentication | ||
|
||
**Severity Level: Warning** | ||
|
||
## Description | ||
|
||
Avoid using the AllowUnencryptedAuthentication switch, which sends credentials and secrets over unencrypted connections. | ||
MJVL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This should be avoided except for compatability with legacy systems. | ||
MJVL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## How | ||
|
||
Avoid using the AllowUnencryptedAuthentication switch. | ||
|
||
## Example 1 | ||
|
||
### Wrong | ||
|
||
```powershell | ||
Invoke-WebRequest foo -AllowUnencryptedAuthentication | ||
``` | ||
|
||
### Correct | ||
|
||
```powershell | ||
Invoke-WebRequest foo | ||
``` |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice, didn't know there was this base class to make implementation so much simpler