aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-05-23 17:04:19 -0500
committerJP Dillingham <jp@dillingham.me>2026-05-23 17:04:19 -0500
commit643be1cf048878fdf6d96738205fbd86fa8dedc7 (patch)
treeb0362c23287420afc2e0da514d947f4e88538f7c /src
parent01836889040aa76883bf81eb0f4b900f6e7fc4be (diff)
downloadslskd-643be1cf048878fdf6d96738205fbd86fa8dedc7.tar.gz
add additional guard clauses to FileService
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Files/FileService.cs57
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);