Skip to content

Commit 89e8399

Browse files
Updating build to check for vulnerabilities (#1026)
* Add Check-CsprojVulnerabilities.ps1 script * Do not print report by default * Add check for security vulnerabilities stage in the pipeline * Update test projects dependencies
1 parent 557b673 commit 89e8399

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

Check-CsprojVulnerabilities.ps1

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
param
2+
(
3+
[String[]]
4+
$CsprojFilePath,
5+
6+
[switch]
7+
$PrintReport
8+
)
9+
10+
if (-not $CsprojFilePath)
11+
{
12+
$CsprojFilePath = @(
13+
"$PSScriptRoot/src/Microsoft.Azure.Functions.PowerShellWorker.csproj"
14+
"$PSScriptRoot/test/Unit/Microsoft.Azure.Functions.PowerShellWorker.Test.csproj"
15+
"$PSScriptRoot/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E.csproj"
16+
)
17+
}
18+
19+
$logFilePath = "$PSScriptRoot/build.log"
20+
21+
try
22+
{
23+
foreach ($projectFilePath in $CsprojFilePath)
24+
{
25+
Write-Host "Analyzing '$projectFilePath' for vulnerabilities..."
26+
27+
$projectFolder = Split-Path $projectFilePath
28+
29+
Push-Location $projectFolder
30+
& { dotnet restore $projectFilePath }
31+
& { dotnet list $projectFilePath package --include-transitive --vulnerable } 3>&1 2>&1 > $logFilePath
32+
Pop-Location
33+
34+
# Check and report if vulnerabilities are found
35+
$report = Get-Content $logFilePath -Raw
36+
$result = $report | Select-String "has no vulnerable packages given the current sources"
37+
38+
if ($result)
39+
{
40+
Write-Host "No vulnerabilities found"
41+
}
42+
else
43+
{
44+
$output = [System.Environment]::NewLine + "Vulnerabilities found!"
45+
if ($PrintReport.IsPresent)
46+
{
47+
$output += $report
48+
}
49+
50+
Write-Host $output -ForegroundColor Red
51+
Exit 1
52+
}
53+
Write-Host ""
54+
}
55+
}
56+
finally
57+
{
58+
if (Test-Path $logFilePath)
59+
{
60+
Remove-Item $logFilePath -Force
61+
}
62+
}

azure-pipelines.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ steps:
4949
- pwsh: ./build.ps1 -NoBuild -Bootstrap
5050
displayName: 'Running ./build.ps1 -NoBuild -Bootstrap'
5151

52+
- pwsh: ./Check-CsprojVulnerabilities.ps1
53+
displayName: 'Check for security vulnerabilities'
54+
5255
- pwsh: |
5356
$ErrorActionPreference = "Stop"
5457

test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
1414
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
1515
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
16+
<PackageReference Include="System.Net.Http" Version="4.3.4" />
17+
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
1618
<PackageReference Include="xunit" Version="2.4.2" />
1719
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
1820
</ItemGroup>

test/Unit/Microsoft.Azure.Functions.PowerShellWorker.Test.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
1313
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.4.0" />
1414
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0-2.final" />
15+
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
16+
<PackageReference Include="System.Net.Http" Version="4.3.4" />
1517
</ItemGroup>
1618

1719
<ItemGroup>

0 commit comments

Comments
 (0)