diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-06-03 18:56:06 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-06-03 18:56:06 -0500 |
| commit | 78561f600754241efdf06c0fcee5ffdbdccda1b6 (patch) | |
| tree | 7504bc8c6d4310d1dd15b188841329f23354986f /src/slskd/Common/Validation/RelativePathAttribute.cs | |
| parent | e479f3a213d13cb8b165237a594bcb86b7462606 (diff) | |
| download | slskd-78561f600754241efdf06c0fcee5ffdbdccda1b6.tar.gz | |
wip
Diffstat (limited to 'src/slskd/Common/Validation/RelativePathAttribute.cs')
| -rw-r--r-- | src/slskd/Common/Validation/RelativePathAttribute.cs | 36 |
1 files changed, 33 insertions, 3 deletions
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 /// </summary> 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."); + } + } } } |
