// Validate a CSAF 2.0 document with BSI's csaf-validator-lib (MIT): the // schema, the mandatory (6.1.x) and the optional (6.2.x) tests. Run from the // directory the library is installed in; prints the result as JSON. import { readFileSync } from 'node:fs' import { register } from 'node:module' import { join } from 'node:path' import { pathToFileURL } from 'node:url' // js-cvss 0.4.4, which the library imports, ships ES modules whose relative // imports have no `.js`; Node needs it, so a resolve hook adds it. register( 'data:text/javascript,' + encodeURIComponent(`export async function resolve(specifier, context, next) { try { return await next(specifier, context) } catch (e) { if (e.code !== 'ERR_MODULE_NOT_FOUND' || !specifier.startsWith('.')) throw e return next(specifier + '.js', context) } }`), ) // By file: the package's export map does not list every module. const lib = (name) => import(pathToFileURL(join(process.cwd(), 'node_modules/@secvisogram/csaf-validator-lib', name)).href) const { default: validateStrict } = await lib('validateStrict.js') const schema = await lib('schemaTests.js') const mandatory = await lib('mandatoryTests.js') const optional = await lib('optionalTests.js') const doc = JSON.parse(readFileSync(process.argv[2], 'utf8')) const result = await validateStrict( [schema.csaf_2_0_strict, ...Object.values(mandatory), ...Object.values(optional)], doc, ) // One result per test; each error and warning is printed with its test. const found = (kind) => result.tests.flatMap((t) => (t[kind] ?? []).map((e) => ({ test: t.name, message: e.message, path: e.instancePath })), ) console.log( JSON.stringify({ isValid: result.isValid, errors: found('errors'), warnings: found('warnings') }), )