|
| 1 | +import Ajv from 'ajv/dist/jtd.js' |
| 2 | +import { validate, parse } from 'license-expressions' |
| 3 | + |
| 4 | +const ajv = new Ajv() |
| 5 | + |
| 6 | +/* |
| 7 | + This is the jtd schema that needs to match the input document so that the |
| 8 | + test is activated. If this schema doesn't match it normally means that the input |
| 9 | + document does not validate against the csaf json schema or optional fields that |
| 10 | + the test checks are not present. |
| 11 | + */ |
| 12 | +const inputSchema = /** @type {const} */ ({ |
| 13 | + additionalProperties: true, |
| 14 | + properties: { |
| 15 | + document: { |
| 16 | + additionalProperties: true, |
| 17 | + properties: { |
| 18 | + license_expression: { |
| 19 | + type: 'string', |
| 20 | + }, |
| 21 | + }, |
| 22 | + }, |
| 23 | + }, |
| 24 | +}) |
| 25 | + |
| 26 | +const validateSchema = ajv.compile(inputSchema) |
| 27 | + |
| 28 | +/** |
| 29 | + * Recursively checks if a parsed license expression contains any license references. |
| 30 | + * |
| 31 | + * @param {import('license-expressions').ParsedSpdxExpression} parsedExpression - The parsed license expression |
| 32 | + * @returns {boolean} True if the expression contains any license references, false otherwise |
| 33 | + */ |
| 34 | +function containsLicenseRef(parsedExpression) { |
| 35 | + // If it's a LicenseRef type directly |
| 36 | + if ('documentRef' in parsedExpression && parsedExpression.documentRef) { |
| 37 | + return true |
| 38 | + } |
| 39 | + |
| 40 | + // If it's a conjunction, check both sides |
| 41 | + if ('conjunction' in parsedExpression) { |
| 42 | + return ( |
| 43 | + containsLicenseRef(parsedExpression.left) || |
| 44 | + containsLicenseRef(parsedExpression.right) |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | + // If it's a LicenseInfo type, it doesn't contain a document reference |
| 49 | + return false |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Checks if a license expression contains any document references. |
| 54 | + * |
| 55 | + * @param {string} licenseToCheck - The license expression to check |
| 56 | + * @returns {boolean} True if the license expression contains any document references, false otherwise |
| 57 | + */ |
| 58 | +export function hasDocumentRef(licenseToCheck) { |
| 59 | + const parseResult = parse(licenseToCheck) |
| 60 | + return containsLicenseRef(parseResult) |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Checks if a license expression is valid, according to SPDX standards. |
| 65 | + * |
| 66 | + * @param {string} licenseToCheck - The license expression to check |
| 67 | + * @returns {boolean} True if the license is valid, false otherwise |
| 68 | + */ |
| 69 | +export function isValidLicenseExpression(licenseToCheck) { |
| 70 | + return ( |
| 71 | + !licenseToCheck || |
| 72 | + (validate(licenseToCheck).valid && !hasDocumentRef(licenseToCheck)) |
| 73 | + ) |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * It MUST be tested that the license expression is valid. |
| 78 | + * |
| 79 | + * @param {unknown} doc |
| 80 | + */ |
| 81 | +export function mandatoryTest_6_1_54(doc) { |
| 82 | + /* |
| 83 | + The `ctx` variable holds the state that is accumulated during the test ran and is |
| 84 | + finally returned by the function. |
| 85 | + */ |
| 86 | + const ctx = { |
| 87 | + errors: |
| 88 | + /** @type {Array<{ instancePath: string; message: string }>} */ ([]), |
| 89 | + isValid: true, |
| 90 | + } |
| 91 | + |
| 92 | + if (!validateSchema(doc)) { |
| 93 | + return ctx |
| 94 | + } |
| 95 | + |
| 96 | + const licenseToCheck = doc.document.license_expression |
| 97 | + if (!isValidLicenseExpression(licenseToCheck)) { |
| 98 | + ctx.isValid = false |
| 99 | + ctx.errors.push({ |
| 100 | + instancePath: '/document/license_expression', |
| 101 | + message: `Invalid license expression: '${licenseToCheck}'`, |
| 102 | + }) |
| 103 | + } |
| 104 | + |
| 105 | + return ctx |
| 106 | +} |
0 commit comments