Throw InvalidRequestError if JSON parsing fails
This commit is contained in:
parent
52f2347108
commit
62712e9ff9
3 changed files with 20 additions and 24 deletions
19
lib/upload-lib.js
generated
19
lib/upload-lib.js
generated
|
|
@ -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
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue