Skip to content

Commit 8e457fc

Browse files
srl295RafaelGSS
andcommitted
feat: add support to end-of-life versions
Fixes: #2 Co-authored-by: RafaelGSS <rafael.nunu@hotmail.com>
1 parent c5d346a commit 8e457fc

5 files changed

Lines changed: 764 additions & 19 deletions

File tree

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ $ npx is-my-node-vulnerable
5151

5252
```
5353

54+
### Output - when end of life
55+
56+
```console
57+
$ node -v
58+
v15.14.0
59+
$ npx is-my-node-vulnerable
60+
██████ █████ ███ ██ ██████ ███████ ██████
61+
██ ██ ██ ██ ████ ██ ██ ██ ██ ██
62+
██ ██ ███████ ██ ██ ██ ██ ███ █████ ██████
63+
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
64+
██████ ██ ██ ██ ████ ██████ ███████ ██ ██
65+
66+
67+
v15.14.0 is end-of-life. There are high chances of being vulnerable. Please upgrade it.
68+
```
69+
70+
End-of-Life versions don't keep track of recent security releases, therefore, it's considered vulnerable by default.
71+
5472
## API
5573

5674
This package also exports a function `isNodeVulnerable` to perform the check in runtime

index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const path = require('path')
77
const debug = require('debug')('is-my-node-vulnerable')
88
const satisfies = require('semver/functions/satisfies')
99
const { danger, vulnerableWarning, bold, separator, allGood } = require('./ascii')
10+
const nv = require('@pkgjs/nv')
1011

1112
setGlobalDispatcher(new Agent({ connections: 20 }))
1213

@@ -73,6 +74,13 @@ function getVulnerabilityList (currentVersion, data) {
7374
}
7475

7576
async function main (currentVersion) {
77+
const isEOL = await isNodeEOL(currentVersion)
78+
if (isEOL) {
79+
console.error(danger)
80+
console.error(`${currentVersion} is end-of-life. There are high chances of being vulnerable. Please upgrade it.`)
81+
process.exit(1)
82+
}
83+
7684
const coreIndex = await getCoreIndex()
7785
const list = getVulnerabilityList(currentVersion, coreIndex)
7886
if (list.length) {
@@ -85,7 +93,34 @@ async function main (currentVersion) {
8593
}
8694
}
8795

96+
/**
97+
* @param {string} version
98+
* @returns {Promise<boolean>} true if the version is end-of-life
99+
*/
100+
async function isNodeEOL (version) {
101+
const myVersionInfo = await nv(version)
102+
if (!myVersionInfo) {
103+
// i.e. isNodeEOL('abcd')
104+
throw Error(`Could not fetch version information for ${version}`)
105+
} else if (myVersionInfo.length !== 1) {
106+
// i.e. isNodeEOL('lts') or isNodeEOL('99')
107+
throw Error(`Did not get exactly one version record for ${version}`)
108+
} else if (!myVersionInfo[0].end) {
109+
// We got a record, but..
110+
// v0.12.18 etc does not have an EOL date, which probably means too old.
111+
return true
112+
}
113+
const now = new Date()
114+
const end = new Date(myVersionInfo[0].end)
115+
return now > end
116+
}
117+
88118
async function isNodeVulnerable (version) {
119+
const isEOL = await isNodeEOL(version)
120+
if (isEOL) {
121+
return true
122+
}
123+
89124
const coreIndex = await getCoreIndex()
90125
const list = getVulnerabilityList(version, coreIndex)
91126
return list.length > 0

0 commit comments

Comments
 (0)