aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Files/FileService.cs
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-10-03 23:28:23 -0500
committerGitHub <noreply@github.com>2026-10-03 23:28:23 -0500
commitfc11d5d691ac008e1507f25ebb5ce3ac7a5cfc60 (patch)
tree3de54f5e8b4001253db53bd20dc153ebe49b271c /src/slskd/Files/FileService.cs
parent1915a370c1bd11aee8849574e116b353a0b7153f (diff)
parenta97a425c7121f19c1613106c0bab7a1dcf616f92 (diff)
downloadslskd-fc11d5d691ac008e1507f25ebb5ce3ac7a5cfc60.tar.gz
Merge branch 'master' into upload-queue-fixes
Diffstat (limited to 'src/slskd/Files/FileService.cs')
-rw-r--r--src/slskd/Files/FileService.cs55
1 files changed, 50 insertions, 5 deletions
diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs
index 8450ba48..70bfc293 100644
--- a/src/slskd/Files/FileService.cs
+++ b/src/slskd/Files/FileService.cs
@@ -59,9 +59,11 @@ namespace slskd.Files
private IEnumerable<string> AllowedDirectories => new[]
{
- Path.GetFullPath(OptionsMonitor.CurrentValue.Directories.Downloads),
- Path.GetFullPath(OptionsMonitor.CurrentValue.Directories.Incomplete),
- };
+ OptionsMonitor.CurrentValue.Directories.Downloads,
+ OptionsMonitor.CurrentValue.Directories.Incomplete,
+ Program.LogDirectory,
+ }.Where(x => !string.IsNullOrWhiteSpace(x))
+ .Select(x => Path.GetFullPath(x));
private ILogger Log { get; } = Serilog.Log.ForContext<FileService>();
private IOptionsMonitor<Options> OptionsMonitor { get; }
@@ -319,7 +321,7 @@ namespace slskd.Files
/// <exception cref="ArgumentException">Thrown if the specified directory has a relative path.</exception>
/// <exception cref="NotFoundException">Thrown if the specified directory does not exist.</exception>
/// <exception cref="UnauthorizedException">Thrown if the specified root directory is restricted.</exception>
- public virtual async Task<FilesystemDirectory> ListContentsAsync(string directory, EnumerationOptions enumerationOptions = null)
+ public virtual async Task<FilesystemDirectory> ListDirectoryContentsAsync(string directory, EnumerationOptions enumerationOptions = null)
{
ArgumentException.ThrowIfNullOrWhiteSpace(directory);
@@ -351,7 +353,7 @@ namespace slskd.Files
try
{
- var contents = dir.GetFileSystemInfos("*", enumerationOptions);
+ var contents = dir.GetFileSystemInfos("*", enumerationOptions ?? new EnumerationOptions());
var files = contents
.OfType<FileInfo>()
@@ -384,6 +386,49 @@ namespace slskd.Files
}
/// <summary>
+ /// Retrieves the contents of the specified <paramref name="filename"/> as a stream.
+ /// </summary>
+ /// <param name="filename">The file for which contents are to be retrieved.</param>
+ /// <returns>A Stream containing the contents of the file.</returns>
+ /// <exception cref="ArgumentNullException">Thrown if the specified filename is null or contains only whitespace.</exception>
+ /// <exception cref="ArgumentException">Thrown if the specified filename contains path traversal segments.</exception>
+ /// <exception cref="UnauthorizedException">Thrown if the specified file is not within an allowed directory.</exception>
+ /// <exception cref="NotFoundException">Thrown if the specified file does not exist.</exception>
+ /// <exception cref="IOException">Thrown if the file can't be opened for some reason.</exception>
+ public virtual Stream GetFileContents(string filename)
+ {
+ ArgumentNullException.ThrowIfNullOrWhiteSpace(filename, nameof(filename));
+
+ if (FileSafety.ContainsTraversalSegments(filename))
+ {
+ Log.Warning("Suspicious attempt to read 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");
+ }
+
+ // important! we must fully expand the path with GetFullPath() to resolve a given relative directory, like '..'
+ if (!AllowedDirectories.Any(allowed => filename.StartsWith(allowed + Path.DirectorySeparatorChar)))
+ {
+ throw new UnauthorizedException($"Only application-controlled directories can be listed");
+ }
+
+ var info = ResolveFileInfo(filename);
+
+ if (!info.Exists)
+ {
+ throw new NotFoundException();
+ }
+
+ try
+ {
+ return new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
+ }
+ catch (Exception ex)
+ {
+ throw new IOException($"Failed to open file {Path.GetFileName(filename)} for reading: {ex.Message}", ex);
+ }
+ }
+
+ /// <summary>
/// Creates a new file with the specified fully qualified <paramref name="filename"/> and the optional <paramref name="options"/>,
/// returning a <see cref="Stream"/> with which the contents of the file can be written.
/// </summary>