From 02444a36017bc094d9f28fa71a782dabb320190d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 7 Jul 2024 18:22:14 -0500 Subject: add ResolveFileInfo() --- src/slskd/Files/FileService.cs | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index c5c0382f..ccd1f904 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -23,6 +23,7 @@ namespace slskd.Files using System.Collections.Generic; using System.IO; using System.Linq; + using System.Security; using System.Threading.Tasks; using OneOf; using Serilog; @@ -50,6 +51,57 @@ namespace slskd.Files private ILogger Log { get; } = Serilog.Log.ForContext(); private IOptionsMonitor OptionsMonitor { get; } + /// + /// Resolves an instance of for the specified , following + /// any symlinks that may be present to their final target. + /// + /// 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 accessed for some reason. + public virtual FileInfo ResolveFileInfo(string filename) + { + ArgumentException.ThrowIfNullOrWhiteSpace(filename, 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); + } + + // if the above didn't throw we are guaranteed a valid instance of FileInfo regardless of whether + // the file exists or if it is a symlink. if it doesn't exist it can't be a symlink, so just return it + if (!info.Exists) + { + return info; + } + + // LinkTarget is guaranteed to be null if the file isn't a symlink. docs: + // > Gets the target path of the link located in FullName, or null if this FileSystemInfo instance doesn't represent a link. + if (info.LinkTarget is null) + { + return info; + } + + // ResolveLinkTarget returns an instance of FileSystemInfo (which is being cast to FileInfo here) as long as + // the link itself exists (which we've checked), and regardless of whether the link target itself exists. + // we should only get this far if: + // 1) the given file exists and + // 2) it is a symlink, meaning + // this line _SHOULD_ be guaranteed to return an instance of FileInfo. + return (FileInfo)info.ResolveLinkTarget(returnFinalTarget: true); + } + /// /// Deletes the specified . /// -- cgit v1.2.3 From e1ec9bd9e3eeb4e30765f06f19188c0493f8a040 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 8 Jul 2024 15:13:06 -0500 Subject: ensure non null FileInfo --- src/slskd/Files/FileService.cs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index ccd1f904..aa2e42d4 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -53,13 +53,13 @@ namespace slskd.Files /// /// Resolves an instance of for the specified , following - /// any symlinks that may be present to their final target. + /// 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 accessed for some reason. + /// 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)); @@ -99,7 +99,23 @@ namespace slskd.Files // 1) the given file exists and // 2) it is a symlink, meaning // this line _SHOULD_ be guaranteed to return an instance of FileInfo. - return (FileInfo)info.ResolveLinkTarget(returnFinalTarget: true); + try + { + info = (FileInfo)info.ResolveLinkTarget(returnFinalTarget: true); + } + catch (Exception ex) + { + Log.Error(ex, "Failed to resolve FileInfo for file '{File}': {Message}", filename, ex.Message); + throw new IOException($"Failed to resolve FileInfo for file '{filename}': {ex.Message}", ex); + } + + if (info is null) + { + Log.Error("Resolved FileInfo for file '{File}' was unexpectedly null", filename); + throw new IOException($"An unexpected error was encountered while resolving FileInfo for '{filename}'"); + } + + return info; } /// -- cgit v1.2.3 From 66c3f66ffbf888956c3b78531ccd58347abe5890 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 8 Jul 2024 15:28:25 -0500 Subject: remove symlink extensions --- src/slskd/Files/FileExtensions.cs | 21 --------------------- src/slskd/Files/FileService.cs | 3 +-- src/slskd/Relay/RelayService.cs | 1 + 3 files changed, 2 insertions(+), 23 deletions(-) (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Files/FileExtensions.cs b/src/slskd/Files/FileExtensions.cs index 21395837..4aa7a648 100644 --- a/src/slskd/Files/FileExtensions.cs +++ b/src/slskd/Files/FileExtensions.cs @@ -59,25 +59,4 @@ public static class FileExtensions return (UnixFileMode)mode; } - -#nullable enable - public static FileInfo? TryFollowSymlink(this FileInfo fileInfo) - { - try - { - return fileInfo.FollowSymlink(); - } - catch (IOException) - { - return null; - } - } - - public static FileInfo FollowSymlink(this FileInfo fileInfo) - { - FileSystemInfo fileSystemInfo = fileInfo; - fileSystemInfo = fileSystemInfo.ResolveLinkTarget(returnFinalTarget: true) ?? fileSystemInfo; - return (FileInfo)fileSystemInfo; - } -#nullable restore } \ No newline at end of file diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index aa2e42d4..eb261893 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -97,8 +97,7 @@ namespace slskd.Files // the link itself exists (which we've checked), and regardless of whether the link target itself exists. // we should only get this far if: // 1) the given file exists and - // 2) it is a symlink, meaning - // this line _SHOULD_ be guaranteed to return an instance of FileInfo. + // 2) it is a symlink, meaning this line _SHOULD_ be guaranteed to return an instance of FileInfo. try { info = (FileInfo)info.ResolveLinkTarget(returnFinalTarget: true); diff --git a/src/slskd/Relay/RelayService.cs b/src/slskd/Relay/RelayService.cs index 4595a516..7d5602a5 100644 --- a/src/slskd/Relay/RelayService.cs +++ b/src/slskd/Relay/RelayService.cs @@ -273,6 +273,7 @@ namespace slskd.Relay /// Initializes a new instance of the class. /// /// + /// /// /// /// -- cgit v1.2.3 From f602be0a4da053cf539aea609cee7962247f7793 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 8 Jul 2024 16:18:27 -0500 Subject: be more defensive --- src/slskd/Files/FileService.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index eb261893..25ef5977 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -102,16 +102,20 @@ namespace slskd.Files { info = (FileInfo)info.ResolveLinkTarget(returnFinalTarget: true); } + catch (Exception ex) when (ex is UnauthorizedAccessException || ex is SecurityException) + { + throw new UnauthorizedException($"Access to the linked file target '{info.LinkTarget}' was denied: {ex.Message}", ex); + } catch (Exception ex) { - Log.Error(ex, "Failed to resolve FileInfo for file '{File}': {Message}", filename, ex.Message); - throw new IOException($"Failed to resolve FileInfo for file '{filename}': {ex.Message}", 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); } if (info is null) { - Log.Error("Resolved FileInfo for file '{File}' was unexpectedly null", filename); - throw new IOException($"An unexpected error was encountered while resolving FileInfo for '{filename}'"); + 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}'"); } return info; -- cgit v1.2.3 From 2eb33c2727a020376883d2cb8d92b15eb79062d8 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 8 Jul 2024 16:19:49 -0500 Subject: adjust message --- src/slskd/Files/FileService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index 25ef5977..a07bd051 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -104,7 +104,7 @@ namespace slskd.Files } catch (Exception ex) when (ex is UnauthorizedAccessException || ex is SecurityException) { - throw new UnauthorizedException($"Access to the linked file target '{info.LinkTarget}' was denied: {ex.Message}", ex); + throw new UnauthorizedException($"Access to the linked file '{filename}->{info.LinkTarget}' was denied: {ex.Message}", ex); } catch (Exception ex) { -- cgit v1.2.3