Throw InvalidRequestError if JSON parsing fails

This commit is contained in:
Angela P Wen
2024-02-28 15:50:02 +00:00
parent 52f2347108
commit 62712e9ff9
3 changed files with 20 additions and 24 deletions

19
lib/upload-lib.js generated
View File

@@ -162,8 +162,7 @@ async function uploadFromActions(sarifPath, checkoutPath, category, logger, { is
return await uploadFiles(getSarifFilePaths(sarifPath), (0, repository_1.parseRepositoryNwo)(util.getRequiredEnvParam("GITHUB_REPOSITORY")), await actionsUtil.getCommitOid(checkoutPath), await actionsUtil.getRef(), await api.getAnalysisKey(), category, util.getRequiredEnvParam("GITHUB_WORKFLOW"), actionsUtil.getWorkflowRunID(), actionsUtil.getWorkflowRunAttempt(), checkoutPath, actionsUtil.getRequiredInput("matrix"), logger);
}
catch (e) {
if ((e instanceof InvalidRequestError || e instanceof SyntaxError) &&
isThirdPartyUpload) {
if (e instanceof InvalidRequestError && isThirdPartyUpload) {
throw new util_1.ConfigurationError(e.message);
}
throw e;
@@ -189,13 +188,7 @@ function getSarifFilePaths(sarifPath) {
// Counts the number of results in the given SARIF file
function countResultsInSarif(sarif) {
let numResults = 0;
let parsedSarif;
try {
parsedSarif = JSON.parse(sarif);
}
catch (e) {
throw new InvalidRequestError(`Invalid SARIF. JSON syntax error: ${(0, util_1.wrapError)(e).message}`);
}
const parsedSarif = JSON.parse(sarif);
if (!Array.isArray(parsedSarif.runs)) {
throw new InvalidRequestError("Invalid SARIF. Missing 'runs' array.");
}
@@ -210,7 +203,13 @@ function countResultsInSarif(sarif) {
// Validates that the given file path refers to a valid SARIF file.
// Throws an error if the file is invalid.
function validateSarifFileSchema(sarifFilePath, logger) {
const sarif = JSON.parse(fs.readFileSync(sarifFilePath, "utf8"));
let sarif;
try {
sarif = JSON.parse(fs.readFileSync(sarifFilePath, "utf8"));
}
catch (e) {
throw new InvalidRequestError(`Invalid SARIF. JSON syntax error: ${(0, util_1.wrapError)(e).message}`);
}
const schema = require("../src/sarif-schema-2.1.0.json");
const result = new jsonschema.Validator().validate(sarif, schema);
// Filter errors related to invalid URIs in the artifactLocation field as this