From 78561f600754241efdf06c0fcee5ffdbdccda1b6 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 3 Jun 2026 18:56:06 -0500 Subject: wip --- .../Common/Validation/AbsolutePathAttribute.cs | 36 +++++++++++++++++++--- .../Common/Validation/RelativePathAttribute.cs | 36 ++++++++++++++++++++-- src/slskd/Core/Options.cs | 8 ++--- 3 files changed, 69 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/slskd/Common/Validation/AbsolutePathAttribute.cs b/src/slskd/Common/Validation/AbsolutePathAttribute.cs index eb1c9069..8ee486c5 100644 --- a/src/slskd/Common/Validation/AbsolutePathAttribute.cs +++ b/src/slskd/Common/Validation/AbsolutePathAttribute.cs @@ -40,16 +40,19 @@ namespace slskd.Validation /// public class AbsolutePathAttribute : ValidationAttribute { - public AbsolutePathAttribute() + public AbsolutePathAttribute(bool platformAgnostic = false) { + PlatformAgnostic = platformAgnostic; } - public AbsolutePathAttribute(OSPlatform? os = null) + public AbsolutePathAttribute(bool platformAgnostic = false, OSPlatform? os = null) { + PlatformAgnostic = platformAgnostic; OS = os; } public OSPlatform? OS { get; } + public bool PlatformAgnostic { get; } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { @@ -57,9 +60,34 @@ namespace slskd.Validation { var path = value.ToString(); - if (!FileSafety.IsPathAbsolute(path, os: OS)) + if (OS.HasValue) { - return new ValidationResult($"The {validationContext.DisplayName} field must be an absolute file path."); + if (!FileSafety.IsPathAbsolute(path, os: OS)) + { + return new ValidationResult($"The {validationContext.DisplayName} field must be an absolute file path."); + } + } + else + { + if (!PlatformAgnostic) + { + if (!FileSafety.IsPathAbsolute(path, os: OS)) // this OS + { + return new ValidationResult($"The {validationContext.DisplayName} field must be an absolute file path."); + } + } + else + { + if (!FileSafety.IsPathAbsolute(path, os: OSPlatform.Linux)) + { + return new ValidationResult($"The {validationContext.DisplayName} field must be an absolute file path."); + } + + if (!FileSafety.IsPathAbsolute(path, os: OSPlatform.Windows)) + { + return new ValidationResult($"The {validationContext.DisplayName} field must be an absolute file path."); + } + } } } diff --git a/src/slskd/Common/Validation/RelativePathAttribute.cs b/src/slskd/Common/Validation/RelativePathAttribute.cs index f465ba98..a3a3d435 100644 --- a/src/slskd/Common/Validation/RelativePathAttribute.cs +++ b/src/slskd/Common/Validation/RelativePathAttribute.cs @@ -40,8 +40,9 @@ namespace slskd.Validation /// public class RelativePathAttribute : ValidationAttribute { - public RelativePathAttribute() + public RelativePathAttribute(bool platformAgnostic = false) { + PlatformAgnostic = platformAgnostic; } public RelativePathAttribute(OSPlatform? os = null) @@ -50,15 +51,44 @@ namespace slskd.Validation } public OSPlatform? OS { get; } + public bool PlatformAgnostic { get; } + protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (value != null && value is string str && !string.IsNullOrEmpty(str)) { var path = value.ToString(); - if (!FileSafety.IsPathRelative(path, os: OS)) + if (OS.HasValue) + { + // this can only be exercised via unit tests; OSPlatform can't be passeed as an attribute argument + // so, this takes precedent over PlatformAgnostic + if (!FileSafety.IsPathRelative(path, os: OS)) + { + return new ValidationResult($"The {validationContext.DisplayName} field must be a relative path."); + } + } + else { - return new ValidationResult($"The {validationContext.DisplayName} field must be a relative path."); + if (!PlatformAgnostic) + { + if (!FileSafety.IsPathRelative(path, os: null)) // this OS + { + return new ValidationResult($"The {validationContext.DisplayName} field must be a relative path."); + } + } + else + { + if (!FileSafety.IsPathRelative(path, os: OSPlatform.Linux)) + { + return new ValidationResult($"The {validationContext.DisplayName} field must be a relative path."); + } + + if (!FileSafety.IsPathRelative(path, os: OSPlatform.Windows)) + { + return new ValidationResult($"The {validationContext.DisplayName} field must be a relative path."); + } + } } } diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index f2a78247..9ad1a626 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -765,7 +765,7 @@ namespace slskd [Argument(default, "incomplete")] [EnvironmentVariable("INCOMPLETE_DIR")] [Description("path where incomplete downloads are saved")] - [AbsolutePath] + [AbsolutePath(platformAgnostic: true)] [NonTraversingPath] [DirectoryExists(ensureWriteable: true)] [RequiresRestart] @@ -777,7 +777,7 @@ namespace slskd [Argument('o', "downloads")] [EnvironmentVariable("DOWNLOADS_DIR")] [Description("path where downloaded files are saved")] - [AbsolutePath] + [AbsolutePath(platformAgnostic: true)] [NonTraversingPath] [DirectoryExists(ensureWriteable: true)] [RequiresRestart] @@ -1072,7 +1072,7 @@ namespace slskd /// /// Gets the destination subdirectory for the files in the batch. /// - [RelativePath] + [RelativePath(platformAgnostic: true)] [NonTraversingPath] [String(AllowNull = true, AllowEmpty = false, AllowWhiteSpace = false, MinimumLength = 1)] public string Subdirectory { get; init; } = "{SOURCE_DIRECTORY}"; @@ -2046,7 +2046,7 @@ namespace slskd [Argument(default, "http-socket")] [EnvironmentVariable("HTTP_SOCKET")] [Description("HTTP listen unix domain socket (UDS) path for web UI")] - [AbsolutePath] + [AbsolutePath(platformAgnostic: false)] [NonTraversingPath] [RequiresRestart] public string Socket { get; init; } -- cgit v1.2.3