T09 · Insecure Skill Coding Practices
- Location
bin/dhl-track.ps1:569- Finding
Predictable Backup Path Can Overwrite an Unrelated File
- Content
View full analysis
Vulnerability Details
File Location:
bin/dhl-track.ps1, lines 569–607
Vulnerability Type: Predictable and unsafe temporary backup file handling
Risk Level: MediumVulnerable Code
powershell $backup = $null $storePath = Get-StorePath if (Test-Path $storePath) { $backup = $storePath + '.bak-test' Move-Item $storePath $backup -Force } try { $fixture = [PSCustomObject]@{ shipments = @( [PSCustomObject]@{ piececode = '1234567890' description = 'Test shipment' plz = '12345' international = $false addedAt = '2026-01-01T00:00:00+00:00' lastChecked = '2026-01-01T00:00:00+00:00' lastStatus = 'old status' lastStatusDate = '2026-01-01T00:00:00+00:00' progressCurrent = 1 progressMax = 5 recipientName = 'Test Recipient' recipientOrt = '12345 Teststadt' preferredDay = '' deliveryWindowFrom = '' deliveryWindowBis = '' delivered = $false events = @() } ) } Save-Json $storePath $fixture Show-All | Out-Null $store = Load-JsonOrNull $storePath if ($store.shipments.Count -ne 1) { Write-Host 'FAIL: fixture count mismatch' return } if ($store.shipments[0].lastStatus -ne 'old status') { Write-Host 'FAIL: fixture lastStatus mismatch' return } Write-Host 'PASS' } finally { if (Test-Path $storePath) { Remove-Item $storePath -Force } if ($backup -and (Test-Path $backup)) { Move-Item $backup $storePath -Force } }Technical Analysis
The
testcommand derives its backup path by appending the fixed suffix.bak-testto the con ...[truncated 1797 chars]- Remediation
View remediation
Remediation Suggestions
- Generate a unique backup name using a cryptographically random value, such as
[System.IO.Path]::GetRandomFileName(). - Place test fixtures and backups in a dedicated temporary directory rather than beside production state.
- Do not use
-Forcewhen creating the backup; fail safely if the destination unexpectedly exists. - Validate that the resolved backup and fixture paths remain within the intended temporary directory.
- Restrict
baseDirto an approved state directory if arbitrary absolute paths are not required. - Use atomic replacement or copy-and-restore operations with explicit error checks.
- Preserve and restore the original file in a way that cannot overwrite unrelated files.
- Add a regression test covering pre-existing backup-path collisions and interrupted restoration.
- Generate a unique backup name using a cryptographically random value, such as
