Skip to content

Commit

Permalink
Add licenses to SBOM
Browse files Browse the repository at this point in the history
  • Loading branch information
eoftedal committed Aug 19, 2024
1 parent ac5d30c commit cc17490
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
dust
dist/
.devcontainer
tmp/
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

### Enhancement

* Include licenses in SBOM output

## 1.5.0

### Enhancement

* Improve CycloneDX vulnerability IDs

## 1.4.1
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Erlend Oftedal <[email protected]>",
"name": "retire-site-scanner",
"version": "1.5.0",
"version": "1.6.0",
"license": "Apache-2.0",
"description": "A scanner for checking a web site using retire.js",
"main": "dist/index.js",
Expand All @@ -24,7 +24,7 @@
"dependencies": {
"cacheable-lookup": "^7.0.0",
"puppeteer": "^22.4.0",
"retire": "^5.1.4",
"retire": "^5.2.0",
"source-map": "^0.7.4"
},
"devDependencies": {
Expand Down
15 changes: 15 additions & 0 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ type CycloneDXComponent = {
version: string;
"bom-ref": string;
purl?: string;
licenses?: Array<{
license?: {
name: string;
};
expression?: string;
}>;
properties: Array<{
name: string;
value: string;
Expand Down Expand Up @@ -196,6 +202,7 @@ export function convertToCycloneDX(resultToConvert: typeof collectedResults) {
version: c.version,
purl: generatePURL(c),
properties: [],
licenses: mapLicenses(c.licenses),
};
components.set(key, comp);
if (!comp.properties.some((c) => c.value == res.url))
Expand Down Expand Up @@ -264,6 +271,14 @@ export function convertToCycloneDX(resultToConvert: typeof collectedResults) {
};
}

function mapLicenses(licenses: string[] | undefined) {
if (!licenses) return [];
if (licenses.length == 0) return [];
if (licenses[0] == "commercial") return [{ license: { name: "Commercial" } }];
return [{ expression: licenses[0] }];
}


export const jsonLogger: Logger = {
open: (url: string) => {
collectedResults.url = url;
Expand Down
28 changes: 24 additions & 4 deletions src/retireWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import retire from "retire/lib/retire";
import { deepScan } from "retire/lib/deepscan";
import { evaluateLicense } from "retire/lib/license";
import { type Repository, type Component } from "retire/lib/types";
import crypto from "crypto";
import log from "./log";
Expand Down Expand Up @@ -180,12 +181,31 @@ const scanner = () =>
loadRetireJSRepo().then(
(repo) =>
({
scanUri: (uri: string) => scanUri(repo, uri),
scanUri: (uri: string) =>
addLicenses(scanUri(repo, uri), repo.advisories),
scanContent: (url: string, contents: string) =>
scanContent(repo.advisories, contents, url),
runFuncs: (evaluate: Evaluator) => runFuncs(repo.advisories, evaluate),
scanUrlBackdoored: (url: string) => scanUrlBackdoored(repo, url),
addLicenses(
scanContent(repo.advisories, contents, url),
repo.advisories,
),
runFuncs: (evaluate: Evaluator) =>
runFuncs(repo.advisories, evaluate).then((r) => {
addLicenses(r, repo.advisories);
return r;
}),
scanUrlBackdoored: (url: string) =>
addLicenses(scanUrlBackdoored(repo, url), repo.advisories),
}) as Scanner,
);

function addLicenses(components: Component[], repo: Repository) {
components.forEach((c) => {
const possibleLicenses = repo[c.component]?.licenses;
if (possibleLicenses) {
c.licenses = evaluateLicense(possibleLicenses, c.version);
}
});
return components;
}

export default scanner;
3 changes: 3 additions & 0 deletions tests/schematest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ describe("cyclonedx-json", () => {
"jsf-0.82.schema.json#/definitions/signature",
);
const result = validator.validate(cycloneDx, jsonSchema);
if (!result.valid) {
console.log(result.errors);
}
expect(result.valid).to.eq(true);
});
});

0 comments on commit cc17490

Please sign in to comment.