@@ -715,6 +715,15 @@ private static ASA_ERROR RunExportCollectCommand(ExportCollectCommandOptions opt
715
715
internal static ASA_ERROR ExportCompareResults ( ConcurrentDictionary < ( RESULT_TYPE , CHANGE_TYPE ) , ConcurrentBag < CompareResult > > resultsIn , ExportOptions opts , string baseFileName , string analysesHash , IEnumerable < AsaRule > rules )
716
716
{
717
717
var results = resultsIn . Select ( x => new KeyValuePair < string , object > ( $ "{ x . Key . Item1 } _{ x . Key . Item2 } ", x . Value ) ) . ToDictionary ( x => x . Key , x => x . Value ) ;
718
+ if ( opts . DisableImplicitFindings )
719
+ {
720
+ var resultKeys = resultsIn . Keys ;
721
+ foreach ( var key in resultKeys )
722
+ {
723
+ var newBag = new ConcurrentBag < CompareResult > ( resultsIn [ key ] . Where ( x => ! x . Rules . Any ( ) ) ) ;
724
+ resultsIn [ key ] = newBag ;
725
+ }
726
+ }
718
727
JsonSerializer serializer = JsonSerializer . Create ( new JsonSerializerSettings ( )
719
728
{
720
729
Formatting = Formatting . Indented ,
@@ -741,7 +750,7 @@ internal static ASA_ERROR ExportCompareResults(ConcurrentDictionary<(RESULT_TYPE
741
750
string filePath = Path . Combine ( path , AsaHelpers . MakeValidFileName ( key ) ) ;
742
751
if ( opts . OutputSarif )
743
752
{
744
- WriteSarifLog ( new Dictionary < string , object > ( ) { { key , results [ key ] } } , rules , filePath ) ;
753
+ WriteSarifLog ( new Dictionary < string , object > ( ) { { key , results [ key ] } } , rules , filePath , opts . DisableImplicitFindings ) ;
745
754
}
746
755
else
747
756
{
@@ -762,12 +771,11 @@ internal static ASA_ERROR ExportCompareResults(ConcurrentDictionary<(RESULT_TYPE
762
771
if ( opts . OutputSarif )
763
772
{
764
773
string pathSarif = Path . Combine ( outputPath , AsaHelpers . MakeValidFileName ( baseFileName + "_summary.Sarif" ) ) ;
765
- WriteSarifLog ( output , rules , pathSarif ) ;
774
+ WriteSarifLog ( output , rules , pathSarif , opts . DisableImplicitFindings ) ;
766
775
Log . Information ( Strings . Get ( "OutputWrittenTo" ) , ( new FileInfo ( pathSarif ) ) . FullName ) ;
767
776
}
768
777
else
769
778
{
770
-
771
779
using ( StreamWriter sw = new ( path ) ) //lgtm[cs/path-injection]
772
780
{
773
781
using JsonWriter writer = new JsonTextWriter ( sw ) ;
@@ -785,9 +793,10 @@ internal static ASA_ERROR ExportCompareResults(ConcurrentDictionary<(RESULT_TYPE
785
793
/// <param name="output">output of the analyzer result</param>
786
794
/// <param name="rules">list of rules used</param>
787
795
/// <param name="outputFilePath">file path of the Sarif log</param>
788
- public static void WriteSarifLog ( Dictionary < string , object > output , IEnumerable < AsaRule > rules , string outputFilePath )
796
+ /// <param name="disableImplicitFindings">If the output should exclude results with no explicit level</param>
797
+ internal static void WriteSarifLog ( Dictionary < string , object > output , IEnumerable < AsaRule > rules , string outputFilePath , bool disableImplicitFindings )
789
798
{
790
- var log = GenerateSarifLog ( output , rules ) ;
799
+ var log = GenerateSarifLog ( output , rules , disableImplicitFindings ) ;
791
800
792
801
var settings = new JsonSerializerSettings ( )
793
802
{
@@ -797,7 +806,7 @@ public static void WriteSarifLog(Dictionary<string, object> output, IEnumerable<
797
806
File . WriteAllText ( outputFilePath , JsonConvert . SerializeObject ( log , settings ) ) ;
798
807
}
799
808
800
- public static SarifLog GenerateSarifLog ( Dictionary < string , object > output , IEnumerable < AsaRule > rules )
809
+ public static SarifLog GenerateSarifLog ( Dictionary < string , object > output , IEnumerable < AsaRule > rules , bool disableImplicitFindings )
801
810
{
802
811
var metadata = ( Dictionary < string , string > ) output [ "metadata" ] ;
803
812
var results = ( Dictionary < string , object > ) output [ "results" ] ;
@@ -899,32 +908,62 @@ public static SarifLog GenerateSarifLog(Dictionary<string, object> output, IEnum
899
908
900
909
artifacts . Add ( artifact ) ;
901
910
int index = artifacts . Count - 1 ;
902
- foreach ( var rule in compareResult . Rules )
911
+ if ( compareResult . Rules . Any ( ) )
903
912
{
904
- var sarifResult = new Result ( ) ;
905
- sarifResult . Locations = new List < Location > ( )
913
+ foreach ( var rule in compareResult . Rules )
906
914
{
907
- new Location ( ) {
908
- PhysicalLocation = new PhysicalLocation ( )
909
- {
910
- ArtifactLocation = new ArtifactLocation ( )
915
+ var sarifResult = new Result ( ) ;
916
+ sarifResult . Locations = new List < Location > ( )
917
+ {
918
+ new Location ( ) {
919
+ PhysicalLocation = new PhysicalLocation ( )
911
920
{
912
- Index = index
921
+ ArtifactLocation = new ArtifactLocation ( )
922
+ {
923
+ Index = index
924
+ }
913
925
}
914
926
}
915
- }
916
- } ;
927
+ } ;
917
928
918
- sarifResult . Level = GetSarifFailureLevel ( ( ANALYSIS_RESULT_TYPE ) rule . Severity ) ;
929
+ sarifResult . Level = GetSarifFailureLevel ( ( ANALYSIS_RESULT_TYPE ) rule . Severity ) ;
919
930
920
- if ( ! string . IsNullOrWhiteSpace ( rule . Name ) )
921
- {
922
- sarifResult . RuleId = rule . Name ;
931
+ if ( ! string . IsNullOrWhiteSpace ( rule . Name ) )
932
+ {
933
+ sarifResult . RuleId = rule . Name ;
934
+ }
935
+
936
+ sarifResult . Message = new Message ( ) { Text = string . Format ( "{0}: {1} ({2})" , rule . Name , compareResult . Identity , compareResult . ChangeType ) } ;
937
+
938
+ sarifResults . Add ( sarifResult ) ;
923
939
}
940
+ }
941
+ else
942
+ {
943
+ if ( ! disableImplicitFindings )
944
+ {
945
+ var sarifResult = new Result ( ) ;
946
+ sarifResult . Locations = new List < Location > ( )
947
+ {
948
+ new Location ( ) {
949
+ PhysicalLocation = new PhysicalLocation ( )
950
+ {
951
+ ArtifactLocation = new ArtifactLocation ( )
952
+ {
953
+ Index = index
954
+ }
955
+ }
956
+ }
957
+ } ;
924
958
925
- sarifResult . Message = new Message ( ) { Text = string . Format ( "{0}: {1} ({2})" , rule . Name , compareResult . Identity , compareResult . ChangeType ) } ;
959
+ sarifResult . Level = GetSarifFailureLevel ( compareResult . Analysis ) ;
960
+
961
+ sarifResult . RuleId = "Default Level" ;
962
+
963
+ sarifResult . Message = new Message ( ) { Text = string . Format ( "Default Level: {0} ({1})" , compareResult . Identity , compareResult . ChangeType ) } ;
926
964
927
- sarifResults . Add ( sarifResult ) ;
965
+ sarifResults . Add ( sarifResult ) ;
966
+ }
928
967
}
929
968
}
930
969
}
0 commit comments