diff --git a/test/index-generator.js b/test/index-generator.js new file mode 100644 index 000000000..f8167906c --- /dev/null +++ b/test/index-generator.js @@ -0,0 +1,43 @@ +'use strict'; + +const { describe, it } = require('node:test'); +const assert = require('node:assert'); + +const { supportedVersionAffected } = require('../tools/create_index/index_generator'); + +const supportedMajors = [22, 24, 26]; + +describe('Testing index_generator supportedVersionAffected', () => { + it('keeps a vulnerability that affects a supported release line', () => { + assert.strictEqual( + supportedVersionAffected({ vulnerable: '22.x || 24.x || 26.x' }, supportedMajors), + true + ); + }); + + it('keeps a vulnerability that only affects older versions of a supported line', () => { + assert.strictEqual( + supportedVersionAffected({ vulnerable: '<24.5.0' }, supportedMajors), + true + ); + }); + + it('drops a vulnerability that only affects end-of-life release lines', () => { + assert.strictEqual( + supportedVersionAffected({ vulnerable: '8.x || 7.x || 4.x || 6.x || 5.x' }, supportedMajors), + false + ); + }); + + it('keeps npm vulnerabilities, which have no vulnerable property', () => { + assert.strictEqual( + supportedVersionAffected({ vulnerable_versions: '<1.0.0' }, supportedMajors), + true + ); + }); + + it('returns a boolean, not a Promise', () => { + const result = supportedVersionAffected({ vulnerable: '8.x' }, supportedMajors); + assert.strictEqual(typeof result, 'boolean'); + }); +}); diff --git a/tools/create_index/index_generator.js b/tools/create_index/index_generator.js index dc1f29f48..114e5e360 100644 --- a/tools/create_index/index_generator.js +++ b/tools/create_index/index_generator.js @@ -1,42 +1,41 @@ const fs = require('node:fs') -const satisfies = require('semver/functions/satisfies') +const intersects = require('semver/ranges/intersects') const nv = require('@pkgjs/nv') // We will fill in this object with our data let vuln = {} -const createIndex = function (vulnDirectoryPath) { +const createIndex = async function (vulnDirectoryPath) { const files = fs.readdirSync(vulnDirectoryPath) - getVulnDirectoryContents(files, vulnDirectoryPath) + const majors = await supportedMajors() + getVulnDirectoryContents(files, vulnDirectoryPath, majors) writeIndex(vuln, vulnDirectoryPath) } -const supportedVersions = async function () { +const supportedMajors = async function () { const versions = await nv('supported') - return versions.map((v) => v.version) + return versions.map((v) => v.major) } -const supportedVersionAffected = async function (json) { - const versions = await supportedVersions() +// Keep a vulnerability if it affects any version of a supported release line. +// Comparing against the whole line (e.g. `24.x`) rather than the latest release +// of that line also keeps ranges such as `<24.5.0`, which do not affect the +// latest release but still affect older versions of a supported line. +const supportedVersionAffected = function (json, majors) { // NPM vulns don't have a vulnerable property if (!json.vulnerable) { return true } - for (const version of versions) { - if (satisfies(version, json.vulnerable)) { - return true - } - } - return false + return majors.some((major) => intersects(`${major}.x`, json.vulnerable)) } -const getVulnDirectoryContents = function (files, vulnDirectoryPath) { +const getVulnDirectoryContents = function (files, vulnDirectoryPath, majors) { for(file of files) { const filename = file.slice(-0, file.toString().indexOf('.json')) if(filename !== 'index') { const data = fs.readFileSync(vulnDirectoryPath + file) const json = JSON.parse(data) - if (supportedVersionAffected(json)) { + if (supportedVersionAffected(json, majors)) { createVulnObject(filename, json) } } @@ -58,3 +57,4 @@ const writeIndex = function(data, vulnDirectoryPath) { } module.exports = createIndex +module.exports.supportedVersionAffected = supportedVersionAffected