Skip to content

Commit

Permalink
Fixed: Improve Bind Address validation and help text
Browse files Browse the repository at this point in the history
Closes #2025

(cherry picked from commit 6bdeafcf8c78e145595f52e885356be1210abe91)
  • Loading branch information
markus101 authored and Qstick committed Jan 24, 2023
1 parent cef06d1 commit 7dc061c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
22 changes: 22 additions & 0 deletions src/NzbDrone.Common/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -376,5 +378,25 @@ public static string EncodeRFC3986(this string value)
.Replace("'", "%27")
.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;
}
}
}
2 changes: 1 addition & 1 deletion src/NzbDrone.Core/Localization/Core/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"BackupRetentionHelpText": "Automatic backups older than the retention period will be cleaned up automatically",
"Backups": "Backups",
"BindAddress": "Bind Address",
"BindAddressHelpText": "Valid IPv4 address or '*' for all interfaces",
"BindAddressHelpText": "Valid IP address, localhost or '*' for all interfaces",
"BindAddressHelpTextWarning": "Requires restart to take effect",
"Blocklist": "Blocklist",
"BlocklistHelpText": "Prevents Readarr from automatically grabbing these files again",
Expand Down
22 changes: 3 additions & 19 deletions src/NzbDrone.Core/Validation/IpValidation.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
using System.Net;
using System.Net.Sockets;
using FluentValidation;
using FluentValidation.Validators;
using NzbDrone.Common.Extensions;

namespace NzbDrone.Core.Validation
{
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 =>
{
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");
return ruleBuilder.Must(x => x.IsValidIpAddress()).WithMessage("Must contain wildcard (*) or a valid IP Address");
}

public static IRuleBuilderOptions<T, string> NotListenAllIp4Address<T>(this IRuleBuilder<T, string> ruleBuilder)
Expand Down
4 changes: 2 additions & 2 deletions src/Readarr.Api.V1/Config/HostConfigController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public class HostConfigController : RestController<HostConfigResource>
_userService = userService;

SharedValidator.RuleFor(c => c.BindAddress)
.ValidIp4Address()
.ValidIpAddress()
.NotListenAllIp4Address()
.When(c => c.BindAddress != "*");
.When(c => c.BindAddress != "*" && c.BindAddress != "localhost");

SharedValidator.RuleFor(c => c.Port).ValidPort();

Expand Down

0 comments on commit 7dc061c

Please sign in to comment.