aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-05-09 15:58:34 -0500
committerJP Dillingham <jp@dillingham.me>2026-05-09 15:58:34 -0500
commitbd07234c29601c2c491d6582c58b257ea4e03296 (patch)
treeaf989fc3369217f2e88f77d4da8b2a3dcf29200a
parentc86fbec378b42dbd5ae6133c8ac706669f5709bb (diff)
downloadslskd-bd07234c29601c2c491d6582c58b257ea4e03296.tar.gz
check for malformed paths in relative/absolute validation
-rw-r--r--src/slskd/Common/Validation/AbsolutePathAttribute.cs17
-rw-r--r--src/slskd/Common/Validation/RelativePathAttribute.cs15
2 files changed, 29 insertions, 3 deletions
diff --git a/src/slskd/Common/Validation/AbsolutePathAttribute.cs b/src/slskd/Common/Validation/AbsolutePathAttribute.cs
index ef6db8ca..9f6184ce 100644
--- a/src/slskd/Common/Validation/AbsolutePathAttribute.cs
+++ b/src/slskd/Common/Validation/AbsolutePathAttribute.cs
@@ -34,6 +34,7 @@ namespace slskd.Validation
{
using System.ComponentModel.DataAnnotations;
using System.IO;
+ using System.Linq;
/// <summary>
/// Validates that the specified path is absolute.
@@ -44,9 +45,21 @@ namespace slskd.Validation
{
if (value != null)
{
- var filePath = value.ToString();
+ var path = value.ToString();
- if (!string.IsNullOrEmpty(filePath) && !Path.IsPathRooted(filePath))
+ var segments = path.Split('/', '\\');
+
+ if (segments.Any(s => string.IsNullOrEmpty(s)))
+ {
+ return new ValidationResult($"The {validationContext.DisplayName} field contains one or more empty segments ('\\\\' or '//')");
+ }
+
+ if (segments.Any(s => s == ".." || s == "."))
+ {
+ return new ValidationResult($"The {validationContext.DisplayName} field contains one or more unsafe path traversal segments ('.' or '..')");
+ }
+
+ if (!string.IsNullOrEmpty(path) && !Path.IsPathRooted(path))
{
return new ValidationResult($"The {validationContext.DisplayName} field must specify an absolute file path.");
}
diff --git a/src/slskd/Common/Validation/RelativePathAttribute.cs b/src/slskd/Common/Validation/RelativePathAttribute.cs
index 6cf87e7a..3bd48766 100644
--- a/src/slskd/Common/Validation/RelativePathAttribute.cs
+++ b/src/slskd/Common/Validation/RelativePathAttribute.cs
@@ -34,9 +34,10 @@ namespace slskd.Validation
{
using System.ComponentModel.DataAnnotations;
using System.IO;
+ using System.Linq;
/// <summary>
- /// Validates that the specified path is relative and contains no path traversal segments.
+ /// Validates that the specified path is relative.
/// </summary>
public class RelativePathAttribute : ValidationAttribute
{
@@ -46,6 +47,18 @@ namespace slskd.Validation
{
var path = value.ToString();
+ var segments = path.Split('/', '\\');
+
+ if (segments.Any(s => string.IsNullOrEmpty(s)))
+ {
+ return new ValidationResult($"The {validationContext.DisplayName} field contains one or more empty segments ('\\\\' or '//')");
+ }
+
+ if (segments.Any(s => s == ".." || s == "."))
+ {
+ return new ValidationResult($"The {validationContext.DisplayName} field contains one or more unsafe path traversal segments ('.' or '..')");
+ }
+
if (!string.IsNullOrEmpty(path) && Path.IsPathRooted(path))
{
return new ValidationResult($"The {validationContext.DisplayName} field must be a relative path.");