mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 10:11:10 +00:00
Merge branch 'bugfix/fix-path-sanitization' into feature/add-artwork-provider-and-metadata-aggregation
This commit is contained in:
commit
deada87e56
12 files changed
+442
-45
No files matched your search
+7
@@ -59,6 +59,13 @@ public interface IPathService
|
||||
/// <returns>A collection of characters that are invalid in the context of paths.</returns>
|
||||
char[] GetInvalidPathCharsForPlatform();
|
||||
|
||||
/// <summary>
|
||||
/// Sanitizes a human-readable <paramref name="name"/> into a safe path segment.
|
||||
/// </summary>
|
||||
/// <param name="name">The human-readable name to sanitize.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing the sanitized path segment, or an error.</returns>
|
||||
Result<PathSegment> SanitizeSegment(string name);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the root portion of the given path.
|
||||
/// </summary>
|
||||
|
||||
+49
@@ -3,7 +3,9 @@ using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Platform;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services;
|
||||
@@ -13,6 +15,8 @@ namespace Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.
|
||||
/// </summary>
|
||||
public class PathService : IPathService
|
||||
{
|
||||
private const int MAX_DIRECTORY_SEGMENT_LENGTH = 100;
|
||||
|
||||
private readonly IPlatformContext _platformContext;
|
||||
|
||||
/// <summary>
|
||||
@@ -110,6 +114,51 @@ public class PathService : IPathService
|
||||
return _platformContext.PathStrategy.GetInvalidPathCharsForPlatform();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sanitizes a human-readable <paramref name="name"/> into a safe path segment, replacing the characters that are invalid for the current platform, and rejecting the segments that could escape the directory.
|
||||
/// </summary>
|
||||
/// <param name="name">The human-readable name to sanitize.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing the sanitized path segment, or an error.</returns>
|
||||
public Result<PathSegment> SanitizeSegment(string name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
return Errors.FileSystemManagement.NameCannotBeEmpty;
|
||||
|
||||
char[] invalidChars = _platformContext.PathStrategy.GetInvalidPathSegmentCharsForPlatform();
|
||||
char[] sanitizedChars = [.. name.Trim().Select(character => Array.IndexOf(invalidChars, character) >= 0 ? ' ' : character)];
|
||||
|
||||
// collapse the consecutive whitespace into a single space, so that the segment does not contain ragged spacing
|
||||
List<char> collapsedChars = [];
|
||||
bool previousWasWhitespace = false;
|
||||
foreach (char character in sanitizedChars)
|
||||
{
|
||||
if (char.IsWhiteSpace(character))
|
||||
{
|
||||
if (!previousWasWhitespace)
|
||||
collapsedChars.Add(' ');
|
||||
previousWasWhitespace = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
collapsedChars.Add(character);
|
||||
previousWasWhitespace = false;
|
||||
}
|
||||
}
|
||||
|
||||
string sanitized = new string([.. collapsedChars]).Trim();
|
||||
if (string.Equals(sanitized, ".", StringComparison.Ordinal) || string.Equals(sanitized, "..", StringComparison.Ordinal))
|
||||
return Errors.FileSystemManagement.InvalidPath;
|
||||
// a segment must never contain a path separator, otherwise the segment could escape its directory or nest additional directories
|
||||
if (sanitized.Contains('/') || sanitized.Contains('\\'))
|
||||
return Errors.FileSystemManagement.InvalidPath;
|
||||
if (sanitized.Length > MAX_DIRECTORY_SEGMENT_LENGTH)
|
||||
sanitized = sanitized[..MAX_DIRECTORY_SEGMENT_LENGTH].TrimEnd();
|
||||
if (sanitized.Length == 0)
|
||||
return Errors.FileSystemManagement.InvalidPath;
|
||||
|
||||
return PathSegment.Create(sanitized, isDirectory: true, isDrive: false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the root portion of the given path.
|
||||
/// </summary>
|
||||
|
||||
+7
-1
@@ -11,7 +11,7 @@ namespace Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.
|
||||
/// </summary>
|
||||
public interface IPathStrategy
|
||||
{
|
||||
public char PathSeparator { get; }
|
||||
char PathSeparator { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Checks if <paramref name="path"/> is a valid path.
|
||||
@@ -56,6 +56,12 @@ public interface IPathStrategy
|
||||
/// <returns>A collection of characters that are invalid in the context of paths</returns>
|
||||
char[] GetInvalidPathCharsForPlatform();
|
||||
|
||||
/// <summary>
|
||||
/// Returns a collection of characters that are invalid for a single path segment.
|
||||
/// </summary>
|
||||
/// <returns>A collection of characters that are invalid in the context of a single path segment.</returns>
|
||||
char[] GetInvalidPathSegmentCharsForPlatform();
|
||||
|
||||
/// <summary>
|
||||
/// Returns the root portion of the given path.
|
||||
/// </summary>
|
||||
|
||||
+9
@@ -154,6 +154,15 @@ public class UnixPathStrategy : IUnixPathStrategy
|
||||
return ['\0'];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a collection of characters that are invalid for a single path segment.
|
||||
/// </summary>
|
||||
/// <returns>A collection of characters that are invalid in the context of a single path segment.</returns>
|
||||
public char[] GetInvalidPathSegmentCharsForPlatform()
|
||||
{
|
||||
return ['\0'];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the root portion of the given path.
|
||||
/// </summary>
|
||||
|
||||
+11
-2
@@ -113,7 +113,7 @@ public class WindowsPathStrategy : IWindowsPathStrategy
|
||||
// handle UNC path
|
||||
yield return PathSegment.Create(@"\\", isDirectory: false, isDrive: true);
|
||||
|
||||
string[] segments = path.Path[2..].Split(new[] { PathSeparator }, StringSplitOptions.RemoveEmptyEntries);
|
||||
string[] segments = path.Path[2..].Split([PathSeparator], StringSplitOptions.RemoveEmptyEntries);
|
||||
for (int i = 0; i < segments.Length; i++)
|
||||
yield return CreatePathSegment(segments[i], i, segments.Length);
|
||||
}
|
||||
@@ -122,7 +122,7 @@ public class WindowsPathStrategy : IWindowsPathStrategy
|
||||
// the drive segment
|
||||
yield return PathSegment.Create(path.Path[..2], isDirectory: false, isDrive: true);
|
||||
// extract the other segments
|
||||
string[] segments = path.Path[3..].Split(new[] { PathSeparator }, StringSplitOptions.RemoveEmptyEntries);
|
||||
string[] segments = path.Path[3..].Split([PathSeparator], StringSplitOptions.RemoveEmptyEntries);
|
||||
for (int i = 0; i < segments.Length; i++)
|
||||
{
|
||||
string segment = segments[i];
|
||||
@@ -190,6 +190,15 @@ public class WindowsPathStrategy : IWindowsPathStrategy
|
||||
return ['<', '>', '"', '/', '|', '?', '*'];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a collection of characters that are invalid for a single path segment.
|
||||
/// </summary>
|
||||
/// <returns>A collection of characters that are invalid in the context of a single path segment.</returns>
|
||||
public char[] GetInvalidPathSegmentCharsForPlatform()
|
||||
{
|
||||
return ['<', '>', '"', '/', '|', '?', '*', ':'];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the root portion of the given path.
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user