From 26786311ffc3a01bdd15158fe0c03bb58d8391ec Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 30 Jun 2023 09:43:11 -0500 Subject: include subdirectories in directory body, return only one --- src/slskd/Files/FileService.cs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index f37c861b..1dfcb715 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -146,7 +146,7 @@ namespace slskd.Files /// The list of found contents. /// Thrown if the specified directory does not exist. /// Thrown if the specified directory is restricted. - public async Task> ListContentsAsync(string rootDirectory, string parentDirectory = null, EnumerationOptions enumerationOptions = null) + public async Task ListContentsAsync(string rootDirectory, string parentDirectory = null, EnumerationOptions enumerationOptions = null) { if (!AllowedDirectories.Any(d => rootDirectory == d)) { @@ -168,20 +168,26 @@ namespace slskd.Files var files = contents .OfType() - .Select(f => FilesystemFile.FromFileInfo(f)); + .Select(f => FilesystemFile.FromFileInfo(f) with + { + FullName = f.FullName.ReplaceFirst(rootDirectory, string.Empty).TrimStart('\\', '/'), + }); var dirs = contents .OfType() - .Prepend(dir) .Select(d => FilesystemDirectory.FromDirectoryInfo(d) with { FullName = d.FullName.ReplaceFirst(rootDirectory, string.Empty).TrimStart('\\', '/'), - Files = files - .Where(f => Path.GetDirectoryName(f.FullName) == d.FullName) - .Select(f => f with { FullName = f.FullName.ReplaceFirst(rootDirectory, string.Empty).TrimStart('\\', '/') }), }); - return dirs; + var response = FilesystemDirectory.FromDirectoryInfo(dir) with + { + FullName = dir.FullName.ReplaceFirst(rootDirectory, string.Empty).TrimStart('\\', '/'), + Files = files, + Directories = dirs, + }; + + return response; }); } } -- cgit v1.2.3 From c310a97b6372c9908757be532da5aba0a8a5e196 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 1 Jul 2023 15:11:35 -0500 Subject: refactor file service to prevent relative navigation --- src/slskd/Files/FileService.cs | 143 ++++++++++++++++++++++++++-------------- src/slskd/Files/IFileService.cs | 26 ++++---- 2 files changed, 110 insertions(+), 59 deletions(-) (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index 1dfcb715..2324a492 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -23,8 +23,10 @@ namespace slskd.Files using System.Collections.Generic; using System.IO; using System.Linq; + using System.Security; using System.Threading.Tasks; using OneOf; + using Serilog; /// /// Manages files on disk. @@ -40,7 +42,13 @@ namespace slskd.Files OptionsSnapshot = optionsSnapshot; } - private IEnumerable AllowedDirectories => new[] { OptionsSnapshot.Value.Directories.Downloads, OptionsSnapshot.Value.Directories.Incomplete }; + private IEnumerable AllowedDirectories => new[] + { + Path.GetFullPath(OptionsSnapshot.Value.Directories.Downloads), + Path.GetFullPath(OptionsSnapshot.Value.Directories.Incomplete), + }; + + private ILogger Log { get; } = Serilog.Log.ForContext(); private IOptionsSnapshot OptionsSnapshot { get; } /// @@ -50,16 +58,33 @@ namespace slskd.Files /// Returns a dictionary keyed on directory name and containing a result for each specified directory. Exceptions are /// contained in the result, and are not thrown. /// - /// The root directory. /// The directories to delete. /// The operation context. + /// Thrown if any of the specified directories have a relative path. + /// + /// Thrown if any of the directories is an exact match for an application-controlled directory. + /// /// Thrown if a specified directory does not exist. - /// Thrown if a specified directory is restricted. - public async Task>> DeleteDirectoriesAsync(string rootDirectory, params string[] directories) + /// Thrown if a specified directory is restricted. + public async Task>> DeleteDirectoriesAsync(params string[] directories) { - if (!AllowedDirectories.Any(d => rootDirectory == d)) + if (directories.Any(directory => Path.GetFullPath(directory) != directory)) { - throw new ForbiddenException($"For security reasons, only application-controlled directories can be deleted"); + throw new ArgumentException("Only absolute paths may be specified", nameof(directories)); + } + + if (directories.Any(directory => AllowedDirectories.Contains(directory))) + { + throw new ArgumentException("Deletion of application-controlled directory roots is not supported"); + } + + // 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)); + + // if any of the resolved directory paths aren't rooted in one of the allowed directories, forbid the entire request + if (!directories.All(directory => IsAllowed(directory))) + { + throw new SecurityException("Only application-controlled directories can be deleted"); } await Task.Yield(); @@ -70,16 +95,18 @@ namespace slskd.Files { try { - Directory.Delete(Path.Combine(rootDirectory, directory), recursive: true); + Directory.Delete(directory, recursive: true); results.Add(directory, true); + + Log.Information("Deleted directory '{Directory}'", directory); } - catch (FileNotFoundException) + catch (DirectoryNotFoundException) { results.Add(directory, new NotFoundException($"The directory '{directory}' does not exist")); } catch (UnauthorizedAccessException) { - results.Add(directory, new ForbiddenException()); + results.Add(directory, new SecurityException()); } catch (Exception ex) { @@ -97,16 +124,25 @@ namespace slskd.Files /// Returns a dictionary keyed on directory name and containing a result for each specified directory. Exceptions are /// contained in the result, and are not thrown. /// - /// The root directory. /// The list of files to delete. /// The operation context. + /// Thrown if any of the specified files have a relative path. /// Thrown if a specified file does not exist. - /// Thrown if a specified file is restricted. - public async Task>> DeleteFilesAsync(string rootDirectory, params string[] files) + /// Thrown if a specified file is restricted. + public async Task>> DeleteFilesAsync(params string[] files) { - if (!AllowedDirectories.Any(d => rootDirectory == d)) + if (files.Any(file => Path.GetFullPath(file) != file)) { - throw new ForbiddenException($"For security reasons, only application-controlled directories can be deleted"); + throw new ArgumentException("Only absolute paths may be specified", 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)); + + // if any of the resolved file paths aren't rooted in one of the allowed directories, forbid the entire request + if (!files.All(file => IsAllowed(file))) + { + throw new SecurityException("Only files in application-controlled directories can be deleted"); } await Task.Yield(); @@ -117,7 +153,7 @@ namespace slskd.Files { try { - File.Delete(Path.Combine(rootDirectory, file)); + File.Delete(file); results.Add(file, true); } catch (FileNotFoundException) @@ -126,7 +162,7 @@ namespace slskd.Files } catch (UnauthorizedAccessException) { - results.Add(file, new ForbiddenException()); + results.Add(file, new SecurityException()); } catch (Exception ex) { @@ -138,56 +174,67 @@ namespace slskd.Files } /// - /// Lists the contents in the specified , optionally applying the specified . + /// Lists the contents in the specified , optionally applying the specified . /// - /// The root directory. - /// An optional subdirectory from which to start the listing. + /// The directory from which to start the listing. /// Optional enumeration options to apply. /// The list of found contents. + /// Thrown if the specified directory has a relative path. /// Thrown if the specified directory does not exist. - /// Thrown if the specified directory is restricted. - public async Task ListContentsAsync(string rootDirectory, string parentDirectory = null, EnumerationOptions enumerationOptions = null) + /// Thrown if the specified root directory is restricted. + public async Task ListContentsAsync(string directory, EnumerationOptions enumerationOptions = null) { - if (!AllowedDirectories.Any(d => rootDirectory == d)) + if (Path.GetFullPath(directory) != directory) { - throw new ForbiddenException($"For security reasons, only application-controlled directories can be deleted"); + throw new ArgumentException("Only absolute paths may be specified", nameof(directory)); } - var fullDirectory = parentDirectory is not null ? Path.Combine(rootDirectory, parentDirectory) : rootDirectory; + // important! we must fully expand the path with GetFullPath() to resolve a given relative directory, like '..' + if (!AllowedDirectories.Any(allowed => directory.StartsWith(allowed))) + { + throw new SecurityException($"For security reasons, only application-controlled directories can be deleted"); + } - if (!Directory.Exists(fullDirectory)) + if (!Directory.Exists(directory)) { - throw new NotFoundException($"The directory '{fullDirectory}' does not exist"); + throw new NotFoundException($"The directory '{directory}' does not exist"); } return await Task.Run(() => { - var dir = new DirectoryInfo(fullDirectory); - - var contents = dir.GetFileSystemInfos("*", enumerationOptions); - - var files = contents - .OfType() - .Select(f => FilesystemFile.FromFileInfo(f) with - { - FullName = f.FullName.ReplaceFirst(rootDirectory, string.Empty).TrimStart('\\', '/'), - }); + var dir = new DirectoryInfo(directory); - var dirs = contents - .OfType() - .Select(d => FilesystemDirectory.FromDirectoryInfo(d) with + try + { + var contents = dir.GetFileSystemInfos("*", enumerationOptions); + + var files = contents + .OfType() + .Select(f => FilesystemFile.FromFileInfo(f) with + { + FullName = f.FullName.ReplaceFirst(directory, string.Empty).TrimStart('\\', '/'), + }); + + var dirs = contents + .OfType() + .Select(d => FilesystemDirectory.FromDirectoryInfo(d) with + { + FullName = d.FullName.ReplaceFirst(directory, string.Empty).TrimStart('\\', '/'), + }); + + var response = FilesystemDirectory.FromDirectoryInfo(dir) with { - FullName = d.FullName.ReplaceFirst(rootDirectory, string.Empty).TrimStart('\\', '/'), - }); + FullName = dir.FullName.ReplaceFirst(directory, string.Empty).TrimStart('\\', '/'), + Files = files, + Directories = dirs, + }; - var response = FilesystemDirectory.FromDirectoryInfo(dir) with + return response; + } + catch (UnauthorizedAccessException ex) { - FullName = dir.FullName.ReplaceFirst(rootDirectory, string.Empty).TrimStart('\\', '/'), - Files = files, - Directories = dirs, - }; - - return response; + throw new SecurityException($"Access to '{directory}' was denied", ex); + } }); } } diff --git a/src/slskd/Files/IFileService.cs b/src/slskd/Files/IFileService.cs index 2d4d66f5..63b5f1a3 100644 --- a/src/slskd/Files/IFileService.cs +++ b/src/slskd/Files/IFileService.cs @@ -20,6 +20,7 @@ namespace slskd.Files using System; using System.Collections.Generic; using System.IO; + using System.Security; using System.Threading.Tasks; using OneOf; @@ -35,12 +36,15 @@ namespace slskd.Files /// Returns a dictionary keyed on directory name and containing a result for each specified directory. Exceptions are /// contained in the result, and are not thrown. /// - /// The root directory. /// The directories to delete. /// The operation context. + /// Thrown if any of the specified directories have a relative path. + /// + /// Thrown if any of the directories is an exact match for an application-controlled directory. + /// /// Thrown if a specified directory does not exist. - /// Thrown if a specified directory is restricted. - Task>> DeleteDirectoriesAsync(string rootDirectory, params string[] directories); + /// Thrown if a specified directory is restricted. + Task>> DeleteDirectoriesAsync(params string[] directories); /// /// Deletes the specified . @@ -49,22 +53,22 @@ namespace slskd.Files /// Returns a dictionary keyed on directory name and containing a result for each specified directory. Exceptions are /// contained in the result, and are not thrown. /// - /// The root directory. /// The list of files to delete. /// The operation context. + /// Thrown if any of the specified files have a relative path. /// Thrown if a specified file does not exist. - /// Thrown if a specified file is restricted. - Task>> DeleteFilesAsync(string rootDirectory, params string[] files); + /// Thrown if a specified file is restricted. + Task>> DeleteFilesAsync(params string[] files); /// - /// Lists the contents in the specified , optionally applying the specified . + /// Lists the contents in the specified , optionally applying the specified . /// - /// The root directory. - /// An optional subdirectory from which to start the listing. + /// The directory from which to start the listing. /// Optional enumeration options to apply. /// The list of found contents. + /// Thrown if the specified directory has a relative path. /// Thrown if the specified directory does not exist. - /// Thrown if the specified root directory is restricted. - Task ListContentsAsync(string rootDirectory, string parentDirectory = null, EnumerationOptions enumerationOptions = null); + /// Thrown if the specified root directory is restricted. + Task ListContentsAsync(string directory, EnumerationOptions enumerationOptions = null); } } \ No newline at end of file -- cgit v1.2.3 From da070602c8db9ec39b447c34857f2a8434e9cdd0 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 2 Jul 2023 12:22:39 -0500 Subject: add tests to ensure only desirable paths can be accessed --- .../Common/Exceptions/UnauthorizedException.cs | 72 +++++++++++ src/slskd/Files/FileService.cs | 22 ++-- src/slskd/Files/IFileService.cs | 6 +- tests/slskd.Tests.Unit/Files/FileServiceTests.cs | 137 +++++++++++++++++++++ 4 files changed, 223 insertions(+), 14 deletions(-) create mode 100644 src/slskd/Common/Exceptions/UnauthorizedException.cs create mode 100644 tests/slskd.Tests.Unit/Files/FileServiceTests.cs (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Common/Exceptions/UnauthorizedException.cs b/src/slskd/Common/Exceptions/UnauthorizedException.cs new file mode 100644 index 00000000..a7179c80 --- /dev/null +++ b/src/slskd/Common/Exceptions/UnauthorizedException.cs @@ -0,0 +1,72 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd +{ + using System; + using System.Diagnostics.CodeAnalysis; + using System.Runtime.Serialization; + + /// + /// Represents errors that originate when a restricted resources is accessed without authorization. + /// + [ExcludeFromCodeCoverage] + [Serializable] + public class UnauthorizedException : SlskdException + { + /// + /// Initializes a new instance of the class. + /// + public UnauthorizedException() + : base() + { + } + + /// + /// Initializes a new instance of the class with a specified error message. + /// + /// The message that describes the error. + public UnauthorizedException(string message) + : base(message) + { + } + + /// + /// Initializes a new instance of the class with a specified error message and a + /// reference to the inner exception that is the cause of this exception. + /// + /// The message that describes the error. + /// + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no + /// inner exception is specified. + /// + public UnauthorizedException(string message, Exception innerException) + : base(message, innerException) + { + } + + /// + /// Initializes a new instance of the class with serialized data. + /// + /// The SerializationInfo that holds the serialized object data about the exception being thrown. + /// The StreamingContext that contains contextual information about the source or destination. + protected UnauthorizedException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +} \ No newline at end of file diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index 2324a492..c820220f 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -65,7 +65,7 @@ namespace slskd.Files /// Thrown if any of the directories is an exact match for an application-controlled directory. /// /// Thrown if a specified directory does not exist. - /// Thrown if a specified directory is restricted. + /// Thrown if a specified directory is restricted. public async Task>> DeleteDirectoriesAsync(params string[] directories) { if (directories.Any(directory => Path.GetFullPath(directory) != directory)) @@ -84,7 +84,7 @@ namespace slskd.Files // if any of the resolved directory paths aren't rooted in one of the allowed directories, forbid the entire request if (!directories.All(directory => IsAllowed(directory))) { - throw new SecurityException("Only application-controlled directories can be deleted"); + throw new UnauthorizedException("Only application-controlled directories can be deleted"); } await Task.Yield(); @@ -104,9 +104,9 @@ namespace slskd.Files { results.Add(directory, new NotFoundException($"The directory '{directory}' does not exist")); } - catch (UnauthorizedAccessException) + catch (UnauthorizedAccessException ex) { - results.Add(directory, new SecurityException()); + results.Add(directory, new UnauthorizedException($"Deletion of the directory '{directory}' was denied: {ex.Message}", ex)); } catch (Exception ex) { @@ -128,7 +128,7 @@ namespace slskd.Files /// The operation context. /// Thrown if any of the specified files have a relative path. /// Thrown if a specified file does not exist. - /// Thrown if a specified file is restricted. + /// Thrown if a specified file is restricted. public async Task>> DeleteFilesAsync(params string[] files) { if (files.Any(file => Path.GetFullPath(file) != file)) @@ -142,7 +142,7 @@ namespace slskd.Files // if any of the resolved file paths aren't rooted in one of the allowed directories, forbid the entire request if (!files.All(file => IsAllowed(file))) { - throw new SecurityException("Only files in application-controlled directories can be deleted"); + throw new UnauthorizedException("Only files in application-controlled directories can be deleted"); } await Task.Yield(); @@ -160,9 +160,9 @@ namespace slskd.Files { results.Add(file, new NotFoundException($"The file '{file}' does not exist")); } - catch (UnauthorizedAccessException) + catch (UnauthorizedAccessException ex) { - results.Add(file, new SecurityException()); + results.Add(file, new UnauthorizedException($"Deletion of the file '{file}' was denied: {ex.Message}", ex)); } catch (Exception ex) { @@ -181,7 +181,7 @@ namespace slskd.Files /// The list of found contents. /// Thrown if the specified directory has a relative path. /// Thrown if the specified directory does not exist. - /// Thrown if the specified root directory is restricted. + /// Thrown if the specified root directory is restricted. public async Task ListContentsAsync(string directory, EnumerationOptions enumerationOptions = null) { if (Path.GetFullPath(directory) != directory) @@ -192,7 +192,7 @@ namespace slskd.Files // important! we must fully expand the path with GetFullPath() to resolve a given relative directory, like '..' if (!AllowedDirectories.Any(allowed => directory.StartsWith(allowed))) { - throw new SecurityException($"For security reasons, only application-controlled directories can be deleted"); + throw new UnauthorizedException($"Only application-controlled directories can be deleted"); } if (!Directory.Exists(directory)) @@ -233,7 +233,7 @@ namespace slskd.Files } catch (UnauthorizedAccessException ex) { - throw new SecurityException($"Access to '{directory}' was denied", ex); + throw new UnauthorizedException($"Access to directory '{directory}' was denied: {ex.Message}", ex); } }); } diff --git a/src/slskd/Files/IFileService.cs b/src/slskd/Files/IFileService.cs index 63b5f1a3..89d4ce76 100644 --- a/src/slskd/Files/IFileService.cs +++ b/src/slskd/Files/IFileService.cs @@ -43,7 +43,7 @@ namespace slskd.Files /// Thrown if any of the directories is an exact match for an application-controlled directory. /// /// Thrown if a specified directory does not exist. - /// Thrown if a specified directory is restricted. + /// Thrown if a specified directory is restricted. Task>> DeleteDirectoriesAsync(params string[] directories); /// @@ -57,7 +57,7 @@ namespace slskd.Files /// The operation context. /// Thrown if any of the specified files have a relative path. /// Thrown if a specified file does not exist. - /// Thrown if a specified file is restricted. + /// Thrown if a specified file is restricted. Task>> DeleteFilesAsync(params string[] files); /// @@ -68,7 +68,7 @@ namespace slskd.Files /// The list of found contents. /// Thrown if the specified directory has a relative path. /// Thrown if the specified directory does not exist. - /// Thrown if the specified root directory is restricted. + /// Thrown if the specified root directory is restricted. Task ListContentsAsync(string directory, EnumerationOptions enumerationOptions = null); } } \ No newline at end of file diff --git a/tests/slskd.Tests.Unit/Files/FileServiceTests.cs b/tests/slskd.Tests.Unit/Files/FileServiceTests.cs new file mode 100644 index 00000000..7c9931ab --- /dev/null +++ b/tests/slskd.Tests.Unit/Files/FileServiceTests.cs @@ -0,0 +1,137 @@ +using Microsoft.Extensions.Options; + +namespace slskd.Tests.Unit.Files +{ + using System; + using System.IO; + using System.Threading.Tasks; + using Moq; + using slskd.Files; + using Xunit; + + public class FileServiceTests : IDisposable + { + public FileServiceTests() + { + OptionsSnapshotMock = new Mock>(); + + Temp = Path.Combine(Path.GetTempPath(), $"slskd.test.{Guid.NewGuid()}"); + Directory.CreateDirectory(Temp); + + FileService = new FileService( + optionsSnapshot: OptionsSnapshotMock.Object); + } + + public void Dispose() + { + Directory.Delete(Temp, recursive: true); + } + + private Mock> OptionsSnapshotMock { get; init; } + private string Temp { get; init; } + private IFileService FileService { get; init; } + + [Fact] + public async Task ListContentsAsync_Throws_ArgumentException_Given_Relative_Path() + { + var ex = await Record.ExceptionAsync(() => FileService.ListContentsAsync(directory: "../")); + + Assert.NotNull(ex); + Assert.IsType(ex); + Assert.Equal("directory", ((ArgumentException)ex).ParamName); + } + + [Fact] + public async Task ListContentsAsync_Throws_UnauthorizedException_Given_Disallowed_Directory() + { + OptionsSnapshotMock.Setup(o => o.Value).Returns(new Options + { + Directories = new Options.DirectoriesOptions + { + Downloads = Path.Combine(Temp, "downloads"), + Incomplete = Path.Combine(Temp, "incomplete"), + } + }); + + var ex = await Record.ExceptionAsync(() => FileService.ListContentsAsync(directory: Path.Combine(Temp, "foo"))); + + Assert.NotNull(ex); + Assert.IsType(ex); + } + + [Fact] + public async Task ListContentsAsync_Throws_NotFoundException_Given_NonExistent_Directory() + { + OptionsSnapshotMock.Setup(o => o.Value).Returns(new Options + { + Directories = new Options.DirectoriesOptions + { + Downloads = Path.Combine(Temp, "downloads"), + Incomplete = Path.Combine(Temp, "incomplete"), + } + }); + + var ex = await Record.ExceptionAsync(() => FileService.ListContentsAsync(directory: Path.Combine(Temp, "downloads", "foo"))); + + Assert.NotNull(ex); + Assert.IsType(ex); + } + + [Fact] + public async Task DeleteDirectoriesAsync_Throws_ArgumentException_Given_Relative_Path() + { + var ex = await Record.ExceptionAsync(() => FileService.DeleteDirectoriesAsync("../foo")); + + Assert.NotNull(ex); + Assert.IsType(ex); + Assert.Equal("directories", ((ArgumentException)ex).ParamName); + } + + [Fact] + public async Task DeleteDirectoriesAsync_Throws_ArgumentException_Given_Disallowed_Path() + { + OptionsSnapshotMock.Setup(o => o.Value).Returns(new Options + { + Directories = new Options.DirectoriesOptions + { + Downloads = Path.Combine(Temp, "downloads"), + Incomplete = Path.Combine(Temp, "incomplete"), + } + }); + + var ex = await Record.ExceptionAsync(() => FileService.DeleteDirectoriesAsync(Path.Combine(Temp, "foo"))); + + Assert.NotNull(ex); + Assert.IsType(ex); + } + + [Fact] + public async Task DeleteFilesAsync_Throws_ArgumentException_Given_Relative_Path() + { + var ex = await Record.ExceptionAsync(() => FileService.DeleteFilesAsync("../foo.bar")); + + Assert.NotNull(ex); + Assert.IsType(ex); + Assert.Equal("files", ((ArgumentException)ex).ParamName); + } + + [Fact] + public async Task DeleteFilesAsync_Throws_ArgumentException_Given_Disallowed_Path() + { + OptionsSnapshotMock.Setup(o => o.Value).Returns(new Options + { + Directories = new Options.DirectoriesOptions + { + Downloads = Path.Combine(Temp, "downloads"), + Incomplete = Path.Combine(Temp, "incomplete"), + } + }); + + var ex = await Record.ExceptionAsync(() => FileService.DeleteFilesAsync(Path.Combine(Temp, "foo"))); + + Assert.NotNull(ex); + Assert.IsType(ex); + } + } +} + -- cgit v1.2.3