# ============================================================================== # ClassNotify Automated Snapshot Sanitizer & Fail-Closed Gate Test # Verifies that markdown, SQL, and other text files are 100% sanitized # and that unredacted secrets abort the release pipeline. # ============================================================================== $ErrorActionPreference = "Stop" $root = $PSScriptRoot if (-not $root) { $root = Get-Location } Write-Host "==========================================================" -ForegroundColor Cyan Write-Host " Snapshot Sanitizer & Security Gate Automated Test " -ForegroundColor Cyan Write-Host "==========================================================" -ForegroundColor Cyan $testStaging = Join-Path $root ".dummy_test_staging" if (Test-Path $testStaging) { Remove-Item $testStaging -Recurse -Force } New-Item -ItemType Directory -Path $testStaging -Force | Out-Null try { $revokedPass = "167d7d74" + "0f039de5" + "68c8c8f7" + "0083c16c" + "00d682d1" + "8d34a27c" $tokyoKey = "id_rsa_" + "tokyo" # 1. Create dummy files with fake secrets $mdFile = Join-Path $testStaging "doc_test.md" $sqlFile = Join-Path $testStaging "schema_test.sql" $envFile = Join-Path $testStaging "config_test.env" $fakePass = ";Password=" + "MySuperSecret999;" $sqlPass = ";Password=" + "SqlSecretPass888;" $envPass = ";Password=" + "EnvSecretPass777;" Set-Content -Path $mdFile -Value "Doc with revoked pass: $revokedPass and fake pass: $fakePass" -Encoding utf8 Set-Content -Path $sqlFile -Value "SELECT * FROM db; -- Connection: Host=127.0.0.1$sqlPass" -Encoding utf8 Set-Content -Path $envFile -Value "CLASSNOTIFY_TEST_DB_CONNECTION=Host=127.0.0.1$envPass" -Encoding utf8 # 2. Run the exact Sanitization routine $binaryExts = @(".exe", ".dll", ".pdb", ".apk", ".aab", ".node", ".jar", ".zip", ".tar.gz", ".png", ".jpg", ".jpeg", ".ico", ".woff", ".woff2", ".ttf", ".eot") $filesToScan = Get-ChildItem -Path $testStaging -Recurse -File | Where-Object { $binaryExts -notcontains $_.Extension.ToLower() -and $_.Name -ne "latest.zip" -and $_.Name -ne "latest-source.txt" -and $_.Name -ne "manifest.json" } foreach ($file in $filesToScan) { $content = [System.IO.File]::ReadAllText($file.FullName, [System.Text.Encoding]::UTF8) $modified = $false if ($content -match "-----BEGIN[ A-Z0-9_-]*PRIVATE KEY-----") { $content = [System.Text.RegularExpressions.Regex]::Replace($content, "-----BEGIN[ A-Z0-9_-]*PRIVATE KEY-----[\s\S]*?-----END[ A-Z0-9_-]*PRIVATE KEY-----", "") $modified = $true } if ($content -match $tokyoKey) { $content = $content.Replace($tokyoKey, "") $modified = $true } if ($content -match $revokedPass) { $content = $content.Replace($revokedPass, "[REVOKED_DB_PASSWORD_REDACTED]") $modified = $true } if ($content -match "(?i)(;\s*Password\s*=\s*)(?!') $modified = $true } if ($content -match 'CLASSNOTIFY_TEST_DB_CONNECTION=Host=[^;\r\n"'''']+') { $content = [System.Text.RegularExpressions.Regex]::Replace($content, 'CLASSNOTIFY_TEST_DB_CONNECTION=Host=[^;\r\n"'''']+', 'CLASSNOTIFY_TEST_DB_CONNECTION=') $modified = $true } if ($modified) { [System.IO.File]::WriteAllText($file.FullName, $content, [System.Text.Encoding]::UTF8) } } # 3. Assert sanitized contents $sanitizedMd = [System.IO.File]::ReadAllText($mdFile, [System.Text.Encoding]::UTF8) if ($sanitizedMd -match $revokedPass) { throw "Assertion failed: Markdown file still contains raw secret!" } if ($sanitizedMd -notmatch '\[REVOKED_DB_PASSWORD_REDACTED\]') { throw "Assertion failed: Markdown file was not properly redacted! Content: $sanitizedMd" } $sanitizedSql = [System.IO.File]::ReadAllText($sqlFile, [System.Text.Encoding]::UTF8) if ($sanitizedSql -match "SqlSecretPass888") { throw "Assertion failed: SQL file still contains raw secret!" } if ($sanitizedSql -notmatch "Password=") { throw "Assertion failed: SQL password was not redacted!" } $sanitizedEnv = [System.IO.File]::ReadAllText($envFile, [System.Text.Encoding]::UTF8) if ($sanitizedEnv -match "EnvSecretPass777") { throw "Assertion failed: Env file still contains raw secret!" } if ($sanitizedEnv -notmatch "CLASSNOTIFY_TEST_DB_CONNECTION=") { throw "Assertion failed: Test connection was not redacted!" } # 4. Verify PowerShell syntax integrity (parameter variables must not be corrupted) $ps1File = Join-Path $testStaging "script_test.ps1" Set-Content -Path $ps1File -Value 'param([string]$Username = $env:CLASSNOTIFY_E2E_USERNAME, [string]$Password = $env:CLASSNOTIFY_E2E_PASSWORD)' -Encoding utf8 $ps1Content = [System.IO.File]::ReadAllText($ps1File, [System.Text.Encoding]::UTF8) if ($ps1Content -match "(?i)(;\s*Password\s*=\s*)(?!