diff --git a/Plugin.BaseTypeExtensions.Tests/AssemblyExtensionsTests.cs b/Plugin.BaseTypeExtensions.Tests/AssemblyExtensionsTests.cs index 9ace8ba..fa8c27b 100644 --- a/Plugin.BaseTypeExtensions.Tests/AssemblyExtensionsTests.cs +++ b/Plugin.BaseTypeExtensions.Tests/AssemblyExtensionsTests.cs @@ -869,6 +869,27 @@ public void MoveManifestResourceToDirectory_ThrowsOnRootedFilename_AndWritesNoth } } + [Theory] + [InlineData(".")] + [InlineData("..")] + public void MoveManifestResourceToDirectory_ThrowsOnDotFilename_AndCreatesNoDirectory(string filename) + { + var root = CreateTempDirectory(); + try + { + var targetDirectory = Path.Join(root, "not-created"); + var assembly = typeof(AssemblyExtensionsTests).Assembly; + Action action = () => assembly.MoveManifestResourceToDirectory( + "Plugin.BaseTypeExtensions.Tests.Resources.FirstResource.txt", targetDirectory, filename); + action.Should().Throw().WithParameterName("filename"); + Directory.Exists(targetDirectory).Should().BeFalse(); + } + finally + { + Directory.Delete(root, recursive: true); + } + } + [Theory] [InlineData("../escape.txt")] [InlineData("sub/file.txt")] @@ -909,6 +930,27 @@ public async Task MoveManifestResourceToDirectoryAsync_ThrowsOnRootedFilename_An } } + [Theory] + [InlineData(".")] + [InlineData("..")] + public async Task MoveManifestResourceToDirectoryAsync_ThrowsOnDotFilename_AndCreatesNoDirectory(string filename) + { + var root = CreateTempDirectory(); + try + { + var targetDirectory = Path.Join(root, "not-created"); + var assembly = typeof(AssemblyExtensionsTests).Assembly; + Func action = () => assembly.MoveManifestResourceToDirectoryAsync( + "Plugin.BaseTypeExtensions.Tests.Resources.FirstResource.txt", targetDirectory, filename); + await action.Should().ThrowAsync().WithParameterName("filename"); + Directory.Exists(targetDirectory).Should().BeFalse(); + } + finally + { + Directory.Delete(root, recursive: true); + } + } + #endregion #region Edge Case Tests diff --git a/Plugin.BaseTypeExtensions/AssemblyExtensions.cs b/Plugin.BaseTypeExtensions/AssemblyExtensions.cs index 6f7df73..8e9c4e7 100644 --- a/Plugin.BaseTypeExtensions/AssemblyExtensions.cs +++ b/Plugin.BaseTypeExtensions/AssemblyExtensions.cs @@ -43,7 +43,7 @@ public enum FileAlreadyExistsBehavior /// Behavior when the target file already exists. /// A object representing the extracted file. /// Thrown when required parameters are null. - /// Thrown when manifestResourceName is empty or whitespace, or when filename is not a plain file name (it has a directory part). + /// Thrown when manifestResourceName is empty or whitespace, or when filename is not a plain file name (it has a directory part, or is "." or ".."). /// Thrown when the target directory cannot be created or accessed. /// Thrown when the manifest resource cannot be found. /// Thrown when file operations fail or when fileAlreadyExistsBehavior is Fail and file exists. @@ -174,7 +174,7 @@ public static FileInfo MoveManifestResourceToDirectory(this Assembly assembly, s /// A token to cancel the operation. /// A representing the asynchronous operation that returns a object for the extracted file. /// Thrown when required parameters are null. - /// Thrown when manifestResourceName is empty or whitespace, or when filename is not a plain file name (it has a directory part). + /// Thrown when manifestResourceName is empty or whitespace, or when filename is not a plain file name (it has a directory part, or is "." or ".."). /// Thrown when the target directory cannot be created or accessed. /// Thrown when the manifest resource cannot be found. /// Thrown when file operations fail or when fileAlreadyExistsBehavior is Fail and file exists. @@ -313,7 +313,7 @@ public static async Task MoveManifestResourceToDirectoryAsync(this Ass /// Behavior when the target file already exists. /// A object representing the extracted file. /// Thrown when required parameters are null. - /// Thrown when manifestResourceName is empty or whitespace, or when filename is not a plain file name (it has a directory part). + /// Thrown when manifestResourceName is empty or whitespace, or when filename is not a plain file name (it has a directory part, or is "." or ".."). /// Thrown when the cache directory cannot be created or accessed. /// Thrown when the manifest resource cannot be found. /// Thrown when file operations fail or when fileAlreadyExistsBehavior is Fail and file exists. @@ -332,7 +332,7 @@ public static FileInfo MoveManifestResourceToCache(this Assembly assembly, strin /// A token to cancel the operation. /// A representing the asynchronous operation that returns a object for the extracted file. /// Thrown when required parameters are null. - /// Thrown when manifestResourceName is empty or whitespace, or when filename is not a plain file name (it has a directory part). + /// Thrown when manifestResourceName is empty or whitespace, or when filename is not a plain file name (it has a directory part, or is "." or ".."). /// Thrown when the cache directory cannot be created or accessed. /// Thrown when the manifest resource cannot be found. /// Thrown when file operations fail or when fileAlreadyExistsBehavior is Fail and file exists. @@ -344,11 +344,13 @@ public static async Task MoveManifestResourceToCacheAsync(this Assembl /// /// Rejects names that would resolve outside the target directory: a rooted path (which - /// would let replace the directory) or one with a directory part such as ... + /// would let replace the directory), one with a directory part such as ../x, + /// or the relative segments . and .., which returns unchanged + /// but which resolve to the target directory or its parent. /// private static void ThrowIfNotPlainFileName(string filename) { - if (string.IsNullOrWhiteSpace(filename) || Path.GetFileName(filename) != filename) + if (string.IsNullOrWhiteSpace(filename) || filename is "." or ".." || Path.GetFileName(filename) != filename) { throw new ArgumentException($"Filename '{filename}' must be a plain file name without any directory part.", nameof(filename)); }