@trustify-da/trustify-da-javascript-client 0.3.0-ea.02983f2 → 0.3.0-ea.0e9ba23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -1
- package/dist/package.json +12 -8
- package/dist/src/analysis.d.ts +5 -5
- package/dist/src/analysis.js +21 -76
- package/dist/src/cli.js +72 -6
- package/dist/src/cyclone_dx_sbom.d.ts +3 -2
- package/dist/src/cyclone_dx_sbom.js +16 -4
- package/dist/src/index.d.ts +65 -11
- package/dist/src/index.js +5 -3
- package/dist/src/license/compatibility.d.ts +18 -0
- package/dist/src/license/compatibility.js +45 -0
- package/dist/src/license/index.d.ts +28 -0
- package/dist/src/license/index.js +100 -0
- package/dist/src/license/licenses_api.d.ts +34 -0
- package/dist/src/license/licenses_api.js +91 -0
- package/dist/src/license/project_license.d.ts +25 -0
- package/dist/src/license/project_license.js +139 -0
- package/dist/src/oci_image/images.d.ts +4 -5
- package/dist/src/oci_image/utils.d.ts +4 -4
- package/dist/src/provider.d.ts +12 -3
- package/dist/src/provider.js +16 -1
- package/dist/src/providers/base_java.d.ts +3 -5
- package/dist/src/providers/base_javascript.d.ts +10 -4
- package/dist/src/providers/base_javascript.js +28 -4
- package/dist/src/providers/golang_gomodules.d.ts +11 -4
- package/dist/src/providers/golang_gomodules.js +12 -4
- package/dist/src/providers/java_gradle.d.ts +9 -3
- package/dist/src/providers/java_gradle.js +11 -2
- package/dist/src/providers/java_gradle_groovy.d.ts +1 -1
- package/dist/src/providers/java_gradle_kotlin.d.ts +1 -1
- package/dist/src/providers/java_maven.d.ts +12 -5
- package/dist/src/providers/java_maven.js +32 -5
- package/dist/src/providers/python_controller.d.ts +5 -2
- package/dist/src/providers/python_controller.js +56 -58
- package/dist/src/providers/python_pip.d.ts +11 -4
- package/dist/src/providers/python_pip.js +45 -53
- package/dist/src/providers/requirements_parser.d.ts +6 -0
- package/dist/src/providers/requirements_parser.js +27 -0
- package/dist/src/sbom.d.ts +3 -1
- package/dist/src/sbom.js +3 -2
- package/dist/src/tools.d.ts +22 -6
- package/dist/src/tools.js +56 -1
- package/package.json +13 -9
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* License compatibility: whether a dependency license is compatible with the project license.
|
|
3
|
+
* Relies on backend-provided license categories.
|
|
4
|
+
*
|
|
5
|
+
* Compatibility is based on restrictiveness hierarchy:
|
|
6
|
+
* PERMISSIVE < WEAK_COPYLEFT < STRONG_COPYLEFT
|
|
7
|
+
*
|
|
8
|
+
* A dependency is compatible if it's equal or less restrictive than the project license.
|
|
9
|
+
* A dependency is incompatible if it's more restrictive than the project license.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Check if a dependency's license is compatible with the project license based on backend categories.
|
|
13
|
+
*
|
|
14
|
+
* @param {string} [projectCategory] - backend category for project license: PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN
|
|
15
|
+
* @param {string} [dependencyCategory] - backend category for dependency license: PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN
|
|
16
|
+
* @returns {'compatible'|'incompatible'|'unknown'}
|
|
17
|
+
*/
|
|
18
|
+
export function getCompatibility(projectCategory, dependencyCategory) {
|
|
19
|
+
if (!projectCategory || !dependencyCategory) {
|
|
20
|
+
return 'unknown';
|
|
21
|
+
}
|
|
22
|
+
const proj = projectCategory.toUpperCase();
|
|
23
|
+
const dep = dependencyCategory.toUpperCase();
|
|
24
|
+
// Unknown categories
|
|
25
|
+
if (proj === 'UNKNOWN' || dep === 'UNKNOWN') {
|
|
26
|
+
return 'unknown';
|
|
27
|
+
}
|
|
28
|
+
// Define restrictiveness levels (higher number = more restrictive)
|
|
29
|
+
const restrictiveness = {
|
|
30
|
+
'PERMISSIVE': 1,
|
|
31
|
+
'WEAK_COPYLEFT': 2,
|
|
32
|
+
'STRONG_COPYLEFT': 3
|
|
33
|
+
};
|
|
34
|
+
const projLevel = restrictiveness[proj];
|
|
35
|
+
const depLevel = restrictiveness[dep];
|
|
36
|
+
if (projLevel === undefined || depLevel === undefined) {
|
|
37
|
+
return 'unknown';
|
|
38
|
+
}
|
|
39
|
+
// Dependency is more restrictive than project → incompatible
|
|
40
|
+
if (depLevel > projLevel) {
|
|
41
|
+
return 'incompatible';
|
|
42
|
+
}
|
|
43
|
+
// Dependency is equal or less restrictive → compatible
|
|
44
|
+
return 'compatible';
|
|
45
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run full license check: resolve project license (with backend identification and details),
|
|
3
|
+
* get dependency licenses from analysis report, and compute incompatibilities.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} sbomContent - CycloneDX SBOM JSON string (the one sent for component analysis)
|
|
6
|
+
* @param {string} manifestPath - path to manifest
|
|
7
|
+
* @param {string} url - the backend url to send the request to
|
|
8
|
+
* @param {import('../index.js').Options} [opts={}]
|
|
9
|
+
* @param {import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport} [analysisResult] - analysis result that includes licenses array from backend
|
|
10
|
+
* @returns {Promise<{ projectLicense: { manifest: Object|null, file: Object|null, mismatch: boolean }, incompatibleDependencies: Array<{ purl: string, licenses: string[], category?: string, reason: string }>, error?: string }>}
|
|
11
|
+
*/
|
|
12
|
+
export function runLicenseCheck(sbomContent: string, manifestPath: string, url: string, opts?: import("../index.js").Options, analysisResult?: import("@trustify-da/trustify-da-api-model/model/v5/AnalysisReport").AnalysisReport): Promise<{
|
|
13
|
+
projectLicense: {
|
|
14
|
+
manifest: any | null;
|
|
15
|
+
file: any | null;
|
|
16
|
+
mismatch: boolean;
|
|
17
|
+
};
|
|
18
|
+
incompatibleDependencies: Array<{
|
|
19
|
+
purl: string;
|
|
20
|
+
licenses: string[];
|
|
21
|
+
category?: string;
|
|
22
|
+
reason: string;
|
|
23
|
+
}>;
|
|
24
|
+
error?: string;
|
|
25
|
+
}>;
|
|
26
|
+
export { getCompatibility } from "./compatibility.js";
|
|
27
|
+
export { getProjectLicense, findLicenseFilePath, identifyLicense as identifyLicenseViaBackend } from "./project_license.js";
|
|
28
|
+
export { licensesFromReport, normalizeLicensesResponse, getLicenseDetails } from "./licenses_api.js";
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* License resolution and dependency license compatibility for component analysis.
|
|
3
|
+
*/
|
|
4
|
+
import { getProjectLicense, findLicenseFilePath, identifyLicense } from './project_license.js';
|
|
5
|
+
import { licensesFromReport, getLicenseDetails } from './licenses_api.js';
|
|
6
|
+
import { getCompatibility } from './compatibility.js';
|
|
7
|
+
export { getProjectLicense, findLicenseFilePath, identifyLicense as identifyLicenseViaBackend } from './project_license.js';
|
|
8
|
+
export { licensesFromReport, normalizeLicensesResponse, getLicenseDetails } from './licenses_api.js';
|
|
9
|
+
export { getCompatibility } from './compatibility.js';
|
|
10
|
+
/**
|
|
11
|
+
* Run full license check: resolve project license (with backend identification and details),
|
|
12
|
+
* get dependency licenses from analysis report, and compute incompatibilities.
|
|
13
|
+
*
|
|
14
|
+
* @param {string} sbomContent - CycloneDX SBOM JSON string (the one sent for component analysis)
|
|
15
|
+
* @param {string} manifestPath - path to manifest
|
|
16
|
+
* @param {string} url - the backend url to send the request to
|
|
17
|
+
* @param {import('../index.js').Options} [opts={}]
|
|
18
|
+
* @param {import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport} [analysisResult] - analysis result that includes licenses array from backend
|
|
19
|
+
* @returns {Promise<{ projectLicense: { manifest: Object|null, file: Object|null, mismatch: boolean }, incompatibleDependencies: Array<{ purl: string, licenses: string[], category?: string, reason: string }>, error?: string }>}
|
|
20
|
+
*/
|
|
21
|
+
export async function runLicenseCheck(sbomContent, manifestPath, url, opts = {}, analysisResult = null) {
|
|
22
|
+
// Resolve project license from manifest and LICENSE file
|
|
23
|
+
const projectLicense = getProjectLicense(manifestPath, opts);
|
|
24
|
+
// Try backend identification for LICENSE file (more accurate than local pattern matching)
|
|
25
|
+
const licenseFilePath = findLicenseFilePath(manifestPath);
|
|
26
|
+
let backendFileId = null;
|
|
27
|
+
if (licenseFilePath) {
|
|
28
|
+
try {
|
|
29
|
+
backendFileId = await identifyLicense(licenseFilePath, { ...opts, TRUSTIFY_DA_BACKEND_URL: url });
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
// Fall back to local detection
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
// Determine final license identifiers
|
|
36
|
+
const manifestSpdx = projectLicense.fromManifest;
|
|
37
|
+
const fileSpdx = backendFileId || projectLicense.fromFile;
|
|
38
|
+
const mismatch = Boolean(manifestSpdx && fileSpdx && manifestSpdx.toLowerCase() !== fileSpdx.toLowerCase());
|
|
39
|
+
// Fetch detailed license info from backend (avoid duplicate calls if same license)
|
|
40
|
+
const licenseDetailsCache = new Map();
|
|
41
|
+
async function getDetails(spdxId) {
|
|
42
|
+
if (!spdxId || !url)
|
|
43
|
+
return null;
|
|
44
|
+
if (licenseDetailsCache.has(spdxId))
|
|
45
|
+
return licenseDetailsCache.get(spdxId);
|
|
46
|
+
try {
|
|
47
|
+
const details = await getLicenseDetails(spdxId, { ...opts, TRUSTIFY_DA_BACKEND_URL: url });
|
|
48
|
+
licenseDetailsCache.set(spdxId, details);
|
|
49
|
+
return details;
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const manifestLicenseInfo = await getDetails(manifestSpdx);
|
|
56
|
+
const fileLicenseInfo = await getDetails(fileSpdx);
|
|
57
|
+
// Extract dependency purls from SBOM (exclude root component)
|
|
58
|
+
const sbomObj = typeof sbomContent === 'string' ? JSON.parse(sbomContent) : sbomContent;
|
|
59
|
+
const rootRef = sbomObj?.metadata?.component?.["bom-ref"] || sbomObj?.metadata?.component?.purl;
|
|
60
|
+
const purls = (sbomObj?.components || [])
|
|
61
|
+
.map(c => c.purl || c["bom-ref"])
|
|
62
|
+
.filter(Boolean)
|
|
63
|
+
.filter(purl => !rootRef || purl !== rootRef);
|
|
64
|
+
if (purls.length === 0) {
|
|
65
|
+
return {
|
|
66
|
+
projectLicense: { manifest: manifestLicenseInfo, file: fileLicenseInfo, mismatch },
|
|
67
|
+
incompatibleDependencies: []
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
// Get dependency licenses from analysis report
|
|
71
|
+
const licenseByPurl = licensesFromReport(analysisResult, purls);
|
|
72
|
+
if (licenseByPurl.size === 0 && analysisResult) {
|
|
73
|
+
return {
|
|
74
|
+
projectLicense: { manifest: manifestLicenseInfo, file: fileLicenseInfo, mismatch },
|
|
75
|
+
incompatibleDependencies: [],
|
|
76
|
+
error: 'No license data available in analysis report'
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
// Check compatibility for each dependency
|
|
80
|
+
const projectCategory = manifestLicenseInfo?.category || fileLicenseInfo?.category;
|
|
81
|
+
const incompatibleDependencies = [];
|
|
82
|
+
for (const purl of purls) {
|
|
83
|
+
const entry = licenseByPurl.get(purl);
|
|
84
|
+
if (!entry)
|
|
85
|
+
continue;
|
|
86
|
+
const status = getCompatibility(projectCategory, entry.category);
|
|
87
|
+
if (status === 'incompatible') {
|
|
88
|
+
incompatibleDependencies.push({
|
|
89
|
+
purl,
|
|
90
|
+
licenses: entry.licenses,
|
|
91
|
+
category: entry.category,
|
|
92
|
+
reason: 'Dependency license(s) are incompatible with the project license.'
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
projectLicense: { manifest: manifestLicenseInfo, file: fileLicenseInfo, mismatch },
|
|
98
|
+
incompatibleDependencies
|
|
99
|
+
};
|
|
100
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fetch license details by SPDX identifier from the backend GET /api/v5/licenses/{spdx}.
|
|
3
|
+
* Returns detailed information about a specific license including category, name, and text.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} spdxId - SPDX identifier (e.g., "Apache-2.0", "MIT")
|
|
6
|
+
* @param {import('../index.js').Options} [opts={}] - options (proxy, token, TRUSTIFY_DA_BACKEND_URL, etc.)
|
|
7
|
+
* @returns {Promise<Object|null>} License details or null if not found
|
|
8
|
+
*/
|
|
9
|
+
export function getLicenseDetails(spdxId: string, opts?: import("../index.js").Options): Promise<any | null>;
|
|
10
|
+
/**
|
|
11
|
+
* Normalize the LicensesResponse shape (array of LicenseProviderResult) into a map of purl -> license info.
|
|
12
|
+
* Each provider result has { status, summary, packages } where packages is { [purl]: { concluded, evidence } }.
|
|
13
|
+
* We merge the first successful provider's packages; concluded has identifiers[], category (PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN).
|
|
14
|
+
*
|
|
15
|
+
* @param {unknown} data - LicensesResponse (array) or analysis report's licenses field
|
|
16
|
+
* @param {string[]} [purls] - optional list of purls to restrict to (for consistency with getLicensesByPurl)
|
|
17
|
+
* @returns {Map<string, { licenses: string[], category?: string }>}
|
|
18
|
+
*/
|
|
19
|
+
export function normalizeLicensesResponse(data: unknown, purls?: string[]): Map<string, {
|
|
20
|
+
licenses: string[];
|
|
21
|
+
category?: string;
|
|
22
|
+
}>;
|
|
23
|
+
/**
|
|
24
|
+
* Build license map from an analysis report that already includes license data (result.licenses).
|
|
25
|
+
* Use this when the dependency analysis response already contains the licenses array to avoid a second request.
|
|
26
|
+
*
|
|
27
|
+
* @param {import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport} analysisReport - full analysis JSON
|
|
28
|
+
* @param {string[]} [purls] - optional list of purls to restrict to
|
|
29
|
+
* @returns {Map<string, { licenses: string[], category?: string }>}
|
|
30
|
+
*/
|
|
31
|
+
export function licensesFromReport(analysisReport: import("@trustify-da/trustify-da-api-model/model/v5/AnalysisReport").AnalysisReport, purls?: string[]): Map<string, {
|
|
32
|
+
licenses: string[];
|
|
33
|
+
category?: string;
|
|
34
|
+
}>;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client for the Trustify DA backend License Analysis API (POST /api/v5/licenses).
|
|
3
|
+
* The same license data shape is returned in the dependency analysis JSON report (result.licenses).
|
|
4
|
+
* @see https://github.com/guacsec/trustify-dependency-analytics#license-analysis-apiv5licenses
|
|
5
|
+
* @see https://github.com/guacsec/trustify-da-api-spec/blob/main/api/v5/openapi.yaml
|
|
6
|
+
*/
|
|
7
|
+
import { selectTrustifyDABackend } from '../index.js';
|
|
8
|
+
import { addProxyAgent, getTokenHeaders } from '../tools.js';
|
|
9
|
+
/**
|
|
10
|
+
* Fetch license details by SPDX identifier from the backend GET /api/v5/licenses/{spdx}.
|
|
11
|
+
* Returns detailed information about a specific license including category, name, and text.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} spdxId - SPDX identifier (e.g., "Apache-2.0", "MIT")
|
|
14
|
+
* @param {import('../index.js').Options} [opts={}] - options (proxy, token, TRUSTIFY_DA_BACKEND_URL, etc.)
|
|
15
|
+
* @returns {Promise<Object|null>} License details or null if not found
|
|
16
|
+
*/
|
|
17
|
+
export async function getLicenseDetails(spdxId, opts = {}) {
|
|
18
|
+
if (!spdxId) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
const url = selectTrustifyDABackend(opts);
|
|
22
|
+
const finalUrl = new URL(`${url}/api/v5/licenses/${encodeURIComponent(spdxId)}`);
|
|
23
|
+
const fetchOptions = addProxyAgent({
|
|
24
|
+
method: 'GET',
|
|
25
|
+
headers: {
|
|
26
|
+
'Accept': 'application/json',
|
|
27
|
+
...getTokenHeaders(opts)
|
|
28
|
+
},
|
|
29
|
+
}, opts);
|
|
30
|
+
try {
|
|
31
|
+
const resp = await fetch(finalUrl, fetchOptions);
|
|
32
|
+
if (!resp.ok) {
|
|
33
|
+
const errorText = await resp.text().catch(() => '');
|
|
34
|
+
throw new Error(`HTTP ${resp.status}: ${errorText || resp.statusText}`);
|
|
35
|
+
}
|
|
36
|
+
return await resp.json();
|
|
37
|
+
}
|
|
38
|
+
catch (err) {
|
|
39
|
+
throw new Error(`Failed to fetch license details: ${err.message}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Normalize the LicensesResponse shape (array of LicenseProviderResult) into a map of purl -> license info.
|
|
44
|
+
* Each provider result has { status, summary, packages } where packages is { [purl]: { concluded, evidence } }.
|
|
45
|
+
* We merge the first successful provider's packages; concluded has identifiers[], category (PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN).
|
|
46
|
+
*
|
|
47
|
+
* @param {unknown} data - LicensesResponse (array) or analysis report's licenses field
|
|
48
|
+
* @param {string[]} [purls] - optional list of purls to restrict to (for consistency with getLicensesByPurl)
|
|
49
|
+
* @returns {Map<string, { licenses: string[], category?: string }>}
|
|
50
|
+
*/
|
|
51
|
+
export function normalizeLicensesResponse(data, purls = []) {
|
|
52
|
+
const map = new Map();
|
|
53
|
+
if (!data || !Array.isArray(data)) {
|
|
54
|
+
return map;
|
|
55
|
+
}
|
|
56
|
+
for (const providerResult of data) {
|
|
57
|
+
const packages = providerResult?.packages;
|
|
58
|
+
if (!packages || typeof packages !== 'object') {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
for (const [purl, pkgLicense] of Object.entries(packages)) {
|
|
62
|
+
const concluded = pkgLicense?.concluded;
|
|
63
|
+
const identifiers = Array.isArray(concluded?.identifiers) ? concluded.identifiers : [];
|
|
64
|
+
const expression = concluded?.expression;
|
|
65
|
+
const licenses = identifiers.length > 0 ? identifiers : (expression ? [expression] : []);
|
|
66
|
+
const category = concluded?.category; // PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN
|
|
67
|
+
if (purls.length === 0 || purls.includes(purl)) {
|
|
68
|
+
map.set(purl, { licenses: licenses.filter(Boolean), category });
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// Use first provider that has packages; backend may return multiple (e.g. deps.dev)
|
|
72
|
+
if (map.size > 0) {
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return map;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Build license map from an analysis report that already includes license data (result.licenses).
|
|
80
|
+
* Use this when the dependency analysis response already contains the licenses array to avoid a second request.
|
|
81
|
+
*
|
|
82
|
+
* @param {import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport} analysisReport - full analysis JSON
|
|
83
|
+
* @param {string[]} [purls] - optional list of purls to restrict to
|
|
84
|
+
* @returns {Map<string, { licenses: string[], category?: string }>}
|
|
85
|
+
*/
|
|
86
|
+
export function licensesFromReport(analysisReport, purls = []) {
|
|
87
|
+
if (!analysisReport?.licenses) {
|
|
88
|
+
return new Map();
|
|
89
|
+
}
|
|
90
|
+
return normalizeLicensesResponse(analysisReport.licenses, purls);
|
|
91
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolve project license from manifest and from LICENSE / LICENSE.md in manifest dir or git root.
|
|
3
|
+
* Uses local pattern matching for LICENSE file identification (synchronous).
|
|
4
|
+
* For more accurate backend-based identification, use identifyLicense() separately.
|
|
5
|
+
* @param {string} manifestPath - path to manifest
|
|
6
|
+
* @returns {{ fromManifest: string|null, fromFile: string|null, mismatch: boolean }}
|
|
7
|
+
*/
|
|
8
|
+
export function getProjectLicense(manifestPath: string): {
|
|
9
|
+
fromManifest: string | null;
|
|
10
|
+
fromFile: string | null;
|
|
11
|
+
mismatch: boolean;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Find LICENSE file path in the same directory as the manifest.
|
|
15
|
+
* @param {string} manifestPath
|
|
16
|
+
* @returns {string|null} - path to LICENSE file or null if not found
|
|
17
|
+
*/
|
|
18
|
+
export function findLicenseFilePath(manifestPath: string): string | null;
|
|
19
|
+
/**
|
|
20
|
+
* Call backend /licenses/identify endpoint to identify license from file.
|
|
21
|
+
* @param {string} licenseFilePath - path to LICENSE file
|
|
22
|
+
* @param {{}} [opts={}] - options (proxy, token, etc.)
|
|
23
|
+
* @returns {Promise<string|null>} - SPDX identifier or null
|
|
24
|
+
*/
|
|
25
|
+
export function identifyLicense(licenseFilePath: string, opts?: {}): Promise<string | null>;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves the project license from the manifest and from a LICENSE / LICENSE.md file.
|
|
3
|
+
* Used to report manifest-vs-file mismatch and as the baseline for dependency license compatibility.
|
|
4
|
+
*/
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { selectTrustifyDABackend } from '../index.js';
|
|
8
|
+
import { matchForLicense, availableProviders } from '../provider.js';
|
|
9
|
+
import { addProxyAgent, getTokenHeaders } from '../tools.js';
|
|
10
|
+
const LICENSE_FILES = ['LICENSE', 'LICENSE.md', 'LICENSE.txt'];
|
|
11
|
+
/**
|
|
12
|
+
* Resolve project license from manifest and from LICENSE / LICENSE.md in manifest dir or git root.
|
|
13
|
+
* Uses local pattern matching for LICENSE file identification (synchronous).
|
|
14
|
+
* For more accurate backend-based identification, use identifyLicense() separately.
|
|
15
|
+
* @param {string} manifestPath - path to manifest
|
|
16
|
+
* @returns {{ fromManifest: string|null, fromFile: string|null, mismatch: boolean }}
|
|
17
|
+
*/
|
|
18
|
+
export function getProjectLicense(manifestPath) {
|
|
19
|
+
const resolved = path.resolve(manifestPath);
|
|
20
|
+
const provider = matchForLicense(resolved, availableProviders);
|
|
21
|
+
const fromManifest = provider.readLicenseFromManifest(resolved);
|
|
22
|
+
const fromFile = readLicenseFromFile(resolved);
|
|
23
|
+
const mismatch = Boolean(fromManifest && fromFile && normalizeSpdx(fromManifest) !== normalizeSpdx(fromFile));
|
|
24
|
+
return {
|
|
25
|
+
fromManifest: fromManifest || null,
|
|
26
|
+
fromFile: fromFile || null,
|
|
27
|
+
mismatch
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Find LICENSE file path in the same directory as the manifest.
|
|
32
|
+
* @param {string} manifestPath
|
|
33
|
+
* @returns {string|null} - path to LICENSE file or null if not found
|
|
34
|
+
*/
|
|
35
|
+
export function findLicenseFilePath(manifestPath) {
|
|
36
|
+
const manifestDir = path.dirname(path.resolve(manifestPath));
|
|
37
|
+
for (const name of LICENSE_FILES) {
|
|
38
|
+
const filePath = path.join(manifestDir, name);
|
|
39
|
+
try {
|
|
40
|
+
if (fs.statSync(filePath).isFile()) {
|
|
41
|
+
return filePath;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
// skip
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Call backend /licenses/identify endpoint to identify license from file.
|
|
52
|
+
* @param {string} licenseFilePath - path to LICENSE file
|
|
53
|
+
* @param {{}} [opts={}] - options (proxy, token, etc.)
|
|
54
|
+
* @returns {Promise<string|null>} - SPDX identifier or null
|
|
55
|
+
*/
|
|
56
|
+
export async function identifyLicense(licenseFilePath, opts = {}) {
|
|
57
|
+
try {
|
|
58
|
+
const fileContent = fs.readFileSync(licenseFilePath);
|
|
59
|
+
const backendUrl = selectTrustifyDABackend(opts);
|
|
60
|
+
const url = new URL(`${backendUrl}/licenses/identify`);
|
|
61
|
+
const tokenHeaders = getTokenHeaders(opts);
|
|
62
|
+
const fetchOptions = addProxyAgent({
|
|
63
|
+
method: 'POST',
|
|
64
|
+
headers: {
|
|
65
|
+
'Content-Type': 'application/octet-stream',
|
|
66
|
+
...tokenHeaders,
|
|
67
|
+
},
|
|
68
|
+
body: fileContent,
|
|
69
|
+
}, opts);
|
|
70
|
+
const resp = await fetch(url, fetchOptions);
|
|
71
|
+
if (!resp.ok) {
|
|
72
|
+
return null; // Fallback to local detection on error
|
|
73
|
+
}
|
|
74
|
+
const data = await resp.json();
|
|
75
|
+
// Extract SPDX identifier from backend response
|
|
76
|
+
return data?.license?.id || data?.spdx_id || data?.identifier || null;
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return null; // Fallback to local detection on error
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Find and read LICENSE or LICENSE.md; use local pattern matching for identification.
|
|
84
|
+
* @param {string} manifestPath
|
|
85
|
+
* @returns {string|null}
|
|
86
|
+
*/
|
|
87
|
+
function readLicenseFromFile(manifestPath) {
|
|
88
|
+
const licenseFilePath = findLicenseFilePath(manifestPath);
|
|
89
|
+
if (!licenseFilePath) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
try {
|
|
93
|
+
const content = fs.readFileSync(licenseFilePath, 'utf-8');
|
|
94
|
+
return detectSpdxFromText(content) || content.split('\n')[0]?.trim() || null;
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Very simple SPDX detection from common license text (first ~500 chars).
|
|
102
|
+
* @param {string} text
|
|
103
|
+
* @returns {string|null}
|
|
104
|
+
*/
|
|
105
|
+
function detectSpdxFromText(text) {
|
|
106
|
+
const head = text.slice(0, 500);
|
|
107
|
+
if (/Apache License,?\s*Version 2\.0/i.test(head)) {
|
|
108
|
+
return 'Apache-2.0';
|
|
109
|
+
}
|
|
110
|
+
if (/MIT License/i.test(head) && /Permission is hereby granted/i.test(head)) {
|
|
111
|
+
return 'MIT';
|
|
112
|
+
}
|
|
113
|
+
if (/GNU GENERAL PUBLIC LICENSE\s+Version 2/i.test(head)) {
|
|
114
|
+
return 'GPL-2.0-only';
|
|
115
|
+
}
|
|
116
|
+
if (/GNU GENERAL PUBLIC LICENSE\s+Version 3/i.test(head)) {
|
|
117
|
+
return 'GPL-3.0-only';
|
|
118
|
+
}
|
|
119
|
+
if (/BSD 2-Clause/i.test(head)) {
|
|
120
|
+
return 'BSD-2-Clause';
|
|
121
|
+
}
|
|
122
|
+
if (/BSD 3-Clause/i.test(head)) {
|
|
123
|
+
return 'BSD-3-Clause';
|
|
124
|
+
}
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Normalize for comparison (lowercase, strip common suffixes).
|
|
129
|
+
* @param {string} spdxOrName
|
|
130
|
+
* @returns {string}
|
|
131
|
+
*/
|
|
132
|
+
function normalizeSpdx(spdxOrName) {
|
|
133
|
+
const s = String(spdxOrName).trim().toLowerCase();
|
|
134
|
+
// e.g. "MIT" vs "MIT License"
|
|
135
|
+
if (s.endsWith(' license')) {
|
|
136
|
+
return s.slice(0, -8);
|
|
137
|
+
}
|
|
138
|
+
return s;
|
|
139
|
+
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="packageurl-js/src/package-url.js" />
|
|
2
1
|
/**
|
|
3
2
|
* Helper class for parsing docker repository/image names:
|
|
4
3
|
*
|
|
@@ -26,7 +25,7 @@ export class Image {
|
|
|
26
25
|
* @param {string} fullName
|
|
27
26
|
* @param {string} [givenTag]
|
|
28
27
|
*/
|
|
29
|
-
constructor(fullName: string, givenTag?: string
|
|
28
|
+
constructor(fullName: string, givenTag?: string);
|
|
30
29
|
repository: string;
|
|
31
30
|
registry: string;
|
|
32
31
|
tag: string;
|
|
@@ -46,12 +45,12 @@ export class Image {
|
|
|
46
45
|
* @param {string} [optionalRegistry]
|
|
47
46
|
* @returns {string}
|
|
48
47
|
*/
|
|
49
|
-
getNameWithoutTag(optionalRegistry?: string
|
|
48
|
+
getNameWithoutTag(optionalRegistry?: string): string;
|
|
50
49
|
/**
|
|
51
50
|
* @param {string} [optionalRegistry]
|
|
52
51
|
* @returns {string}
|
|
53
52
|
*/
|
|
54
|
-
getFullName(optionalRegistry?: string
|
|
53
|
+
getFullName(optionalRegistry?: string): string;
|
|
55
54
|
/**
|
|
56
55
|
* @returns {string}
|
|
57
56
|
*/
|
|
@@ -79,7 +78,7 @@ export class ImageRef {
|
|
|
79
78
|
* @param {string} [platform]
|
|
80
79
|
* @param {import("index.js").Options} [opts={}]
|
|
81
80
|
*/
|
|
82
|
-
constructor(image: string, platform?: string
|
|
81
|
+
constructor(image: string, platform?: string, opts?: import("index.js").Options);
|
|
83
82
|
/** @type {Image} */
|
|
84
83
|
image: Image;
|
|
85
84
|
/** @type {Platform} */
|
|
@@ -4,20 +4,20 @@
|
|
|
4
4
|
* @param {import("../index.js").Options} [opts={}] - optional various options to pass along the application
|
|
5
5
|
* @returns {{}}
|
|
6
6
|
*/
|
|
7
|
-
export function generateImageSBOM(imageRef: import(
|
|
7
|
+
export function generateImageSBOM(imageRef: import("./images").ImageRef, opts?: import("../index.js").Options): {};
|
|
8
8
|
/**
|
|
9
9
|
*
|
|
10
10
|
* @param {string} image
|
|
11
11
|
* @param {import("../index.js").Options} [opts={}] - optional various options to pass along the application
|
|
12
12
|
* @returns {ImageRef}
|
|
13
13
|
*/
|
|
14
|
-
export function parseImageRef(image: string, opts?: import("../index.js").Options
|
|
14
|
+
export function parseImageRef(image: string, opts?: import("../index.js").Options): ImageRef;
|
|
15
15
|
/**
|
|
16
16
|
* Gets the platform information for an image
|
|
17
17
|
* @param {import("../index.js").Options} [opts={}] - optional various options to pass along the application
|
|
18
18
|
* @returns {Platform|null} - The platform information or null
|
|
19
19
|
*/
|
|
20
|
-
export function getImagePlatform(opts?: import("../index.js").Options
|
|
20
|
+
export function getImagePlatform(opts?: import("../index.js").Options): Platform | null;
|
|
21
21
|
/**
|
|
22
22
|
* Gets the digests for an image
|
|
23
23
|
* @param {import('./images').ImageRef} imageRef - The image reference
|
|
@@ -25,7 +25,7 @@ export function getImagePlatform(opts?: import("../index.js").Options | undefine
|
|
|
25
25
|
* @returns {Object.<string, string>} - The image digests
|
|
26
26
|
* @throws {Error} If the image info is invalid
|
|
27
27
|
*/
|
|
28
|
-
export function getImageDigests(imageRef: import(
|
|
28
|
+
export function getImageDigests(imageRef: import("./images").ImageRef, opts?: import("../index.js").Options): {
|
|
29
29
|
[x: string]: string;
|
|
30
30
|
};
|
|
31
31
|
export type SyftImageSource = {
|
package/dist/src/provider.d.ts
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Match a provider by manifest type only (no lock file check). Used for license reading.
|
|
3
|
+
* @param {string} manifestPath - path or name of the manifest
|
|
4
|
+
* @param {[Provider]} providers - list of providers to iterate over
|
|
5
|
+
* @returns {Provider}
|
|
6
|
+
* @throws {Error} when the manifest is not supported and no provider was matched
|
|
7
|
+
*/
|
|
8
|
+
export function matchForLicense(manifestPath: string, providers: [Provider]): Provider;
|
|
1
9
|
/**
|
|
2
10
|
* Match a provider from a list or providers based on file type.
|
|
3
11
|
* Each provider MUST export 'isSupported' taking a file name-type and returning true if supported.
|
|
@@ -10,7 +18,7 @@
|
|
|
10
18
|
*/
|
|
11
19
|
export function match(manifest: string, providers: [Provider]): Provider;
|
|
12
20
|
/** @typedef {{ecosystem: string, contentType: string, content: string}} Provided */
|
|
13
|
-
/** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string): void, provideComponent: function(string, {}): Provided
|
|
21
|
+
/** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string): void, provideComponent: function(string, {}): Provided | Promise<Provided>, provideStack: function(string, {}): Provided | Promise<Provided>, readLicenseFromManifest: function(string): string | null}} Provider */
|
|
14
22
|
/**
|
|
15
23
|
* MUST include all providers here.
|
|
16
24
|
* @type {[Provider]}
|
|
@@ -24,6 +32,7 @@ export type Provided = {
|
|
|
24
32
|
export type Provider = {
|
|
25
33
|
isSupported: (arg0: string) => boolean;
|
|
26
34
|
validateLockFile: (arg0: string) => void;
|
|
27
|
-
provideComponent: (arg0: string, arg1: {}) => Provided
|
|
28
|
-
provideStack: (arg0: string, arg1: {}) => Provided
|
|
35
|
+
provideComponent: (arg0: string, arg1: {}) => Provided | Promise<Provided>;
|
|
36
|
+
provideStack: (arg0: string, arg1: {}) => Provided | Promise<Provided>;
|
|
37
|
+
readLicenseFromManifest: (arg0: string) => string | null;
|
|
29
38
|
};
|
package/dist/src/provider.js
CHANGED
|
@@ -8,7 +8,7 @@ import Javascript_pnpm from './providers/javascript_pnpm.js';
|
|
|
8
8
|
import Javascript_yarn from './providers/javascript_yarn.js';
|
|
9
9
|
import pythonPipProvider from './providers/python_pip.js';
|
|
10
10
|
/** @typedef {{ecosystem: string, contentType: string, content: string}} Provided */
|
|
11
|
-
/** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string): void, provideComponent: function(string, {}): Provided
|
|
11
|
+
/** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string): void, provideComponent: function(string, {}): Provided | Promise<Provided>, provideStack: function(string, {}): Provided | Promise<Provided>, readLicenseFromManifest: function(string): string | null}} Provider */
|
|
12
12
|
/**
|
|
13
13
|
* MUST include all providers here.
|
|
14
14
|
* @type {[Provider]}
|
|
@@ -23,6 +23,21 @@ export const availableProviders = [
|
|
|
23
23
|
golangGomodulesProvider,
|
|
24
24
|
pythonPipProvider
|
|
25
25
|
];
|
|
26
|
+
/**
|
|
27
|
+
* Match a provider by manifest type only (no lock file check). Used for license reading.
|
|
28
|
+
* @param {string} manifestPath - path or name of the manifest
|
|
29
|
+
* @param {[Provider]} providers - list of providers to iterate over
|
|
30
|
+
* @returns {Provider}
|
|
31
|
+
* @throws {Error} when the manifest is not supported and no provider was matched
|
|
32
|
+
*/
|
|
33
|
+
export function matchForLicense(manifestPath, providers) {
|
|
34
|
+
const base = path.parse(manifestPath).base;
|
|
35
|
+
const provider = providers.find(prov => prov.isSupported(base));
|
|
36
|
+
if (!provider) {
|
|
37
|
+
throw new Error(`${base} is not supported`);
|
|
38
|
+
}
|
|
39
|
+
return provider;
|
|
40
|
+
}
|
|
26
41
|
/**
|
|
27
42
|
* Match a provider from a list or providers based on file type.
|
|
28
43
|
* Each provider MUST export 'isSupported' taking a file name-type and returning true if supported.
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
/// <reference types="packageurl-js/src/package-url.js" />
|
|
3
|
-
export type ecosystem_maven = import('../provider').Provider;
|
|
1
|
+
export type ecosystem_maven = import("../provider").Provider;
|
|
4
2
|
/** @typedef {import('../provider').Provider} */
|
|
5
3
|
/** @typedef {import('../provider').Provided} Provided */
|
|
6
4
|
/** @typedef {{name: string, version: string}} Package */
|
|
@@ -51,7 +49,7 @@ export default class Base_Java {
|
|
|
51
49
|
* @param {import('child_process').ExecFileOptionsWithStringEncoding} [opts={}]
|
|
52
50
|
* @protected
|
|
53
51
|
*/
|
|
54
|
-
protected _invokeCommand(bin: any, args: any, opts?: import("child_process").ExecFileOptionsWithStringEncoding
|
|
52
|
+
protected _invokeCommand(bin: any, args: any, opts?: import("child_process").ExecFileOptionsWithStringEncoding): string;
|
|
55
53
|
/**
|
|
56
54
|
*
|
|
57
55
|
* @param {string} manifestPath
|
|
@@ -70,7 +68,7 @@ export default class Base_Java {
|
|
|
70
68
|
normalizePath(thePath: any): string;
|
|
71
69
|
#private;
|
|
72
70
|
}
|
|
73
|
-
export type Provided = import(
|
|
71
|
+
export type Provided = import("../provider").Provided;
|
|
74
72
|
export type Package = {
|
|
75
73
|
name: string;
|
|
76
74
|
version: string;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
/** @typedef {import('../provider
|
|
1
|
+
export type purlType = import("../provider").Provider;
|
|
2
|
+
/** @typedef {import('../provider').Provider} */
|
|
3
|
+
/** @typedef {import('../provider').Provided} Provided */
|
|
3
4
|
/**
|
|
4
5
|
* The ecosystem identifier for JavaScript/npm packages
|
|
5
6
|
* @type {string}
|
|
@@ -85,6 +86,12 @@ export default class Base_javascript {
|
|
|
85
86
|
* @returns {Provided} The provided data for component analysis
|
|
86
87
|
*/
|
|
87
88
|
provideComponent(manifestPath: string, opts?: any): Provided;
|
|
89
|
+
/**
|
|
90
|
+
* Read license from manifest (package.json). Reused by npm, pnpm, yarn.
|
|
91
|
+
* @param {string} manifestPath - path to package.json
|
|
92
|
+
* @returns {string|null}
|
|
93
|
+
*/
|
|
94
|
+
readLicenseFromManifest(manifestPath: string): string | null;
|
|
88
95
|
/**
|
|
89
96
|
* Builds the dependency tree for the project
|
|
90
97
|
* @param {boolean} includeTransitive - Whether to include transitive dependencies
|
|
@@ -121,7 +128,6 @@ export default class Base_javascript {
|
|
|
121
128
|
protected _parseDepTreeOutput(output: string): string;
|
|
122
129
|
#private;
|
|
123
130
|
}
|
|
124
|
-
export type
|
|
125
|
-
export type Provided = import('../provider.js').Provided;
|
|
131
|
+
export type Provided = import("../provider").Provided;
|
|
126
132
|
import Manifest from './manifest.js';
|
|
127
133
|
import Sbom from '../sbom.js';
|