-
-
Notifications
You must be signed in to change notification settings - Fork 168
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
Follow CycloneDX 1.5 spec for SPDX license expressions #975
Changes from 8 commits
553c94f
9f84449
58170a1
9c47c1d
002e541
c7907fa
9cbb828
c4ef4cb
a229a7c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ tmp/ | |
|
||
# BOM generated by CDXGEN during local development | ||
bom.json | ||
bomresults | ||
|
||
# Logs | ||
logs | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -274,6 +274,72 @@ function toBase64(hexString) { | |
return Buffer.from(hexString, "hex").toString("base64"); | ||
} | ||
|
||
/** | ||
* Return the current timestamp in YYYY-MM-DDTHH:MM:SSZ format. | ||
* | ||
* @returns {string} ISO formatted timestamp, without milliseconds. | ||
*/ | ||
export function getTimestamp() { | ||
return new Date().toISOString().split(".")[0] + "Z"; | ||
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. For the record, I think jsonschema date-time is wrong to leave out millseconds and move away from ISO8601 format. |
||
} | ||
|
||
/** | ||
* Method to determine if a license is a valid SPDX license expression | ||
* | ||
* @param {string} license License string | ||
* @returns {boolean} true if the license is a valid SPDX license expression | ||
* @see https://spdx.dev/learn/handling-license-info/ | ||
**/ | ||
export function isSpdxLicenseExpression(license) { | ||
const licenseLoweCase = (license || "").toLowerCase(); | ||
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. Can we rename this variable to 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. I will change it. |
||
if (!licenseLoweCase) { | ||
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. This condition will always be false since we substitute empty value above. 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. If the function receives an 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. Oh yeah. For some reason, I forgot this falsy thing and assumed only python behaves this way. Another learning for today! |
||
return false; | ||
} | ||
|
||
if (/[(\s]+/gi.test(licenseLoweCase)) { | ||
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.
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. Will change it. |
||
return true; | ||
} | ||
|
||
if (licenseLoweCase.endsWith("+")) { | ||
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. Interesting! 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. This is for the "GPL-2.0+" (or latter) scenario. If this would be part of a more complex case it would be covered by the check for |
||
return true; // GPL-2.0+ means GPL-2.0 or any later version, at the licensee’s option. | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Convert the array of licenses to a CycloneDX 1.5 compliant license array. | ||
* This should return an array containing: | ||
* - one or more SPDX license if no expression is present | ||
* - the first license expression if at least one is present | ||
* | ||
* @param {Array} licenses Array of licenses | ||
* @returns {Array} CycloneDX 1.5 compliant license array | ||
*/ | ||
export function adjustLicenseInformation(licenses) { | ||
if (!licenses || !Array.isArray(licenses)) { | ||
return []; | ||
} | ||
|
||
const expressions = licenses.filter((f) => { | ||
return f.expression; | ||
}); | ||
if (expressions.length >= 1) { | ||
if (expressions.length > 1) { | ||
console.warn("multiple license expressions found", expressions); | ||
} | ||
return [{ expression: expressions[0].expression }]; | ||
} else { | ||
return licenses.map((l) => { | ||
if (typeof l.license === "object") { | ||
return l; | ||
} else { | ||
return { license: l }; | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Performs a lookup + validation of the license specified in the | ||
* package. If the license is a valid SPDX license ID, set the 'id' | ||
|
@@ -286,8 +352,8 @@ export function getLicenses(pkg) { | |
if (!Array.isArray(license)) { | ||
license = [license]; | ||
} | ||
return license | ||
.map((l) => { | ||
return adjustLicenseInformation( | ||
license.map((l) => { | ||
let licenseContent = {}; | ||
if (typeof l === "string" || l instanceof String) { | ||
if ( | ||
|
@@ -309,6 +375,8 @@ export function getLicenses(pkg) { | |
licenseContent.name = "CUSTOM"; | ||
} | ||
licenseContent.url = l; | ||
} else if (isSpdxLicenseExpression(l)) { | ||
licenseContent.expression = l; | ||
} else { | ||
licenseContent.name = l; | ||
} | ||
|
@@ -322,7 +390,7 @@ export function getLicenses(pkg) { | |
} | ||
return licenseContent; | ||
}) | ||
.map((l) => ({ license: l })); | ||
); | ||
} else { | ||
const knownLicense = getKnownLicense(undefined, pkg); | ||
if (knownLicense) { | ||
|
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.
Thank you!