forked from security-code-scan/security-code-scan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
643 lines (553 loc) · 27.8 KB
/
Program.cs
File metadata and controls
643 lines (553 loc) · 27.8 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis.MSBuild;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Collections.Immutable;
using System.Collections.Generic;
using SecurityCodeScan.Analyzers.Taint;
using System.Reflection;
using Microsoft.CodeAnalysis;
using System.Globalization;
using Mono.Options;
using SecurityCodeScan.Config;
using System.Text.RegularExpressions;
using System.Collections.Concurrent;
using Microsoft.CodeAnalysis.Text;
using System.Text;
using System.Threading.Tasks.Dataflow;
using System.Diagnostics;
using DotNet.Globbing;
using SecurityCodeScan.Analyzers.Locale;
namespace SecurityCodeScan.Tool
{
internal abstract class Runner
{
protected int _analysis_warnings = 0;
protected int _errors = 0;
protected int _warnings = 0;
private List<DiagnosticAnalyzer> _analyzers;
protected Func<ImmutableArray<Diagnostic>, ParsedOptions, ConcurrentDictionary<string, DiagnosticDescriptor>, SarifV2ErrorLogger, (int, int, int)> _logDiagnostics;
protected ParsedOptions _parsedOptions;
protected ConcurrentDictionary<string, DiagnosticDescriptor> _descriptors;
protected SarifV2ErrorLogger _logger;
public Runner(
List<DiagnosticAnalyzer> analyzers,
Func<ImmutableArray<Diagnostic>, ParsedOptions, ConcurrentDictionary<string, DiagnosticDescriptor>, SarifV2ErrorLogger, (int, int, int)> logDiagnostics,
ParsedOptions parsedOptions,
ConcurrentDictionary<string, DiagnosticDescriptor> descriptors,
SarifV2ErrorLogger logger)
{
_analyzers = analyzers;
_logDiagnostics = logDiagnostics;
_parsedOptions = parsedOptions;
_descriptors = descriptors;
_logger = logger;
}
public abstract Task Run(Project project);
public virtual async Task<(int, int, int)> WaitForCompletion()
{
return await Task.FromResult((_analysis_warnings, _errors, _warnings)).ConfigureAwait(false);
}
protected async Task<ImmutableArray<Diagnostic>> GetDiagnostics(Project project)
{
var compilation = await project.GetCompilationAsync().ConfigureAwait(false);
var compilationWithAnalyzers = compilation.WithAnalyzers(_analyzers.ToImmutableArray(), project.AnalyzerOptions);
return await compilationWithAnalyzers.GetAllDiagnosticsAsync().ConfigureAwait(false);
}
}
internal class SingleThreadRunner : Runner
{
private bool _verbose;
public SingleThreadRunner(
bool verbose,
List<DiagnosticAnalyzer> analyzers,
Func<ImmutableArray<Diagnostic>, ParsedOptions, ConcurrentDictionary<string, DiagnosticDescriptor>, SarifV2ErrorLogger, (int, int, int)> logDiagnostics,
ParsedOptions parsedOptions,
ConcurrentDictionary<string, DiagnosticDescriptor> descriptors,
SarifV2ErrorLogger logger)
: base(analyzers, logDiagnostics, parsedOptions, descriptors, logger)
{
_verbose = verbose;
}
public override async Task Run(Project project)
{
if (_verbose)
Console.WriteLine($"Starting: {project.FilePath}");
var diagnostics = await GetDiagnostics(project).ConfigureAwait(false);
(var analysisWarnings, var errors, var warnings) = _logDiagnostics(diagnostics, _parsedOptions, _descriptors, _logger);
_analysis_warnings += analysisWarnings;
_errors += errors;
_warnings += warnings;
}
}
internal class MultiThreadRunner : Runner
{
private TransformBlock<Project, ImmutableArray<Diagnostic>> _scanBlock;
private ActionBlock<ImmutableArray<Diagnostic>> _resultsBlock;
public MultiThreadRunner(
bool verbose,
List<DiagnosticAnalyzer> analyzers,
Func<ImmutableArray<Diagnostic>, ParsedOptions, ConcurrentDictionary<string, DiagnosticDescriptor>, SarifV2ErrorLogger, (int, int, int)> logDiagnostics,
ParsedOptions parsedOptions,
ConcurrentDictionary<string, DiagnosticDescriptor> descriptors,
SarifV2ErrorLogger logger,
int threads)
: base(analyzers, logDiagnostics, parsedOptions, descriptors, logger)
{
_scanBlock = new TransformBlock<Project, ImmutableArray<Diagnostic>>(async project =>
{
if (verbose)
Console.WriteLine($"Starting: {project.FilePath}");
return await GetDiagnostics(project).ConfigureAwait(false);
},
new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = threads,
EnsureOrdered = false,
BoundedCapacity = 32
});
_resultsBlock = new ActionBlock<ImmutableArray<Diagnostic>>(diagnostics =>
{
(var analysisWarnings, var errors, var warnings) = logDiagnostics(diagnostics, _parsedOptions, descriptors, logger);
_analysis_warnings += analysisWarnings;
_errors += errors;
_warnings += warnings;
},
new ExecutionDataflowBlockOptions
{
EnsureOrdered = false
});
_scanBlock.LinkTo(_resultsBlock, new DataflowLinkOptions { PropagateCompletion = true });
}
public override async Task Run(Project project)
{
if (!await _scanBlock.SendAsync(project).ConfigureAwait(false))
{
throw new Exception("Thread synchronization error.");
}
}
public override async Task<(int, int, int)> WaitForCompletion()
{
_scanBlock.Complete();
await _resultsBlock.Completion.ConfigureAwait(false);
return await base.WaitForCompletion().ConfigureAwait(false);
}
}
internal class ParsedOptions
{
public string solutionPath = null;
public string sarifFile = null;
public string config = null;
public int? threads = null;
public bool shouldShowHelp = false;
public bool verbose = false;
public bool ignoreMsBuildErrors = false;
public bool ignoreCompilerErrors = false;
public bool showBanner = true;
public bool cwe = false;
public bool failOnWarning = false;
public HashSet<string> excludeWarnings = new HashSet<string>();
public HashSet<string> includeWarnings = new HashSet<string>();
public List<Glob> excludeProjects = new List<Glob>();
public List<Glob> includeProjects = new List<Glob>();
public string sdkPath = null;
public OptionSet inputOptions = null;
public void Parse(string[] args)
{
try
{
string includeWarningsList = null;
string excludeWarningsList = null;
string includeProjectsList = null;
string excludeProjectsList = null;
inputOptions = new OptionSet
{
{ "<>", "(Required) solution or project path", r => { solutionPath = r; } },
{ "w|excl-warn=", "(Optional) semicolon delimited list of warnings to exclude", r => { excludeWarningsList = r; } },
{ "incl-warn=", "(Optional) semicolon delimited list of warnings to include", r => { includeWarningsList = r; } },
{ "p|excl-proj=", "(Optional) semicolon delimited list of glob project patterns to exclude", r => { excludeProjectsList = r; } },
{ "incl-proj=", "(Optional) semicolon delimited list of glob project patterns to include", r => { includeProjectsList = r; } },
{ "x|export=", "(Optional) SARIF file path", r => { sarifFile = r; } },
{ "c|config=", "(Optional) path to additional configuration file", r => { config = r; } },
{ "cwe", "(Optional) show CWE IDs", r => { cwe = r != null; } },
{ "t|threads=", "(Optional) run analysis in parallel (experimental)", (int r) => { threads = r; } },
{ "sdk-path=", "(Optional) Path to .NET SDK to use.", r => { sdkPath = r; } },
{ "ignore-msbuild-errors", "(Optional) Don't stop on MSBuild errors", r => { ignoreMsBuildErrors = r != null; } },
{ "ignore-compiler-errors", "(Optional) Don't exit with non-zero code on compilation errors", r => { ignoreCompilerErrors = r != null; } },
{ "f|fail-any-warn","(Optional) fail on security warnings with non-zero exit code", r => { failOnWarning = r != null; } },
{ "n|no-banner", "(Optional) don't show the banner", r => { showBanner = r == null; } },
{ "v|verbose", "(Optional) more diagnostic messages", r => { verbose = r != null; } },
{ "h|?|help", "show this message and exit", h => shouldShowHelp = h != null },
};
inputOptions.Parse(args);
void SplitBy<T>(bool toUpper, char separator, ICollection<T> outContainer, string delimitedList, Func<string, T> factory)
{
if (delimitedList == null)
return;
if (toUpper)
delimitedList = delimitedList.ToUpperInvariant();
foreach (var item in delimitedList.Split(separator))
{
outContainer.Add(factory(item.Trim()));
}
}
SplitBy(true, ';', excludeWarnings, excludeWarningsList, x => x);
SplitBy(true, ';', includeWarnings, includeWarningsList, x => x);
SplitBy(false, ';', excludeProjects, excludeProjectsList, x => Glob.Parse(x));
SplitBy(false, ';', includeProjects, includeProjectsList, x => Glob.Parse(x));
}
catch
{
shouldShowHelp = true;
}
}
}
internal class Program
{
private static readonly Regex ProjRegex = new Regex(@"^.*'([^']+\.[a-z]+)'.*$", RegexOptions.Compiled);
private static async Task<int> Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
var startTime = DateTime.Now;
var versionString = FileVersionInfo.GetVersionInfo(Assembly.GetEntryAssembly().Location).FileVersion;
var parsedOptions = new ParsedOptions();
parsedOptions.Parse(args);
if (parsedOptions.showBanner)
{
Console.WriteLine($@"
╔═╗┌─┐┌─┐┬ ┬┬─┐┬┌┬┐┬ ┬ ╔═╗┌─┐┌┬┐┌─┐ ╔═╗┌─┐┌─┐┌┐┌
╚═╗├┤ │ │ │├┬┘│ │ └┬┘ ║ │ │ ││├┤ ╚═╗│ ├─┤│││
╚═╝└─┘└─┘└─┘┴└─┴ ┴ ┴ ╚═╝└─┘─┴┘└─┘ ╚═╝└─┘┴ ┴┘└┘
.NET tool by Jaroslav Lobačevski v{versionString}");
Console.WriteLine("\n");
}
if (parsedOptions.includeWarnings.Any() && parsedOptions.excludeWarnings.Any())
{
LogError(false, "\nOnly --excl-warn or --incl-warn should be specified.\n");
parsedOptions.shouldShowHelp = true;
}
if (parsedOptions.excludeProjects.Any() && parsedOptions.includeProjects.Any())
{
LogError(false, "\nOnly --excl-proj or --incl-proj should be specified.\n");
parsedOptions.shouldShowHelp = true;
}
if (parsedOptions.shouldShowHelp || parsedOptions.solutionPath == null)
{
var name = AppDomain.CurrentDomain.FriendlyName;
Console.WriteLine("\nUsage:\n");
parsedOptions.inputOptions.WriteOptionDescriptions(Console.Out);
Console.WriteLine("\nExample:\n");
Console.WriteLine($" {name} my.sln/my.csproj --excl-proj=**/*Test*/** --export=out.sarif --excl-warn=SCS1234;SCS2345 --config=setting.yml");
return 1;
}
var returnCode = 0;
// Attempt to set the version of MSBuild.
if (parsedOptions.sdkPath != null)
{
void ApplyDotNetSdkEnvironmentVariables(string dotNetSdkPath)
{
const string MSBUILD_EXE_PATH = nameof(MSBUILD_EXE_PATH);
const string MSBuildExtensionsPath = nameof(MSBuildExtensionsPath);
const string MSBuildSDKsPath = nameof(MSBuildSDKsPath);
var variables = new Dictionary<string, string>
{
[MSBUILD_EXE_PATH] = Path.Combine(dotNetSdkPath, "MSBuild.dll"),
[MSBuildExtensionsPath] = dotNetSdkPath,
[MSBuildSDKsPath] = Path.Combine(dotNetSdkPath, "Sdks")
};
foreach (var kvp in variables)
{
Environment.SetEnvironmentVariable(kvp.Key, kvp.Value);
}
}
ApplyDotNetSdkEnvironmentVariables(parsedOptions.sdkPath);
// Find and load NuGet assemblies if msbuildPath is in a VS installation
string nugetPath = Path.GetFullPath(Path.Combine(parsedOptions.sdkPath, "..", "..", "..", "Common7", "IDE", "CommonExtensions", "Microsoft", "NuGet"));
if (Directory.Exists(nugetPath))
{
MSBuildLocator.RegisterMSBuildPath(new string[] { parsedOptions.sdkPath, nugetPath });
}
else
{
MSBuildLocator.RegisterMSBuildPath(parsedOptions.sdkPath);
}
}
else
{
var visualStudioInstances = MSBuildLocator.QueryVisualStudioInstances().ToArray();
var instance = visualStudioInstances.OrderByDescending(x => x.Version).FirstOrDefault();
if (instance != null)
{
if (parsedOptions.verbose)
Console.WriteLine($"Using MSBuild at '{instance.MSBuildPath}' to load projects.");
MSBuildLocator.RegisterInstance(instance);
}
else
{
Console.WriteLine($"Failed to find MSBuild path. Try specifying `sdk-path=` as a command line parameter.");
return 1;
}
}
var properties = new Dictionary<string, string>() { { "AdditionalFileItemNames", "$(AdditionalFileItemNames);Content" } };
var solutionDirectory = Path.GetDirectoryName(parsedOptions.solutionPath) + Path.DirectorySeparatorChar;
using (var workspace = MSBuildWorkspace.Create(properties))
{
// Print message for WorkspaceFailed event to help diagnosing project load failures.
workspace.WorkspaceFailed += (o, e) =>
{
if (e.Diagnostic.Message.Contains(".shproj") || e.Diagnostic.Message.Contains(".sqlproj") || e.Diagnostic.Message.Contains(".fsproj"))
{
return;
}
var kind = e.Diagnostic.Kind;
if (kind == WorkspaceDiagnosticKind.Warning && !parsedOptions.verbose)
return;
var match = ProjRegex.Matches(e.Diagnostic.Message);
if (match.Count == 1)
{
var path = match[0].Groups[1].Value;
if (path.StartsWith(solutionDirectory))
path = match[0].Groups[1].Value.Remove(0, solutionDirectory.Length);
if ((parsedOptions.includeProjects.Any() && !parsedOptions.includeProjects.Any(x => x.IsMatch(path))) ||
parsedOptions.excludeProjects.Any(x => x.IsMatch(path)))
{
return;
}
}
LogError(kind == WorkspaceDiagnosticKind.Failure, e.Diagnostic.Message);
if (kind == WorkspaceDiagnosticKind.Failure && !parsedOptions.ignoreMsBuildErrors)
returnCode = 2;
};
List<Project> projects;
if (parsedOptions.solutionPath.EndsWith(".sln"))
{
Console.WriteLine($"Loading solution '{parsedOptions.solutionPath}'");
// Attach progress reporter so we print projects as they are loaded.
var solution = await workspace.OpenSolutionAsync(parsedOptions.solutionPath, new ConsoleProgressReporter(parsedOptions.verbose)).ConfigureAwait(false);
projects = new List<Project>(solution.Projects.Count());
foreach (var project in solution.Projects)
{
if (project.FilePath.EndsWith(".shproj") || project.FilePath.EndsWith(".sqlproj") || project.FilePath.EndsWith(".fsproj"))
{
Console.WriteLine($"Skipped: {project.FilePath} excluded from analysis");
continue;
}
var path = project.FilePath;
if (path.StartsWith(solutionDirectory))
path = path.Remove(0, solutionDirectory.Length);
if ((parsedOptions.includeProjects.Any() && !parsedOptions.includeProjects.Any(x => x.IsMatch(path))) ||
parsedOptions.excludeProjects.Any(x => x.IsMatch(path)))
{
Console.WriteLine($"Skipped: {project.FilePath} excluded from analysis");
continue;
}
projects.Add(project);
}
}
else
{
// Attach progress reporter so we print projects as they are loaded.
projects = new List<Project>() { await workspace.OpenProjectAsync(parsedOptions.solutionPath, new ConsoleProgressReporter(parsedOptions.verbose)).ConfigureAwait(false) };
}
Console.WriteLine($"Finished loading solution '{parsedOptions.solutionPath}'");
if (returnCode != 0)
return returnCode;
var analyzers = new List<DiagnosticAnalyzer>();
LoadAnalyzers(parsedOptions, analyzers);
(var count, var errors, _) = await GetDiagnostics(parsedOptions, versionString, projects, analyzers).ConfigureAwait(false);
var elapsed = DateTime.Now - startTime;
if (parsedOptions.verbose)
Console.WriteLine($@"Completed in {elapsed:hh\:mm\:ss}");
Console.WriteLine($@"Found {count} security issues.");
if (errors > 0 && !parsedOptions.ignoreCompilerErrors)
{
if (parsedOptions.verbose)
Console.WriteLine($@"Exiting with 2 due to compilation errors.");
return 2;
}
if (parsedOptions.failOnWarning && count > 0)
{
if (parsedOptions.verbose)
Console.WriteLine($@"Exiting with 1 due to warnings.");
return 1;
}
return 0;
}
}
private static void LogError(bool error, string msg)
{
if (error)
Console.ForegroundColor = ConsoleColor.Red;
else
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Error.WriteLine(msg);
Console.ForegroundColor = ConsoleColor.White;
}
private static async Task<(int, int, int)> GetDiagnostics(
ParsedOptions parsedOptions,
string versionString,
IEnumerable<Project> projects,
List<DiagnosticAnalyzer> analyzers)
{
Stream stream = null;
SarifV2ErrorLogger logger = null;
try
{
if (parsedOptions.sarifFile != null)
{
if (File.Exists(parsedOptions.sarifFile))
File.Delete(parsedOptions.sarifFile);
stream = File.Open(parsedOptions.sarifFile, FileMode.CreateNew);
}
try
{
if (stream != null)
{
var v = new Version(versionString);
logger = new SarifV2ErrorLogger(stream, "Security Code Scan", versionString, new Version($"{v.Major}.{v.Minor}.{v.Build}.0"), CultureInfo.CurrentCulture);
}
var descriptors = new ConcurrentDictionary<string, DiagnosticDescriptor>();
Runner runner;
if (parsedOptions.threads.HasValue)
{
runner = new MultiThreadRunner(parsedOptions.verbose, analyzers, LogDiagnostics, parsedOptions, descriptors, logger, Debugger.IsAttached ? 1 : parsedOptions.threads.Value);
}
else
{
runner = new SingleThreadRunner(parsedOptions.verbose, analyzers, LogDiagnostics, parsedOptions, descriptors, logger);
}
foreach (var project in projects)
{
await runner.Run(project).ConfigureAwait(false);
}
return await runner.WaitForCompletion().ConfigureAwait(false);
}
finally
{
if (logger != null)
logger.Dispose();
}
}
finally
{
if (stream != null)
stream.Close();
}
}
private static void LoadAnalyzers(ParsedOptions parsedOptions, List<DiagnosticAnalyzer> analyzers)
{
var types = typeof(PathTraversalTaintAnalyzer).GetTypeInfo().Assembly.DefinedTypes;
AdditionalConfiguration.Path = parsedOptions.config;
foreach (var type in types)
{
if (type.IsAbstract)
continue;
var secAttributes = type.GetCustomAttributes(typeof(DiagnosticAnalyzerAttribute), false)
.Cast<DiagnosticAnalyzerAttribute>();
foreach (var attribute in secAttributes)
{
var analyzer = (DiagnosticAnalyzer)Activator.CreateInstance(type.AsType());
// First pass. Analyzers may support more than one diagnostic.
// If all supported diagnostics are excluded, don't load the analyzer - save CPU time.
if (parsedOptions.includeWarnings.Any() && !analyzer.SupportedDiagnostics.Any(x => parsedOptions.includeWarnings.Contains(x.Id)))
continue;
else if (analyzer.SupportedDiagnostics.All(x => parsedOptions.excludeWarnings.Contains(x.Id)))
continue;
analyzers.Add(analyzer);
break;
}
}
}
private static (int, int, int) LogDiagnostics(
ImmutableArray<Diagnostic> diagnostics,
ParsedOptions parsedOptions,
ConcurrentDictionary<string, DiagnosticDescriptor> descriptors,
SarifV2ErrorLogger logger)
{
var analysis_issues = 0;
var errors = 0;
var warnings = 0;
foreach (var diag in diagnostics)
{
var d = diag;
if (d.Severity == DiagnosticSeverity.Hidden || d.Severity == DiagnosticSeverity.Info)
continue;
if (!d.Id.StartsWith("SCS"))
{
LogError(d.Severity == DiagnosticSeverity.Error, d.ToString());
if (d.Severity == DiagnosticSeverity.Error)
++errors;
else
++warnings;
continue;
}
// Second pass. Analyzers may support more than one diagnostic.
// Filter excluded diagnostics.
if (parsedOptions.excludeWarnings.Contains(d.Id))
continue;
else if (parsedOptions.includeWarnings.Any() && !parsedOptions.includeWarnings.Contains(d.Id))
continue;
++analysis_issues;
// fix locations for diagnostics from additional files
if (d.Location == Location.None)
{
var match = WebConfigMessageRegex.Matches(d.GetMessage());
if (match.Count > 1)
throw new Exception("Unexpected");
if (match.Count != 0)
{
if (!descriptors.TryGetValue(d.Id, out var descr))
{
var msg = $"{match[0].Groups[1].Value}.";
descr = new DiagnosticDescriptor(d.Id, msg, msg, d.Descriptor.Category, d.Severity, d.Descriptor.IsEnabledByDefault);
descriptors.TryAdd(d.Id, descr);
}
var line = new LinePosition(int.Parse(match[0].Groups[3].Value) - 1, 0);
var capture = match[0].Groups[4].Value.TrimEnd('.');
d = Diagnostic.Create(descr, Location.Create(match[0].Groups[2].Value, new TextSpan(0, capture.Length), new LinePositionSpan(line, line)));
}
}
if (parsedOptions.cwe)
{
var cwe = LocaleUtil.GetLocalString($"{d.Id}_cwe");
var msg = d.ToString();
if (!cwe.ToString().StartsWith("??")) // overall all IDs must have corresponding CWE, but some are special like SCS0000
{
msg = msg.Replace($"{d.Id}:", $"{d.Id}: CWE-{cwe}:");
}
Console.WriteLine(msg);
}
else
{
Console.WriteLine(d.ToString());
}
if (logger != null)
logger.LogDiagnostic(d, null);
}
return (analysis_issues, errors, warnings);
}
private static readonly Regex WebConfigMessageRegex = new Regex(@"(.*) in (.*)\((\d+)\): (.*)", RegexOptions.Compiled);
private class ConsoleProgressReporter : IProgress<ProjectLoadProgress>
{
private bool _verbose;
public ConsoleProgressReporter(bool verbose)
{
_verbose = verbose;
}
public void Report(ProjectLoadProgress loadProgress)
{
if (!_verbose && loadProgress.Operation != ProjectLoadOperation.Resolve)
return;
var projectDisplay = Path.GetFileName(loadProgress.FilePath);
if (loadProgress.TargetFramework != null)
{
projectDisplay += $" ({loadProgress.TargetFramework})";
}
Console.WriteLine($"{loadProgress.Operation,-15} {loadProgress.ElapsedTime,-15:m\\:ss\\.fffffff} {projectDisplay}");
}
}
}
}