Skip to content

Commit f5dd636

Browse files
authored
Feature: Use Regex to validate OID (#2205)
* Feature: Use Regex to validate OID * Docs: #2205
1 parent 6bad148 commit f5dd636

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

Source/NETworkManager.Utilities/RegexHelper.cs

+3
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,7 @@ public static class RegexHelper
8383

8484
// Match a number (like 12, 12.4, 12,3)
8585
public const string NumberRegex = @"^\d+((\.|,)\d+)?$";
86+
87+
// Match an SNMP OID (like 1.3.6.1)
88+
public const string SNMOIODRegex = @"^[012]\.(?:[0-9]|[1-3][0-9])(\.\d+)*$";
8689
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using Lextm.SharpSnmpLib;
1+
using NETworkManager.Utilities;
22
using System.Globalization;
3+
using System.Text.RegularExpressions;
34
using System.Windows.Controls;
45

56
namespace NETworkManager.Validators;
@@ -10,26 +11,19 @@ public class SNMPOIDValidator : ValidationRule
1011

1112
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
1213
{
13-
// Use SharpSNMP new ObjectIdentifiert to validate oid
14-
try
15-
{
16-
var oidValue = (value as string).Replace(" ", "");
14+
var oidValue = (value as string).Replace(" ", "");
1715

18-
if (Wrapper.Mode == Models.Network.SNMPMode.Get && oidValue.Contains(';'))
16+
if (Wrapper.Mode == Models.Network.SNMPMode.Get && oidValue.Contains(';'))
17+
{
18+
foreach (var oid in oidValue.Split(';'))
1919
{
20-
foreach (var oid in oidValue.Split(';'))
21-
_ = new ObjectIdentifier(oid);
22-
23-
return ValidationResult.ValidResult;
20+
if (!Regex.IsMatch(oid, RegexHelper.SNMOIODRegex))
21+
return new ValidationResult(false, Localization.Resources.Strings.EnterValidOID);
2422
}
25-
26-
_ = new ObjectIdentifier(oidValue);
27-
}
28-
catch (System.ArgumentException)
29-
{
30-
return new ValidationResult(false, Localization.Resources.Strings.EnterValidOID);
23+
24+
return ValidationResult.ValidResult;
3125
}
3226

33-
return ValidationResult.ValidResult;
27+
return Regex.IsMatch(oidValue, RegexHelper.SNMOIODRegex) ? ValidationResult.ValidResult : new ValidationResult(false, Localization.Resources.Strings.EnterValidOID);
3428
}
3529
}

docs/Changelog/next-release.md

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Release date: **xx.xx.2023**
4040
- Get request can query multiple OIDs at once now [#2167](https://github.com/BornToBeRoot/NETworkManager/pull/2167){:target="\_blank"}
4141
- SHA256, SHA384 and SHA512 for SNMPv3 auth added [#2166](https://github.com/BornToBeRoot/NETworkManager/pull/2166){:target="\_blank"}
4242
- AES192 and AES256 for SNMPv3 privacy added [#2166](https://github.com/BornToBeRoot/NETworkManager/pull/2166){:target="\_blank"}
43+
- Use Regex to validate OID [#2205](https://github.com/BornToBeRoot/NETworkManager/pull/2205){:target="\_blank"}
4344
- Connections
4445
- Automatically refresh is now enabled if it was enabled on the last exit [#2116](https://github.com/BornToBeRoot/NETworkManager/pull/2116){:target="\_blank"}
4546
- Listeners

0 commit comments

Comments
 (0)