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

View file

@ -207,10 +207,7 @@ export async function uploadFromActions(
logger,
);
} catch (e) {
if (
(e instanceof InvalidRequestError || e instanceof SyntaxError) &&
isThirdPartyUpload
) {
if (e instanceof InvalidRequestError && isThirdPartyUpload) {
throw new ConfigurationError(e.message);
}
throw e;
@ -239,14 +236,7 @@ function getSarifFilePaths(sarifPath: string) {
// Counts the number of results in the given SARIF file
function countResultsInSarif(sarif: string): number {
let numResults = 0;
let parsedSarif;
try {
parsedSarif = JSON.parse(sarif);
} catch (e) {
throw new InvalidRequestError(
`Invalid SARIF. JSON syntax error: ${wrapError(e).message}`,
);
}
const parsedSarif = JSON.parse(sarif);
if (!Array.isArray(parsedSarif.runs)) {
throw new InvalidRequestError("Invalid SARIF. Missing 'runs' array.");
}
@ -265,7 +255,14 @@ function countResultsInSarif(sarif: string): number {
// Validates that the given file path refers to a valid SARIF file.
// Throws an error if the file is invalid.
export function validateSarifFileSchema(sarifFilePath: string, logger: Logger) {
const sarif = JSON.parse(fs.readFileSync(sarifFilePath, "utf8")) as SarifFile;
let sarif;
try {
sarif = JSON.parse(fs.readFileSync(sarifFilePath, "utf8")) as SarifFile;
} catch (e) {
throw new InvalidRequestError(
`Invalid SARIF. JSON syntax error: ${wrapError(e).message}`,
);
}
const schema = require("../src/sarif-schema-2.1.0.json") as jsonschema.Schema;
const result = new jsonschema.Validator().validate(sarif, schema);