@@ -12,7 +12,11 @@ import * as api from "./api-client";
1212import * as diffUtils from "./diff-informed-analysis-utils" ;
1313import { Logger } from "./logging" ;
1414import * as sarif from "./sarif" ;
15- import { RecordingLogger , setupTests } from "./testing-utils" ;
15+ import {
16+ checkExpectedLogMessages ,
17+ RecordingLogger ,
18+ setupTests ,
19+ } from "./testing-utils" ;
1620import * as uploadLib from "./upload-lib" ;
1721import { UploadPayload } from "./upload-lib/types" ;
1822import { GitHubVariant , initializeEnvironment , withTmpDir } from "./util" ;
@@ -1014,12 +1018,22 @@ for (const analysisKind of analyses.supportedAnalysisKinds) {
10141018 ) ;
10151019}
10161020
1021+ function readValidSarifWithLocationUri ( uri : string ) : Partial < sarif . Log > {
1022+ const input = sarif . readSarifFile (
1023+ `${ __dirname } /../src/testdata/valid-sarif.sarif` ,
1024+ ) ;
1025+ input . runs ! [ 0 ] . results ! [ 0 ] . locations ! [ 0 ] . physicalLocation ! . artifactLocation ! . uri =
1026+ uri ;
1027+ return input ;
1028+ }
1029+
10171030function runFilterAlertsByDiffRange (
10181031 input : Partial < sarif . Log > ,
10191032 diffRanges : diffUtils . DiffThunkRange [ ] ,
1033+ logger : Logger = new RecordingLogger ( ) ,
10201034) : Partial < sarif . Log > {
10211035 sinon . stub ( diffUtils , "readDiffRangesJsonFile" ) . returns ( diffRanges ) ;
1022- return uploadLib . filterAlertsByDiffRange ( new RecordingLogger ( ) , input ) ;
1036+ return uploadLib . filterAlertsByDiffRange ( logger , input ) ;
10231037}
10241038
10251039test . serial (
@@ -1043,3 +1057,123 @@ test.serial(
10431057 t . deepEqual ( actualOutput , expectedOutput ) ;
10441058 } ,
10451059) ;
1060+
1061+ for ( const [ filePath , uri ] of [
1062+ [ "with space/board.js" , "with%20space/board.js" ] ,
1063+ [ "src/[id]/board.js" , "src/%5Bid%5D/board.js" ] ,
1064+ [ "src/file#1.js" , "src/file%231.js" ] ,
1065+ [ "src/100%.js" , "src/100%25.js" ] ,
1066+ [ "src/literal%20.js" , "src/literal%2520.js" ] ,
1067+ [ "src/foo:bar.js" , "src/foo%3Abar.js" ] ,
1068+ [ "src/caf\u00e9.js" , "src/caf%C3%A9.js" ] ,
1069+ [ "src/caf\u00e9.js" , "src/caf%c3%a9.js" ] ,
1070+ ] ) {
1071+ test . serial (
1072+ `filterAlertsByDiffRange matches percent-encoded URI ${ uri } ` ,
1073+ ( t ) => {
1074+ const input = readValidSarifWithLocationUri ( uri ) ;
1075+ const actualOutput = runFilterAlertsByDiffRange ( input , [
1076+ { path : filePath , startLine : 1 , endLine : 3 } ,
1077+ ] ) ;
1078+
1079+ t . is ( actualOutput . runs ?. [ 0 ] . results ?. length , 1 ) ;
1080+ t . is ( actualOutput . runs ?. [ 1 ] . results ?. length , 0 ) ;
1081+ } ,
1082+ ) ;
1083+ }
1084+
1085+ test . serial (
1086+ "filterAlertsByDiffRange filters out percent-encoded URI when outside diff line range" ,
1087+ ( t ) => {
1088+ const input = readValidSarifWithLocationUri ( "with%20space/board.js" ) ;
1089+ const actualOutput = runFilterAlertsByDiffRange ( input , [
1090+ { path : "with space/board.js" , startLine : 10 , endLine : 20 } ,
1091+ ] ) ;
1092+
1093+ t . is ( actualOutput . runs ?. [ 0 ] . results ?. length , 0 ) ;
1094+ } ,
1095+ ) ;
1096+
1097+ test . serial (
1098+ "filterAlertsByDiffRange filters out percent-encoded URI when path does not match" ,
1099+ ( t ) => {
1100+ const input = readValidSarifWithLocationUri ( "with%20space/board.js" ) ;
1101+ const actualOutput = runFilterAlertsByDiffRange ( input , [
1102+ { path : "other/board.js" , startLine : 1 , endLine : 3 } ,
1103+ ] ) ;
1104+
1105+ t . is ( actualOutput . runs ?. [ 0 ] . results ?. length , 0 ) ;
1106+ } ,
1107+ ) ;
1108+
1109+ test . serial (
1110+ "filterAlertsByDiffRange keeps alert when relatedLocations has a matching percent-encoded URI" ,
1111+ ( t ) => {
1112+ const input = sarif . readSarifFile (
1113+ `${ __dirname } /../src/testdata/valid-sarif.sarif` ,
1114+ ) ;
1115+ input . runs ! [ 1 ] . results ! [ 1 ] . relatedLocations ! [ 0 ] . physicalLocation ! . artifactLocation ! . uri =
1116+ "with%20space/ParseObject.js" ;
1117+
1118+ const actualOutput = runFilterAlertsByDiffRange ( input , [
1119+ { path : "with space/ParseObject.js" , startLine : 2280 , endLine : 2285 } ,
1120+ ] ) ;
1121+
1122+ t . is ( actualOutput . runs ?. [ 0 ] . results ?. length , 0 ) ;
1123+ t . is ( actualOutput . runs ?. [ 1 ] . results ?. length , 1 ) ;
1124+ t . is (
1125+ actualOutput . runs ?. [ 1 ] . results ?. [ 0 ] . ruleId ,
1126+ "js/inconsistent-use-of-new" ,
1127+ ) ;
1128+ } ,
1129+ ) ;
1130+
1131+ for ( const uri of [ "dir%ZZ/board.js" , "src/bad%.js" , "src/bad%C3%28.js" ] ) {
1132+ test . serial (
1133+ `filterAlertsByDiffRange falls back to raw path for malformed URI ${ uri } ` ,
1134+ ( t ) => {
1135+ const logger = new RecordingLogger ( ) ;
1136+ const input = readValidSarifWithLocationUri ( uri ) ;
1137+ const actualOutput = runFilterAlertsByDiffRange (
1138+ input ,
1139+ [ { path : uri , startLine : 1 , endLine : 3 } ] ,
1140+ logger ,
1141+ ) ;
1142+
1143+ t . is ( actualOutput . runs ?. [ 0 ] . results ?. length , 1 ) ;
1144+ checkExpectedLogMessages ( t , logger . messages , [
1145+ `Failed to decode location URI "${ uri } ", comparing it as a raw path` ,
1146+ ] ) ;
1147+ } ,
1148+ ) ;
1149+ }
1150+
1151+ test . serial (
1152+ "filterAlertsByDiffRange filters out malformed URI when raw path does not match" ,
1153+ ( t ) => {
1154+ const logger = new RecordingLogger ( ) ;
1155+ const input = readValidSarifWithLocationUri ( "dir%ZZ/board.js" ) ;
1156+ const actualOutput = runFilterAlertsByDiffRange (
1157+ input ,
1158+ [ { path : "src/different.js" , startLine : 1 , endLine : 3 } ] ,
1159+ logger ,
1160+ ) ;
1161+
1162+ t . is ( actualOutput . runs ?. [ 0 ] . results ?. length , 0 ) ;
1163+ checkExpectedLogMessages ( t , logger . messages , [
1164+ 'Failed to decode location URI "dir%ZZ/board.js", comparing it as a raw path' ,
1165+ ] ) ;
1166+ } ,
1167+ ) ;
1168+
1169+ test . serial (
1170+ "filterAlertsByDiffRange matches raw unencoded path containing valid percent-encoding sequence" ,
1171+ ( t ) => {
1172+ const input = readValidSarifWithLocationUri ( "dir/100%20done.js" ) ;
1173+ const actualOutput = runFilterAlertsByDiffRange ( input , [
1174+ { path : "dir/100%20done.js" , startLine : 1 , endLine : 3 } ,
1175+ ] ) ;
1176+
1177+ t . is ( actualOutput . runs ?. [ 0 ] . results ?. length , 1 ) ;
1178+ } ,
1179+ ) ;
0 commit comments