Skip to content

Commit

Permalink
Fixes empty string handling in Azure.NSG.LateralTraversal #3130 (#3158)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Oct 31, 2024
1 parent 070cca7 commit d02c026
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ See [upgrade notes][1] for helpful information when upgrading from previous vers

## Unreleased

- Bug fixes:
- Fixed evaluation of `Azure.NSG.LateralTraversal` with empty string properties by @BernieWhite.
[#3130](https://github.com/Azure/PSRule.Rules.Azure/issues/3130)

## v1.40.0-B0029 (pre-release)

What's changed since v1.39.3:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace PSRule.Rules.Azure.Data.Network;
/// <summary>
/// A basic implementation of an evaluator for checking NSG rules.
/// </summary>
internal sealed partial class NetworkSecurityGroupEvaluator : INetworkSecurityGroupEvaluator
internal sealed class NetworkSecurityGroupEvaluator : INetworkSecurityGroupEvaluator
{
private const string PROPERTIES = "properties";
private const string DIRECTION = "direction";
Expand Down Expand Up @@ -76,7 +76,7 @@ private static SecurityRule GetRule(PSObject o)
if (o.TryProperty(propertyName, out string[] value) && value.Length > 0)
return value;

return o.TryProperty(propertyName, out string s) && s != ANY ? [s] : null;
return o.TryProperty(propertyName, out string s) && s != ANY && !string.IsNullOrEmpty(s) ? [s] : null;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Management.Automation;

namespace PSRule.Rules.Azure.Data.Network;

#nullable enable

public sealed class NetworkSecurityGroupEvaluatorTests
{
[Fact]
public void Outbound_WhenMatchingRule_ShouldReturnAccess()
{
var evaluator = new NetworkSecurityGroupEvaluator();
evaluator.With([Rule(access: "Deny", destinationAddressPrefix: "VirtualNetwork", destinationPortRanges: ["3389"])]);

Assert.Equal(Access.Deny, evaluator.Outbound("VirtualNetwork", 3389));
}

[Fact]
public void With_WhenDestinationPortRangeIsEmptyString_ShouldUseDestinationPortRanges()
{
var evaluator = new NetworkSecurityGroupEvaluator();
evaluator.With([Rule(access: "Deny", destinationPortRange: "", destinationPortRanges: ["80", "443"])]);

Assert.Equal(Access.Deny, evaluator.Outbound("Virtual Network", 443));
}

#region Helper methods

private static PSObject Rule(string direction = "Outbound", string access = "Allow", string protocol = "Tcp", string? destinationAddressPrefix = default, string? destinationPortRange = default, string[]? destinationPortRanges = default)
{
var properties = new PSObject();
properties.Properties.Add(new PSNoteProperty("direction", direction));
properties.Properties.Add(new PSNoteProperty("access", access));
properties.Properties.Add(new PSNoteProperty("protocol", protocol));
properties.Properties.Add(new PSNoteProperty("destinationAddressPrefix", destinationAddressPrefix));

if (destinationPortRange != null)
properties.Properties.Add(new PSNoteProperty("destinationPortRange", destinationPortRange));

if (destinationPortRanges != null)
properties.Properties.Add(new PSNoteProperty("destinationPortRanges", destinationPortRanges));

var result = new PSObject();
result.Properties.Add(new PSNoteProperty("properties", properties));
return result;
}

#endregion Helper methods
}

#nullable restore

0 comments on commit d02c026

Please sign in to comment.