Skip to content

Commit 2cd22bd

Browse files
feat(CSAF2.1): #199 add informative test 6.3.17
1 parent 4a1299b commit 2cd22bd

File tree

9 files changed

+8790
-1
lines changed

9 files changed

+8790
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ export const informativeTest_6_3_8: DocumentTest
481481
export const informativeTest_6_3_9: DocumentTest
482482
export const informativeTest_6_3_10: DocumentTest
483483
export const informativeTest_6_3_11: DocumentTest
484+
export const informativeTest_6_3_17: DocumentTest
484485
```
485486
486487
[(back to top)](#bsi-csaf-validator-lib)
@@ -577,5 +578,8 @@ For the complete list of dependencies please take a look at [package.json](https
577578
- [undici](https://undici.nodejs.org)
578579
- [@js-joda/core](https://js-joda.github.io/js-joda/)
579580
- [@js-joda/timezone](https://js-joda.github.io/js-joda/)
581+
- [aboutcode licenses](https://scancode-licensedb.aboutcode.org/index.json)
582+
- [SPDX licenses](https://raw.githubusercontent.com/spdx/license-list-data/refs/heads/main/json/licenses.json)
583+
- [license-expressions](https://github.com/lkoskela/license-expressions)
580584

581585
[(back to top)](#bsi-csaf-validator-lib)

csaf_2_1/informativeTests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ export {
1111
} from '../informativeTests.js'
1212
export { informativeTest_6_3_1 } from './informativeTests/informativeTest_6_3_1.js'
1313
export { informativeTest_6_3_4 } from './informativeTests/informativeTest_6_3_4.js'
14+
export { informativeTest_6_3_17 } from './informativeTests/informativeTest_6_3_17.js'
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import Ajv from 'ajv/dist/jtd.js'
2+
3+
import license_information from '../../lib/license/license_information.js'
4+
import { validate } from 'license-expressions'
5+
6+
const ajv = new Ajv()
7+
8+
const CONSIDERED_LICENSE_KEYS = new Set(
9+
license_information.licenses
10+
.filter((license) => !license.deprecated)
11+
.map((license) => license.license_key)
12+
)
13+
14+
const inputSchema = /** @type {const} */ ({
15+
additionalProperties: true,
16+
properties: {
17+
document: {
18+
additionalProperties: true,
19+
properties: {
20+
license_expression: {
21+
type: 'string',
22+
},
23+
},
24+
},
25+
},
26+
})
27+
28+
const validateInput = ajv.compile(inputSchema)
29+
30+
/**
31+
* @param {string} licenseToCheck
32+
*/
33+
export function checkLicense(licenseToCheck) {
34+
// first do asimple check with aboutcode and spdx license ids
35+
// then check whether the license is a valid SPDX license expression
36+
return (
37+
CONSIDERED_LICENSE_KEYS.has(licenseToCheck) ||
38+
validate(licenseToCheck).valid
39+
)
40+
}
41+
42+
/**
43+
* It MUST be tested that the all license identifiers and exceptions are listed either
44+
* in the official SPDX license identifier list or AboutCode's "ScanCode LicenseDB".
45+
* @param {unknown} doc
46+
* @returns
47+
*/
48+
export function informativeTest_6_3_17(doc) {
49+
const ctx = {
50+
infos: /** @type {Array<{ message: string; instancePath: string }>} */ ([]),
51+
}
52+
53+
if (!validateInput(doc)) {
54+
return ctx
55+
}
56+
57+
const licenseToCheck = doc.document.license_expression
58+
59+
if (!checkLicense(licenseToCheck)) {
60+
ctx.infos.push({
61+
instancePath: '/document/license_expression',
62+
message: `Invalid license: '${licenseToCheck}'`,
63+
})
64+
}
65+
66+
return ctx
67+
}

0 commit comments

Comments
 (0)