Skip to content

Commit 2ffdec8

Browse files
Fix path traversal vulnerability and add build/lint convenience scripts (#65)
* Initial plan * Fix PathHelpers security vulnerability and add build/lint scripts Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com> * Add missing sections to README (Building from Source, Project Structure, CI/CD Pipeline) Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com> * Fix path traversal vulnerability and duplicate README heading Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com> * Remove Building from Source, Project Structure, and CI/CD Pipeline sections from README Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
1 parent f7e93dc commit 2ffdec8

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

build.bat

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@echo off
2+
setlocal
3+
4+
REM Restore dependencies
5+
dotnet restore
6+
if errorlevel 1 exit /b 1
7+
8+
REM Build the project
9+
dotnet build --configuration Release
10+
if errorlevel 1 exit /b 1
11+
12+
REM Run tests
13+
dotnet test --configuration Release
14+
if errorlevel 1 exit /b 1
15+
16+
echo Build completed successfully!

build.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Restore dependencies
5+
dotnet restore
6+
7+
# Build the project
8+
dotnet build --configuration Release
9+
10+
# Run tests
11+
dotnet test --configuration Release

lint.bat

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@echo off
2+
setlocal
3+
4+
REM Run markdown linter
5+
call npx markdownlint-cli2 "**/*.md"
6+
if errorlevel 1 exit /b 1
7+
8+
REM Run spell checker
9+
call npx cspell "**/*.{md,cs}"
10+
if errorlevel 1 exit /b 1
11+
12+
REM Run YAML linter
13+
call yamllint .
14+
if errorlevel 1 exit /b 1
15+
16+
echo Linting completed successfully!

lint.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Run markdown linter
5+
npx markdownlint-cli2 "**/*.md"
6+
7+
# Run spell checker
8+
npx cspell "**/*.{md,cs}"
9+
10+
# Run YAML linter
11+
yamllint .

src/DemaConsulting.BuildMark/PathHelpers.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ 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 security validation: ensure the combined path is still under the base path.
50+
// This defense-in-depth approach protects against edge cases that might bypass the
51+
// initial validation, ensuring the final path stays within the intended directory.
52+
var fullBasePath = Path.GetFullPath(basePath);
53+
var fullCombinedPath = Path.GetFullPath(combinedPath);
54+
55+
// Use GetRelativePath to verify the relationship between paths
56+
var relativeCheck = Path.GetRelativePath(fullBasePath, fullCombinedPath);
57+
if (relativeCheck.StartsWith("..") || Path.IsPathRooted(relativeCheck))
58+
{
59+
throw new ArgumentException($"Invalid path component: {relativePath}", nameof(relativePath));
60+
}
61+
62+
return combinedPath;
4863
}
4964
}

0 commit comments

Comments
 (0)