New: Validate that folders in paths don't start or end with a space

Closes #6709
This commit is contained in:
Mark McDowall 2024-04-17 17:21:35 -07:00
parent b81c3ee4a8
commit 3b4a2c2653
3 changed files with 48 additions and 2 deletions

View File

@ -3,6 +3,7 @@ using System.IO;
using FluentAssertions; using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common.Disk;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Test.Common; using NzbDrone.Test.Common;
@ -335,5 +336,30 @@ namespace NzbDrone.Common.Test
result[2].Should().Be(@"TV"); result[2].Should().Be(@"TV");
result[3].Should().Be(@"Series Title"); result[3].Should().Be(@"Series Title");
} }
[TestCase(@"C:\Test\")]
[TestCase(@"C:\Test")]
[TestCase(@"C:\Test\TV\")]
[TestCase(@"C:\Test\TV")]
public void IsPathValid_should_be_true(string path)
{
path.AsOsAgnostic().IsPathValid(PathValidationType.CurrentOs).Should().BeTrue();
}
[TestCase(@"C:\Test \")]
[TestCase(@"C:\Test ")]
[TestCase(@"C:\ Test\")]
[TestCase(@"C:\ Test")]
[TestCase(@"C:\Test \TV")]
[TestCase(@"C:\ Test\TV")]
[TestCase(@"C:\Test \TV\")]
[TestCase(@"C:\ Test\TV\")]
[TestCase(@" C:\Test\TV\")]
[TestCase(@" C:\Test\TV")]
public void IsPathValid_should_be_false(string path)
{
path.AsOsAgnostic().IsPathValid(PathValidationType.CurrentOs).Should().BeFalse();
}
} }
} }

View File

@ -154,6 +154,23 @@ namespace NzbDrone.Common.Extensions
return false; return false;
} }
if (path.Trim() != path)
{
return false;
}
var directoryInfo = new DirectoryInfo(path);
while (directoryInfo != null)
{
if (directoryInfo.Name.Trim() != directoryInfo.Name)
{
return false;
}
directoryInfo = directoryInfo.Parent;
}
if (validationType == PathValidationType.AnyOs) if (validationType == PathValidationType.AnyOs)
{ {
return IsPathValidForWindows(path) || IsPathValidForNonWindows(path); return IsPathValidForWindows(path) || IsPathValidForNonWindows(path);
@ -291,6 +308,11 @@ namespace NzbDrone.Common.Extensions
return processName; return processName;
} }
public static string CleanPath(this string path)
{
return Path.Join(path.Split(Path.DirectorySeparatorChar).Select(s => s.Trim()).ToArray());
}
public static string GetAppDataPath(this IAppFolderInfo appFolderInfo) public static string GetAppDataPath(this IAppFolderInfo appFolderInfo)
{ {
return appFolderInfo.AppDataFolder; return appFolderInfo.AppDataFolder;

View File

@ -92,8 +92,6 @@ namespace Sonarr.Api.V3.Series
.When(s => s.Path.IsNullOrWhiteSpace()); .When(s => s.Path.IsNullOrWhiteSpace());
PostValidator.RuleFor(s => s.Title).NotEmpty(); PostValidator.RuleFor(s => s.Title).NotEmpty();
PostValidator.RuleFor(s => s.TvdbId).GreaterThan(0).SetValidator(seriesExistsValidator); PostValidator.RuleFor(s => s.TvdbId).GreaterThan(0).SetValidator(seriesExistsValidator);
PutValidator.RuleFor(s => s.Path).IsValidPath();
} }
[HttpGet] [HttpGet]