Skip to content

Commit d433dab

Browse files
akreamCopilot
andcommitted
decode SARIF URIs when filtering diff-informed alerts
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: b1744ae3-8204-4901-8dc7-16368fd6d865
1 parent 166cc10 commit d433dab

4 files changed

Lines changed: 83 additions & 4 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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,3 +1043,61 @@ test.serial(
10431043
t.deepEqual(actualOutput, expectedOutput);
10441044
},
10451045
);
1046+
1047+
for (const [filePath, uri] of [
1048+
["src/with space.js", "src/with%20space.js"],
1049+
["src/caf\u00e9.js", "src/caf%C3%A9.js"],
1050+
["src/caf\u00e9.js", "src/caf%c3%a9.js"],
1051+
["src/!()'*.js", "src/%21%28%29%27%2A.js"],
1052+
["src/literal%20.js", "src/literal%2520.js"],
1053+
]) {
1054+
test.serial(`filterAlertsByDiffRange decodes URI ${uri}`, (t) => {
1055+
const input = sarif.readSarifFile(
1056+
`${__dirname}/../src/testdata/valid-sarif.sarif`,
1057+
);
1058+
input.runs![0].results![0].locations![0].physicalLocation!.artifactLocation!.uri =
1059+
uri;
1060+
1061+
const actualOutput = runFilterAlertsByDiffRange(input, [
1062+
{ path: filePath, startLine: 1, endLine: 3 },
1063+
]);
1064+
1065+
t.is(actualOutput.runs?.[0].results?.length, 1);
1066+
});
1067+
}
1068+
1069+
for (const uri of ["src/bad%.js", "src/bad%GG.js", "src/bad%C3%28.js"]) {
1070+
test.serial(
1071+
`filterAlertsByDiffRange falls back for malformed URI ${uri}`,
1072+
(t) => {
1073+
const input = sarif.readSarifFile(
1074+
`${__dirname}/../src/testdata/valid-sarif.sarif`,
1075+
);
1076+
input.runs![0].results![0].locations![0].physicalLocation!.artifactLocation!.uri =
1077+
uri;
1078+
1079+
const actualOutput = runFilterAlertsByDiffRange(input, [
1080+
{ path: uri, startLine: 0, endLine: 0 },
1081+
]);
1082+
1083+
t.is(actualOutput.runs?.[0].results?.length, 1);
1084+
},
1085+
);
1086+
}
1087+
1088+
test.serial(
1089+
"filterAlertsByDiffRange filters malformed URI without a matching raw path",
1090+
(t) => {
1091+
const input = sarif.readSarifFile(
1092+
`${__dirname}/../src/testdata/valid-sarif.sarif`,
1093+
);
1094+
input.runs![0].results![0].locations![0].physicalLocation!.artifactLocation!.uri =
1095+
"src/bad%GG.js";
1096+
1097+
const actualOutput = runFilterAlertsByDiffRange(input, [
1098+
{ path: "src/different.js", startLine: 0, endLine: 0 },
1099+
]);
1100+
1101+
t.is(actualOutput.runs?.[0].results?.length, 0);
1102+
},
1103+
);

‎src/upload-lib.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,17 +1099,29 @@ 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 locationPath = locationUri;
1110+
try {
1111+
locationPath = 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 === locationPath &&
11131125
((range.startLine <= locationStartLine &&
11141126
range.endLine >= locationStartLine) ||
11151127
(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.

0 commit comments

Comments
 (0)