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.BuildMark/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
24 changes: 24 additions & 0 deletions test/DemaConsulting.BuildMark.Tests/PathHelpersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ public void PathHelpers_SafePathCombine_ValidPaths_CombinesCorrectly()
Assert.AreEqual(Path.Combine(basePath, relativePath), result);
}

/// <summary>
/// Test that SafePathCombine throws ArgumentNullException for null basePath.
/// </summary>
[TestMethod]
public void PathHelpers_SafePathCombine_NullBasePath_ThrowsArgumentNullException()
{
// Act & Assert
var exception = Assert.Throws<ArgumentNullException>(() =>
PathHelpers.SafePathCombine(null!, "subfolder/file.txt"));
Assert.AreEqual("basePath", exception.ParamName);
}

/// <summary>
/// Test that SafePathCombine throws ArgumentNullException for null relativePath.
/// </summary>
[TestMethod]
public void PathHelpers_SafePathCombine_NullRelativePath_ThrowsArgumentNullException()
{
// Act & Assert
var exception = Assert.Throws<ArgumentNullException>(() =>
PathHelpers.SafePathCombine("/home/user/project", null!));
Assert.AreEqual("relativePath", exception.ParamName);
}

/// <summary>
/// Test that SafePathCombine throws ArgumentException for path traversal with double dots.
/// </summary>
Expand Down