From 421e4c01cee5d001d90bf948b51a1b7032c53990 Mon Sep 17 00:00:00 2001 From: Dihan Britz Date: Sun, 8 Mar 2026 21:53:13 +0200 Subject: [PATCH 1/3] Potential fix for code scanning alert no. 32: Complex condition Complex return condition simplified through variable assignment Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> --- src/TypeGuard.Core/Rules/IpAddressRules.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/TypeGuard.Core/Rules/IpAddressRules.cs b/src/TypeGuard.Core/Rules/IpAddressRules.cs index bbfcea4..6702aa6 100644 --- a/src/TypeGuard.Core/Rules/IpAddressRules.cs +++ b/src/TypeGuard.Core/Rules/IpAddressRules.cs @@ -44,10 +44,15 @@ public bool IsValid(IPAddress value) byte[] b = value.GetAddressBytes(); - return b[0] == 10 - || b[0] == 172 && b[1] >= 16 && b[1] <= 31 - || b[0] == 192 && b[1] == 168 - || b[0] == 169 && b[1] == 254; + bool is10Range = b[0] == 10; + bool is172Range = b[0] == 172 && b[1] >= 16 && b[1] <= 31; + bool is192Range = b[0] == 192 && b[1] == 168; + bool isApipaRange = b[0] == 169 && b[1] == 254; + + return is10Range + || is172Range + || is192Range + || isApipaRange; } /// From 42b5158809d098918f726f16d380dbdcdf2baf8f Mon Sep 17 00:00:00 2001 From: DoubledDoge Date: Sun, 8 Mar 2026 22:15:03 +0200 Subject: [PATCH 2/3] fix: Updated for main branch --- .editorconfig | 1 - 1 file changed, 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index a556da0..b8535d2 100644 --- a/.editorconfig +++ b/.editorconfig @@ -10,7 +10,6 @@ indent_style = space # Code files [*.{cs,csx}] indent_size = 4 -end_of_line = lf # XML project files [*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}] From 9a5ed166650d50c28f5db0882e95e8a5ca39e368 Mon Sep 17 00:00:00 2001 From: DoubledDoge Date: Sun, 8 Mar 2026 22:30:02 +0200 Subject: [PATCH 3/3] refactor: Extracted private IP address check into IpAddressHelper --- src/TypeGuard.Core/Rules/IpAddressRules.cs | 54 ++++++++-------------- 1 file changed, 20 insertions(+), 34 deletions(-) diff --git a/src/TypeGuard.Core/Rules/IpAddressRules.cs b/src/TypeGuard.Core/Rules/IpAddressRules.cs index 6702aa6..98ccab4 100644 --- a/src/TypeGuard.Core/Rules/IpAddressRules.cs +++ b/src/TypeGuard.Core/Rules/IpAddressRules.cs @@ -37,22 +37,8 @@ public class PrivateIpRule(string? customMessage = null) : IValidatorRule public bool IsValid(IPAddress value) { - if (value.AddressFamily != AddressFamily.InterNetwork) - { - return false; - } - - byte[] b = value.GetAddressBytes(); - - bool is10Range = b[0] == 10; - bool is172Range = b[0] == 172 && b[1] >= 16 && b[1] <= 31; - bool is192Range = b[0] == 192 && b[1] == 168; - bool isApipaRange = b[0] == 169 && b[1] == 254; - - return is10Range - || is172Range - || is192Range - || isApipaRange; + return value.AddressFamily == AddressFamily.InterNetwork + && IpAddressHelper.IsPrivateIpv4(value.GetAddressBytes()); } /// @@ -129,24 +115,11 @@ public class PublicIpRule(string? customMessage = null) : IValidatorRule public bool IsValid(IPAddress value) { - if (IPAddress.IsLoopback(value)) - { - return false; - } - - if (value.AddressFamily != AddressFamily.InterNetwork) - { - return true; - } - - byte[] b = value.GetAddressBytes(); - - return !( - b[0] == 10 - || b[0] == 172 && b[1] >= 16 && b[1] <= 31 - || b[0] == 192 && b[1] == 168 - || b[0] == 169 && b[1] == 254 - ); + return !IPAddress.IsLoopback(value) + && ( + value.AddressFamily != AddressFamily.InterNetwork + || !IpAddressHelper.IsPrivateIpv4(value.GetAddressBytes()) + ); } /// @@ -290,3 +263,16 @@ private static HashSet BuildSet(IEnumerable values, string return set.Count == 0 ? throw new ArgumentException("Cannot be empty.", paramName) : set; } } + +internal static class IpAddressHelper +{ + internal static bool IsPrivateIpv4(byte[] addressBytes) + { + bool is10Range = addressBytes[0] == 10; + bool is172Range = addressBytes[0] == 172 && addressBytes[1] >= 16 && addressBytes[1] <= 31; + bool is192Range = addressBytes[0] == 192 && addressBytes[1] == 168; + bool isApipaRange = addressBytes[0] == 169 && addressBytes[1] == 254; + + return is10Range || is172Range || is192Range || isApipaRange; + } +}