From c76f272f380833ad01e98f7946a7e114a1635a6c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 22 Jun 2023 09:57:28 -0500 Subject: obscure full path of files, throw ForbiddenException if root is not whitelisted --- src/slskd/Files/FileService.cs | 39 +++++++++++++++++++++------------------ src/slskd/Files/IFileService.cs | 9 ++++----- 2 files changed, 25 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index dfb5ad7b..7194cc04 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -32,7 +32,6 @@ namespace slskd.Files /// /// Initializes a new instance of the class. /// - /// public FileService( IOptionsSnapshot optionsSnapshot) { @@ -45,28 +44,32 @@ namespace slskd.Files /// Lists the contents in the specified , optionally applying the /// specified . /// - /// The directory from which to start the listing. + /// The root directory. + /// An optional subdirectory from which to start the listing. /// Optional enumeration options to apply. /// The list of found contents. - /// - /// Thrown if the specified directory is not rooted in an allowed directory. - /// /// Thrown if the specified directory does not exist. - public async Task> ListContentsAsync(string parentDirectory, EnumerationOptions enumerationOptions = null) + /// Thrown if the specified directory is restricted. + public async Task> ListContentsAsync(string rootDirectory, string parentDirectory = null, EnumerationOptions enumerationOptions = null) { - if (!IsPermissibleDirectory(parentDirectory, OptionsSnapshot.Value.Directories)) + var configuredDirectories = OptionsSnapshot.Value.Directories; + var allowedDirectories = new[] { configuredDirectories.Incomplete, configuredDirectories.Downloads }; + + if (!allowedDirectories.Any(d => rootDirectory == d)) { - throw new InvalidDirectoryException($"The directory '{parentDirectory}' is not rooted in any of the allowed directories"); + throw new ForbiddenException($"For security reasons, only the configured Downloads and Incomplete directories can be listed"); } - if (!Directory.Exists(parentDirectory)) + var fullDirectory = Path.Combine(rootDirectory, parentDirectory); + + if (!Directory.Exists(fullDirectory)) { - throw new NotFoundException($"The directory '{parentDirectory}' does not exist"); + throw new NotFoundException($"The directory '{fullDirectory}' does not exist"); } return await Task.Run(() => { - var dir = new DirectoryInfo(parentDirectory); + var dir = new DirectoryInfo(fullDirectory); var contents = dir.GetFileSystemInfos("*", enumerationOptions); @@ -77,16 +80,16 @@ namespace slskd.Files var dirs = contents .OfType() .Prepend(dir) - .Select(d => FilesystemDirectory.FromDirectoryInfo(d) with { Files = files.Where(f => f.Parent == d.FullName) }); + .Select(d => FilesystemDirectory.FromDirectoryInfo(d) with + { + FullName = d.FullName.ReplaceFirst(rootDirectory, string.Empty).TrimStart('\\', '/'), + Files = files + .Where(f => Path.GetDirectoryName(f.FullName) == d.FullName) + .Select(f => f with { FullName = f.FullName.ReplaceFirst(rootDirectory, string.Empty).TrimStart('\\', '/') }), + }); return dirs; }); } - - private bool IsPermissibleDirectory(string dir, Options.DirectoriesOptions options) - { - var allowedRoots = new[] { options.Downloads, options.Incomplete }; - return allowedRoots.Any(r => dir.StartsWith(r)); - } } } \ No newline at end of file diff --git a/src/slskd/Files/IFileService.cs b/src/slskd/Files/IFileService.cs index 0a750b59..8e7c780a 100644 --- a/src/slskd/Files/IFileService.cs +++ b/src/slskd/Files/IFileService.cs @@ -30,13 +30,12 @@ namespace slskd.Files /// Lists the contents in the specified , optionally applying the /// specified . /// - /// The directory from which to start the listing. + /// The root directory. + /// An optional subdirectory from which to start the listing. /// Optional enumeration options to apply. /// The list of found contents. - /// - /// Thrown if the specified directory is not rooted in an allowed directory. - /// /// Thrown if the specified directory does not exist. - Task> ListContentsAsync(string parentDirectory, EnumerationOptions enumerationOptions = null); + /// Thrown if the specified directory is restricted. + Task> ListContentsAsync(string rootDirectory, string parentDirectory = null, EnumerationOptions enumerationOptions = null); } } \ No newline at end of file -- cgit v1.2.3