Skip to content

Commit 6acd1ff

Browse files
UlisesGasconRafaelGSS
authored andcommitted
feat: added affectedEnvironments validation
1 parent 4f0392a commit 6acd1ff

File tree

4 files changed

+70
-19
lines changed

4 files changed

+70
-19
lines changed

action.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@ const { isNodeVulnerable } = require('./index')
44
async function run () {
55
// Inputs
66
const nodeVersion = core.getInput('node-version', { required: true })
7-
core.info(`Checking Node.js version ${nodeVersion}...`)
8-
const isVulnerable = await isNodeVulnerable(nodeVersion)
7+
const platform = core.getInput('platform', { required: false })
8+
9+
if (platform && !['linux', 'win', 'osx'].includes(platform)) {
10+
core.setFailed(`platform ${platform} is not valid. Please use linux, win or osx.`)
11+
}
12+
13+
core.info(`Checking Node.js version ${nodeVersion} with platform ${platform}...`)
14+
const isVulnerable = await isNodeVulnerable(nodeVersion, platform)
915
if (isVulnerable) {
1016
core.setFailed(`Node.js version ${nodeVersion} is vulnerable. Please upgrade!`)
1117
} else {

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ inputs:
1111
description: 'Node.js version to check'
1212
required: true
1313
default: '16.13.0'
14+
platform:
15+
description: 'Platform to check'
16+
required: false
1417

1518
# https://actions-cool.github.io/github-action-branding/
1619
branding:

dist/index.js

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const { request, stream, setGlobalDispatcher, Agent } = __nccwpck_require__(1773
5151
const EE = __nccwpck_require__(2361)
5252
const fs = __nccwpck_require__(7147)
5353
const path = __nccwpck_require__(1017)
54+
const os = __nccwpck_require__(2037)
5455
const debug = __nccwpck_require__(8237)('is-my-node-vulnerable')
5556
const satisfies = __nccwpck_require__(6055)
5657
const { danger, vulnerableWarning, bold, separator, allGood } = __nccwpck_require__(9139)
@@ -106,21 +107,38 @@ async function getCoreIndex () {
106107
}
107108
}
108109

109-
function getVulnerabilityList (currentVersion, data) {
110+
function getVulnerabilityList (currentVersion, data, systemEnvironment) {
110111
const list = []
111112
for (const key in data) {
112113
const vuln = data[key]
113114
if (
114-
satisfies(currentVersion, vuln.vulnerable) &&
115-
!satisfies(currentVersion, vuln.patched)
115+
(
116+
satisfies(currentVersion, vuln.vulnerable) &&
117+
!satisfies(currentVersion, vuln.patched)
118+
) && (
119+
(!systemEnvironment || !Array.isArray(vuln.affectedEnvironments)) ||
120+
vuln.affectedEnvironments.includes(systemEnvironment)
121+
)
116122
) {
117123
list.push(`${bold(vuln.cve)}: ${vuln.overview}\n${bold('Patched versions')}: ${vuln.patched}`)
118124
}
119125
}
120126
return list
121127
}
122128

123-
async function main (currentVersion) {
129+
const getSystemEnvironment = (platform) => {
130+
switch (platform) {
131+
case 'darwin':
132+
return 'osx'
133+
case 'win32':
134+
return 'win'
135+
default:
136+
return 'linux'
137+
}
138+
}
139+
140+
async function main (currentVersion, platform) {
141+
const systemEnvironment = getSystemEnvironment(platform)
124142
const isEOL = await isNodeEOL(currentVersion)
125143
if (isEOL) {
126144
console.error(danger)
@@ -129,7 +147,7 @@ async function main (currentVersion) {
129147
}
130148

131149
const coreIndex = await getCoreIndex()
132-
const list = getVulnerabilityList(currentVersion, coreIndex)
150+
const list = getVulnerabilityList(currentVersion, coreIndex, systemEnvironment)
133151
if (list.length) {
134152
console.error(danger)
135153
console.error(vulnerableWarning + '\n')
@@ -162,14 +180,14 @@ async function isNodeEOL (version) {
162180
return now > end
163181
}
164182

165-
async function isNodeVulnerable (version) {
183+
async function isNodeVulnerable (version, systemEnvironment) {
166184
const isEOL = await isNodeEOL(version)
167185
if (isEOL) {
168186
return true
169187
}
170188

171189
const coreIndex = await getCoreIndex()
172-
const list = getVulnerabilityList(version, coreIndex)
190+
const list = getVulnerabilityList(version, coreIndex, systemEnvironment)
173191
return list.length > 0
174192
}
175193

@@ -41519,8 +41537,14 @@ const { isNodeVulnerable } = __nccwpck_require__(2932)
4151941537
async function run () {
4152041538
// Inputs
4152141539
const nodeVersion = core.getInput('node-version', { required: true })
41522-
core.info(`Checking Node.js version ${nodeVersion}...`)
41523-
const isVulnerable = await isNodeVulnerable(nodeVersion)
41540+
const platform = core.getInput('platform', { required: false })
41541+
41542+
if (platform && !['linux', 'win', 'osx'].includes(platform)) {
41543+
core.setFailed(`platform ${platform} is not valid. Please use linux, win or osx.`)
41544+
}
41545+
41546+
core.info(`Checking Node.js version ${nodeVersion} with platform ${platform}...`)
41547+
const isVulnerable = await isNodeVulnerable(nodeVersion, platform)
4152441548
if (isVulnerable) {
4152541549
core.setFailed(`Node.js version ${nodeVersion} is vulnerable. Please upgrade!`)
4152641550
} else {

index.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { request, stream, setGlobalDispatcher, Agent } = require('undici')
44
const EE = require('events')
55
const fs = require('fs')
66
const path = require('path')
7+
const os = require('os')
78
const debug = require('debug')('is-my-node-vulnerable')
89
const satisfies = require('semver/functions/satisfies')
910
const { danger, vulnerableWarning, bold, separator, allGood } = require('./ascii')
@@ -59,21 +60,38 @@ async function getCoreIndex () {
5960
}
6061
}
6162

62-
function getVulnerabilityList (currentVersion, data) {
63+
function getVulnerabilityList (currentVersion, data, systemEnvironment) {
6364
const list = []
6465
for (const key in data) {
6566
const vuln = data[key]
6667
if (
67-
satisfies(currentVersion, vuln.vulnerable) &&
68-
!satisfies(currentVersion, vuln.patched)
68+
(
69+
satisfies(currentVersion, vuln.vulnerable) &&
70+
!satisfies(currentVersion, vuln.patched)
71+
) && (
72+
(!systemEnvironment || !Array.isArray(vuln.affectedEnvironments)) ||
73+
vuln.affectedEnvironments.includes(systemEnvironment)
74+
)
6975
) {
7076
list.push(`${bold(vuln.cve)}: ${vuln.overview}\n${bold('Patched versions')}: ${vuln.patched}`)
7177
}
7278
}
7379
return list
7480
}
7581

76-
async function main (currentVersion) {
82+
const getSystemEnvironment = (platform) => {
83+
switch (platform) {
84+
case 'darwin':
85+
return 'osx'
86+
case 'win32':
87+
return 'win'
88+
default:
89+
return 'linux'
90+
}
91+
}
92+
93+
async function main (currentVersion, platform) {
94+
const systemEnvironment = getSystemEnvironment(platform)
7795
const isEOL = await isNodeEOL(currentVersion)
7896
if (isEOL) {
7997
console.error(danger)
@@ -82,7 +100,7 @@ async function main (currentVersion) {
82100
}
83101

84102
const coreIndex = await getCoreIndex()
85-
const list = getVulnerabilityList(currentVersion, coreIndex)
103+
const list = getVulnerabilityList(currentVersion, coreIndex, systemEnvironment)
86104
if (list.length) {
87105
console.error(danger)
88106
console.error(vulnerableWarning + '\n')
@@ -115,14 +133,14 @@ async function isNodeEOL (version) {
115133
return now > end
116134
}
117135

118-
async function isNodeVulnerable (version) {
136+
async function isNodeVulnerable (version, systemEnvironment) {
119137
const isEOL = await isNodeEOL(version)
120138
if (isEOL) {
121139
return true
122140
}
123141

124142
const coreIndex = await getCoreIndex()
125-
const list = getVulnerabilityList(version, coreIndex)
143+
const list = getVulnerabilityList(version, coreIndex, systemEnvironment)
126144
return list.length > 0
127145
}
128146

@@ -132,7 +150,7 @@ if (process.argv[2] !== '-r') {
132150

133151
// CLI
134152
if (require.main === module) {
135-
main(process.version)
153+
main(process.version, os.platform())
136154
} else {
137155
module.exports = {
138156
isNodeVulnerable

0 commit comments

Comments
 (0)