diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-05-07 17:13:28 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-05-07 17:13:28 -0500 |
| commit | fb600f5b90e6c313635687414b50869bf86ab653 (patch) | |
| tree | d50d0c2837ef97d451a1e68b9a5c200d8678b6e8 /src | |
| parent | 43524239edba37a1e7b8b9a8e34a0c8f31c4f332 (diff) | |
| download | slskd-fb600f5b90e6c313635687414b50869bf86ab653.tar.gz | |
add a validator for relative paths
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/Validation/AbsolutePathAttribute.cs (renamed from src/slskd/Common/Validation/AbsoluteFilePathAttribute.cs) | 4 | ||||
| -rw-r--r-- | src/slskd/Common/Validation/RelativePathAttribute.cs | 72 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 2 |
3 files changed, 75 insertions, 3 deletions
diff --git a/src/slskd/Common/Validation/AbsoluteFilePathAttribute.cs b/src/slskd/Common/Validation/AbsolutePathAttribute.cs index 8f87e479..ef6db8ca 100644 --- a/src/slskd/Common/Validation/AbsoluteFilePathAttribute.cs +++ b/src/slskd/Common/Validation/AbsolutePathAttribute.cs @@ -1,4 +1,4 @@ -// <copyright file="AbsoluteFilePathAttribute.cs" company="JP Dillingham"> +// <copyright file="AbsolutePathAttribute.cs" company="JP Dillingham"> // ▄▄▄▄ ▄▄▄▄ ▄▄▄▄ // ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █ // █__ --█ █__ --█ ◄█ - █ @@ -38,7 +38,7 @@ namespace slskd.Validation /// <summary> /// Validates that the specified path is absolute. /// </summary> - public class AbsoluteFilePathAttribute : ValidationAttribute + public class AbsolutePathAttribute : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) { diff --git a/src/slskd/Common/Validation/RelativePathAttribute.cs b/src/slskd/Common/Validation/RelativePathAttribute.cs new file mode 100644 index 00000000..05f7630d --- /dev/null +++ b/src/slskd/Common/Validation/RelativePathAttribute.cs @@ -0,0 +1,72 @@ +// <copyright file="RelativePathAttribute.cs" company="JP Dillingham"> +// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄ +// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █ +// █__ --█ █__ --█ ◄█ - █ +// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█ +// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉ +// │ Copyright (c) JP Dillingham. +// │ +// │ 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, version 3. +// │ +// │ 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/. +// │ +// │ This program is distributed with Additional Terms pursuant to Section 7 +// │ of the AGPLv3. See the LICENSE file in the root directory of this +// │ project for the complete terms and conditions. +// │ +// │ https://slskd.org +// │ +// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌ +// │ SPDX-FileCopyrightText: JP Dillingham +// │ SPDX-License-Identifier: AGPL-3.0-only +// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈ +// </copyright> + +namespace slskd.Validation +{ + using System; + using System.ComponentModel.DataAnnotations; + using System.IO; + + /// <summary> + /// Validates that the specified path is relative and contains no path traversal segments. + /// </summary> + public class RelativePathAttribute : ValidationAttribute + { + protected override ValidationResult IsValid(object value, ValidationContext validationContext) + { + if (value != null) + { + var path = value.ToString(); + + if (!string.IsNullOrEmpty(path)) + { + if (Path.IsPathRooted(path)) + { + return new ValidationResult($"The {validationContext.DisplayName} field must be a relative path."); + } + + // use a dummy absolute base to normalize the path; GetFullPath resolves all .. and . segments + var dummyBase = Path.GetTempPath().TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) + + Path.DirectorySeparatorChar; + var combined = Path.GetFullPath(Path.Combine(dummyBase, path)); + + if (!combined.StartsWith(dummyBase, StringComparison.OrdinalIgnoreCase)) + { + return new ValidationResult($"The {validationContext.DisplayName} field must not contain path traversal segments."); + } + } + } + + return ValidationResult.Success; + } + } +} diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 8cc369af..1ca5b84a 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -2010,7 +2010,7 @@ namespace slskd [Argument(default, "http-socket")] [EnvironmentVariable("HTTP_SOCKET")] [Description("HTTP listen unix domain socket (UDS) path for web UI")] - [AbsoluteFilePath] + [AbsolutePath] [RequiresRestart] public string Socket { get; init; } |
