Skip to content

Commit 5d54f5f

Browse files
CopilotMalcolmnixon
andcommitted
Improve PathHelpers validation with GetFullPath check
Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
1 parent 1fb9727 commit 5d54f5f

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/DemaConsulting.TemplateDotNetTool/PathHelpers.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ internal static string SafePathCombine(string basePath, string relativePath)
4444
// 1. relativePath doesn't contain ".." (path traversal)
4545
// 2. relativePath is not an absolute path (IsPathRooted check)
4646
// This ensures the combined path will always be under basePath
47-
return Path.Combine(basePath, relativePath);
47+
var combinedPath = Path.Combine(basePath, relativePath);
48+
49+
// Additional validation: ensure the combined path is still under the base path
50+
var fullBasePath = Path.GetFullPath(basePath);
51+
var fullCombinedPath = Path.GetFullPath(combinedPath);
52+
53+
if (!fullCombinedPath.StartsWith(fullBasePath, StringComparison.OrdinalIgnoreCase))
54+
{
55+
throw new ArgumentException($"Invalid path component: {relativePath}", nameof(relativePath));
56+
}
57+
58+
return combinedPath;
4859
}
4960
}

0 commit comments

Comments
 (0)