diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-05-25 18:19:42 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-05-25 18:19:42 -0500 |
| commit | 65178904483e4496c1140bee11b2fd4d79da121f (patch) | |
| tree | ae513b5d1608b38b0ef7e1c1fd5117f34a8cf35e /src/slskd/Common/CommonExtensions.cs | |
| parent | 3e5f041578198f50b07833a7b4af8a7010cb3247 (diff) | |
| download | slskd-65178904483e4496c1140bee11b2fd4d79da121f.tar.gz | |
add ReplacePathTraversalSegments
Diffstat (limited to 'src/slskd/Common/CommonExtensions.cs')
| -rw-r--r-- | src/slskd/Common/CommonExtensions.cs | 18 |
1 files changed, 17 insertions, 1 deletions
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 } /// <summary> - /// Replaces any occurrence of an invalid filename character with the specified <see paramref="replacement"/>. + /// Replaces any occurrence of an invalid filename character with the specified <paramref name="replacement"/>. /// </summary> /// <param name="path">The path to sanitize.</param> /// <param name="replacement">The character with which to replace invalid characters.</param> @@ -489,6 +489,22 @@ namespace slskd } /// <summary> + /// Replaces any path traversal segments in the specified <paramref name="path"/> with the specified + /// <paramref name="replacement"/>. + /// </summary> + /// <param name="path">The path to sanitize.</param> + /// <param name="replacement">The string with which to replace path traversal segments.</param> + /// <returns>The sanitized path.</returns> + 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)); + } + + /// <summary> /// Converts a ModelStateDictionary into a human-readable format. /// </summary> /// <param name="dictionary">The ModelStateDictionary to format.</param> |
