Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ The following tests are not yet implemented and therefore missing:
- Recommended Test 6.2.43
- Recommended Test 6.2.44
- Recommended Test 6.2.45
- Recommended Test 6.2.46

**Informative Tests**

Expand Down Expand Up @@ -461,6 +460,7 @@ export const recommendedTest_6_2_17: DocumentTest
export const recommendedTest_6_2_18: DocumentTest
export const recommendedTest_6_2_22: DocumentTest
export const recommendedTest_6_2_23: DocumentTest
export const recommendedTest_6_2_46: DocumentTest
```

[(back to top)](#bsi-csaf-validator-lib)
Expand Down
1 change: 1 addition & 0 deletions csaf_2_1/recommendedTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ export { recommendedTest_6_2_27 } from './recommendedTests/recommendedTest_6_2_2
export { recommendedTest_6_2_28 } from './recommendedTests/recommendedTest_6_2_28.js'
export { recommendedTest_6_2_29 } from './recommendedTests/recommendedTest_6_2_29.js'
export { recommendedTest_6_2_38 } from './recommendedTests/recommendedTest_6_2_38.js'
export { recommendedTest_6_2_46 } from './recommendedTests/recommendedTest_6_2_46.js'
246 changes: 246 additions & 0 deletions csaf_2_1/recommendedTests/recommendedTest_6_2_46.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
import Ajv from 'ajv/dist/jtd.js'
import { parse, validate } from 'license-expressions'
import license_information from '../../lib/license/license_information.js'
import translations from '../../lib/language_specific_translation/translations.js'
import bcp47 from 'bcp47'

const ajv = new Ajv()

/*
This is the jtd schema that needs to match the input document so that the
test is activated. If this schema doesn't match it normally means that the input
document does not validate against the csaf json schema or optional fields that
the test checks are not present.
*/
const inputSchema = /** @type {const} */ ({
additionalProperties: true,
properties: {
document: {
additionalProperties: true,
properties: {
license_expression: {
type: 'string',
},
},
optionalProperties: {
lang: {
type: 'string',
},
notes: {
elements: {
additionalProperties: true,
optionalProperties: {
category: {
type: 'string',
},
title: {
type: 'string',
},
},
},
},
},
},
},
})

const validateSchema = ajv.compile(inputSchema)

const ABOUT_CODE_LICENSE_REF_PREFIX = 'LicenseRef-scancode-'

const ABOUT_CODE_LICENSE_KEYS = new Set(
license_information.licenses
.filter((license) => license.source === 'aboutCode')
.map((license) => license.license_key)
)

const SPDX_LICENSE_KEYS = new Set(
license_information.licenses
.filter((license) => license.source === 'spdx')
.map((license) => license.license_key)
)

/**
* Check whether license identifiers are not listed Aboutcode's "ScanCode LicenseDB"
* @param {string} licenseRefToCheck
* @return {boolean}
*/
function isAboutCodeLicense(licenseRefToCheck) {
if (!licenseRefToCheck.startsWith(ABOUT_CODE_LICENSE_REF_PREFIX)) {
return false
} else {
const licenseKey = licenseRefToCheck.substring(
ABOUT_CODE_LICENSE_REF_PREFIX.length
)
return ABOUT_CODE_LICENSE_KEYS.has(licenseKey)
}
}

/**
* Recursively checks if a parsed license expression contains not listed licenses.
*
* @param {import('license-expressions').ParsedSpdxExpression} parsedExpression - The parsed license expression
* @returns {Array<string>} all not listed licenses
*/
function notListedLicenses(parsedExpression) {
/** @type {Array<string>} */
const deprecatedLicenses = []
// If it's a LicenseRef type directly
if ('licenseRef' in parsedExpression) {
if (!isAboutCodeLicense(parsedExpression.licenseRef)) {
deprecatedLicenses.push(parsedExpression.licenseRef)
}
}

if (
'license' in parsedExpression &&
!SPDX_LICENSE_KEYS.has(parsedExpression.license)
) {
deprecatedLicenses.push(parsedExpression.license)
}

if (
'exception' in parsedExpression &&
parsedExpression.exception &&
!SPDX_LICENSE_KEYS.has(parsedExpression.exception)
) {
deprecatedLicenses.push(parsedExpression.exception)
}

// If it's a conjunction, check both sides
if ('conjunction' in parsedExpression) {
deprecatedLicenses.push(...notListedLicenses(parsedExpression.left))
deprecatedLicenses.push(...notListedLicenses(parsedExpression.right))
}

// If it's a LicenseInfo type, it doesn't contain not listed licenses
return deprecatedLicenses
}

/**
* Checks if a license expression string contains any not listed licenses.
*
* @param {string} licenseToCheck - The license expression to check
* @returns {Array<string>} all not listed licenses
*/
function allNotListedLicenses(licenseToCheck) {
const parseResult = parse(licenseToCheck)
return notListedLicenses(parseResult)
}

/**
* check if the license_expression contains license identifiers or exceptions
* that are not listed in the SPDX license list or Aboutcode's "ScanCode LicenseDB"
*
* @param {string} licenseToCheck - The license expression to check
* @returns {Array<string>} all not listed licenses
*/
export function getNotListedLicenses(licenseToCheck) {
if (!licenseToCheck || !validate(licenseToCheck).valid) {
return []
} else {
return allNotListedLicenses(licenseToCheck)
}
}

/**
* Checks if the document language is specified and not English
*
* @param {string | undefined} language - The language expression to check
* @returns {boolean} True if the language is valid, false otherwise
*/
export function isLangSpecifiedAndNotEnglish(language) {
return (
!!language && !(bcp47.parse(language)?.langtag.language.language === 'en')
)
}

/**
* test whether exactly one item in document notes exists that has the given title.
* The category of this item MUST be legal_disclaimer.
* @param {({} & { category?: string | undefined; title?: string | undefined; } & Record<string, unknown>)[]} notes
* @param {string} titleToFind
* @returns {boolean} True if the language is valid, false otherwise
*/
function containsOneLegalDisclaimerWithTitle(notes, titleToFind) {
return (
notes.filter(
(note) =>
note.category === 'legal_disclaimer' && note.title === titleToFind
).length === 1
)
}
Comment on lines +165 to +172
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the implementation in mandatory tests => wrong category should be separate error.


/**
* Get the language specific translation of the term License
* @param {{ document: { lang?: string; }; }} doc
* @return {string | undefined}
*/
export function getLicenseInDocumentLang(doc) {
if (!doc.document.lang) {
return undefined
}
const language = bcp47.parse(doc.document.lang)?.langtag.language.language

/** @type {Record<string, Record <string,string>>}*/
const translationByLang = translations.translation
if (!language || !translationByLang[language]) {
return undefined
} else {
return translationByLang[language]['license']
}
}

/**
* If the document language is specified but not English, and the license_expression contains license
* identifiers or exceptions that are not listed in the SPDX license list or Aboutcode's "ScanCode LicenseDB",
* it MUST be tested that exactly one item in document notes exists that has the language specific translation
* of the term License as title. The category of this item MUST be legal_disclaimer.
* If no language-specific translation has been recorded, the test MUST be skipped
* and output information to the user that no such translation is known.
*
* @param {unknown} doc
*/
export function recommendedTest_6_2_46(doc) {
/*
The `ctx` variable holds the state that is accumulated during the test run and is
finally returned by the function.
*/
const ctx = {
warnings:
/** @type {Array<{ instancePath: string; message: string }>} */ ([]),
}

if (!validateSchema(doc)) {
return ctx
}

const licenseInDocLang = getLicenseInDocumentLang(doc)
if (!licenseInDocLang) {
return ctx
}

const licenseToCheck = doc.document.license_expression
if (isLangSpecifiedAndNotEnglish(doc.document.lang)) {
const notListedLicenses = getNotListedLicenses(licenseToCheck)
if (notListedLicenses.length > 0) {
const notes = doc.document.notes
if (
!notes ||
!containsOneLegalDisclaimerWithTitle(notes, licenseInDocLang)
) {
ctx.warnings.push({
instancePath: '/document/notes',
message:
`The license_expression contains contains the following license identifiers that ` +
`are nor listed in Aboutcode or SPDX license list: ` +
`"${notListedLicenses.join(', ')}". ` +
`Therefore exactly one note with ` +
`title "License" and category "legal_disclaimer" must exist`,
})
}
}
}

return ctx
}
17 changes: 17 additions & 0 deletions lib/language_specific_translation/translations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* javascript version of JSON file: csaf_2.1/language_specific_translation/translations.json
*/
export default {
$schema:
'https://raw.githubusercontent.com/oasis-tcs/csaf/master/csaf_2.1/test/language_specific_translation/translations_json_schema.json',
translation_version: '2.1',
translation: {
de: {
license: 'Lizenz',
product_description: 'Produktbeschreibung',
reasoning_for_supersession: 'Begründung für die Ersetzung',
reasoning_for_withdrawal: 'Begründung für die Zurückziehung',
superseding_document: 'Ersetzendes Dokument',
},
},
}
Loading
Loading