diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/FileSafety.cs | 11 | ||||
| -rw-r--r-- | src/slskd/Integrations/FTP/FTPService.cs | 2 |
2 files changed, 7 insertions, 6 deletions
diff --git a/src/slskd/Common/FileSafety.cs b/src/slskd/Common/FileSafety.cs index bf8302d1..6a6dea22 100644 --- a/src/slskd/Common/FileSafety.cs +++ b/src/slskd/Common/FileSafety.cs @@ -102,7 +102,7 @@ public static class FileSafety throw new ArgumentNullException(nameof(segments), $"One or more segments is null"); } - root = root.TrimEnd('/').TrimEnd('\\'); + root = root.TrimEnd('/', '\\'); if (segments.Length == 0) { @@ -114,9 +114,9 @@ public static class FileSafety foreach (var segment in segments) { - // if any segment is rooted (leading slash), Path.Combine drops everyting up to that segment - // and it becomes the new root. example: Path.Combine("/home/users/foo", "/etc") results in "/etc" - // not what we want! + // disallow any segments that: + // windows: are drive letter (C:\) or UNC path (\\) + // linux: begin with a forward slash (any number, covering both local and UNC paths) if (IsPathAbsolute(segment, os)) { throw new ArgumentException($"Absolute paths are not permitted in segments: '{segment}'"); @@ -141,6 +141,7 @@ public static class FileSafety // ensure the segments we're combining don't contain traversal segments "." or ".." // untrusted input could use this to "break out" of the root directory and access sensitive areas + // this covers cases where the entire segment is . or .., or foo\..\bar, or \.. or ..\ or \..\ if (parts.Any(s => s is "." or "..")) { throw new ArgumentException($"Path traversal is not permitted in segments: '{segment}'"); @@ -152,7 +153,7 @@ public static class FileSafety var combined = string.Join(sep, [root, .. segments]); - if (combined.TrimEnd('/').TrimEnd('\\') == root.TrimEnd('/').TrimEnd('\\')) + if (combined.TrimEnd('/', '\\') == root.TrimEnd('/', '\\')) { return combined; } diff --git a/src/slskd/Integrations/FTP/FTPService.cs b/src/slskd/Integrations/FTP/FTPService.cs index 8c270d6a..597a34f3 100644 --- a/src/slskd/Integrations/FTP/FTPService.cs +++ b/src/slskd/Integrations/FTP/FTPService.cs @@ -109,7 +109,7 @@ namespace slskd.Integrations.FTP private async Task AttemptUploadAsync(string filename) { var fileAndParentDirectory = GetFileAndParentDirectoryFromFilename(filename); - var remotePath = FtpOptions.RemotePath.TrimEnd('/').TrimEnd('\\'); + var remotePath = FtpOptions.RemotePath.TrimEnd('/', '\\'); var remoteFilename = $"{remotePath}/{fileAndParentDirectory}"; var existsMode = FtpOptions.OverwriteExisting ? FtpRemoteExists.Overwrite : FtpRemoteExists.Skip; |
