From 7b31ec2a4fa43e147d846639f5f6207f4d41ab29 Mon Sep 17 00:00:00 2001 From: Murat Kirazkaya Date: Thu, 19 Nov 2026 23:09:09 +0300 Subject: [PATCH] tools: fix active release line filter in core index supportedVersionAffected() is async but was called without await, so the filter never ran and index.json kept entries that only affect end-of-life release lines. Await the supported lines once and keep an entry when its range intersects a supported line, so ranges like <24.5.0 are kept. Refs: https://github.com/nodejs/security-wg/pull/942 Assisted-by: a closed-source coding agent Signed-off-by: Murat Kirazkaya --- test/index-generator.js | 43 +++++++++++++++++++++++++++ tools/create_index/index_generator.js | 30 +++++++++---------- 2 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 test/index-generator.js 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