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
4 changes: 4 additions & 0 deletions src/DemaConsulting.VersionMark/PathHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ internal static class PathHelpers
/// <exception cref="ArgumentException">Thrown when relativePath contains invalid characters or path traversal sequences.</exception>
internal static string SafePathCombine(string basePath, string relativePath)
{
// Validate inputs
ArgumentNullException.ThrowIfNull(basePath);
ArgumentNullException.ThrowIfNull(relativePath);

// Ensure the relative path doesn't contain path traversal sequences
if (relativePath.Contains("..") || Path.IsPathRooted(relativePath))
{
Expand Down
30 changes: 30 additions & 0 deletions test/DemaConsulting.VersionMark.Tests/PathHelpersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,36 @@ namespace DemaConsulting.VersionMark.Tests;
[TestClass]
public class PathHelpersTests
{
/// <summary>
/// Test that SafePathCombine throws ArgumentNullException when basePath is null.
/// </summary>
[TestMethod]
public void PathHelpers_SafePathCombine_NullBasePath_ThrowsArgumentNullException()
{
// Arrange
string? basePath = null;
var relativePath = "subfolder/file.txt";

// Act & Assert
Assert.Throws<ArgumentNullException>(() =>
PathHelpers.SafePathCombine(basePath!, relativePath));
}

/// <summary>
/// Test that SafePathCombine throws ArgumentNullException when relativePath is null.
/// </summary>
[TestMethod]
public void PathHelpers_SafePathCombine_NullRelativePath_ThrowsArgumentNullException()
{
// Arrange
var basePath = "/home/user/project";
string? relativePath = null;

// Act & Assert
Assert.Throws<ArgumentNullException>(() =>
PathHelpers.SafePathCombine(basePath, relativePath!));
}

/// <summary>
/// Test that SafePathCombine correctly combines valid paths.
/// </summary>
Expand Down