Skip to content

Commit

Permalink
WIP: attempt at validating CNA container schema and converting ajv er…
Browse files Browse the repository at this point in the history
…rors to json-editor errors per request of Vulnogram#192. Doesn't appear to be useful because errors are duplicative. Could be useful with proper if-branching targeting specify data types if ajv finds errors json-editor doesn't.
  • Loading branch information
scotluns committed Apr 16, 2024
1 parent bf798cc commit b936a20
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
15 changes: 14 additions & 1 deletion default/cve5/conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,19 @@ module.exports = {
});
}
}
if (path == "root.containers.cna") {
if(cve5CnaContainerSchemaValidator !== undefined && !cve5CnaContainerSchemaValidator(value)) {
for (let e of cve5CnaContainerSchemaValidator.errors.filter(usefulAjvErrors)) {
if (e.message in excludeErrors) continue;
errors.push({
path: path + e.instancePath.replaceAll("/","."),
property: 'format',
message: e.message,
source: "ajv",
})
}
}
}
return errors;
}
],
Expand All @@ -415,4 +428,4 @@ module.exports = {
return errors;
},*/
router: router
}
}
40 changes: 39 additions & 1 deletion public/js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ var editorLabel = document.getElementById('editorLabel');
var iconTheme = 'vgi-';
var starting_value = {};
var sourceEditor;
let cve5Schema;
let cve5SchemaValidator;
let cve5CnaContainerSchema;
let cve5CnaContainerSchemaValidator;
const cve5schemaUrl = "https://raw.githubusercontent.com/CVEProject/cve-schema/2aa608b6733cc2730a43901472ef0e706d0ef2b5/schema/v5.0/docs/CVE_JSON_bundled.json"
const excludeErrors = {
"must have required property 'rejectedReasons'": true,
"must match exactly one schema in oneOf": true,
"must match a schema in anyOf": true,
"must NOT have additional properties": true,
"must have required property 'vendor'": true,
"must have required property 'product'": true,
"must have required property 'collectionURL'": true,
"must have required property 'packageName'": true,
};
const usefulAjvErrors = (e) => !(e.message in excludeErrors);

JSONEditor.defaults.languages.en.error_oneOf = "Please fill in the required fields *";

Expand Down Expand Up @@ -1316,6 +1332,28 @@ function loadJSON(res, id, message, editorOptions) {
}
docEditor = new JSONEditor(document.getElementById('docEditor'), editorOptions ? editorOptions : docEditorOptions);
docEditor.on('ready', async function () {
if (ajv7) {
Ajv = new ajv7({allErrors: true});
await fetch(cve5schemaUrl).then(
resp => resp.text()
).then(
text => JSON.parse(text)
).then(
schema => {

cve5Schema = {...schema};
cve5SchemaValidator = Ajv.compile(cve5Schema, {strict: false})
cve5CnaContainerSchema = {
definitions: schema.definitions,
oneOf:[
{...schema.definitions.cnaPublishedContainer},
{...schema.definitions.cnaRejectedContainer}
],
};
cve5CnaContainerSchemaValidator = Ajv.compile(cve5CnaContainerSchema, {strict: false})
}
)
}
await docEditor.root.setValue(res, true);
infoMsg.textContent = message ? message : '';
//errMsg.textContent = "";
Expand Down Expand Up @@ -1528,4 +1566,4 @@ function showAlert(msg, smallmsg, timer, showCancel) {
setTimeout(function () {
document.getElementById("alertDialog").close();
}, timer);
}
}

0 comments on commit b936a20

Please sign in to comment.