diff options
| author | JP Dillingham <jp@dillingham.me> | 2023-01-11 09:11:06 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-11 09:11:06 -0600 |
| commit | d06431f2024daf81d99ad7ba6e3dcca3692fefc8 (patch) | |
| tree | 1ac021e1b39d26ee1a4b2b40c5480c3e8e74f85f /src | |
| parent | 8948a338854b9033cc38cd6188866433a81d28d4 (diff) | |
| parent | 403924e041ed678725cd347fd19b263f0efeabb8 (diff) | |
| download | slskd-d06431f2024daf81d99ad7ba6e3dcca3692fefc8.tar.gz | |
Merge pull request #847 from slskd/content
Fix bug with validation of ContentPath
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/Validation/DirectoryExistsAttributes.cs | 75 | ||||
| -rw-r--r-- | src/slskd/Common/Validation/DirectoryIsRelativeAttribute.cs | 38 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 3 |
3 files changed, 49 insertions, 67 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 diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 063da9e6..05331cd4 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1391,8 +1391,7 @@ namespace slskd [EnvironmentVariable("CONTENT_PATH")] [Description("path to static web content")] [StringLength(255, MinimumLength = 1)] - [DirectoryExists] - [DirectoryIsRelative] + [DirectoryExists(relativeToApplicationDirectory: true)] [RequiresRestart] public string ContentPath { get; init; } = "wwwroot"; |
