From 66523b27f6120836ba82cfb6f56b679d7b37f58a Mon Sep 17 00:00:00 2001 From: pn04 <33034498+pn04@users.noreply.github.com> Date: Thu, 13 Oct 2022 00:36:56 +0530 Subject: added a DirectoryIsRelative validation attribute *added DirectoryIsRelative attribute to check if a directory is relative *added DirectoryIsRelative to ContentPath *added unit test cases --- .../Validation/DirectoryIsRelativeAttribute.cs | 38 ++++++++++++++++++++++ src/slskd/Core/Options.cs | 1 + 2 files changed, 39 insertions(+) create mode 100644 src/slskd/Common/Validation/DirectoryIsRelativeAttribute.cs (limited to 'src') diff --git a/src/slskd/Common/Validation/DirectoryIsRelativeAttribute.cs b/src/slskd/Common/Validation/DirectoryIsRelativeAttribute.cs new file mode 100644 index 00000000..e4e13bc8 --- /dev/null +++ b/src/slskd/Common/Validation/DirectoryIsRelativeAttribute.cs @@ -0,0 +1,38 @@ +// +// 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/. +// + +namespace slskd.Validation +{ + using System.ComponentModel.DataAnnotations; + using System.IO; + + /// + /// Validates that the specified path is relative. + /// + 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 4c146d32..484a514b 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1167,6 +1167,7 @@ namespace slskd [Description("path to static web content")] [StringLength(255, MinimumLength = 1)] [DirectoryExists] + [DirectoryIsRelative] [RequiresRestart] public string ContentPath { get; init; } = "wwwroot"; -- cgit v1.2.3