-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
292 lines (266 loc) · 7.61 KB
/
index.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
module.exports = licensee
var Arborist = require('@npmcli/arborist')
var blueOakList = require('@blueoak/list')
var correctLicenseMetadata = require('correct-license-metadata')
var hasOwn = require('hasown')
var npmLicenseCorrections = require('npm-license-corrections')
var osi = require('spdx-osi')
var parse = require('spdx-expression-parse')
var satisfies = require('semver').satisfies
var spdxAllowed = require('spdx-whitelisted')
function licensee (configuration, path, callback) {
if (!validConfiguration(configuration)) {
return callback(new Error('Invalid configuration'))
}
configuration.licenses = compileLicenseAllowlist(configuration)
configuration.licensesParsed = (configuration.licenses || [])
.reduce(function (allowlist, element) {
try {
var parsed = parse(element)
if (has(parsed, 'conjunction')) {
throw new Error('Cannot match against "' + JSON.stringify(element) + '".')
}
return allowlist.concat(parsed)
} catch (e) {
return allowlist
}
}, [])
if (
configuration.licenses.length === 0 &&
(!configuration.packages || Object.keys(configuration.packages).length === 0)
) {
callback(new Error('No licenses or packages allowed.'))
} else {
var arborist = new Arborist({ path })
arborist.loadActual({ forceActual: true })
.then(function (tree) {
var dependencies = Array.from(tree.inventory.values())
.filter(function (dependency) {
return !dependency.isProjectRoot
})
if (configuration.filterPackages) {
dependencies = configuration.filterPackages(dependencies)
}
callback(null, findIssues(configuration, dependencies))
})
.catch(function (error) {
return callback(error)
})
}
}
function validConfiguration (configuration) {
return (
isObject(configuration) &&
has(configuration, 'licenses') &&
isObject(configuration.licenses) &&
(!has(configuration.licenses, 'blueOak') ||
(
blueOakList.some(({ name }) =>
name.toLowerCase() === configuration.licenses.blueOak.toLowerCase()
)
)) &&
(has(configuration, 'packages')
? (
// Validate `packages` property.
isObject(configuration.packages) &&
Object.keys(configuration.packages)
.every(function (key) {
return isString(configuration.packages[key])
})
) : true)
)
}
function isObject (argument) {
return argument && typeof argument === 'object'
}
function isString (argument) {
return typeof argument === 'string'
}
function findIssues (configuration, dependencies) {
var results = []
dependencies.forEach(function (dependency) {
if (
configuration.productionOnly &&
dependency.dev
) return
var result = resultForPackage(configuration, dependency)
// Deduplicate.
var existing = results.find(function (existing) {
return (
existing.name === result.name &&
existing.version === result.version
)
})
if (existing) {
if (existing.duplicates) {
existing.duplicates.push(result)
} else {
existing.duplicates = [result]
}
} else {
results.push(result)
}
})
results.sort(function (a, b) {
return a.name.localeCompare(b.name)
})
return results
}
function resultForPackage (configuration, tree) {
var packageAllowlist = configuration.packages || {}
var result = {
name: tree.package.name,
version: tree.version,
license: tree.package.license,
author: tree.package.author,
contributors: tree.package.contributors,
repository: tree.package.repository,
homepage: tree.package.homepage,
parent: tree.parent,
path: tree.realpath
}
// Find and apply any manual license metadata correction.
var manualCorrection = (
configuration.corrections &&
npmLicenseCorrections.find(function (correction) {
return (
correction.name === result.name &&
correction.version === result.version
)
})
)
if (manualCorrection) {
result.license = manualCorrection.license
result.corrected = 'manual'
}
// Find and apply any automatic license metadata correction.
var automaticCorrection = (
configuration.corrections &&
correctLicenseMetadata(tree.package)
)
if (automaticCorrection) {
result.license = automaticCorrection
result.corrected = 'automatic'
}
// Check if ignored.
var ignore = configuration.ignore
if (ignore && Array.isArray(ignore)) {
var ignored = ignore.some(function (ignore) {
if (typeof ignore !== 'object') return false
if (
ignore.prefix &&
typeof ignore.prefix === 'string' &&
startsWith(result.name, ignore.prefix)
) return true
if (
ignore.scope &&
typeof ignore.scope === 'string' &&
startsWith(result.name, '@' + ignore.scope + '/')
) return true
if (
ignore.author &&
typeof ignore.author === 'string' &&
personMatches(result.author, ignore.author)
) return true
return false
})
if (ignored) {
result.approved = true
result.ignored = ignored
return result
}
}
result.approved = false
var packageAllowed = Object.keys(packageAllowlist)
.some(function (name) {
return (
result.name === name &&
satisfies(result.version, packageAllowlist[name]) === true
)
})
if (packageAllowed) {
result.approved = true
result.package = true
return result
}
if (!result.license || typeof result.license !== 'string') {
return result
}
var validSPDX = true
var parsed
try {
parsed = parse(result.license)
} catch (e) {
validSPDX = false
}
var licenseAllowlist = configuration.licensesParsed
// Check against licensing rule.
var licenseAllowed = (
validSPDX &&
spdxAllowed(parsed, licenseAllowlist)
)
if (licenseAllowed) {
result.approved = true
}
return result
}
function startsWith (string, prefix) {
return string.toLowerCase().indexOf(prefix.toLowerCase()) === 0
}
function personMatches (person, string) {
if (!person) return false
if (typeof person === 'string') {
return contains(person, string)
}
if (typeof person === 'object') {
if (matches('name')) return true
if (matches('email')) return true
if (matches('url')) return true
}
return false
function matches (key) {
return (
person[key] &&
typeof person[key] === 'string' &&
contains(person[key], string)
)
}
}
function contains (string, substring) {
return string.toLowerCase().indexOf(substring.toLowerCase()) !== -1
}
function licensesFromBlueOak (rating) {
rating = rating.toLowerCase()
var ids = []
for (var index = 0; index < blueOakList.length; index++) {
var element = blueOakList[index]
element.licenses.forEach(function (license) {
try {
parse(license.id)
ids.push(license.id)
} catch (e) {
// pass
}
})
if (rating === element.name.toLowerCase()) break
}
return ids
}
function compileLicenseAllowlist (configuration) {
var licenses = configuration.licenses
var allowlist = []
var spdx = licenses.spdx
if (spdx) pushMissing(spdx, allowlist)
var blueOak = licenses.blueOak
if (blueOak) pushMissing(licensesFromBlueOak(blueOak), allowlist)
if (licenses.osi) pushMissing(osi, allowlist)
return allowlist
}
function pushMissing (source, sink) {
source.forEach(function (element) {
if (sink.indexOf(element) === -1) sink.push(element)
})
}
function has (object, key) {
return hasOwn(object, key) && object[key] !== undefined
}