-
Notifications
You must be signed in to change notification settings - Fork 9
feat(CSAF2.1): #364 add recommended test 6.2.44 - License #369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rainer-exxcellent
wants to merge
3
commits into
main
Choose a base branch
from
feat/363-csaf-2.1_recommended_test_6.2.44
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
e2cae8f
feat(CSAF2.1): #364 add recommended test 6.2.44
rainer-exxcellent e1aa2ba
feat(CSAF2.1): #287 add recommended test 6.2.44 - improve messages an…
rainer-exxcellent 29957cd
feat(CSAF2.1): #287 add recommended test 6.2.44 - format license_info…
rainer-exxcellent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| import Ajv from 'ajv/dist/jtd.js' | ||
| import { parse, validate } from 'license-expressions' | ||
| import license_information from '../../lib/license/license_information.js' | ||
|
|
||
| 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', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| const ABOUT_CODE_LICENSE_REF_PREFIX = 'LicenseRef-scancode-' | ||
|
|
||
| const DEPRECATED_ABOUT_CODE_LICENSE_KEYS = new Set( | ||
| license_information.licenses | ||
| .filter((license) => license.deprecated && license.source === 'aboutCode') | ||
| .map((license) => license.license_key) | ||
| ) | ||
|
|
||
| const DEPRECATED_SPDX_LICENSE_KEYS = new Set( | ||
| license_information.licenses | ||
| .filter((license) => license.deprecated && license.source === 'spdx') | ||
| .map((license) => license.license_key) | ||
| ) | ||
|
|
||
| const validateSchema = ajv.compile(inputSchema) | ||
|
|
||
| /** | ||
| * Check whether the license identifiers ref is a deprecated AboutCode's license | ||
| * Ignores other license inventorying entities | ||
| * @param {string} licenseRefToCheck | ||
| * @return {boolean} | ||
| */ | ||
| function isDeprecatedLicenseRef(licenseRefToCheck) { | ||
| if (!licenseRefToCheck.startsWith(ABOUT_CODE_LICENSE_REF_PREFIX)) { | ||
| return false | ||
| } else { | ||
| const licenseKey = licenseRefToCheck.substring( | ||
| ABOUT_CODE_LICENSE_REF_PREFIX.length | ||
| ) | ||
| return DEPRECATED_ABOUT_CODE_LICENSE_KEYS.has(licenseKey) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Recursively checks if a parsed license expression contains deprecated licenses | ||
| * | ||
| * @param {import('license-expressions').ParsedSpdxExpression} parsedExpression - The parsed license expression | ||
| * @returns {Array<string>} all deprecated licenses | ||
| */ | ||
| function allDeprecatedLicenses(parsedExpression) { | ||
| /** @type {Array<string>} */ | ||
| const deprecatedLicenses = [] | ||
| // If it's a LicenseRef type directly | ||
| if ( | ||
| 'licenseRef' in parsedExpression && | ||
| isDeprecatedLicenseRef(parsedExpression.licenseRef) | ||
| ) { | ||
| deprecatedLicenses.push(parsedExpression.licenseRef) | ||
| } | ||
|
|
||
| if ( | ||
| 'license' in parsedExpression && | ||
| DEPRECATED_SPDX_LICENSE_KEYS.has(parsedExpression.license) | ||
| ) { | ||
| deprecatedLicenses.push(parsedExpression.license) | ||
| } | ||
|
|
||
| if ( | ||
| 'exception' in parsedExpression && | ||
| parsedExpression.exception && | ||
| DEPRECATED_SPDX_LICENSE_KEYS.has(parsedExpression.exception) | ||
| ) { | ||
| deprecatedLicenses.push(parsedExpression.exception) | ||
| } | ||
|
|
||
| // If it's a conjunction, check both sides | ||
| if ('conjunction' in parsedExpression) { | ||
| deprecatedLicenses.push(...allDeprecatedLicenses(parsedExpression.left)) | ||
| deprecatedLicenses.push(...allDeprecatedLicenses(parsedExpression.right)) | ||
| } | ||
|
|
||
| // If it's a LicenseInfo type, it doesn't contain not listed licenses | ||
| return deprecatedLicenses | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a license expression string contains deprecated licenses | ||
| * | ||
| * @param {string} licenseToCheck - The license expression to check | ||
| * @returns {Array<string>} all deprecated licenses | ||
| */ | ||
| export function allDeprecatedInLicenseString(licenseToCheck) { | ||
| const parseResult = parse(licenseToCheck) | ||
| return allDeprecatedLicenses(parseResult) | ||
| } | ||
|
|
||
| /** | ||
| * It MUST be tested that all license identifier and exceptions used are not deprecated. | ||
| * This SHALL be tested for the SPDX license list and Aboutcode's "ScanCode LicenseDB". | ||
| * The test MAY be skipped for other license inventorying entities. | ||
| * | ||
| * @param {unknown} doc | ||
| */ | ||
| export function recommendedTest_6_2_44(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 licenseToCheck = doc.document.license_expression | ||
|
|
||
| if (validate(licenseToCheck).valid) { | ||
| const deprecatedLicenseIdentifiers = | ||
| allDeprecatedInLicenseString(licenseToCheck) | ||
|
|
||
| deprecatedLicenseIdentifiers.forEach((licenseKey) => { | ||
| ctx.warnings.push({ | ||
| instancePath: '/document/license_expression', | ||
| message: `License identifier ${licenseKey} is deprecated `, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm... License identifier is a fixed term. I know that you don't mean that here as also expressions can be deprecated. Therefore, I suggest to either switch to |
||
| }) | ||
| }) | ||
| } | ||
|
|
||
| return ctx | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are missing the
AdditionRefshere.