diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-06-04 22:24:26 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-06-04 22:24:26 -0500 |
| commit | fec423393232bc3b9b3a8b40ebeafb489c871e65 (patch) | |
| tree | 15e253525c2a2505def627ed2efb6c19daa629ec /src/slskd/Common/FileSafety.cs | |
| parent | 913faaf93a702971b85508815710f79b5d86a2ba (diff) | |
| download | slskd-fec423393232bc3b9b3a8b40ebeafb489c871e65.tar.gz | |
don't strip roots or restore slashes
Diffstat (limited to 'src/slskd/Common/FileSafety.cs')
| -rw-r--r-- | src/slskd/Common/FileSafety.cs | 22 |
1 files changed, 3 insertions, 19 deletions
diff --git a/src/slskd/Common/FileSafety.cs b/src/slskd/Common/FileSafety.cs index a07de6d1..73dd4b40 100644 --- a/src/slskd/Common/FileSafety.cs +++ b/src/slskd/Common/FileSafety.cs @@ -412,35 +412,26 @@ public static class FileSafety /// <summary> /// Sanitizes the specified <paramref name="path"/> to make it suitable and safe on the local operating system - /// by converting to the correct slashes, dropping a root (if present), replacing any invalid characters with - /// the specified <paramref name="replacement"/>, and by dropping any path traversal segments. + /// by converting to the correct slashes, replacing any invalid characters with the specified + /// <paramref name="replacement"/>, and by dropping any path traversal segments. /// </summary> /// <param name="path">The path.</param> /// <param name="replacement">The character to substitute for invalid characters.</param> - /// <param name="stripRoot">An optional value indicating whether to strip the path's root, if present.</param> /// <param name="os">An optional operating system override, for testing.</param> /// <returns>The sanitized path.</returns> - public static string SanitizePath(string path, char replacement = '_', bool stripRoot = false, OperatingSystem? os = null) + public static string SanitizePath(string path, char replacement = '_', OperatingSystem? os = null) { if (path is null) { return null; } - var leadingSlashCount = path.TakeWhile(c => c == '/' || c == '\\').Count(); - os ??= Compute.OperatingSystem(); var sep = os.Value == OperatingSystem.Windows ? '\\' : '/'; // flip slashes the correct way path = LocalizePath(path, os); - if (stripRoot) - { - // strip C:\ or //server, if present (regardless of slash variant, etc) - path = StripPathRoot(path, os); - } - // for each segment, drop nulls (created by double slashes), sanitize, and replace traversal strings var segments = path .Split('/', '\\') @@ -449,13 +440,6 @@ public static class FileSafety var newPath = string.Join(sep, segments); - if (!stripRoot) - { - // leading slashes are stripped by Split() so we must add them back, - // unless we intended to strip the root - return new string(sep, leadingSlashCount) + newPath; - } - return newPath; } |
