aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-05-16 14:53:34 -0500
committerJP Dillingham <jp@dillingham.me>2026-05-16 14:53:34 -0500
commit8265857697b6116f97fc435d7313f0a8161b9e13 (patch)
tree5b17e9067ee60b074db40cedbc1a6d755b4875f6
parentc2055a762d3d5df2ac26845ca7a45a292cc6b6e1 (diff)
downloadslskd-8265857697b6116f97fc435d7313f0a8161b9e13.tar.gz
add NonTraversingPathAttribute
-rw-r--r--src/slskd/Common/Validation/NonTraversingPathAttribute.cs57
-rw-r--r--tests/slskd.Tests.Unit/Common/Validation/NonTraversingPathAttributeTests.cs94
2 files changed, 151 insertions, 0 deletions
diff --git a/src/slskd/Common/Validation/NonTraversingPathAttribute.cs b/src/slskd/Common/Validation/NonTraversingPathAttribute.cs
new file mode 100644
index 00000000..1e2616bd
--- /dev/null
+++ b/src/slskd/Common/Validation/NonTraversingPathAttribute.cs
@@ -0,0 +1,57 @@
+// <copyright file="NonTraversingPathAttribute.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 does not contain traversal segments '.' or '..'.
+ /// </summary>
+ public class NonTraversingPathAttribute : 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.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
diff --git a/tests/slskd.Tests.Unit/Common/Validation/NonTraversingPathAttributeTests.cs b/tests/slskd.Tests.Unit/Common/Validation/NonTraversingPathAttributeTests.cs
new file mode 100644
index 00000000..12efb899
--- /dev/null
+++ b/tests/slskd.Tests.Unit/Common/Validation/NonTraversingPathAttributeTests.cs
@@ -0,0 +1,94 @@
+using System.ComponentModel.DataAnnotations;
+using slskd.Validation;
+using Xunit;
+
+namespace slskd.Tests.Unit.Common.Validation;
+
+public class NonTraversingPathAttributeTests
+{
+ private static (bool IsValid, string ErrorMessage) Validate(string value)
+ {
+ var attribute = new NonTraversingPathAttribute();
+ var context = new ValidationContext(new object()) { DisplayName = "Field", MemberName = "Field" };
+ var result = attribute.GetValidationResult(value, context);
+ return (result == null, result?.ErrorMessage);
+ }
+
+ // paths can be null or empty; [Required] dictates whether they must be defined
+ [Fact]
+ public void Null_Passes()
+ {
+ var (isValid, _) = Validate(null);
+ Assert.True(isValid);
+ }
+
+ [Fact]
+ public void EmptyString_Passes()
+ {
+ var (isValid, _) = Validate(string.Empty);
+ Assert.True(isValid);
+ }
+
+ public class NonTraversal_Passes
+ {
+ [Theory]
+ // Windows-style
+ [InlineData("C:\\Music\\foo\\Windows")]
+ [InlineData("C:/Music/foo/Windows")]
+ // Unix-style
+ [InlineData("/home/user/foo/etc")]
+ [InlineData("\\home\\user\\foo\\etc")]
+ // UNC (backslash)
+ [InlineData("\\\\server\\share\\foo\\folder")]
+ // UNC (forward slash)
+ [InlineData("//server/foo/share")]
+ // soulseek
+ [InlineData("@foo/bar/baz")]
+ [InlineData("@foo\\bar\\baz")]
+ // filenames
+ [InlineData("@foo/bar/baz/baz../..qux/file.ext")]
+ public void NonTraversalSegment_Passes(string value)
+ {
+ var (isValid, errorMessage) = Validate(value);
+ Assert.True(isValid);
+ Assert.Null(errorMessage);
+ }
+ }
+
+ // Paths containing . or .. segments, which are valid directory names in isolation
+ // but indicate traversal or ambiguity and must be rejected regardless of whether
+ // the path is otherwise absolute or relative.
+ public class Traversal_Fails
+ {
+ [Theory]
+ // Windows-style
+ [InlineData("C:\\Music\\..\\Windows")]
+ [InlineData("C:\\Music\\.\\Artist")]
+ [InlineData("C:/Music/../Windows")]
+ [InlineData("C:/Music/./Artist")]
+ [InlineData("C:\\..")]
+ [InlineData("D:\\a\\b\\..\\c")]
+ // Unix-style
+ [InlineData("/../etc")]
+ [InlineData("/home/user/../etc")]
+ [InlineData("/home/user/./Music")]
+ [InlineData("/home/../../etc")]
+ // UNC (backslash)
+ [InlineData("\\\\server\\..\\Windows")]
+ [InlineData("\\\\server\\share\\..\\other")]
+ [InlineData("\\\\server\\share\\.\\folder")]
+ // UNC (forward slash)
+ [InlineData("//server/../share")]
+ [InlineData("//server/share/../other")]
+ // soulseek
+ [InlineData("@foo/bar/baz/baz/../file.ext")]
+ [InlineData("@foo/bar/baz/./../file.ext")]
+ [InlineData("@foo/bar/baz/./baz/file.ext")]
+ public void TraversalSegment_Fails(string value)
+ {
+ var (isValid, errorMessage) = Validate(value);
+ Assert.False(isValid);
+ Assert.Contains("traversal segments", errorMessage);
+ }
+ }
+}