mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2025-09-17 17:14:18 +02:00
Fixed: Improve Bind Address validation and help text
This commit is contained in:
@@ -0,0 +1,25 @@
|
|||||||
|
using System.Globalization;
|
||||||
|
using FluentAssertions;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using NzbDrone.Common.Extensions;
|
||||||
|
|
||||||
|
namespace NzbDrone.Common.Test.ExtensionTests.StringExtensionTests
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class IsValidIPAddressFixture
|
||||||
|
{
|
||||||
|
[TestCase("192.168.0.1")]
|
||||||
|
[TestCase("::1")]
|
||||||
|
[TestCase("2001:db8:4006:812::200e")]
|
||||||
|
public void should_validate_ip_address(string input)
|
||||||
|
{
|
||||||
|
input.IsValidIpAddress().Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCase("sonarr.tv")]
|
||||||
|
public void should_not_parse_non_ip_address(string input)
|
||||||
|
{
|
||||||
|
input.IsValidIpAddress().Should().BeFalse();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
@@ -231,5 +232,25 @@ namespace NzbDrone.Common.Extensions
|
|||||||
.Replace("'", "%27")
|
.Replace("'", "%27")
|
||||||
.Replace("%7E", "~");
|
.Replace("%7E", "~");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static bool IsValidIpAddress(this string value)
|
||||||
|
{
|
||||||
|
if (!IPAddress.TryParse(value, out var parsedAddress))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parsedAddress.Equals(IPAddress.Parse("255.255.255.255")))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parsedAddress.IsIPv6Multicast)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return parsedAddress.AddressFamily == AddressFamily.InterNetwork || parsedAddress.AddressFamily == AddressFamily.InterNetworkV6;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -54,7 +54,7 @@
|
|||||||
"Backups": "Backups",
|
"Backups": "Backups",
|
||||||
"BeforeUpdate": "Before update",
|
"BeforeUpdate": "Before update",
|
||||||
"BindAddress": "Bind Address",
|
"BindAddress": "Bind Address",
|
||||||
"BindAddressHelpText": "Valid IP4 address or '*' for all interfaces",
|
"BindAddressHelpText": "Valid IP address, localhost or '*' for all interfaces",
|
||||||
"BookSearch": "Book Search",
|
"BookSearch": "Book Search",
|
||||||
"BookSearchTypes": "Book Search Types",
|
"BookSearchTypes": "Book Search Types",
|
||||||
"Branch": "Branch",
|
"Branch": "Branch",
|
||||||
|
@@ -1,30 +1,14 @@
|
|||||||
using System.Net;
|
|
||||||
using System.Net.Sockets;
|
|
||||||
using FluentValidation;
|
using FluentValidation;
|
||||||
using FluentValidation.Validators;
|
using FluentValidation.Validators;
|
||||||
|
using NzbDrone.Common.Extensions;
|
||||||
|
|
||||||
namespace NzbDrone.Core.Validation
|
namespace NzbDrone.Core.Validation
|
||||||
{
|
{
|
||||||
public static class IpValidation
|
public static class IpValidation
|
||||||
{
|
{
|
||||||
public static IRuleBuilderOptions<T, string> ValidIp4Address<T>(this IRuleBuilder<T, string> ruleBuilder)
|
public static IRuleBuilderOptions<T, string> ValidIpAddress<T>(this IRuleBuilder<T, string> ruleBuilder)
|
||||||
{
|
{
|
||||||
return ruleBuilder.Must(x =>
|
return ruleBuilder.Must(x => x.IsValidIpAddress()).WithMessage("Must contain wildcard (*) or a valid IP Address");
|
||||||
{
|
|
||||||
IPAddress parsedAddress;
|
|
||||||
|
|
||||||
if (!IPAddress.TryParse(x, out parsedAddress))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parsedAddress.Equals(IPAddress.Parse("255.255.255.255")))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return parsedAddress.AddressFamily == AddressFamily.InterNetwork;
|
|
||||||
}).WithMessage("Must contain wildcard (*) or a valid IPv4 Address");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IRuleBuilderOptions<T, string> NotListenAllIp4Address<T>(this IRuleBuilder<T, string> ruleBuilder)
|
public static IRuleBuilderOptions<T, string> NotListenAllIp4Address<T>(this IRuleBuilder<T, string> ruleBuilder)
|
||||||
|
@@ -33,9 +33,9 @@ namespace Prowlarr.Api.V1.Config
|
|||||||
_userService = userService;
|
_userService = userService;
|
||||||
|
|
||||||
SharedValidator.RuleFor(c => c.BindAddress)
|
SharedValidator.RuleFor(c => c.BindAddress)
|
||||||
.ValidIp4Address()
|
.ValidIpAddress()
|
||||||
.NotListenAllIp4Address()
|
.NotListenAllIp4Address()
|
||||||
.When(c => c.BindAddress != "*");
|
.When(c => c.BindAddress != "*" && c.BindAddress != "localhost");
|
||||||
|
|
||||||
SharedValidator.RuleFor(c => c.Port).ValidPort();
|
SharedValidator.RuleFor(c => c.Port).ValidPort();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user