Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SARIF validation] turn URI errors into warnings #2487

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/upload-lib.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/upload-lib.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/upload-lib.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/upload-lib.test.js.map

Large diffs are not rendered by default.

54 changes: 33 additions & 21 deletions src/testdata/with-invalid-uri.sarif
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,42 @@
"name": "LGTM.com",
"organization": "Semmle",
"version": "1.24.0-SNAPSHOT",
"rules": []
"rules": [
{
"id": "js/unused-local-variable",
"shortDescription": {
"text": "Unused local variable"
},
"helpUri": "not a valid URI"
}
]
}
},
"results" : [ {
"ruleId" : "js/unused-local-variable",
"ruleIndex" : 0,
"message" : {
"text" : "Unused variable foo."
},
"locations" : [ {
"physicalLocation" : {
"artifactLocation" : {
"uri" : "not a valid URI",
"uriBaseId" : "%SRCROOT%",
"index" : 0
},
"region" : {
"startLine" : 2,
"startColumn" : 7,
"endColumn" : 10
"results": [
{
"ruleId": "js/unused-local-variable",
"ruleIndex": 0,
"message": {
"text": "Unused variable foo."
},
"locations": [
{
"physicalLocation": {
"artifactLocation": {
"uri": "not a valid URI",
"uriBaseId": "%SRCROOT%",
"index": 0
},
"region": {
"startLine": 2,
"startColumn": 7,
"endColumn": 10
}
}
}
}
} ]
} ],
]
}
],
"columnKind": "utf16CodeUnits",
"properties": {
"semmle.formatSpecifier": "2.1.0",
Expand Down
3 changes: 2 additions & 1 deletion src/upload-lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,10 @@ test("accept results with invalid artifactLocation.uri value", (t) => {
const sarifFile = `${__dirname}/../src/testdata/with-invalid-uri.sarif`;
uploadLib.validateSarifFileSchema(sarifFile, mockLogger);

t.deepEqual(loggedMessages.length, 2);
t.deepEqual(loggedMessages.length, 3);
t.deepEqual(
loggedMessages[1],
"Warning: 'not a valid URI' is not a valid URI in 'instance.runs[0].tool.driver.rules[0].helpUri'.",
"Warning: 'not a valid URI' is not a valid URI in 'instance.runs[0].results[0].locations[0].physicalLocation.artifactLocation.uri'.",
);
});
Expand Down
17 changes: 13 additions & 4 deletions src/upload-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,20 @@ export function validateSarifFileSchema(sarifFilePath: string, logger: Logger) {
const result = new jsonschema.Validator().validate(sarif, schema);
// Filter errors related to invalid URIs in the artifactLocation field as this
// is a breaking change. See https://github.com/github/codeql-action/issues/1703
const errors = (result.errors || []).filter(
(err) => err.argument !== "uri-reference",
const warningAttributes = ["uri-reference", "uri"];
const errors = (result.errors ?? []).filter(
(err) =>
!(
err.name === "format" &&
typeof err.argument === "string" &&
aeisenberg marked this conversation as resolved.
Show resolved Hide resolved
warningAttributes.includes(err.argument)
),
);
const warnings = (result.errors || []).filter(
(err) => err.argument === "uri-reference",
const warnings = (result.errors ?? []).filter(
(err) =>
err.name === "format" &&
typeof err.argument === "string" &&
aeisenberg marked this conversation as resolved.
Show resolved Hide resolved
warningAttributes.includes(err.argument),
);

for (const warning of warnings) {
Expand Down
Loading