aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-05-13 20:40:03 -0500
committerJP Dillingham <jp@dillingham.me>2026-05-13 20:40:03 -0500
commit29ad269c1b688d7e06935cf95ba337bbe946ced4 (patch)
treed7ad930a7645ea9237b70003125b68ee5daa7d06 /src
parent95847e83d2be4cdb865c17a82cbc140d06bad7c6 (diff)
downloadslskd-29ad269c1b688d7e06935cf95ba337bbe946ced4.tar.gz
add validators for absolute and relative paths
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Common/Validation/AbsolutePathAttribute.cs (renamed from src/slskd/Common/Validation/AbsoluteFilePathAttribute.cs)19
-rw-r--r--src/slskd/Common/Validation/RelativePathAttribute.cs63
2 files changed, 75 insertions, 7 deletions
diff --git a/src/slskd/Common/Validation/AbsoluteFilePathAttribute.cs b/src/slskd/Common/Validation/AbsolutePathAttribute.cs
index 8f87e479..4c9f5e53 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">
// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄
// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █
// █__ --█ █__ --█ ◄█ - █
@@ -33,22 +33,27 @@
namespace slskd.Validation
{
using System.ComponentModel.DataAnnotations;
- using System.IO;
/// <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)
{
- if (value != null)
+ if (value != null && value is string str && !string.IsNullOrEmpty(str))
{
- var filePath = value.ToString();
+ var path = value.ToString();
- if (!string.IsNullOrEmpty(filePath) && !Path.IsPathRooted(filePath))
+ if (!FileSafety.IsPathAbsolute(path))
{
- return new ValidationResult($"The {validationContext.DisplayName} field must specify an absolute file path.");
+ return new ValidationResult($"The {validationContext.DisplayName} field must be an absolute file path.");
+ }
+
+ // unrelated to absolute/relative check; we disallow traversal segments across the board so check while we're here
+ if (FileSafety.ContainsTraversalSegments(path))
+ {
+ return new ValidationResult($"The {validationContext.DisplayName} field contains one or more unsafe path traversal segments ('.' or '..')");
}
}
diff --git a/src/slskd/Common/Validation/RelativePathAttribute.cs b/src/slskd/Common/Validation/RelativePathAttribute.cs
new file mode 100644
index 00000000..edaef1be
--- /dev/null
+++ b/src/slskd/Common/Validation/RelativePathAttribute.cs
@@ -0,0 +1,63 @@
+// <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.ComponentModel.DataAnnotations;
+
+ /// <summary>
+ /// Validates that the specified path is relative.
+ /// </summary>
+ public class RelativePathAttribute : ValidationAttribute
+ {
+ protected override ValidationResult IsValid(object value, ValidationContext validationContext)
+ {
+ if (value != null && value is string str && !string.IsNullOrEmpty(str))
+ {
+ var path = value.ToString();
+
+ if (!FileSafety.IsPathRelative(path))
+ {
+ return new ValidationResult($"The {validationContext.DisplayName} field must be a relative path.");
+ }
+
+ // unrelated to absolute/relative check; we disallow traversal segments across the board so check while we're here
+ if (FileSafety.ContainsTraversalSegments(path))
+ {
+ return new ValidationResult($"The {validationContext.DisplayName} field contains one or more unsafe path traversal segments ('.' or '..')");
+ }
+ }
+
+ return ValidationResult.Success;
+ }
+ }
+} \ No newline at end of file