Fixes path segment sanitization allowing path separators

This commit is contained in:
BinaryAlley committed 2026-08-24 14:18:49 -07:00
1 parent c82fadc7a2
commit 942db4c90e
12 files changed
+442 -45

No files matched your search

@@ -520,4 +520,241 @@ public class PathServiceTests
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
_mockPathStrategy.DidNotReceive().GetPathRoot(Arg.Any<FileSystemPathId>());
}
[Fact]
public void SanitizeSegment_WithValidName_ShouldReturnTheNameAsASegment()
{
// Arrange
char[] invalidChars = ['<', '>', ':', '"', '|', '?', '*'];
_mockPathStrategy.GetInvalidPathSegmentCharsForPlatform().Returns(invalidChars);
// Act
Result<PathSegment> result = _sut.SanitizeSegment("Valid Name");
// Assert
Assert.False(result.IsFailure);
Assert.Equal("Valid Name", result.Value.Name);
_mockPathStrategy.Received(1).GetInvalidPathSegmentCharsForPlatform();
}
[Fact]
public void SanitizeSegment_WithLeadingAndTrailingWhitespace_ShouldTrimTheName()
{
// Arrange
_mockPathStrategy.GetInvalidPathSegmentCharsForPlatform().Returns(['<', '>', ':', '"', '|', '?', '*']);
// Act
Result<PathSegment> result = _sut.SanitizeSegment(" Valid Name ");
// Assert
Assert.False(result.IsFailure);
Assert.Equal("Valid Name", result.Value.Name);
}
[Fact]
public void SanitizeSegment_WithInvalidCharacters_ShouldReplaceThemWithSpaces()
{
// Arrange
char[] invalidChars = ['<', '>', ':', '"', '|', '?', '*'];
_mockPathStrategy.GetInvalidPathSegmentCharsForPlatform().Returns(invalidChars);
// Act
Result<PathSegment> result = _sut.SanitizeSegment("Bad<Name>");
// Assert
Assert.False(result.IsFailure);
Assert.Equal("Bad Name", result.Value.Name);
}
[Fact]
public void SanitizeSegment_WithConsecutiveWhitespace_ShouldCollapseItIntoASingleSpace()
{
// Arrange
_mockPathStrategy.GetInvalidPathSegmentCharsForPlatform().Returns(['<', '>', ':', '"', '|', '?', '*']);
// Act
Result<PathSegment> result = _sut.SanitizeSegment("Name with spaces");
// Assert
Assert.False(result.IsFailure);
Assert.Equal("Name with spaces", result.Value.Name);
}
[Fact]
public void SanitizeSegment_WithDotSegment_ShouldReturnInvalidPath()
{
// Arrange
_mockPathStrategy.GetInvalidPathSegmentCharsForPlatform().Returns(['<', '>', ':', '"', '|', '?', '*']);
// Act
Result<PathSegment> result = _sut.SanitizeSegment(".");
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
}
[Fact]
public void SanitizeSegment_WithDotDotSegment_ShouldReturnInvalidPath()
{
// Arrange
_mockPathStrategy.GetInvalidPathSegmentCharsForPlatform().Returns(['<', '>', ':', '"', '|', '?', '*']);
// Act
Result<PathSegment> result = _sut.SanitizeSegment("..");
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
}
[Fact]
public void SanitizeSegment_WithForwardSlash_ShouldReturnInvalidPath()
{
// Arrange
// the forward slash is not an invalid character for a segment on Unix-like platforms, so only the path service can reject it
_mockPathStrategy.GetInvalidPathSegmentCharsForPlatform().Returns(['\0']);
// Act
Result<PathSegment> result = _sut.SanitizeSegment("a/b");
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
}
[Fact]
public void SanitizeSegment_WithBackslash_ShouldReturnInvalidPath()
{
// Arrange
// the backslash is not an invalid character for a segment on Windows platforms, so only the path service can reject it
_mockPathStrategy.GetInvalidPathSegmentCharsForPlatform().Returns(['<', '>', '"', '|', '?', '*', ':']);
// Act
Result<PathSegment> result = _sut.SanitizeSegment("a\\b");
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
}
[Theory]
[InlineData("../..")] // parent directory escape
[InlineData("a/../b")] // embedded parent directory escape
[InlineData("..\\..")] // Windows parent directory escape
[InlineData("a\\..\\b")] // embedded Windows parent directory escape
public void SanitizeSegment_WithPathTraversal_ShouldReturnInvalidPath(string name)
{
// Arrange
_mockPathStrategy.GetInvalidPathSegmentCharsForPlatform().Returns(['\0']);
// Act
Result<PathSegment> result = _sut.SanitizeSegment(name);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
}
[Fact]
public void SanitizeSegment_WithAColon_WhenTheColonIsValidForThePlatform_ShouldKeepTheColon()
{
// Arrange
// the colon is not an invalid character for a segment on Unix-like platforms, so it must be preserved
_mockPathStrategy.GetInvalidPathSegmentCharsForPlatform().Returns(['\0']);
// Act
Result<PathSegment> result = _sut.SanitizeSegment("Name:WithColon");
// Assert
Assert.False(result.IsFailure);
Assert.Equal("Name:WithColon", result.Value.Name);
}
[Fact]
public void SanitizeSegment_WithAColon_WhenTheColonIsInvalidForThePlatform_ShouldReplaceItWithASpace()
{
// Arrange
// the colon is an invalid character for a segment on Windows platforms, so it must be replaced
_mockPathStrategy.GetInvalidPathSegmentCharsForPlatform().Returns(['<', '>', '"', '|', '?', '*', ':']);
// Act
Result<PathSegment> result = _sut.SanitizeSegment("Name:WithColon");
// Assert
Assert.False(result.IsFailure);
Assert.Equal("Name WithColon", result.Value.Name);
}
[Fact]
public void SanitizeSegment_WithEmptyName_ShouldReturnNameCannotBeEmpty()
{
// Arrange
_mockPathStrategy.GetInvalidPathSegmentCharsForPlatform().Returns(['<', '>', ':', '"', '|', '?', '*']);
// Act
Result<PathSegment> result = _sut.SanitizeSegment(string.Empty);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.FileSystemManagement.NameCannotBeEmpty, result.FirstError);
_mockPathStrategy.DidNotReceive().GetInvalidPathSegmentCharsForPlatform();
}
[Fact]
public void SanitizeSegment_WithWhitespaceOnlyName_ShouldReturnNameCannotBeEmpty()
{
// Arrange
_mockPathStrategy.GetInvalidPathSegmentCharsForPlatform().Returns(['<', '>', ':', '"', '|', '?', '*']);
// Act
Result<PathSegment> result = _sut.SanitizeSegment(" ");
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.FileSystemManagement.NameCannotBeEmpty, result.FirstError);
}
[Fact]
public void SanitizeSegment_WithNameMadeOnlyOfInvalidCharacters_ShouldReturnInvalidPath()
{
// Arrange
_mockPathStrategy.GetInvalidPathSegmentCharsForPlatform().Returns(['<', '>', ':', '"', '|', '?', '*']);
// Act
Result<PathSegment> result = _sut.SanitizeSegment("***");
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
}
[Fact]
public void SanitizeSegment_WithNameLongerThanTheMaximumLength_ShouldCapTheSegment()
{
// Arrange
_mockPathStrategy.GetInvalidPathSegmentCharsForPlatform().Returns(['<', '>', ':', '"', '|', '?', '*']);
// Act
Result<PathSegment> result = _sut.SanitizeSegment(new string('a', 150));
// Assert
Assert.False(result.IsFailure);
Assert.Equal(100, result.Value.Name.Length);
Assert.Equal(new string('a', 100), result.Value.Name);
}
[Fact]
public void SanitizeSegment_WithNameLongerThanTheMaximumLengthAndTrailingWhitespace_ShouldTrimAfterCapping()
{
// Arrange
_mockPathStrategy.GetInvalidPathSegmentCharsForPlatform().Returns(['<', '>', ':', '"', '|', '?', '*']);
// Act
Result<PathSegment> result = _sut.SanitizeSegment(new string('a', 110) + new string(' ', 50));
// Assert
Assert.False(result.IsFailure);
Assert.Equal(100, result.Value.Name.Length);
}
}
@@ -507,6 +507,40 @@ public class UnixPathStrategyTests
Assert.Equal(result1, result2);
}
[Fact]
public void GetInvalidPathSegmentCharsForPlatform_WhenCalled_ShouldReturnOnlyNullCharacter()
{
// Act
char[] result = _sut.GetInvalidPathSegmentCharsForPlatform();
// Assert
Assert.Single(result);
Assert.Contains('\0', result);
}
[Fact]
public void GetInvalidPathSegmentCharsForPlatform_WhenCalled_ShouldNotContainTheColon()
{
// Act
char[] result = _sut.GetInvalidPathSegmentCharsForPlatform();
// Assert
Assert.DoesNotContain(':', result);
Assert.DoesNotContain('/', result);
Assert.DoesNotContain('\\', result);
}
[Fact]
public void GetInvalidPathSegmentCharsForPlatform_WhenCalled_ShouldReturnSameResultOnMultipleCalls()
{
// Act
char[] result1 = _sut.GetInvalidPathSegmentCharsForPlatform();
char[] result2 = _sut.GetInvalidPathSegmentCharsForPlatform();
// Assert
Assert.Equal(result1, result2);
}
[Fact]
public void GetPathRoot_WithValidUnixPath_ShouldReturnRootSegment()
{
@@ -625,6 +625,39 @@ public class WindowsPathStrategyTests
Assert.Equal(result1, result2);
}
[Fact]
public void GetInvalidPathSegmentCharsForPlatform_WhenCalled_ShouldReturnExpectedCharacters()
{
// Act
char[] result = _sut.GetInvalidPathSegmentCharsForPlatform();
// Assert
Assert.Equal(['<', '>', '"', '/', '|', '?', '*', ':'], result);
}
[Fact]
public void GetInvalidPathSegmentCharsForPlatform_WhenCalled_ShouldContainTheColon()
{
// Act
char[] result = _sut.GetInvalidPathSegmentCharsForPlatform();
// Assert
Assert.Contains(':', result);
Assert.DoesNotContain('\\', result);
Assert.DoesNotContain('\0', result);
}
[Fact]
public void GetInvalidPathSegmentCharsForPlatform_WhenCalled_ShouldReturnSameResultOnMultipleCalls()
{
// Act
char[] result1 = _sut.GetInvalidPathSegmentCharsForPlatform();
char[] result2 = _sut.GetInvalidPathSegmentCharsForPlatform();
// Assert
Assert.Equal(result1, result2);
}
[Fact]
public void GetPathRoot_WithValidWindowsPath_ShouldReturnRootSegment()
{
@@ -1,6 +1,8 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.Infrastructure.Models.DTO.Themes;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services;
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
using Lumina.Infrastructure.Common.Models.DTO.Configuration;
using Lumina.Infrastructure.Core.Themes;
using Lumina.Infrastructure.Fixtures.Common.Models.DTO.Configuration;
@@ -55,26 +57,6 @@ public class ThemeServiceTests : IDisposable
_sut = CreateSut(_options);
}
/// <summary>
/// Cleans up the temporary storage used by the tests.
/// </summary>
public void Dispose()
{
try
{
if (Directory.Exists(_testRootPath))
Directory.Delete(_testRootPath, recursive: true);
}
catch (IOException)
{
// best effort cleanup of the per-test temp directory
}
catch (UnauthorizedAccessException)
{
// best effort cleanup of the per-test temp directory
}
}
[Fact]
public void MaxArchiveBytes_WhenConfigured_ShouldReturnConfiguredValue()
{
@@ -1151,6 +1133,26 @@ public class ThemeServiceTests : IDisposable
Assert.False(_sut.HasThemePack("bundled-theme"));
}
/// <summary>
/// Cleans up the temporary storage used by the tests.
/// </summary>
public void Dispose()
{
try
{
if (Directory.Exists(_testRootPath))
Directory.Delete(_testRootPath, recursive: true);
}
catch (IOException)
{
// best effort cleanup of the per-test temp directory
}
catch (UnauthorizedAccessException)
{
// best effort cleanup of the per-test temp directory
}
}
/// <summary>
/// Creates a valid theme pack that declares the default, home and library templates and their files.
/// </summary>
@@ -1207,6 +1209,9 @@ public class ThemeServiceTests : IDisposable
private static ThemeService CreateSut(ThemeEngineOptionsDto options)
{
ILogger<ThemeService> logger = Substitute.For<ILogger<ThemeService>>();
return new ThemeService(Options.Create(options), logger);
IPathService pathService = Substitute.For<IPathService>();
pathService.SanitizeSegment(Arg.Any<string>())
.Returns(callInfo => PathSegment.Create(callInfo.Arg<string>(), isDirectory: true, isDrive: false));
return new ThemeService(Options.Create(options), logger, pathService);
}
}