aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2023-01-11 08:59:30 -0600
committerJP Dillingham <jp@dillingham.me>2023-01-11 08:59:30 -0600
commited792319f23887c7579a3acde2aea5b3fd916eae (patch)
tree81401e75046e78d98d4f88d5ae89fc61af541d83
parent8948a338854b9033cc38cd6188866433a81d28d4 (diff)
downloadslskd-ed792319f23887c7579a3acde2aea5b3fd916eae.tar.gz
fix relative directory validation
-rw-r--r--src/slskd/Common/Validation/DirectoryExistsAttributes.cs75
-rw-r--r--src/slskd/Common/Validation/DirectoryIsRelativeAttribute.cs38
2 files changed, 48 insertions, 65 deletions
diff --git a/src/slskd/Common/Validation/DirectoryExistsAttributes.cs b/src/slskd/Common/Validation/DirectoryExistsAttributes.cs
index 77a46777..e8bf0224 100644
--- a/src/slskd/Common/Validation/DirectoryExistsAttributes.cs
+++ b/src/slskd/Common/Validation/DirectoryExistsAttributes.cs
@@ -27,50 +27,71 @@ namespace slskd.Validation
/// </summary>
public class DirectoryExistsAttribute : ValidationAttribute
{
- public DirectoryExistsAttribute(bool ensureWriteable = false)
+ /// <summary>
+ /// Initializes a new instance of the <see cref="DirectoryExistsAttribute"/> class.
+ /// </summary>
+ /// <param name="relativeToApplicationDirectory">A value indicating that the provided path must be relative (not rooted) to the application directory.</param>
+ /// <param name="ensureWriteable">A value indicating that the provided path, if it exists, should be probed to ensure it is writeable.</param>
+ public DirectoryExistsAttribute(bool relativeToApplicationDirectory = false, bool ensureWriteable = false)
{
EnsureWriteable = ensureWriteable;
+ RelativeToApplicationDirectory = relativeToApplicationDirectory;
}
private bool EnsureWriteable { get; set; }
+ private bool RelativeToApplicationDirectory { get; set; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
- if (value != null)
+ if (value is not string || string.IsNullOrEmpty((string)value))
{
- var dir = Path.GetFullPath(value?.ToString());
+ return new ValidationResult($"The {validationContext.DisplayName} field must be specified");
+ }
- // default directories are exempt from validation, as there's an additional check (and creation)
- // at startup for these directories.
- if (new[] { Program.DefaultDownloadsDirectory, Program.DefaultIncompleteDirectory }.Contains(dir))
- {
- return ValidationResult.Success;
- }
+ var stringValue = (string)value;
- // empty values are valid, because they will fall back to defaults
- if (string.IsNullOrEmpty(dir))
+ if (RelativeToApplicationDirectory)
+ {
+ if (Path.IsPathRooted(stringValue))
{
- return ValidationResult.Success;
+ return new ValidationResult($"The {validationContext.DisplayName} field specifies a non-relative directory path: '{value}'.");
}
- if (!Directory.Exists(dir))
+ stringValue = Path.Combine(AppContext.BaseDirectory, stringValue);
+ }
+
+ var dir = Path.GetFullPath(stringValue);
+
+ // default directories are exempt from validation, as there's an additional check (and creation)
+ // at startup for these directories.
+ if (new[] { Program.DefaultDownloadsDirectory, Program.DefaultIncompleteDirectory }.Contains(dir))
+ {
+ return ValidationResult.Success;
+ }
+
+ // empty values are valid, because they will fall back to defaults
+ if (string.IsNullOrEmpty(dir))
+ {
+ return ValidationResult.Success;
+ }
+
+ if (!Directory.Exists(dir))
+ {
+ return new ValidationResult($"The {validationContext.DisplayName} field specifies a non-existent directory '{dir}'.");
+ }
+
+ if (EnsureWriteable)
+ {
+ try
{
- return new ValidationResult($"The {validationContext.DisplayName} field specifies a non-existent directory '{dir}'.");
+ var file = Guid.NewGuid().ToString();
+ var probe = Path.Combine(dir, file);
+ File.WriteAllText(probe, string.Empty);
+ File.Delete(probe);
}
-
- if (EnsureWriteable)
+ catch (Exception)
{
- try
- {
- var file = Guid.NewGuid().ToString();
- var probe = Path.Combine(dir, file);
- File.WriteAllText(probe, string.Empty);
- File.Delete(probe);
- }
- catch (Exception)
- {
- return new ValidationResult($"The {validationContext.DisplayName} field specifies a directory '{dir}' that exists, but is not writeable.");
- }
+ return new ValidationResult($"The {validationContext.DisplayName} field specifies a directory '{dir}' that exists, but is not writeable.");
}
}
diff --git a/src/slskd/Common/Validation/DirectoryIsRelativeAttribute.cs b/src/slskd/Common/Validation/DirectoryIsRelativeAttribute.cs
deleted file mode 100644
index e4e13bc8..00000000
--- a/src/slskd/Common/Validation/DirectoryIsRelativeAttribute.cs
+++ /dev/null
@@ -1,38 +0,0 @@
-// <copyright file="DirectoryIsRelativeAttribute.cs" company="slskd Team">
-// Copyright (c) slskd Team. All rights reserved.
-//
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU Affero General Public License as published
-// by the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU Affero General Public License for more details.
-//
-// You should have received a copy of the GNU Affero General Public License
-// along with this program. If not, see https://www.gnu.org/licenses/.
-// </copyright>
-
-namespace slskd.Validation
-{
- using System.ComponentModel.DataAnnotations;
- using System.IO;
-
- /// <summary>
- /// Validates that the specified path is relative.
- /// </summary>
- public class DirectoryIsRelativeAttribute : ValidationAttribute
- {
- protected override ValidationResult IsValid(object value, ValidationContext validationContext)
- {
- if (Path.IsPathRooted(value?.ToString()))
- {
- return new ValidationResult($"The {validationContext.DisplayName} field specifies a non-realtive directory path: '{value}'.");
- }
-
- return ValidationResult.Success;
- }
- }
-} \ No newline at end of file