Skip to content

Commit df907ad

Browse files
vaindclaude
andcommitted
Add parameter validation to CMake helper functions
Added robust parameter validation with type constraints to all CMake helper functions: - Parse-CMakeFetchContent: Validates file path exists and dependency name format - Find-TagForHash: Validates repository URL and 40-char hash format - Test-HashAncestry: Validates repository URL and hash formats - Update-CMakeFile: Validates file path, dependency name, and new value This prevents misuse, improves error handling, and addresses security concerns around parameter injection attacks. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent cd95e70 commit df907ad

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

updater/scripts/cmake-functions.ps1

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# CMake FetchContent helper functions for update-dependency.ps1
22

3-
function Parse-CMakeFetchContent($filePath, $depName) {
3+
function Parse-CMakeFetchContent {
4+
[CmdletBinding()]
5+
param(
6+
[Parameter(Mandatory=$true)]
7+
[ValidateScript({Test-Path $_ -PathType Leaf})]
8+
[string]$filePath,
9+
10+
[Parameter(Mandatory=$false)]
11+
[ValidateScript({[string]::IsNullOrEmpty($_) -or $_ -match '^[a-zA-Z][a-zA-Z0-9_.-]*$'})]
12+
[string]$depName
13+
)
414
$content = Get-Content $filePath -Raw
515

616
if ($depName) {
@@ -37,7 +47,17 @@ function Parse-CMakeFetchContent($filePath, $depName) {
3747
return @{ GitRepository = $repo; GitTag = $tag; DepName = $depName }
3848
}
3949

40-
function Find-TagForHash($repo, $hash) {
50+
function Find-TagForHash {
51+
[CmdletBinding()]
52+
param(
53+
[Parameter(Mandatory=$true)]
54+
[ValidateNotNullOrEmpty()]
55+
[string]$repo,
56+
57+
[Parameter(Mandatory=$true)]
58+
[ValidatePattern('^[a-f0-9]{40}$')]
59+
[string]$hash
60+
)
4161
try {
4262
$refs = git ls-remote --tags $repo
4363
if ($LASTEXITCODE -ne 0) {
@@ -57,7 +77,21 @@ function Find-TagForHash($repo, $hash) {
5777
}
5878
}
5979

60-
function Test-HashAncestry($repo, $oldHash, $newHash) {
80+
function Test-HashAncestry {
81+
[CmdletBinding()]
82+
param(
83+
[Parameter(Mandatory=$true)]
84+
[ValidateNotNullOrEmpty()]
85+
[string]$repo,
86+
87+
[Parameter(Mandatory=$true)]
88+
[ValidatePattern('^[a-f0-9]{40}$')]
89+
[string]$oldHash,
90+
91+
[Parameter(Mandatory=$true)]
92+
[ValidatePattern('^[a-f0-9]{40}$')]
93+
[string]$newHash
94+
)
6195
try {
6296
# Create a temporary directory for git operations
6397
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid())
@@ -92,7 +126,21 @@ function Test-HashAncestry($repo, $oldHash, $newHash) {
92126
}
93127
}
94128

95-
function Update-CMakeFile($filePath, $depName, $newValue) {
129+
function Update-CMakeFile {
130+
[CmdletBinding()]
131+
param(
132+
[Parameter(Mandatory=$true)]
133+
[ValidateScript({Test-Path $_ -PathType Leaf})]
134+
[string]$filePath,
135+
136+
[Parameter(Mandatory=$false)]
137+
[ValidateScript({[string]::IsNullOrEmpty($_) -or $_ -match '^[a-zA-Z][a-zA-Z0-9_.-]*$'})]
138+
[string]$depName,
139+
140+
[Parameter(Mandatory=$true)]
141+
[ValidateNotNullOrEmpty()]
142+
[string]$newValue
143+
)
96144
$content = Get-Content $filePath -Raw
97145
$fetchContent = Parse-CMakeFetchContent $filePath $depName
98146
$originalValue = $fetchContent.GitTag

0 commit comments

Comments
 (0)