diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-10-02 18:57:13 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-10-02 18:57:13 -0500 |
| commit | 2820f93e8967d2b80e9e1c556113655ac4a5474e (patch) | |
| tree | 6a377f482ce1b986cb0935d8654dd594bf0851aa /src/slskd/Files/FileService.cs | |
| parent | ffd4a8c60476fe75d3c37d8323b51b4367fb7492 (diff) | |
| parent | 072a8338e92d154772db678cf5f182d9c6150fc3 (diff) | |
| download | slskd-2820f93e8967d2b80e9e1c556113655ac4a5474e.tar.gz | |
Merge remote-tracking branch 'origin/master' into logs
Diffstat (limited to 'src/slskd/Files/FileService.cs')
| -rw-r--r-- | src/slskd/Files/FileService.cs | 87 |
1 files changed, 53 insertions, 34 deletions
diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index e3479210..665fe919 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -68,42 +68,19 @@ namespace slskd.Files private IOptionsMonitor<Options> OptionsMonitor { get; } /// <summary> - /// Resolves an instance of <see cref="FileInfo"/> for the specified <paramref name="filename"/>, following + /// Resolves an instance of <see cref="FileInfo"/> for the specified <paramref name="info"/>, following /// any symlinks that may be present to their final target. A non-null return value is guaranteed. /// </summary> - /// <param name="filename">The fully qualified filename for which to resolve the FileInfo instance.</param> + /// <param name="info">The FileInfo instance for which to follow symlinks.</param> /// <returns>The resolved FileInfo instance.</returns> - /// <exception cref="ArgumentException">Thrown if the specified filename is null or contains only whitespace.</exception> + /// <exception cref="ArgumentException">Thrown if the specified FileInfo is null.</exception> /// <exception cref="UnauthorizedException">Thrown if the specified or linked file is restricted.</exception> /// <exception cref="IOException">Thrown if the specified or linked file can't be resolved for some reason.</exception> - 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 @@ -131,24 +108,66 @@ 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; } /// <summary> + /// Resolves an instance of <see cref="FileInfo"/> for the specified <paramref name="filename"/>, following + /// any symlinks that may be present to their final target. A non-null return value is guaranteed. + /// </summary> + /// <param name="filename">The fully qualified filename for which to resolve the FileInfo instance.</param> + /// <returns>The resolved FileInfo instance.</returns> + /// <exception cref="ArgumentException">Thrown if the specified filename is null or contains only whitespace.</exception> + /// <exception cref="UnauthorizedException">Thrown if the specified or linked file is restricted.</exception> + /// <exception cref="IOException">Thrown if the specified or linked file can't be resolved for some reason.</exception> + 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); + } + + /// <summary> /// Deletes the specified <paramref name="directories"/>. /// </summary> /// <remarks> |
