From 97279b7524aef6ab27f49b2696da448a00ba92dd Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 4 Dec 2021 10:24:50 -0600 Subject: check that downloads and incomplete dirs are writeable --- .../Common/Validation/DirectoryExistsAttributes.cs | 39 +++++++++++++++++++++- src/slskd/Options.cs | 4 +-- 2 files changed, 40 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/slskd/Common/Validation/DirectoryExistsAttributes.cs b/src/slskd/Common/Validation/DirectoryExistsAttributes.cs index 45d7f71b..8094b4d2 100644 --- a/src/slskd/Common/Validation/DirectoryExistsAttributes.cs +++ b/src/slskd/Common/Validation/DirectoryExistsAttributes.cs @@ -18,23 +18,60 @@ namespace slskd.Validation { using System.ComponentModel.DataAnnotations; + using System; using System.IO; + using System.Linq; /// /// Validates that the directory at the specified path exists. /// public class DirectoryExistsAttribute : ValidationAttribute { + public DirectoryExistsAttribute(bool ensureWriteable = false) + { + EnsureWriteable = ensureWriteable; + } + + private bool EnsureWriteable { get; set; } + protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (value != null) { var dir = Path.GetFullPath(value?.ToString()); - if (!dir.StartsWith(Program.DefaultAppDirectory) && !string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) + // 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 + { + 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 ValidationResult.Success; diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs index 7546449f..15b954f7 100644 --- a/src/slskd/Options.cs +++ b/src/slskd/Options.cs @@ -273,7 +273,7 @@ namespace slskd [Argument(default, "incomplete")] [EnvironmentVariable("INCOMPLETE_DIR")] [Description("path where incomplete downloads are saved")] - [DirectoryExists] + [DirectoryExists(ensureWriteable: true)] [RequiresRestart] public string Incomplete { get; private set; } = Program.DefaultIncompleteDirectory; @@ -283,7 +283,7 @@ namespace slskd [Argument('o', "downloads")] [EnvironmentVariable("DOWNLOADS_DIR")] [Description("path where downloaded files are saved")] - [DirectoryExists] + [DirectoryExists(ensureWriteable: true)] [RequiresRestart] public string Downloads { get; private set; } = Program.DefaultDownloadsDirectory; -- cgit v1.2.3 From 7596778656ec695fb03e106ffe2e03d5ee2caee0 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 4 Dec 2021 10:45:51 -0600 Subject: fix linting --- src/slskd/Common/Validation/DirectoryExistsAttributes.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Common/Validation/DirectoryExistsAttributes.cs b/src/slskd/Common/Validation/DirectoryExistsAttributes.cs index 8094b4d2..77a46777 100644 --- a/src/slskd/Common/Validation/DirectoryExistsAttributes.cs +++ b/src/slskd/Common/Validation/DirectoryExistsAttributes.cs @@ -17,8 +17,8 @@ namespace slskd.Validation { - using System.ComponentModel.DataAnnotations; using System; + using System.ComponentModel.DataAnnotations; using System.IO; using System.Linq; @@ -42,7 +42,7 @@ namespace slskd.Validation // 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)) + if (new[] { Program.DefaultDownloadsDirectory, Program.DefaultIncompleteDirectory }.Contains(dir)) { return ValidationResult.Success; } -- cgit v1.2.3