Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions test/index-generator.js
Original file line number Diff line number Diff line change
@@ -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];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use semver since it's already installed to get this list.


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');
});
});
30 changes: 15 additions & 15 deletions tools/create_index/index_generator.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
Expand All @@ -58,3 +57,4 @@ const writeIndex = function(data, vulnDirectoryPath) {
}

module.exports = createIndex
module.exports.supportedVersionAffected = supportedVersionAffected
Loading