diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-05-23 19:37:31 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-05-23 19:37:31 -0500 |
| commit | 746f7ceefa61e62ad801b825f80b87fe9c853c44 (patch) | |
| tree | 52afa6e3ee234950bbd338e3d086b682da277b58 /src/slskd/Files/FileService.cs | |
| parent | 0bc3f2e327d8ae9a8a0bd1058cb90c59c58933ee (diff) | |
| parent | 8870f4f5590373d54370e64c7b9081521245d8dc (diff) | |
| download | slskd-746f7ceefa61e62ad801b825f80b87fe9c853c44.tar.gz | |
Merge pull request #1740 from slskd/paths
Change path manipulation logic (again)
Diffstat (limited to 'src/slskd/Files/FileService.cs')
| -rw-r--r-- | src/slskd/Files/FileService.cs | 57 |
1 files changed, 55 insertions, 2 deletions
diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index 4b281863..3b304d99 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -79,6 +79,17 @@ namespace slskd.Files { ArgumentException.ThrowIfNullOrWhiteSpace(filename, nameof(filename)); + if (Path.GetFullPath(filename) != filename) + { + throw new ArgumentException("Only absolute paths may be specified", nameof(filename)); + } + + if (FileSafety.ContainsTraversalSegments(filename)) + { + Log.Warning("Suspicious attempt to resolve file info for a filename containing unsafe path segments (one or more of path traversal characters '.' and '..'). Requested file: {File}", filename); + throw new ArgumentException("Filenames containing traversal segments are not allowed", nameof(filename)); + } + FileInfo info; try @@ -153,11 +164,22 @@ namespace slskd.Files /// <exception cref="UnauthorizedException">Thrown if a specified directory is restricted.</exception> public virtual async Task<Dictionary<string, OneOf<bool, Exception>>> DeleteDirectoriesAsync(params string[] directories) { + if (directories is null or [] || directories.Any(directory => string.IsNullOrEmpty(directory))) + { + throw new ArgumentException("One or more directories are null or empty"); + } + if (directories.Any(directory => Path.GetFullPath(directory) != directory)) { throw new ArgumentException("Only absolute paths may be specified", nameof(directories)); } + if (directories.Any(directory => FileSafety.ContainsTraversalSegments(directory))) + { + Log.Warning("Suspicious attempt to delete a directory with a path containing unsafe path segments (one or more of path traversal characters '.' and '..'). Requested directories: {Directories}", string.Join(", ", directories)); + throw new ArgumentException("Directories containing traversal segments are not allowed", nameof(directories)); + } + if (directories.Any(directory => AllowedDirectories.Contains(directory))) { throw new ArgumentException("Deletion of application-controlled directory roots is not supported"); @@ -216,11 +238,22 @@ namespace slskd.Files /// <exception cref="UnauthorizedException">Thrown if a specified file is restricted.</exception> public virtual async Task<Dictionary<string, OneOf<bool, Exception>>> DeleteFilesAsync(params string[] files) { + if (files is null or [] || files.Any(file => string.IsNullOrWhiteSpace(file))) + { + throw new ArgumentException("One or more files are null or empty", nameof(files)); + } + if (files.Any(file => Path.GetFullPath(file) != file)) { throw new ArgumentException("Only absolute paths may be specified", nameof(files)); } + if (files.Any(file => FileSafety.ContainsTraversalSegments(file))) + { + Log.Warning("Suspicious attempt to delete a file with a filename containing unsafe path segments (one or more of path traversal characters '.' and '..'). Requested file(s): {File}", string.Join(", ", files)); + throw new ArgumentException("Paths containing traversal segments are not allowed", nameof(files)); + } + // important! we must fully expand the given paths with GetFullPath() to resolve a given relative directory, like '..' bool IsAllowed(string path) => AllowedDirectories.Any(allowed => path.StartsWith(allowed + Path.DirectorySeparatorChar) || path == allowed); @@ -269,15 +302,23 @@ namespace slskd.Files /// <exception cref="UnauthorizedException">Thrown if the specified root directory is restricted.</exception> public virtual async Task<FilesystemDirectory> ListContentsAsync(string directory, EnumerationOptions enumerationOptions = null) { + ArgumentException.ThrowIfNullOrWhiteSpace(directory); + if (Path.GetFullPath(directory) != directory) { throw new ArgumentException("Only absolute paths may be specified", nameof(directory)); } + if (FileSafety.ContainsTraversalSegments(directory)) + { + Log.Warning("Suspicious attempt to list a directory with a path containing unsafe path segments (one or more of path traversal characters '.' and '..'). Requested directory: {Directory}", directory); + throw new ArgumentException("Paths containing traversal segments are not allowed", nameof(directory)); + } + // important! we must fully expand the path with GetFullPath() to resolve a given relative directory, like '..' - if (!AllowedDirectories.Any(allowed => directory.StartsWith(allowed))) + if (!AllowedDirectories.Any(allowed => directory.StartsWith(allowed + Path.DirectorySeparatorChar) || directory == allowed)) { - throw new UnauthorizedException($"Only application-controlled directories can be deleted"); + throw new UnauthorizedException($"Only application-controlled directories can be listed"); } if (!Directory.Exists(directory)) @@ -340,6 +381,12 @@ namespace slskd.Files { ArgumentNullException.ThrowIfNullOrWhiteSpace(filename, nameof(filename)); + if (FileSafety.ContainsTraversalSegments(filename)) + { + Log.Warning("Suspicious attempt to create a file with a filename containing unsafe path segments (one or more of path traversal characters '.' and '..'). Requested file: {File}", filename); + throw new ArgumentException("Filenames containing traversal segments are not allowed"); + } + var path = Path.GetDirectoryName(filename); UnixFileMode? unixCreateMode = options?.UnixCreateMode ?? OptionsMonitor.CurrentValue.Permissions.File.Mode?.ToUnixFileMode(); @@ -411,6 +458,12 @@ namespace slskd.Files ArgumentNullException.ThrowIfNullOrWhiteSpace(sourceFilename, nameof(sourceFilename)); ArgumentNullException.ThrowIfNullOrWhiteSpace(destinationDirectory, nameof(destinationDirectory)); + if (FileSafety.ContainsTraversalSegments(sourceFilename) || FileSafety.ContainsTraversalSegments(destinationDirectory)) + { + Log.Warning("Suspicious attempt to move a file with either a source filename or destination directory name containing unsafe path segments (one or more of path traversal characters '.' and '..'). Source: {Source}, Destination: {Destination}", sourceFilename, destinationDirectory); + throw new ArgumentException("Filenames and directories containing traversal segments are not allowed"); + } + if (!File.Exists(sourceFilename)) { throw new FileNotFoundException($"The specified source file does not exist", fileName: sourceFilename); |
