-
Notifications
You must be signed in to change notification settings - Fork 8
/
is-vulnerable.js
213 lines (183 loc) · 6.04 KB
/
is-vulnerable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const { danger, allGood, bold, vulnerableWarning, separator } = require('./ascii')
const { request } = require('https')
const fs = require('fs')
const path = require('path')
const satisfies = require('semver/functions/satisfies')
const STORE = {
security: {
url: 'https://raw.githubusercontent.com/nodejs/security-wg/main/vuln/core/index.json',
jsonFile: path.join(__dirname, 'security.json'),
etagFile: path.join(__dirname, 'security.etag'),
etagValue: ''
},
schedule: {
url: 'https://raw.githubusercontent.com/nodejs/Release/main/schedule.json',
jsonFile: path.join(__dirname, 'schedule.json'),
etagFile: path.join(__dirname, 'schedule.etag'),
etagValue: ''
}
}
async function readLocal (file) {
return require(file)
}
function debug (msg) {
if (process.env.DEBUG) {
console.debug(msg)
}
}
function loadETag () {
for (const [key, obj] of Object.entries(STORE)) {
if (fs.existsSync(obj.etagFile)) {
debug(`Loading local ETag for '${key}'`)
obj.etagValue = fs.readFileSync(obj.etagFile).toString()
}
}
}
async function fetchJson (obj) {
await new Promise((resolve) => {
request(obj.url, (res) => {
if (res.statusCode !== 200) {
console.error(`Request to Github returned http status ${res.statusCode}. Aborting...`)
process.nextTick(() => { process.exit(1) })
}
const fileStream = fs.createWriteStream(obj.jsonFile)
res.pipe(fileStream)
fileStream.on('finish', () => {
fileStream.close()
resolve()
})
fileStream.on('error', (err) => {
console.error(`Error ${err.message} while writing to '${obj.jsonFile}'. Aborting...`)
process.nextTick(() => { process.exit(1) })
})
}).on('error', (err) => {
console.error(`Request to Github returned error ${err.message}. Aborting...`)
process.nextTick(() => { process.exit(1) })
}).end()
})
return readLocal(obj.jsonFile)
}
async function getJson (obj) {
return new Promise((resolve) => {
request(obj.url, { method: 'HEAD' }, (res) => {
if (res.statusCode !== 200) {
console.error(`Request to Github returned http status ${res.statusCode}. Aborting...`)
process.nextTick(() => { process.exit(1) })
}
res.on('data', () => {})
const { etag } = res.headers
if (!obj.etagValue || obj.eTagValue !== etag || !fs.existsSync(obj.jsonFile)) {
obj.etagValue = etag
fs.writeFileSync(obj.etagFile, etag)
debug('Creating local core.json')
resolve(fetchJson(obj))
} else {
debug(`No updates from upstream. Getting a cached version: ${obj.jsonFile}`)
resolve(readLocal(obj.jsonFile))
}
}).on('error', (err) => {
console.error(`Request to Github returned error ${err.message}. Aborting...`)
process.nextTick(() => { process.exit(1) })
}).end()
})
}
const checkPlatform = platform => {
const availablePlatforms = ['aix', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', 'win32', 'android']
if (platform && !availablePlatforms.includes(platform)) {
throw new Error(`platform ${platform} is not valid. Please use ${availablePlatforms.join(',')}.`)
}
}
const isSystemAffected = (platform, affectedEnvironments) => {
// No platform specified (legacy mode)
if (!platform || !Array.isArray(affectedEnvironments)) {
return true
}
// If the environment is matching or all the environments are affected
if (affectedEnvironments.includes(platform) || affectedEnvironments.includes('all')) {
return true
}
// Default to false
return false
}
function getVulnerabilityList (currentVersion, data, platform) {
const list = []
for (const key in data) {
const vuln = data[key]
if (
(
satisfies(currentVersion, vuln.vulnerable) &&
!satisfies(currentVersion, vuln.patched)
) && isSystemAffected(platform, vuln.affectedEnvironments)
) {
const severity = vuln.severity === 'unknown' ? '' : `(${vuln.severity})`
list.push(`${bold(vuln.cve)}${severity}: ${vuln.overview}\n${bold('Patched versions')}: ${vuln.patched}`)
}
}
return list
}
async function cli (currentVersion, platform) {
checkPlatform(platform)
const isEOL = await isNodeEOL(currentVersion)
if (isEOL) {
console.error(danger)
console.error(`${currentVersion} is end-of-life. There are high chances of being vulnerable. Please upgrade it.`)
process.exit(1)
}
const securityJson = await getJson(STORE.security)
const list = getVulnerabilityList(currentVersion, securityJson, platform)
if (list.length) {
console.error(danger)
console.error(vulnerableWarning + '\n')
console.error(`${list.join(`\n${separator}\n\n`)}\n${separator}`)
process.exit(1)
} else {
console.info(allGood)
}
}
async function getVersionInfo (version) {
const scheduleJson = await getJson(STORE.schedule)
if (scheduleJson[version.toLowerCase()]) {
return scheduleJson[version.toLowerCase()]
}
for (const [key, value] of Object.entries(scheduleJson)) {
if (satisfies(version, key)) {
return value
}
}
return null
}
/**
* @param {string} version
* @returns {Promise<boolean>} true if the version is end-of-life
*/
async function isNodeEOL (version) {
const myVersionInfo = await getVersionInfo(version)
if (!myVersionInfo) {
// i.e. isNodeEOL('abcd') or isNodeEOL('lts') or isNodeEOL('99')
throw Error(`Could not fetch version information for ${version}`)
} else if (!myVersionInfo.end) {
// We got a record, but..
// v0.12.18 etc does not have an EOL date, which probably means too old.
return true
}
const now = new Date()
const end = new Date(myVersionInfo.end)
return now > end
}
async function isNodeVulnerable (version, platform) {
checkPlatform(platform)
const isEOL = await isNodeEOL(version)
if (isEOL) {
return true
}
const coreIndex = await getJson(STORE.security)
const list = getVulnerabilityList(version, coreIndex, platform)
return list.length > 0
}
if (process.argv[2] !== '-r') {
loadETag()
}
module.exports = {
isNodeVulnerable,
cli
}