Skip to content

Commit 8b728b0

Browse files
committed
fix: decode SARIF location URIs in diff-informed alert filtering
1 parent 52cf185 commit 8b728b0

4 files changed

Lines changed: 162 additions & 6 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/upload-lib.test.ts‎

Lines changed: 136 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import * as api from "./api-client";
1212
import * as diffUtils from "./diff-informed-analysis-utils";
1313
import { Logger } from "./logging";
1414
import * as sarif from "./sarif";
15-
import { RecordingLogger, setupTests } from "./testing-utils";
15+
import {
16+
checkExpectedLogMessages,
17+
RecordingLogger,
18+
setupTests,
19+
} from "./testing-utils";
1620
import * as uploadLib from "./upload-lib";
1721
import { UploadPayload } from "./upload-lib/types";
1822
import { 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+
10171030
function 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

10251039
test.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+
);

‎src/upload-lib.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,17 +1099,30 @@ export function filterAlertsByDiffRange(
10991099
return locations.some((physicalLocation) => {
11001100
const locationUri = physicalLocation?.artifactLocation?.uri;
11011101
const locationStartLine = physicalLocation?.region?.startLine;
1102-
if (!locationUri || locationStartLine === undefined) {
1102+
if (
1103+
typeof locationUri !== "string" ||
1104+
!locationUri ||
1105+
locationStartLine === undefined
1106+
) {
11031107
return false;
11041108
}
1109+
let decodedLocationUri = locationUri;
1110+
try {
1111+
decodedLocationUri = decodeURIComponent(locationUri);
1112+
} catch {
1113+
logger.debug(
1114+
`Failed to decode location URI "${locationUri}", comparing it as a raw path`,
1115+
);
1116+
}
11051117
// Alert filtering here replicates the same behavior as the restrictAlertsTo
11061118
// extensible predicate in CodeQL. See the restrictAlertsTo documentation
11071119
// https://codeql.github.com/codeql-standard-libraries/csharp/codeql/util/AlertFiltering.qll/predicate.AlertFiltering$restrictAlertsTo.3.html
11081120
// for more details, such as why the filtering applies only to the first line
11091121
// of an alert location.
11101122
return diffRanges.some(
11111123
(range) =>
1112-
range.path === locationUri &&
1124+
(range.path === decodedLocationUri ||
1125+
range.path === locationUri) &&
11131126
((range.startLine <= locationStartLine &&
11141127
range.endLine >= locationStartLine) ||
11151128
(range.startLine === 0 && range.endLine === 0)),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fixed diff-informed alert filtering incorrectly removing alerts in changed files whose paths contain spaces, special characters, or non-ASCII characters. [#4283](https://github.com/github/codeql-action/pull/4283)

0 commit comments

Comments
 (0)