From 65178904483e4496c1140bee11b2fd4d79da121f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 25 May 2026 18:19:42 -0500 Subject: add ReplacePathTraversalSegments --- src/slskd/Common/CommonExtensions.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Common/CommonExtensions.cs b/src/slskd/Common/CommonExtensions.cs index 5ff3e519..ce1b5afb 100644 --- a/src/slskd/Common/CommonExtensions.cs +++ b/src/slskd/Common/CommonExtensions.cs @@ -471,7 +471,7 @@ namespace slskd } /// - /// Replaces any occurrence of an invalid filename character with the specified . + /// Replaces any occurrence of an invalid filename character with the specified . /// /// The path to sanitize. /// The character with which to replace invalid characters. @@ -488,6 +488,22 @@ namespace slskd return sanitized; } + /// + /// Replaces any path traversal segments in the specified with the specified + /// . + /// + /// The path to sanitize. + /// The string with which to replace path traversal segments. + /// The sanitized path. + public static string ReplacePathTraversalSegments(this string path, string replacement = "_") + { + return string.Join(Path.DirectorySeparatorChar, + path + .LocalizePath() + .Split(Path.DirectorySeparatorChar) + .Select(s => s is "." or ".." ? replacement : s)); + } + /// /// Converts a ModelStateDictionary into a human-readable format. /// -- cgit v1.2.3