Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Plugin.BaseTypeExtensions.Tests/AssemblyExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgumentException>().WithParameterName("filename");
Directory.Exists(targetDirectory).Should().BeFalse();
}
finally
{
Directory.Delete(root, recursive: true);
}
}

[Theory]
[InlineData("../escape.txt")]
[InlineData("sub/file.txt")]
Expand Down Expand Up @@ -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<Task> action = () => assembly.MoveManifestResourceToDirectoryAsync(
"Plugin.BaseTypeExtensions.Tests.Resources.FirstResource.txt", targetDirectory, filename);
await action.Should().ThrowAsync<ArgumentException>().WithParameterName("filename");
Directory.Exists(targetDirectory).Should().BeFalse();
}
finally
{
Directory.Delete(root, recursive: true);
}
}

#endregion

#region Edge Case Tests
Expand Down
14 changes: 8 additions & 6 deletions Plugin.BaseTypeExtensions/AssemblyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public enum FileAlreadyExistsBehavior
/// <param name="fileAlreadyExistsBehavior">Behavior when the target file already exists.</param>
/// <returns>A <see cref="FileInfo"/> object representing the extracted file.</returns>
/// <exception cref="ArgumentNullException">Thrown when required parameters are null.</exception>
/// <exception cref="ArgumentException">Thrown when manifestResourceName is empty or whitespace, or when filename is not a plain file name (it has a directory part).</exception>
/// <exception cref="ArgumentException">Thrown when manifestResourceName is empty or whitespace, or when filename is not a plain file name (it has a directory part, or is "." or "..").</exception>
/// <exception cref="DirectoryNotFoundException">Thrown when the target directory cannot be created or accessed.</exception>
/// <exception cref="FileNotFoundException">Thrown when the manifest resource cannot be found.</exception>
/// <exception cref="IOException">Thrown when file operations fail or when fileAlreadyExistsBehavior is Fail and file exists.</exception>
Expand Down Expand Up @@ -174,7 +174,7 @@ public static FileInfo MoveManifestResourceToDirectory(this Assembly assembly, s
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A <see cref="Task{FileInfo}"/> representing the asynchronous operation that returns a <see cref="FileInfo"/> object for the extracted file.</returns>
/// <exception cref="ArgumentNullException">Thrown when required parameters are null.</exception>
/// <exception cref="ArgumentException">Thrown when manifestResourceName is empty or whitespace, or when filename is not a plain file name (it has a directory part).</exception>
/// <exception cref="ArgumentException">Thrown when manifestResourceName is empty or whitespace, or when filename is not a plain file name (it has a directory part, or is "." or "..").</exception>
/// <exception cref="DirectoryNotFoundException">Thrown when the target directory cannot be created or accessed.</exception>
/// <exception cref="FileNotFoundException">Thrown when the manifest resource cannot be found.</exception>
/// <exception cref="IOException">Thrown when file operations fail or when fileAlreadyExistsBehavior is Fail and file exists.</exception>
Expand Down Expand Up @@ -313,7 +313,7 @@ public static async Task<FileInfo> MoveManifestResourceToDirectoryAsync(this Ass
/// <param name="fileAlreadyExistsBehavior">Behavior when the target file already exists.</param>
/// <returns>A <see cref="FileInfo"/> object representing the extracted file.</returns>
/// <exception cref="ArgumentNullException">Thrown when required parameters are null.</exception>
/// <exception cref="ArgumentException">Thrown when manifestResourceName is empty or whitespace, or when filename is not a plain file name (it has a directory part).</exception>
/// <exception cref="ArgumentException">Thrown when manifestResourceName is empty or whitespace, or when filename is not a plain file name (it has a directory part, or is "." or "..").</exception>
/// <exception cref="DirectoryNotFoundException">Thrown when the cache directory cannot be created or accessed.</exception>
/// <exception cref="FileNotFoundException">Thrown when the manifest resource cannot be found.</exception>
/// <exception cref="IOException">Thrown when file operations fail or when fileAlreadyExistsBehavior is Fail and file exists.</exception>
Expand All @@ -332,7 +332,7 @@ public static FileInfo MoveManifestResourceToCache(this Assembly assembly, strin
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A <see cref="Task{FileInfo}"/> representing the asynchronous operation that returns a <see cref="FileInfo"/> object for the extracted file.</returns>
/// <exception cref="ArgumentNullException">Thrown when required parameters are null.</exception>
/// <exception cref="ArgumentException">Thrown when manifestResourceName is empty or whitespace, or when filename is not a plain file name (it has a directory part).</exception>
/// <exception cref="ArgumentException">Thrown when manifestResourceName is empty or whitespace, or when filename is not a plain file name (it has a directory part, or is "." or "..").</exception>
/// <exception cref="DirectoryNotFoundException">Thrown when the cache directory cannot be created or accessed.</exception>
/// <exception cref="FileNotFoundException">Thrown when the manifest resource cannot be found.</exception>
/// <exception cref="IOException">Thrown when file operations fail or when fileAlreadyExistsBehavior is Fail and file exists.</exception>
Expand All @@ -344,11 +344,13 @@ public static async Task<FileInfo> MoveManifestResourceToCacheAsync(this Assembl

/// <summary>
/// Rejects names that would resolve outside the target directory: a rooted path (which
/// <see cref="Path.Combine(string, string)"/> would let replace the directory) or one with a directory part such as <c>..</c>.
/// <see cref="Path.Combine(string, string)"/> would let replace the directory), one with a directory part such as <c>../x</c>,
/// or the relative segments <c>.</c> and <c>..</c>, which <see cref="Path.GetFileName(string)"/> returns unchanged
/// but which resolve to the target directory or its parent.
/// </summary>
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));
}
Expand Down
Loading