diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-06-03 23:17:24 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-06-03 23:17:24 -0500 |
| commit | 48f9412b8e9be15b36a2bd7335d6b7a8a0448421 (patch) | |
| tree | e0c98e2952532ffe1ab9c79ea503791f8fcd258d /src | |
| parent | 70520ee0e09526cd86a4bdf6573cbc6f3af717da (diff) | |
| download | slskd-48f9412b8e9be15b36a2bd7335d6b7a8a0448421.tar.gz | |
so dumb
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/OperatingSystem.cs | 7 | ||||
| -rw-r--r-- | src/slskd/Common/Validation/AbsolutePathAttribute.cs | 39 | ||||
| -rw-r--r-- | src/slskd/Common/Validation/RelativePathAttribute.cs | 39 |
3 files changed, 67 insertions, 18 deletions
diff --git a/src/slskd/Common/OperatingSystem.cs b/src/slskd/Common/OperatingSystem.cs index 97c205a6..0c3dae2c 100644 --- a/src/slskd/Common/OperatingSystem.cs +++ b/src/slskd/Common/OperatingSystem.cs @@ -35,8 +35,9 @@ namespace slskd; public enum OperatingSystem { None = 0, - Current = 1, - Linux = 2, - Windows = 3, + Linux = 1, + Windows = 2, + Current = 3, Any = 4, + All = 5, }
\ No newline at end of file diff --git a/src/slskd/Common/Validation/AbsolutePathAttribute.cs b/src/slskd/Common/Validation/AbsolutePathAttribute.cs index eb1c9069..9393e524 100644 --- a/src/slskd/Common/Validation/AbsolutePathAttribute.cs +++ b/src/slskd/Common/Validation/AbsolutePathAttribute.cs @@ -33,23 +33,24 @@ namespace slskd.Validation { using System.ComponentModel.DataAnnotations; - using System.Runtime.InteropServices; /// <summary> /// Validates that the specified path is absolute. /// </summary> public class AbsolutePathAttribute : ValidationAttribute { - public AbsolutePathAttribute() + public AbsolutePathAttribute(OperatingSystem os = OperatingSystem.Current) { - } + if (os != OperatingSystem.Current && os != OperatingSystem.Any && os != OperatingSystem.All) + { + throw new System.ArgumentException("OperatingSystem argument for AbsolutePathAttribute must be one of: Current, Any, All", nameof(os)); + } - public AbsolutePathAttribute(OSPlatform? os = null) - { OS = os; } - public OSPlatform? OS { get; } + public OperatingSystem? OS { get; } + private OperatingSystem? Injected { get; } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { @@ -57,9 +58,31 @@ namespace slskd.Validation { var path = value.ToString(); - if (!FileSafety.IsPathAbsolute(path, os: OS)) + // this property can only be set via reflection in a unit test; this is here only to make this testable + if (Injected.HasValue) + { + if (!FileSafety.IsPathAbsolute(path, os: Injected)) + { + return new ValidationResult($"The {validationContext.DisplayName} field must be an absolute path."); + } + + return ValidationResult.Success; + } + + if (OS == OperatingSystem.Current) + { + if (!FileSafety.IsPathAbsolute(path, os: Compute.OperatingSystem())) + { + return new ValidationResult($"The {validationContext.DisplayName} field must be an absolute path on the current operating system."); + } + + return ValidationResult.Success; + } + + // OS == OperatingSystem.All or .Any + if (!FileSafety.IsPathAbsolute(path, os: OperatingSystem.Linux) || !FileSafety.IsPathAbsolute(path, os: OperatingSystem.Windows)) { - return new ValidationResult($"The {validationContext.DisplayName} field must be an absolute file path."); + return new ValidationResult($"The {validationContext.DisplayName} field must be an absolute path on all operating systems."); } } diff --git a/src/slskd/Common/Validation/RelativePathAttribute.cs b/src/slskd/Common/Validation/RelativePathAttribute.cs index f465ba98..755ea311 100644 --- a/src/slskd/Common/Validation/RelativePathAttribute.cs +++ b/src/slskd/Common/Validation/RelativePathAttribute.cs @@ -33,30 +33,55 @@ namespace slskd.Validation { using System.ComponentModel.DataAnnotations; - using System.Runtime.InteropServices; /// <summary> /// Validates that the specified path is relative. /// </summary> public class RelativePathAttribute : ValidationAttribute { - public RelativePathAttribute() + public RelativePathAttribute(OperatingSystem os = OperatingSystem.Current) { - } + if (os != OperatingSystem.Current && os != OperatingSystem.Any && os != OperatingSystem.All) + { + throw new System.ArgumentException("OperatingSystem argument for RelativePathAttribute must be one of: Current, Any, All", nameof(os)); + } - public RelativePathAttribute(OSPlatform? os = null) - { OS = os; } - public OSPlatform? OS { get; } + public OperatingSystem OS { get; } + + private OperatingSystem? Injected { 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)) + // this property can only be set via reflection in a unit test; this is here only to make this testable + if (Injected.HasValue) + { + if (!FileSafety.IsPathRelative(path, os: Injected)) + { + return new ValidationResult($"The {validationContext.DisplayName} field must be a relative path on the current operating system."); + } + + return ValidationResult.Success; + } + + if (OS == OperatingSystem.Current) + { + if (!FileSafety.IsPathRelative(path, os: Compute.OperatingSystem())) + { + return new ValidationResult($"The {validationContext.DisplayName} field must be a relative path on all operating systems."); + } + + return ValidationResult.Success; + } + + // OS == OperatingSystem.All or .Any; + if (!FileSafety.IsPathRelative(path, os: OperatingSystem.Linux) || !FileSafety.IsPathRelative(path, os: OperatingSystem.Windows)) { return new ValidationResult($"The {validationContext.DisplayName} field must be a relative path."); } |
