From fbeb97958bafe339b470f41db2780d2db505427f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 6 Sep 2026 14:03:08 -0500 Subject: add an overload of ResolveFileInfo accepting a FileInfo instance --- src/slskd/Files/FileService.cs | 87 +++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 34 deletions(-) (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index 62d17f20..8450ba48 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -67,42 +67,19 @@ namespace slskd.Files private IOptionsMonitor OptionsMonitor { get; } /// - /// Resolves an instance of for the specified , following + /// Resolves an instance of for the specified , following /// any symlinks that may be present to their final target. A non-null return value is guaranteed. /// - /// The fully qualified filename for which to resolve the FileInfo instance. + /// The FileInfo instance for which to follow symlinks. /// The resolved FileInfo instance. - /// Thrown if the specified filename is null or contains only whitespace. + /// Thrown if the specified FileInfo is null. /// Thrown if the specified or linked file is restricted. /// Thrown if the specified or linked file can't be resolved for some reason. - public virtual FileInfo ResolveFileInfo(string filename) + public virtual FileInfo ResolveFileInfo(FileInfo info) { - 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 - { - info = new FileInfo(filename); - } - catch (Exception ex) when (ex is UnauthorizedAccessException || ex is SecurityException) - { - throw new UnauthorizedException($"Access to the file '{filename}' was denied: {ex.Message}", ex); - } - catch (Exception ex) + if (info is null) { - throw new IOException($"Failed to access file '{filename}': {ex.Message}", ex); + throw new ArgumentException("The specified FileInfo is null", nameof(info)); } // if the above didn't throw we are guaranteed a valid instance of FileInfo regardless of whether @@ -130,23 +107,65 @@ namespace slskd.Files } catch (Exception ex) when (ex is UnauthorizedAccessException || ex is SecurityException) { - throw new UnauthorizedException($"Access to the linked file '{filename}->{info.LinkTarget}' was denied: {ex.Message}", ex); + throw new UnauthorizedException($"Access to the linked file '{info.Name}->{info.LinkTarget}' was denied: {ex.Message}", ex); } catch (Exception ex) { - Log.Error(ex, "Failed to resolve FileInfo for linked file '{File}->{Link}': {Message}", filename, info.LinkTarget, ex.Message); - throw new IOException($"Failed to resolve FileInfo for linked file '{filename}->{info.LinkTarget}': {ex.Message}", ex); + Log.Error(ex, "Failed to resolve FileInfo for linked file '{File}->{Link}': {Message}", info.Name, info.LinkTarget, ex.Message); + throw new IOException($"Failed to resolve FileInfo for linked file '{info.Name}->{info.LinkTarget}': {ex.Message}", ex); } if (info is null) { - Log.Error("Resolved FileInfo for linked file '{File}->{Link}' was unexpectedly null", filename, info.LinkTarget); - throw new IOException($"An unexpected error was encountered while resolving FileInfo for linked file '{filename}->{info.LinkTarget}'"); + Log.Error("Resolved FileInfo for linked file '{File}->{Link}' was unexpectedly null", info.Name, info.LinkTarget); + throw new IOException($"An unexpected error was encountered while resolving FileInfo for linked file '{info.Name}->{info.LinkTarget}'"); } return info; } + /// + /// Resolves an instance of for the specified , following + /// any symlinks that may be present to their final target. A non-null return value is guaranteed. + /// + /// The fully qualified filename for which to resolve the FileInfo instance. + /// The resolved FileInfo instance. + /// Thrown if the specified filename is null or contains only whitespace. + /// Thrown if the specified or linked file is restricted. + /// Thrown if the specified or linked file can't be resolved for some reason. + public virtual FileInfo ResolveFileInfo(string filename) + { + 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 + { + info = new FileInfo(filename); + } + catch (Exception ex) when (ex is UnauthorizedAccessException || ex is SecurityException) + { + throw new UnauthorizedException($"Access to the file '{filename}' was denied: {ex.Message}", ex); + } + catch (Exception ex) + { + throw new IOException($"Failed to access file '{filename}': {ex.Message}", ex); + } + + return ResolveFileInfo(info); + } + /// /// Deletes the specified . /// -- cgit v1.2.3