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
2 changes: 1 addition & 1 deletion .claude/skills/ai-sdlc-gates/assets/agents-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Say the main thing first: the answer, the decision, or the result. Give the reason after it, and only when the
reason changes what the reader does next. Lead with the action or the result, number the steps, and end with one
next action. Cut every word that adds nothing. Say what you assume, what can go wrong, and what you have not
next action. Give the full link to every pull request, issue or page the reader must open. Cut every word that adds nothing. Say what you assume, what can go wrong, and what you have not
checked. When a request can be read in more than one way, ask one question to make it clear before you start.
Inside a run, look up what the substrate holds. Before Checkpoint Alpha, `grilling` asks the rest in rounds, and
what the person leaves open goes to Checkpoint Alpha. In the build loop, an unclear story is a stop; any other open
Expand Down
3 changes: 3 additions & 0 deletions .claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ $script:Defaults = [ordered]@{
build = ''
test = ''
}
secretScan = [ordered]@{
allowPaths = @()
}
tracker = [ordered]@{
repository = ''
keyPattern = ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,18 @@ Describe 'Create-GitleaksBaseline' {
Should -Invoke Invoke-WebRequest -Times 0 -Exactly
}

It 'scans with the downloaded configuration and redacts the findings' {
It 'scans with the downloaded rules plus the allowlists, and redacts the findings' {
Invoke-Create | Out-Null

Should -Invoke gitleaks -Times 1 -Exactly -ParameterFilter { $args[0] -eq 'git' -and ($args -contains '--redact') -and (($args -join ' ') -match 'gitleaks\.toml') }
Should -Invoke gitleaks -Times 1 -Exactly -ParameterFilter { $args[0] -eq 'git' -and ($args -contains '--redact') -and (($args -join ' ') -match 'gitleaks-ai-sdlc-[0-9a-f]{32}\.toml') }
}

It 'removes the downloaded configuration and the temporary clone afterwards' {
It 'removes the downloaded configuration, the scan config and the temporary clone afterwards' {
$r = Invoke-Create

Test-Path -LiteralPath (Join-Path $r.Work 'gitleaks.toml') | Should -BeFalse
Test-Path -LiteralPath (Join-Path $r.Temp 'gitleaksbaseline') | Should -BeFalse
@(Get-ChildItem -LiteralPath $r.Temp -Filter 'gitleaks-ai-sdlc-*.toml') | Should -BeNullOrEmpty
}

It 'downloads the rule set that <Configuration> names from its pinned URL' -ForEach @(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,18 @@ if (Test-Path -LiteralPath $tempDirectory) {
}

$failed = $false
$scanConfig = $null
try {
Write-Host "Clone a fresh copy of the repository to $tempDirectory"
git clone --quiet $remoteUrl $tempDirectory
if ($LASTEXITCODE -ne 0 -or -not (Test-Path -LiteralPath $tempDirectory)) { throw "git clone of $remoteUrl failed with exit code $LASTEXITCODE" }

# The pinned rules plus both allowlists: the one of the AI-SDLC and secretScan.allowPaths of the
# aisdlc.json in the clone, so the baseline follows what is committed, not the working copy.
Import-Module (Join-Path $PSScriptRoot 'GitleaksConfig.psm1') -Force
$scanConfig = New-GitleaksConfig -Path (Join-Path $tempDrive ('gitleaks-ai-sdlc-' + [guid]::NewGuid().ToString('n') + '.toml')) -RulesPath $gitleakconfigurationFile -RepoRoot $tempDirectory
Write-Host "Running gitleaks on the clone to create baseline file: $gitleaksBaselinePath"
gitleaks git $tempDirectory --config "$gitleakconfigurationFile" --report-format=json --redact --report-path "$gitleaksBaselinePath" --exit-code=99
gitleaks git $tempDirectory --config "$scanConfig" --report-format=json --redact --report-path "$gitleaksBaselinePath" --exit-code=99
$scanExit = $LASTEXITCODE
if ($scanExit -ne 0 -and $scanExit -ne 99) { throw "gitleaks failed with exit code $scanExit, so no baseline was written" }
}
Expand All @@ -147,6 +152,9 @@ finally {
if (Test-Path -LiteralPath $gitleakconfigurationFile) {
Remove-Item -LiteralPath $gitleakconfigurationFile -Force -ProgressAction SilentlyContinue
}
if ($scanConfig -and (Test-Path -LiteralPath $scanConfig)) {
Remove-Item -LiteralPath $scanConfig -Force -ProgressAction SilentlyContinue
}
}
if ($failed) { exit 1 }

Expand Down
74 changes: 74 additions & 0 deletions .claude/skills/ai-sdlc-gates/scripts/GitleaksConfig.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,78 @@ Describe 'GitleaksConfig' {
$findings | Should -HaveCount 1
$findings[0].File | Should -Match 'other\.json$'
}

Context 'the paths a repository names in secretScan.allowPaths' {
BeforeEach {
$script:Repo = Join-Path $TestDrive ([guid]::NewGuid().ToString('n'))
New-Item -ItemType Directory -Path $script:Repo -Force | Out-Null
}

It 'adds them as an allowlist of their own, next to the allowlist of the AI-SDLC' {
Set-Content -LiteralPath (Join-Path $script:Repo 'aisdlc.json') -Value '{ "secretScan": { "allowPaths": [ "^\\.runtime/", "(^|/)app-secrets\\.yaml$" ] } }'

$text = Get-Content -LiteralPath (New-GitleaksConfig -Path (Join-Path $TestDrive 'repo.toml') -RepoRoot $script:Repo) -Raw

([regex]::Matches($text, '(?m)^\[\[allowlists\]\]')).Count | Should -Be 2
$text | Should -Match ([regex]::Escape("'''^\.runtime/'''"))
$text | Should -Match ([regex]::Escape("'''(^|/)app-secrets\.yaml$'''"))
}

It 'writes the same config as before when aisdlc.json names no paths' {
Set-Content -LiteralPath (Join-Path $script:Repo 'aisdlc.json') -Value '{ "repository": "contoso" }'

$with = Get-Content -LiteralPath (New-GitleaksConfig -Path (Join-Path $TestDrive 'none.toml') -RepoRoot $script:Repo) -Raw
$without = Get-Content -LiteralPath (New-GitleaksConfig -Path (Join-Path $TestDrive 'plain.toml')) -Raw

$with | Should -Be $without
}

It 'refuses a pattern that is <Why>, and names the field' -ForEach @(
@{ Why = 'holding a quote'; Pattern = "^a'''b" }
@{ Why = 'not a regular expression'; Pattern = '^a[' }
) {
$json = @{ secretScan = @{ allowPaths = @($Pattern) } } | ConvertTo-Json -Depth 3
Set-Content -LiteralPath (Join-Path $script:Repo 'aisdlc.json') -Value $json

{ New-GitleaksConfig -Path (Join-Path $TestDrive 'bad.toml') -RepoRoot $script:Repo } | Should -Throw '*secretScan.allowPaths*'
}

It 'accepts any valid pattern, because the repository decides what it skips' -ForEach @(
@{ Pattern = 'app-secrets\.yaml' }
@{ Pattern = '^config/' }
@{ Pattern = '^.*' }
) {
$json = @{ secretScan = @{ allowPaths = @($Pattern) } } | ConvertTo-Json -Depth 3
Set-Content -LiteralPath (Join-Path $script:Repo 'aisdlc.json') -Value $json

$text = Get-Content -LiteralPath (New-GitleaksConfig -Path (Join-Path $TestDrive 'any.toml') -RepoRoot $script:Repo 6>$null) -Raw

$text | Should -Match ([regex]::Escape("'''$Pattern'''"))
}

It 'says which paths the scan skips, so a reviewer sees a wider list' {
Set-Content -LiteralPath (Join-Path $script:Repo 'aisdlc.json') -Value '{ "secretScan": { "allowPaths": [ "^\\.runtime/" ] } }'

$out = New-GitleaksConfig -Path (Join-Path $TestDrive 'shown.toml') -RepoRoot $script:Repo 6>&1 | Out-String

$out | Should -Match 'secretScan\.allowPaths'
$out | Should -Match ([regex]::Escape('^\.runtime/'))
}

It 'makes gitleaks skip a named path, and still find the same secret elsewhere' -Skip:(-not (Get-Command gitleaks -ErrorAction SilentlyContinue)) {
Set-Content -LiteralPath (Join-Path $script:Repo 'aisdlc.json') -Value '{ "secretScan": { "allowPaths": [ "(^|/)app-secrets\\.yaml$" ] } }'
$scan = Join-Path $script:Repo 'scan'
New-Item -ItemType Directory -Path $scan -Force | Out-Null
Set-Content -LiteralPath (Join-Path $scan 'app-secrets.yaml') -Value (New-ManifestLine)
Set-Content -LiteralPath (Join-Path $scan 'other.json') -Value (New-ManifestLine)
$config = New-GitleaksConfig -Path (Join-Path $TestDrive 'repo-scan.toml') -RepoRoot $script:Repo
$report = Join-Path $TestDrive 'repo-report.json'

gitleaks dir $scan --config $config --no-banner --report-format json --report-path $report *> $null

$findings = @(Get-Content -LiteralPath $report -Raw | ConvertFrom-Json)
$findings | Should -HaveCount 1
$findings[0].File | Should -Match 'other\.json$'
}
}
}
48 changes: 45 additions & 3 deletions .claude/skills/ai-sdlc-gates/scripts/GitleaksConfig.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
request of a consumer would fail its secret scan. The allowlist here skips that one file, in the
shared pull request job (Test-GitleaksBaseline.ps1) and in the pre-flight (Invoke-Preflight.ps1).

A repository can name more paths in secretScan.allowPaths of aisdlc.json: files that hold secrets
on purpose, because the application needs them there. With -RepoRoot, New-GitleaksConfig adds
them as a second allowlist. Without the field, the config is the same as without -RepoRoot.

New-GitleaksConfig writes a small TOML file that extends a rule set and adds the allowlist. With
-RulesPath it extends that file, for example a pinned rule set from gitleaks-rules.json. Without it,
it extends the default rules that gitleaks carries.
Expand All @@ -30,6 +34,8 @@
Test-GitleaksRulesFile -Path $file -Pin $pin
#>

Import-Module (Join-Path $PSScriptRoot 'AiSdlcConfig.psm1')

# Each entry is a regular expression on the path of a file, relative to the scanned folder.
$script:AllowedPaths = @(
'(^|/)skills/ai-sdlc-gates/sync-manifest\.json$'
Expand Down Expand Up @@ -67,11 +73,34 @@ function Get-GitleaksAllowedPath {
return $script:AllowedPaths
}

function Get-GitleaksRepoAllowedPath {
# The paths a repository names in secretScan.allowPaths of aisdlc.json: files that hold secrets on
# purpose, such as the secrets file of an application. A scan never reads them.
[CmdletBinding()]
param([Parameter(Mandatory)][string]$RepoRoot)

$config = Get-AiSdlcConfig -RepoRoot $RepoRoot
$patterns = @($config.secretScan.allowPaths | Where-Object { $_ })
# Which paths to skip is the choice of the repository. The check only stops a pattern that would
# break the TOML file or that gitleaks cannot read: a quote, a control character, or a bad regex.
foreach ($pattern in $patterns) {
$reason = if ($pattern -isnot [string]) { 'is not a string' }
elseif ($pattern -match "['`"\p{Cc}]") { 'holds a quote or a control character' }
else {
try { $null = [regex]::new($pattern) }
catch { 'is not a valid regular expression' }
}
if ($reason) { throw "secretScan.allowPaths in aisdlc.json: the pattern '$pattern' $reason. Nothing was scanned." }
}
return $patterns
}

function New-GitleaksConfig {
[CmdletBinding()]
param(
[Parameter(Mandatory)][string]$Path,
[string]$RulesPath
[string]$RulesPath,
[string]$RepoRoot
)

$extend = if ($RulesPath) {
Expand All @@ -91,9 +120,22 @@ function New-GitleaksConfig {
$paths
']'
''
) -join "`n"
)
$repoPaths = if ($RepoRoot) { @(Get-GitleaksRepoAllowedPath -RepoRoot $RepoRoot) } else { @() }
if ($repoPaths.Count -gt 0) {
Write-Host "The secret scan skips the paths in secretScan.allowPaths of aisdlc.json: $($repoPaths -join ', ')"
$toml += @(
'[[allowlists]]'
'description = "Paths the repository holds secrets in on purpose: secretScan.allowPaths in aisdlc.json."'
'paths = ['
(($repoPaths | ForEach-Object { " '''$_'''" }) -join ",`n")
']'
''
)
}
$toml = $toml -join "`n"
[IO.File]::WriteAllText($Path, $toml, [Text.UTF8Encoding]::new($false))
return $Path
}

Export-ModuleMember -Function Get-GitleaksAllowedPath, New-GitleaksConfig, Get-GitleaksRulesPin, Test-GitleaksRulesFile
Export-ModuleMember -Function Get-GitleaksAllowedPath, Get-GitleaksRepoAllowedPath, New-GitleaksConfig, Get-GitleaksRulesPin, Test-GitleaksRulesFile
2 changes: 1 addition & 1 deletion .claude/skills/ai-sdlc-gates/scripts/Invoke-Preflight.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ try {
$reportPath = Join-Path ([System.IO.Path]::GetTempPath()) ('preflight-gitleaks-' + [guid]::NewGuid().ToString('n') + '.json')
$range = (git rev-parse --verify --quiet "$TargetBranch" 2>$null) ? "$TargetBranch..HEAD" : 'HEAD~1..HEAD'
# The pinned rules of CI plus the allowlist of the AI-SDLC: the blob hashes in sync-manifest.json look like keys.
$glConfig = New-GitleaksConfig -Path (Join-Path ([System.IO.Path]::GetTempPath()) ('gitleaks-ai-sdlc-' + [guid]::NewGuid().ToString('n') + '.toml')) -RulesPath $rulesFile
$glConfig = New-GitleaksConfig -Path (Join-Path ([System.IO.Path]::GetTempPath()) ('gitleaks-ai-sdlc-' + [guid]::NewGuid().ToString('n') + '.toml')) -RulesPath $rulesFile -RepoRoot $RepoRoot
$glArgs = @('git', '--no-banner', '--redact', '--config', $glConfig, '--report-format', 'json', '--report-path', $reportPath, '--log-opts', $range)
if (Test-Path -LiteralPath $baseline) { $glArgs += @('--baseline-path', $baseline) }
Remove-Item -LiteralPath $reportPath -ErrorAction SilentlyContinue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ if (-not (Test-GitleaksRulesFile -Path $gitleakconfigurationFile -Pin $pin)) {

# The pinned rules plus the allowlist of the AI-SDLC: the blob hashes in sync-manifest.json look like keys.
$scanConfig = Join-Path ([IO.Path]::GetTempPath()) ('gitleaks-ai-sdlc-' + [guid]::NewGuid().ToString('n') + '.toml')
$null = New-GitleaksConfig -Path $scanConfig -RulesPath $gitleakconfigurationFile
$null = New-GitleaksConfig -Path $scanConfig -RulesPath $gitleakconfigurationFile -RepoRoot $currentDirectory

# A repository without a baseline is scanned in full: every finding counts.
$gitleaksArgs = @('detect', "--source=$currentDirectory", '--config', $scanConfig, '--redact', '--exit-code=99')
Expand Down
Loading
Loading