Merge pull request #927 from dangoor/dangoor/multilicense

Handle complex licenses (e.g. X AND Y)
This commit is contained in:
Kevin Dangoor
2025-05-07 13:06:06 -04:00
committed by GitHub
10 changed files with 265 additions and 177 deletions

View File

@@ -21,6 +21,7 @@ const npmChange: Change = {
}
]
}
const rubyChange: Change = {
change_type: 'added',
manifest: 'Gemfile.lock',
@@ -73,6 +74,32 @@ const pipChange: Change = {
]
}
const complexLicenseChange: Change = {
change_type: 'added',
manifest: 'requirements.txt',
ecosystem: 'pip',
name: 'package-1',
version: '1.1.1',
package_url: 'pkg:pypi/package-1@1.1.1',
license: 'MIT AND Apache-2.0',
source_repository_url: 'github.com/some-repo',
scope: 'runtime',
vulnerabilities: [
{
severity: 'moderate',
advisory_ghsa_id: 'second-random_string',
advisory_summary: 'not so dangerous',
advisory_url: 'github.com/future-funk'
},
{
severity: 'low',
advisory_ghsa_id: 'third-random_string',
advisory_summary: 'dont page me',
advisory_url: 'github.com/future-funk'
}
]
}
jest.mock('@actions/core')
const mockOctokit = {
@@ -100,105 +127,6 @@ beforeEach(async () => {
jest.resetModules()
})
test('it should handle SPDX expressions in allow-list that matches a single license project', async () => {
const change: Change = getChangeWithLicense('MIT')
const changes: Changes = [change]
const {forbidden} = await getInvalidLicenseChanges(changes, {
allow: ['EPL-1.0 OR MIT']
})
expect(forbidden).toStrictEqual([])
})
test('it should handle SPDX expressions in allow-list with operators and a valid triple licensed project', async () => {
const change: Change = getChangeWithLicense(
'EPL-1.0 AND LGPL-2.1 AND LGPL-2.1-only'
)
const changes: Changes = [change]
const {forbidden} = await getInvalidLicenseChanges(changes, {
allow: ['EPL-1.0 AND LGPL-2.1 AND LGPL-2.1-only']
})
expect(forbidden).toStrictEqual([])
})
test('it should handle a valid triple licensed project that does not have a match in the allow-list', async () => {
const change = getChangeWithLicense('EPL-1.0 AND LGPL-2.1 AND LGPL-2.1-only')
const changes: Changes = [change]
const {forbidden} = await getInvalidLicenseChanges(changes, {
allow: ['EPL-1.0', 'LGPL-2.1', 'LGPL-2.1-only']
})
expect(forbidden[0]).toBe(change)
expect(forbidden.length).toEqual(1)
})
test('it should handle license with OR SPDX expression and only match on one license in the allow-list', async () => {
const change = getChangeWithLicense('EPL-1.0 OR LGPL-2.1')
const changes: Changes = [change]
for (const allowedLicense of ['EPL-1.0', 'LGPL-2.1']) {
const {forbidden} = await getInvalidLicenseChanges(changes, {
allow: [allowedLicense]
})
expect(forbidden).toStrictEqual([])
}
})
test('it should handle SPDX expressions in allow-list with operators when license matches', async () => {
const changes: Changes = [
npmChange // MIT license
]
const {forbidden} = await getInvalidLicenseChanges(changes, {
allow: ['MIT OR Apache-2.0', 'MIT', 'BSD-3-Clause']
})
expect(forbidden).toStrictEqual([])
})
test('it should handle SPDX expressions in allow-list with operators when license does not match', async () => {
const changes: Changes = [
npmChange // MIT license
]
const {forbidden} = await getInvalidLicenseChanges(changes, {
allow: ['MIT AND Apache-2.0', 'BSD-3-Clause']
})
expect(forbidden[0]).toBe(npmChange)
expect(forbidden.length).toEqual(1)
})
test('it should handle SPDX expressions in deny-list with operators when license matches deny list entry', async () => {
const changes: Changes = [
npmChange // MIT license
]
const {forbidden} = await getInvalidLicenseChanges(changes, {
deny: ['MIT OR Apache-2.0', 'BSD-3-Clause']
})
expect(forbidden[0]).toBe(npmChange)
expect(forbidden.length).toEqual(1)
})
test('it should handle SPDX expressions in deny-list with operators when license does not match any deny list entry', async () => {
const changes: Changes = [
npmChange // MIT license
]
const {forbidden} = await getInvalidLicenseChanges(changes, {
deny: ['MIT AND Apache-2.0', 'BSD-3-Clause']
})
expect(forbidden).toStrictEqual([])
})
test('it adds license outside the allow list to forbidden changes', async () => {
const changes: Changes = [
npmChange, // MIT license
@@ -227,6 +155,30 @@ test('it adds license inside the deny list to forbidden changes', async () => {
expect(forbidden.length).toEqual(1)
})
test('it handles allowed complex licenses', async () => {
const changes: Changes = [
complexLicenseChange // MIT AND Apache-2.0 license
]
const {forbidden} = await getInvalidLicenseChanges(changes, {
allow: ['MIT', 'Apache-2.0']
})
expect(forbidden.length).toEqual(0)
})
test('it handles complex licenses not all on the allow list', async () => {
const changes: Changes = [
complexLicenseChange // MIT AND Apache-2.0 license
]
const {forbidden} = await getInvalidLicenseChanges(changes, {
allow: ['MIT']
})
expect(forbidden.length).toEqual(1)
})
test('it does not add license outside the allow list to forbidden changes if it is in removed changes', async () => {
const changes: Changes = [
{...npmChange, change_type: 'removed'},
@@ -362,25 +314,3 @@ describe('GH License API fallback', () => {
expect(unlicensed.length).toEqual(0)
})
})
function getChangeWithLicense(license: string): Change {
return {
manifest: 'pom.xml',
change_type: 'added',
ecosystem: 'maven',
name: 'dummy-library',
version: '1.0.0',
package_url: 'pkg:org.something:sdummy-library@1.0.0',
license,
source_repository_url: 'github.com/some-repo',
scope: 'runtime',
vulnerabilities: [
{
severity: 'critical',
advisory_ghsa_id: 'first-random_string',
advisory_summary: 'very dangerous',
advisory_url: 'github.com/future-funk'
}
]
}
}

View File

@@ -145,47 +145,47 @@ describe('satisfies', () => {
const units = [
{
candidate: 'MIT',
constraint: 'MIT',
allowList: ['MIT'],
expected: true
},
{
candidate: 'Apache-2.0',
constraint: 'MIT',
allowList: ['MIT'],
expected: false
},
{
candidate: 'MIT OR Apache-2.0',
constraint: 'MIT',
allowList: ['MIT'],
expected: true
},
{
candidate: 'MIT OR Apache-2.0',
constraint: 'Apache-2.0',
allowList: ['Apache-2.0'],
expected: true
},
{
candidate: 'MIT OR Apache-2.0',
constraint: 'BSD-3-Clause',
allowList: ['BSD-3-Clause'],
expected: false
},
{
candidate: 'MIT OR Apache-2.0',
constraint: 'Apache-2.0 OR BSD-3-Clause',
allowList: ['Apache-2.0', 'BSD-3-Clause'],
expected: true
},
{
candidate: 'MIT AND Apache-2.0',
constraint: 'MIT AND Apache-2.0',
allowList: ['MIT', 'Apache-2.0'],
expected: true
},
{
candidate: 'MIT OR Apache-2.0',
constraint: 'MIT AND Apache-2.0',
expected: false
allowList: ['MIT', 'Apache-2.0'],
expected: true
},
{
candidate: 'ISC OR (MIT AND Apache-2.0)',
constraint: 'MIT AND Apache-2.0',
allowList: ['MIT', 'Apache-2.0'],
expected: true
},
@@ -193,29 +193,29 @@ describe('satisfies', () => {
// or unknown licenses will return 'false'
{
candidate: 'MIT',
constraint: 'MiT',
allowList: ['MiT'],
expected: false
},
{
candidate: 'MIT AND (ISC OR',
constraint: 'MIT',
allowList: ['MIT'],
expected: false
},
{
candidate: 'MIT OR ISC OR Apache-2.0',
constraint: '',
allowList: [],
expected: false
},
{
candidate: '',
constraint: '(BSD-3-Clause AND ISC) OR MIT',
allowList: ['BSD-3-Clause', 'ISC', 'MIT'],
expected: false
}
]
for (const unit of units) {
const got: boolean = spdx.satisfies(unit.candidate, unit.constraint)
test(`should return ${unit.expected} for ("${unit.candidate}", "${unit.constraint}")`, () => {
const got: boolean = spdx.satisfies(unit.candidate, unit.allowList)
test(`should return ${unit.expected} for ("${unit.candidate}", "${unit.allowList}")`, () => {
expect(got).toBe(unit.expected)
})
}

169
dist/index.js generated vendored
View File

@@ -435,10 +435,7 @@ function getInvalidLicenseChanges(changes, licenses) {
try {
if (allow !== undefined) {
if (spdx.isValid(license)) {
let found = false;
for (const allowedLicense of allow) {
found || (found = spdx.satisfies(allowedLicense, license));
}
const found = spdx.satisfies(license, allow);
validityCache.set(license, found);
}
else {
@@ -447,10 +444,7 @@ function getInvalidLicenseChanges(changes, licenses) {
}
else if (deny !== undefined) {
if (spdx.isValid(license)) {
let found = false;
for (const deniedLicense of deny) {
found || (found = spdx.satisfies(deniedLicense, license));
}
const found = spdx.satisfiesAny(license, deny);
validityCache.set(license, !found);
}
else {
@@ -1388,6 +1382,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isValid = exports.satisfiesAll = exports.satisfiesAny = exports.satisfies = void 0;
const spdxlib = __importStar(__nccwpck_require__(1452));
const spdx_satisfies_1 = __importDefault(__nccwpck_require__(5131));
const spdx_expression_parse_1 = __importDefault(__nccwpck_require__(3326));
/*
* NOTE: spdx-license-satisfies methods depend on spdx-expression-parse
@@ -1397,9 +1392,9 @@ const spdx_expression_parse_1 = __importDefault(__nccwpck_require__(3326));
*/
// accepts a pair of well-formed SPDX expressions. the
// candidate is tested against the constraint
function satisfies(candidateExpr, constraintExpr) {
function satisfies(candidateExpr, allowList) {
try {
return spdxlib.satisfies(candidateExpr, constraintExpr);
return (0, spdx_satisfies_1.default)(candidateExpr, allowList);
}
catch (_) {
return false;
@@ -21956,6 +21951,155 @@ module.exports = function (source) {
}
/***/ }),
/***/ 5131:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
var compare = __nccwpck_require__(7369)
var parse = __nccwpck_require__(3326)
var ranges = __nccwpck_require__(9344)
function rangesAreCompatible (first, second) {
return (
first.license === second.license ||
ranges.some(function (range) {
return (
licenseInRange(first.license, range) &&
licenseInRange(second.license, range)
)
})
)
}
function licenseInRange (license, range) {
return (
range.indexOf(license) !== -1 ||
range.some(function (element) {
return (
Array.isArray(element) &&
element.indexOf(license) !== -1
)
})
)
}
function identifierInRange (identifier, range) {
return (
identifier.license === range.license ||
compare.gt(identifier.license, range.license) ||
compare.eq(identifier.license, range.license)
)
}
function licensesAreCompatible (first, second) {
if (first.exception !== second.exception) {
return false
} else if (second.hasOwnProperty('license')) {
if (second.hasOwnProperty('plus')) {
if (first.hasOwnProperty('plus')) {
// first+, second+
return rangesAreCompatible(first, second)
} else {
// first, second+
return identifierInRange(first, second)
}
} else {
if (first.hasOwnProperty('plus')) {
// first+, second
return identifierInRange(second, first)
} else {
// first, second
return first.license === second.license
}
}
}
}
function replaceGPLOnlyOrLaterWithRanges (argument) {
var license = argument.license
if (license) {
if (endsWith(license, '-or-later')) {
argument.license = license.replace('-or-later', '')
argument.plus = true
} else if (endsWith(license, '-only')) {
argument.license = license.replace('-only', '')
delete argument.plus
}
} else if (argument.left && argument.right) {
argument.left = replaceGPLOnlyOrLaterWithRanges(argument.left)
argument.right = replaceGPLOnlyOrLaterWithRanges(argument.right)
}
return argument
}
function endsWith (string, substring) {
return string.indexOf(substring) === string.length - substring.length
}
function licenseString (e) {
if (e.hasOwnProperty('noassertion')) return 'NOASSERTION'
if (e.license) {
return (
e.license +
(e.plus ? '+' : '') +
(e.exception ? ('WITH ' + e.exception) : '')
)
}
}
// Expand the given expression into an equivalent array where each member is an array of licenses AND'd
// together and the members are OR'd together. For example, `(MIT OR ISC) AND GPL-3.0` expands to
// `[[GPL-3.0 AND MIT], [ISC AND MIT]]`. Note that within each array of licenses, the entries are
// normalized (sorted) by license name.
function expand (expression) {
return sort(expandInner(expression))
}
function expandInner (expression) {
if (!expression.conjunction) return [{ [licenseString(expression)]: expression }]
if (expression.conjunction === 'or') return expandInner(expression.left).concat(expandInner(expression.right))
if (expression.conjunction === 'and') {
var left = expandInner(expression.left)
var right = expandInner(expression.right)
return left.reduce(function (result, l) {
right.forEach(function (r) { result.push(Object.assign({}, l, r)) })
return result
}, [])
}
}
function sort (licenseList) {
var sortedLicenseLists = licenseList
.filter(function (e) { return Object.keys(e).length })
.map(function (e) { return Object.keys(e).sort() })
return sortedLicenseLists.map(function (list, i) {
return list.map(function (license) { return licenseList[i][license] })
})
}
function isANDCompatible (parsedExpression, parsedLicenses) {
return parsedExpression.every(function (element) {
return parsedLicenses.some(function (approvedLicense) {
return licensesAreCompatible(element, approvedLicense)
})
})
}
function satisfies (spdxExpression, arrayOfLicenses) {
var parsedExpression = expand(replaceGPLOnlyOrLaterWithRanges(parse(spdxExpression)))
var parsedLicenses = arrayOfLicenses.map(function (l) { return replaceGPLOnlyOrLaterWithRanges(parse(l)) })
for (const parsed of parsedLicenses) {
if (parsed.hasOwnProperty('conjunction')) {
throw new Error('Approved licenses cannot be AND or OR expressions.')
}
}
return parsedExpression.some(function (o) { return isANDCompatible(o, parsedLicenses) })
}
module.exports = satisfies
/***/ }),
/***/ 770:
@@ -50351,6 +50495,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isValid = exports.satisfiesAll = exports.satisfiesAny = exports.satisfies = void 0;
const spdxlib = __importStar(__nccwpck_require__(1452));
const spdx_satisfies_1 = __importDefault(__nccwpck_require__(5131));
const spdx_expression_parse_1 = __importDefault(__nccwpck_require__(3326));
/*
* NOTE: spdx-license-satisfies methods depend on spdx-expression-parse
@@ -50360,9 +50505,9 @@ const spdx_expression_parse_1 = __importDefault(__nccwpck_require__(3326));
*/
// accepts a pair of well-formed SPDX expressions. the
// candidate is tested against the constraint
function satisfies(candidateExpr, constraintExpr) {
function satisfies(candidateExpr, allowList) {
try {
return spdxlib.satisfies(candidateExpr, constraintExpr);
return (0, spdx_satisfies_1.default)(candidateExpr, allowList);
}
catch (_) {
return false;

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

25
dist/licenses.txt generated vendored
View File

@@ -1646,6 +1646,31 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
spdx-satisfies
MIT
The MIT License
Copyright (c) spdx-satisfies.js contributors
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
tunnel
MIT
The MIT License (MIT)

15
package-lock.json generated
View File

@@ -20,7 +20,7 @@
"jest": "^29.7.0",
"octokit": "^3.1.2",
"spdx-expression-parse": "^3.0.1",
"spdx-satisfies": "^5.0.1",
"spdx-satisfies": "^6.0.0",
"ts-jest": "^29.2.5",
"yaml": "^2.3.4",
"zod": "^3.24.1"
@@ -29,7 +29,6 @@
"@types/jest": "^29.5.12",
"@types/node": "^20",
"@types/spdx-expression-parse": "^3.0.4",
"@types/spdx-satisfies": "^0.1.1",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"@vercel/ncc": "^0.38.3",
@@ -2180,12 +2179,6 @@
"integrity": "sha512-XrojSCTzVxPAfWeAiw8Hg27OW/4jalE7yiohCHRPprqfPyt2oG+Osy1HstUPMF26cEdno3IeEhv31Pzl0wwsQw==",
"dev": true
},
"node_modules/@types/spdx-satisfies": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/@types/spdx-satisfies/-/spdx-satisfies-0.1.2.tgz",
"integrity": "sha512-v8OtFJhx4gHvOktcvP1cdeAXYhUq1O5XP+NTxyZoxDSaKYGf3BFWb0P4Ik/JfRxsscKn8fDe4w9Obv92bNQ26Q==",
"dev": true
},
"node_modules/@types/stack-utils": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-2.0.3.tgz",
@@ -7372,9 +7365,9 @@
"integrity": "sha512-mcdpQFV7UDAgLpXEE/jOMqvK4LBoO0uTQg0uvXUewmEFhpiZx5yJSZITHB8w1ZahKdhfZqP5GPEOKLyEq5p8XA=="
},
"node_modules/spdx-satisfies": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/spdx-satisfies/-/spdx-satisfies-5.0.1.tgz",
"integrity": "sha512-Nwor6W6gzFp8XX4neaKQ7ChV4wmpSh2sSDemMFSzHxpTw460jxFYeOn+jq4ybnSSw/5sc3pjka9MQPouksQNpw==",
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/spdx-satisfies/-/spdx-satisfies-6.0.0.tgz",
"integrity": "sha512-oOWQocnRbFVtBnBITfFgzjhnOklHossTvI+6C1hB2slvp3HgTsfru5wuo8HY2rQpwSm5JuIhNzIuqOfR5IuojQ==",
"dependencies": {
"spdx-compare": "^1.0.0",
"spdx-expression-parse": "^3.0.0",

View File

@@ -36,7 +36,7 @@
"jest": "^29.7.0",
"octokit": "^3.1.2",
"spdx-expression-parse": "^3.0.1",
"spdx-satisfies": "^5.0.1",
"spdx-satisfies": "^6.0.0",
"ts-jest": "^29.2.5",
"yaml": "^2.3.4",
"zod": "^3.24.1"
@@ -45,7 +45,6 @@
"@types/jest": "^29.5.12",
"@types/node": "^20",
"@types/spdx-expression-parse": "^3.0.4",
"@types/spdx-satisfies": "^0.1.1",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"@vercel/ncc": "^0.38.3",
@@ -63,4 +62,4 @@
"cross-spawn": ">=7.0.5",
"@octokit/request-error@5.0.1": "5.1.1"
}
}
}

View File

@@ -88,20 +88,14 @@ export async function getInvalidLicenseChanges(
try {
if (allow !== undefined) {
if (spdx.isValid(license)) {
let found = false
for (const allowedLicense of allow) {
found ||= spdx.satisfies(allowedLicense, license)
}
const found = spdx.satisfies(license, allow)
validityCache.set(license, found)
} else {
invalidLicenseChanges.unresolved.push(change)
}
} else if (deny !== undefined) {
if (spdx.isValid(license)) {
let found = false
for (const deniedLicense of deny) {
found ||= spdx.satisfies(deniedLicense, license)
}
const found = spdx.satisfiesAny(license, deny)
validityCache.set(license, !found)
} else {
invalidLicenseChanges.unresolved.push(change)

4
src/spdx-satisfies.d.ts vendored Normal file
View File

@@ -0,0 +1,4 @@
declare module 'spdx-satisfies' {
function spdxSatisfies(candidate: string, allowList: string[]): boolean
export = spdxSatisfies
}

View File

@@ -1,4 +1,5 @@
import * as spdxlib from '@onebeyond/spdx-license-satisfies'
import spdxSatisfies from 'spdx-satisfies'
import parse from 'spdx-expression-parse'
/*
@@ -10,12 +11,9 @@ import parse from 'spdx-expression-parse'
// accepts a pair of well-formed SPDX expressions. the
// candidate is tested against the constraint
export function satisfies(
candidateExpr: string,
constraintExpr: string
): boolean {
export function satisfies(candidateExpr: string, allowList: string[]): boolean {
try {
return spdxlib.satisfies(candidateExpr, constraintExpr)
return spdxSatisfies(candidateExpr, allowList)
} catch (_) {
return false
}