-
Notifications
You must be signed in to change notification settings - Fork 0
218 lines (180 loc) · 10.5 KB
/
securitycodescan-analysis.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# This workflow integrates SecurityCodeScan with GitHub's Code Scanning feature
# SecurityCodeScan is a vulnerability patterns detector for C# and VB.NET
name: SecurityCodeScan
on:
push:
branches: [ main ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ main ]
schedule:
- cron: '29 7 * * 6'
jobs:
SCS:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: nuget/setup-nuget@v1.0.5
- uses: microsoft/setup-msbuild@v1.0.2
- name: Set up projects for analysis
#uses: security-code-scan/security-code-scan-add-action@main
shell: pwsh
run: |
cd dotnet4.5.2
$ErrorActionPreference = "Stop";
[string]$SecurityCodeScanPackageName = 'SecurityCodeScan.VS2019'
[string]$SecurityCodeScanPackageVersion = '5.1.0'
[string]$SecurityCodeScanPackagePath = $null
foreach ($projectFile in Get-ChildItem -Path . -Include *.csproj, *.vbproj -File -Recurse -Force) {
$project = [xml](Get-Content -LiteralPath $projectFile.FullName -Raw)
$propertyGroup = $project.CreateElement('PropertyGroup', $project.Project.NamespaceURI)
$errorLog = $project.CreateElement('ErrorLog', $project.Project.NamespaceURI)
$errorLog.InnerText = 'analysis.sarif'
$propertyGroup.AppendChild($errorLog) | Out-Null
$additionalFileItemNames = $project.CreateElement("AdditionalFileItemNames", $project.Project.NamespaceURI)
$additionalFileItemNames.InnerText = '$(AdditionalFileItemNames);Content'
$propertyGroup.AppendChild($additionalFileItemNames) | Out-Null
$project.Project.AppendChild($propertyGroup) | Out-Null
$project.Save($projectFile.FullName)
$packagesConfigFile = $projectFile.Directory.GetFileSystemInfos('packages.config')
if ($project.Project.Sdk -or ($project.Project.ItemGroup.PackageReference | ? { $_ }) -or (-not $packagesConfigFile.Exists)) {
$project.Project.ItemGroup.PackageReference |
Where-Object Include -like 'SecurityCodeScan*' |
ForEach-Object { $_.SelectSingleNode('..').RemoveChild($_) | Out-Null }
$itemGroup = $project.CreateElement('ItemGroup')
$packageReference = $project.CreateElement('PackageReference')
$packageReferenceInclude = $project.CreateAttribute('Include')
$packageReferenceInclude.Value = $SecurityCodeScanPackageName
$packageReference.Attributes.Append($packageReferenceInclude) | Out-Null
$packageReferenceVersion = $project.CreateAttribute('Version')
$packageReferenceVersion.Value = $SecurityCodeScanPackageVersion
$packageReference.Attributes.Append($packageReferenceVersion) | Out-Null
$packageReferencePrivateAssets = $project.CreateAttribute('PrivateAssets')
$packageReferencePrivateAssets.Value = 'All'
$packageReference.Attributes.Append($packageReferencePrivateAssets) | Out-Null
$packageReferenceIncludeAssets = $project.CreateAttribute('IncludeAssets')
$packageReferenceIncludeAssets.Value = 'runtime; build; native; contentfiles; analyzers; buildtransitive'
$packageReference.Attributes.Append($packageReferenceIncludeAssets) | Out-Null
$itemGroup.AppendChild($packageReference) | Out-Null
$project.Project.AppendChild($itemGroup) | Out-Null
if (-not $project.Project.Sdk) {
$propertyGroup = $project.CreateElement('PropertyGroup')
$restoreProjectStyle = $project.CreateElement('RestoreProjectStyle')
$restoreProjectStyle.InnerText = 'PackageReference'
$propertyGroup.AppendChild($restoreProjectStyle) | Out-Null
$project.Project.AppendChild($propertyGroup) | Out-Null
}
$project.Save($projectFile.FullName)
}
else {
if (-not $SecurityCodeScanPackagePath) {
if (-not (Test-Path packages -PathType Container)) {
$SecurityCodeScanPackagePath = (New-Item -Name packages -ItemType Directory).FullName
}
else {
$SecurityCodeScanPackagePath = (Get-Item -Path packages).FullName
}
nuget install $SecurityCodeScanPackageName -Version $SecurityCodeScanPackageVersion -OutputDirectory $SecurityCodeScanPackagePath
}
$project.Project.ItemGroup.Analyzer |
Where-Object Include -like '*SecurityCodeScan*' |
ForEach-Object { $_.SelectSingleNode('..').RemoveChild($_) | Out-Null }
$propertyGroup = $project.CreateElement('PropertyGroup', $project.Project.NamespaceURI)
$restoreProjectStyle = $project.CreateElement('RestoreProjectStyle', $project.Project.NamespaceURI)
$restoreProjectStyle.InnerText = 'PackagesConfig'
$propertyGroup.AppendChild($restoreProjectStyle) | Out-Null
$project.Project.AppendChild($propertyGroup) | Out-Null
$itemGroup = $project.CreateElement('ItemGroup', $project.Project.NamespaceURI)
$analyzer = $project.CreateElement('Analyzer', $project.Project.NamespaceURI)
$analyzerInclude = $project.CreateAttribute('Include')
$analyzerInclude.Value = Join-Path -Path $SecurityCodeScanPackagePath -ChildPath "$($SecurityCodeScanPackageName).$($SecurityCodeScanPackageVersion)\analyzers\dotnet\$($SecurityCodeScanPackageName).dll"
$analyzer.Attributes.Append($analyzerInclude) | Out-Null
$itemGroup.AppendChild($analyzer) | Out-Null
$analyzer = $project.CreateElement('Analyzer', $project.Project.NamespaceURI)
$analyzerInclude = $project.CreateAttribute('Include')
$analyzerInclude.Value = Join-Path -Path $SecurityCodeScanPackagePath -ChildPath "$($SecurityCodeScanPackageName).$($SecurityCodeScanPackageVersion)\analyzers\dotnet\YamlDotNet.dll"
$analyzer.Attributes.Append($analyzerInclude) | Out-Null
$itemGroup.AppendChild($analyzer) | Out-Null
$project.Project.AppendChild($itemGroup) | Out-Null
$project.Save($projectFile.FullName)
$packagesConfig = [xml](Get-Content -LiteralPath $packagesConfigFile.FullName -Raw)
$packagesConfig.packages.package |
Where-Object id -like '*SecurityCodeScan*' |
ForEach-Object { $_.SelectSingleNode('..').RemoveChild($_) | Out-Null }
$package = $packagesConfig.CreateElement('package')
$packageId = $packagesConfig.CreateAttribute('id')
$packageId.Value = $SecurityCodeScanPackageName
$package.Attributes.Append($packageId) | Out-Null
$packageVersion = $packagesConfig.CreateAttribute('version')
$packageVersion.Value = $SecurityCodeScanPackageVersion
$package.Attributes.Append($packageVersion) | Out-Null
$packageDevelopmentDependency = $packagesConfig.CreateAttribute('developmentDependency')
$packageDevelopmentDependency.Value = 'true'
$package.Attributes.Append($packageDevelopmentDependency) | Out-Null
$packagesConfig.packages.AppendChild($package) | Out-Null
$packagesConfig.Save($packagesConfigFile.FullName)
}
}
- name: Restore
working-directory: dotnet4.5.2
run: nuget restore -SolutionDirectory .
- name: Build
working-directory: dotnet4.5.2
run: msbuild /p:Configuration=Release
- name: Convert sarif for uploading to GitHub
# uses: security-code-scan/security-code-scan-results-action@main
shell: bash
run: |
cd dotnet4.5.2
dotnet tool install --global Sarif.Multitool --version 2.3.10
outputDir="../../results"
mkdir $outputDir
cat << EOF > convert.js
const fs = require('fs')
var args = process.argv.slice(2);
var sarif = JSON.parse(fs.readFileSync(args[0], "utf8"));
for (run of sarif.runs) {
run.tool.driver.name = "SecurityCodeScan";
run.tool.driver.fullName = "Vulnerability Patterns Detector for C# and VB.NET";
run.tool.driver.informationUri = "https://security-code-scan.github.io";
run.results = run.results.filter((e => e.ruleId.startsWith("SCS")));
run.tool.driver.rules = run.tool.driver.rules.filter((e => e.id.startsWith("SCS")));
for (let i = 0; i < run.results.length; ++i) {
run.results[i].ruleIndex = undefined;
run.results[i].relatedLocations = undefined;
if (run.results[i].locations === undefined) {
const match = run.results[i].message.text.match(/(.*) in (.*)\((\d+)\)(:.*)/);
run.results[i].message.text = match[1];
run.results[i].locations = [{
"physicalLocation" : {
"artifactLocation" : {
"uri" : "file:///" + match[2].replace(/\\\\/g, "/")
},
"region" : {
"startLine": Number(match[3]),
"startColumn": 1,
"endLine": Number(match[3]),
"endColumn": 1
}
}
}];
}
}
for (rule of run.tool.driver.rules) {
rule.shortDescription = undefined;
rule.help = { "text" : rule.helpUri};
}
run.language = undefined;
}
var converted = JSON.stringify(sarif, null, 2);
fs.writeFileSync(args[1], converted);
EOF
i=0
for sarifFile in $(find ./ -name '*.sarif')
do
sarif transform $sarifFile --output $sarifFile -f --sarif-output-version Current
node convert.js $sarifFile $sarifFile
mv $sarifFile $outputDir/$((i++)).sarif
done
- name: Upload sarif
uses: github/codeql-action/upload-sarif@v1