@repometry/cli 0.2.0 → 0.4.0
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 +5 -2
- package/package.json +2 -2
- package/target/main/resources/language-catalog.json +54 -0
- package/target/main/resources/licenses/spdx.json +746 -0
- package/target/main/resources/report/json/config-v2.schema.json +2 -1
- package/target/main/resources/report/json/file-metrics-v1.schema.json +2 -0
- package/target/main/resources/report/json/report-v2.schema.json +6 -3
- package/target/main/resources/report/json/repository-summary-v1.schema.json +14 -0
- package/target/main/resources/viewer/index.html +4 -0
- package/target/main/resources/viewer/styles.css +7 -2
- package/target/main/ts/api/git/git.d.ts +2 -1
- package/target/main/ts/api/git/git.js +4 -4
- package/target/main/ts/api/git/inspection-source.js +2 -2
- package/target/main/ts/api/git/local.d.ts +1 -1
- package/target/main/ts/api/git/local.js +5 -4
- package/target/main/ts/api/import/finding-import.js +2 -9
- package/target/main/ts/api/import/license-import.d.ts +2 -0
- package/target/main/ts/api/import/license-import.js +36 -0
- package/target/main/ts/api/import/repository-path.d.ts +1 -0
- package/target/main/ts/api/import/repository-path.js +10 -0
- package/target/main/ts/cli/imports.d.ts +2 -0
- package/target/main/ts/cli/imports.js +6 -2
- package/target/main/ts/cli/run.js +10 -4
- package/target/main/ts/compose/analysis-report.js +5 -2
- package/target/main/ts/compose/analysis-results.d.ts +3 -2
- package/target/main/ts/compose/analysis-results.js +15 -6
- package/target/main/ts/compose/analysis-steps.js +3 -0
- package/target/main/ts/compose/analyze-snapshot.d.ts +1 -1
- package/target/main/ts/compose/analyze-snapshot.js +7 -4
- package/target/main/ts/compose/built-ins.d.ts +2 -2
- package/target/main/ts/compose/built-ins.js +2 -2
- package/target/main/ts/compose/inspect.d.ts +2 -0
- package/target/main/ts/compose/inspect.js +7 -4
- package/target/main/ts/compose/language-discovery.js +3 -1
- package/target/main/ts/compose/load-plugins.js +4 -2
- package/target/main/ts/compose/phases.d.ts +7 -3
- package/target/main/ts/compose/phases.js +26 -10
- package/target/main/ts/plugins/built-in.d.ts +2 -0
- package/target/main/ts/plugins/built-in.js +4 -1
- package/target/main/ts/plugins/duplication/duplication.d.ts +1 -2
- package/target/main/ts/plugins/duplication/duplication.js +18 -17
- package/target/main/ts/plugins/json-spans.d.ts +11 -0
- package/target/main/ts/plugins/json-spans.js +86 -0
- package/target/main/ts/plugins/language-js/babel-provider.d.ts +34 -2
- package/target/main/ts/plugins/language-js/babel-provider.js +39 -35
- package/target/main/ts/plugins/language-js/js-security.d.ts +17 -2
- package/target/main/ts/plugins/language-js/js-security.js +24 -23
- package/target/main/ts/plugins/language-js/structural-observations.d.ts +2 -1
- package/target/main/ts/plugins/language-js/structural-observations.js +2 -2
- package/target/main/ts/plugins/legal/composer-lock.d.ts +2 -0
- package/target/main/ts/plugins/legal/composer-lock.js +24 -0
- package/target/main/ts/plugins/legal/dependencies.d.ts +73 -0
- package/target/main/ts/plugins/legal/dependencies.js +27 -0
- package/target/main/ts/plugins/legal/licenses.d.ts +19 -0
- package/target/main/ts/plugins/legal/licenses.js +112 -0
- package/target/main/ts/plugins/legal/npm-lock.d.ts +3 -0
- package/target/main/ts/plugins/legal/npm-lock.js +30 -0
- package/target/main/ts/plugins/legal/policy.d.ts +72 -0
- package/target/main/ts/plugins/legal/policy.js +149 -0
- package/target/main/ts/plugins/legal/sbom.d.ts +3 -0
- package/target/main/ts/plugins/legal/sbom.js +131 -0
- package/target/main/ts/plugins/legal/unverified-lock.d.ts +1 -0
- package/target/main/ts/plugins/legal/unverified-lock.js +21 -0
- package/target/main/ts/plugins/maintainability/maintainability.d.ts +1 -2
- package/target/main/ts/plugins/maintainability/maintainability.js +34 -33
- package/target/main/ts/plugins/maintainability/structural-smells.d.ts +1 -2
- package/target/main/ts/plugins/maintainability/structural-smells.js +5 -5
- package/target/main/ts/plugins/security/actions-security.d.ts +17 -2
- package/target/main/ts/plugins/security/actions-security.js +21 -16
- package/target/main/ts/plugins/security/dockerfile.d.ts +17 -2
- package/target/main/ts/plugins/security/dockerfile.js +23 -18
- package/target/main/ts/plugins/security/npm-supply-chain.d.ts +17 -2
- package/target/main/ts/plugins/security/npm-supply-chain.js +32 -110
- package/target/main/ts/plugins/security/secrets.d.ts +18 -6
- package/target/main/ts/plugins/security/secrets.js +27 -23
- package/target/main/ts/plugins/source/forges.js +25 -32
- package/target/main/ts/report/html/app/viewer-issue-group.js +1 -1
- package/target/main/ts/report/html/app/viewer-metric-registry.js +1 -1
- package/target/main/ts/report/html/app/viewer-metrics.d.ts +2 -1
- package/target/main/ts/report/html/app/viewer-metrics.js +7 -2
- package/target/main/ts/report/html/app/viewer-overview-cards.d.ts +1 -0
- package/target/main/ts/report/html/app/viewer-overview-cards.js +20 -1
- package/target/main/ts/report/html/app/viewer-overview-lead.js +4 -1
- package/target/main/ts/report/html/app/viewer-shell.js +3 -1
- package/target/main/ts/report/html/app/viewer-tokenizer.js +37 -9
- package/target/main/ts/report/html/app/viewer-trend-data.js +1 -0
- package/target/main/ts/report/html/app/viewer.js +5 -2
- package/target/main/ts/report/json/validation/report.js +2 -2
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { isRecord } from '@repometry/core/internal/engine/plugin.js';
|
|
2
|
+
import { locateJsonSpans, pointer } from '../json-spans.js';
|
|
3
|
+
import { declaredLicense } from './policy.js';
|
|
4
|
+
export function readComposerLock(text) {
|
|
5
|
+
const lock = JSON.parse(text);
|
|
6
|
+
if (!isRecord(lock) || !Array.isArray(lock.packages)
|
|
7
|
+
|| (lock['packages-dev'] != null && !Array.isArray(lock['packages-dev']))) {
|
|
8
|
+
throw new Error('The Composer lockfile must contain packages and a packages-dev array when present');
|
|
9
|
+
}
|
|
10
|
+
const packages = Object.entries({ packages: lock.packages, 'packages-dev': lock['packages-dev'] ?? [] })
|
|
11
|
+
.flatMap(([section, entries]) => entries.flatMap((entry, index) => {
|
|
12
|
+
if (!isRecord(entry) || typeof entry.name !== 'string' || !entry.name.trim())
|
|
13
|
+
return [];
|
|
14
|
+
const license = declaredLicense(entry.license);
|
|
15
|
+
return [{ at: pointer([section, index]), name: entry.name, dev: section === 'packages-dev',
|
|
16
|
+
...(typeof entry.version === 'string' ? { version: entry.version } : {}),
|
|
17
|
+
...(license === undefined ? {} : { license }) }];
|
|
18
|
+
}));
|
|
19
|
+
if (packages.length === 0)
|
|
20
|
+
return [];
|
|
21
|
+
const spans = locateJsonSpans(text, new Set(packages.map(({ at }) => at)), 2);
|
|
22
|
+
return packages.map(({ at, ...entry }) => ({ ...entry, ...spans.get(at) ?? { line: 1, endLine: 1 } }));
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=composer-lock.js.map
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { LicenseImportFormat } from '@repometry/core/internal/model/license-evidence.js';
|
|
2
|
+
import type { DataContract } from '@repometry/core/internal/engine/plugin.js';
|
|
3
|
+
export declare const maxLicenseLength = 256;
|
|
4
|
+
export interface Dependency {
|
|
5
|
+
readonly name: string;
|
|
6
|
+
readonly version?: string;
|
|
7
|
+
readonly license?: string;
|
|
8
|
+
readonly overlongLicense?: true;
|
|
9
|
+
readonly dev: boolean;
|
|
10
|
+
readonly line: number;
|
|
11
|
+
readonly endLine: number;
|
|
12
|
+
}
|
|
13
|
+
export interface DependencySource {
|
|
14
|
+
readonly path: string;
|
|
15
|
+
readonly packages: readonly Dependency[];
|
|
16
|
+
readonly unverified?: true;
|
|
17
|
+
readonly format?: LicenseImportFormat;
|
|
18
|
+
}
|
|
19
|
+
export declare const dependencySources: DataContract<readonly DependencySource[]>;
|
|
20
|
+
export declare const dependencySourcesSchema: {
|
|
21
|
+
readonly type: 'array';
|
|
22
|
+
readonly items: {
|
|
23
|
+
readonly type: 'object';
|
|
24
|
+
readonly additionalProperties: false;
|
|
25
|
+
readonly required: readonly ['path', 'packages'];
|
|
26
|
+
readonly properties: {
|
|
27
|
+
readonly path: {
|
|
28
|
+
readonly type: 'string';
|
|
29
|
+
readonly minLength: 1;
|
|
30
|
+
};
|
|
31
|
+
readonly packages: {
|
|
32
|
+
readonly type: 'array';
|
|
33
|
+
readonly items: {
|
|
34
|
+
readonly type: 'object';
|
|
35
|
+
readonly additionalProperties: false;
|
|
36
|
+
readonly required: readonly ['name', 'dev', 'line', 'endLine'];
|
|
37
|
+
readonly properties: {
|
|
38
|
+
readonly name: {
|
|
39
|
+
readonly type: 'string';
|
|
40
|
+
readonly minLength: 1;
|
|
41
|
+
};
|
|
42
|
+
readonly version: {
|
|
43
|
+
readonly type: 'string';
|
|
44
|
+
};
|
|
45
|
+
readonly license: {
|
|
46
|
+
readonly type: 'string';
|
|
47
|
+
};
|
|
48
|
+
readonly overlongLicense: {
|
|
49
|
+
readonly const: true;
|
|
50
|
+
};
|
|
51
|
+
readonly dev: {
|
|
52
|
+
readonly type: 'boolean';
|
|
53
|
+
};
|
|
54
|
+
readonly line: {
|
|
55
|
+
readonly type: 'integer';
|
|
56
|
+
readonly minimum: 1;
|
|
57
|
+
};
|
|
58
|
+
readonly endLine: {
|
|
59
|
+
readonly type: 'integer';
|
|
60
|
+
readonly minimum: 1;
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
readonly unverified: {
|
|
66
|
+
readonly const: true;
|
|
67
|
+
};
|
|
68
|
+
readonly format: {
|
|
69
|
+
readonly enum: readonly ["cyclonedx", "spdx"];
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { licenseImportFormats } from '@repometry/core/internal/model/license-evidence.js';
|
|
2
|
+
import { isRecord } from '@repometry/core/internal/engine/plugin.js';
|
|
3
|
+
export const maxLicenseLength = 256;
|
|
4
|
+
const dependency = {
|
|
5
|
+
type: 'object', additionalProperties: false, required: ['name', 'dev', 'line', 'endLine'],
|
|
6
|
+
properties: {
|
|
7
|
+
name: { type: 'string', minLength: 1 }, version: { type: 'string' }, license: { type: 'string' }, overlongLicense: { const: true },
|
|
8
|
+
dev: { type: 'boolean' }, line: { type: 'integer', minimum: 1 }, endLine: { type: 'integer', minimum: 1 },
|
|
9
|
+
},
|
|
10
|
+
};
|
|
11
|
+
export const dependencySources = {
|
|
12
|
+
id: 'repometry.license-dependencies/v1',
|
|
13
|
+
accepts: (value) => Array.isArray(value) && value.every(source => isRecord(source)
|
|
14
|
+
&& (source.format === undefined || licenseImportFormats.some(format => format === source.format))
|
|
15
|
+
&& (source.unverified === undefined || source.unverified === true) && typeof source.path === 'string' && source.path.length > 0 && Array.isArray(source.packages)
|
|
16
|
+
&& source.packages.every(item => isRecord(item) && typeof item.name === 'string' && item.name.length > 0
|
|
17
|
+
&& (item.version === undefined || typeof item.version === 'string') && (item.license === undefined || typeof item.license === 'string')
|
|
18
|
+
&& (item.overlongLicense === undefined || item.overlongLicense === true)
|
|
19
|
+
&& typeof item.dev === 'boolean' && typeof item.line === 'number' && Number.isSafeInteger(item.line) && item.line >= 1
|
|
20
|
+
&& typeof item.endLine === 'number' && Number.isSafeInteger(item.endLine) && item.endLine >= item.line)),
|
|
21
|
+
};
|
|
22
|
+
export const dependencySourcesSchema = {
|
|
23
|
+
type: 'array',
|
|
24
|
+
items: { type: 'object', additionalProperties: false, required: ['path', 'packages'],
|
|
25
|
+
properties: { path: { type: 'string', minLength: 1 }, packages: { type: 'array', items: dependency }, unverified: { const: true }, format: { enum: licenseImportFormats } } },
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=dependencies.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { JsonValue } from '@repometry/core/internal/model/plugin-data.js';
|
|
2
|
+
export declare function licenses(options?: JsonValue | undefined): {
|
|
3
|
+
id: string;
|
|
4
|
+
version: string;
|
|
5
|
+
api: string;
|
|
6
|
+
tools?: Readonly<Record<string, string>>;
|
|
7
|
+
operations: readonly import("@repometry/core").Operation[];
|
|
8
|
+
rules?: readonly import("@repometry/core/internal/engine/plugin.js").RuleDescriptor[];
|
|
9
|
+
config?: import("@repometry/core/internal/engine/plugin.js").PluginConfigSchema;
|
|
10
|
+
settings?: JsonValue;
|
|
11
|
+
kinds: readonly import("@repometry/core/internal/engine/plugin.js").PluginKind[];
|
|
12
|
+
providesLanguages?: readonly import("@repometry/core/internal/model/language.js").ProvidedLanguage[];
|
|
13
|
+
languageProvenanceRequiresSource?: true;
|
|
14
|
+
produces: readonly import("@repometry/core/internal/engine/plugin.js").ProductDeclaration[];
|
|
15
|
+
claims: {
|
|
16
|
+
globs: string[];
|
|
17
|
+
scanOnly: boolean;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { checkedDependencies, licenseFiles, licenseLockKind, licenseLockNames } from '@repometry/core/internal/model/license-evidence.js';
|
|
2
|
+
import { bundledPlugin, pluginManifestSchema } from '@repometry/core/internal/engine/plugin-manifest.js';
|
|
3
|
+
import { findingResult } from '@repometry/core/internal/engine/author-contracts.js';
|
|
4
|
+
import { dependencySources, dependencySourcesSchema, maxLicenseLength } from './dependencies.js';
|
|
5
|
+
import { readComposerLock } from './composer-lock.js';
|
|
6
|
+
import { listsDependencies } from './unverified-lock.js';
|
|
7
|
+
import { readSbom } from './sbom.js';
|
|
8
|
+
import { readNpmLock } from './npm-lock.js';
|
|
9
|
+
import { licenseDecision, licenseListVersion, licensePolicy, policySchema } from './policy.js';
|
|
10
|
+
const id = 'licenses';
|
|
11
|
+
const checks = [
|
|
12
|
+
{ id: 'denied-license', description: 'A dependency declares a license outside the configured policy.' },
|
|
13
|
+
{ id: 'unknown-license', description: 'A dependency license is missing or could not be identified.' },
|
|
14
|
+
{ id: 'unverified-dependencies', description: 'A lockfile lists dependencies without license metadata.' },
|
|
15
|
+
];
|
|
16
|
+
const checked = {
|
|
17
|
+
id: checkedDependencies, label: 'Checked dependencies', unit: 'count', scope: 'file',
|
|
18
|
+
aggregate: 'sum', better: 'none', group: 'Legal',
|
|
19
|
+
};
|
|
20
|
+
export function licenses(options = undefined) {
|
|
21
|
+
const policy = licensePolicy(options);
|
|
22
|
+
return {
|
|
23
|
+
...bundledPlugin({
|
|
24
|
+
schema: pluginManifestSchema, id, version: '1.0.0', category: 'legal', optionsSchema: policySchema,
|
|
25
|
+
include: licenseLockNames.map(name => '**/' + name), tools: { 'spdx-license-list': licenseListVersion }, checks,
|
|
26
|
+
create: () => ({
|
|
27
|
+
produces: [
|
|
28
|
+
{ contract: dependencySources.id, persistence: 'report', valueSchema: dependencySourcesSchema },
|
|
29
|
+
{ contract: findingResult.id, persistence: 'report', metrics: [checked] },
|
|
30
|
+
],
|
|
31
|
+
operations: [{
|
|
32
|
+
id: 'read', role: 'provider', scope: 'repository', inputs: { licenseFiles }, output: dependencySources,
|
|
33
|
+
execute(context) {
|
|
34
|
+
return context.read('licenseFiles', licenseFiles).flatMap(({ path, bytes, format }) => {
|
|
35
|
+
try {
|
|
36
|
+
const text = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
37
|
+
const sources = format === undefined ? readLockfile(path, text) : [{ path, format, packages: readSbom(format, text) }];
|
|
38
|
+
return sources.map(source => ({ ...source, packages: source.packages.map(boundLicense) }));
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
context.emitDiagnostic?.('Unable to read ' + (format ?? 'lockfile') + ' license metadata: ' + (error instanceof Error ? error.message : String(error)), path);
|
|
42
|
+
return [];
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
}, {
|
|
47
|
+
id: 'check', role: 'inspection', scope: 'repository', inputs: { dependencies: dependencySources }, output: findingResult,
|
|
48
|
+
execute(context) {
|
|
49
|
+
const findings = [];
|
|
50
|
+
const measurements = [];
|
|
51
|
+
const ignored = [];
|
|
52
|
+
for (const { path, packages, unverified } of context.read('dependencies', dependencySources)) {
|
|
53
|
+
const inspected = policy.dev ? packages : packages.filter(dependency => !dependency.dev);
|
|
54
|
+
measurements.push({ definition: checkedDependencies, target: 'file', path, value: inspected.length });
|
|
55
|
+
if (unverified)
|
|
56
|
+
findings.push({ check: 'unverified-dependencies', level: 'low', path, anchor: '(file)',
|
|
57
|
+
message: 'This lockfile has no supported dependency license data. Import an SBOM to check the dependencies.' });
|
|
58
|
+
for (const dependency of inspected) {
|
|
59
|
+
const { name, version, license, line, endLine } = dependency;
|
|
60
|
+
const decision = licenseDecision(license, policy);
|
|
61
|
+
if (decision === 'allowed')
|
|
62
|
+
continue;
|
|
63
|
+
const check = decision === 'denied' ? 'denied-license' : 'unknown-license';
|
|
64
|
+
const label = version === undefined ? name : name + '@' + version;
|
|
65
|
+
const reason = policy.exceptions.get(label) ?? policy.exceptions.get(name);
|
|
66
|
+
if (reason !== undefined)
|
|
67
|
+
ignored.push({ index: findings.length, reason });
|
|
68
|
+
findings.push({ check, level: decision === 'denied' ? 'high' : 'medium', path, line, endLine, anchor: name,
|
|
69
|
+
message: findingMessage(label, dependency, decision, policy.preset) });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return { findings, measurements, ...(ignored.length ? { ignored } : {}) };
|
|
73
|
+
},
|
|
74
|
+
}],
|
|
75
|
+
}),
|
|
76
|
+
}),
|
|
77
|
+
claims: { globs: licenseLockNames.map(name => '**/' + name), scanOnly: true },
|
|
78
|
+
...(options === undefined ? {} : { settings: options }),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function boundLicense(dependency) {
|
|
82
|
+
if ((dependency.license?.length ?? 0) <= maxLicenseLength)
|
|
83
|
+
return dependency;
|
|
84
|
+
const { license: _, ...rest } = dependency;
|
|
85
|
+
return { ...rest, overlongLicense: true };
|
|
86
|
+
}
|
|
87
|
+
function findingMessage(label, { license, overlongLicense }, decision, preset) {
|
|
88
|
+
if (overlongLicense)
|
|
89
|
+
return `${label} declares a license expression longer than ${maxLicenseLength} characters.`;
|
|
90
|
+
if (license === undefined)
|
|
91
|
+
return `${label} declares no license.`;
|
|
92
|
+
if (license === 'UNLICENSED')
|
|
93
|
+
return `${label} is unlicensed, which the ${preset} policy does not allow.`;
|
|
94
|
+
if (decision === 'denied')
|
|
95
|
+
return `${label} is licensed under ${license}, which the ${preset} policy does not allow.`;
|
|
96
|
+
if (decision === 'unclassified')
|
|
97
|
+
return `${label} declares ${license}, which the ${preset} policy could not classify. Add the unclassified SPDX identifier to allow or deny.`;
|
|
98
|
+
return `${label} declares ${license}, which could not be identified as a valid SPDX expression.`;
|
|
99
|
+
}
|
|
100
|
+
function readLockfile(path, text) {
|
|
101
|
+
const name = path.slice(path.lastIndexOf('/') + 1);
|
|
102
|
+
switch (licenseLockKind(name)) {
|
|
103
|
+
case 'npm': {
|
|
104
|
+
const packages = readNpmLock(text);
|
|
105
|
+
return [packages === undefined ? { path, packages: [], unverified: true } : { path, packages }];
|
|
106
|
+
}
|
|
107
|
+
case 'composer': return [{ path, packages: readComposerLock(text) }];
|
|
108
|
+
case 'unverified': return listsDependencies(name, text) ? [{ path, packages: [], unverified: true }] : [];
|
|
109
|
+
}
|
|
110
|
+
return [];
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=licenses.js.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { isRecord } from '@repometry/core/internal/engine/plugin.js';
|
|
2
|
+
import { locateJsonSpans, pointer } from '../json-spans.js';
|
|
3
|
+
import { declaredLicense } from './policy.js';
|
|
4
|
+
/** Older and unknown versions lack the supported package metadata. */
|
|
5
|
+
export function readNpmLock(text) {
|
|
6
|
+
const lock = JSON.parse(text);
|
|
7
|
+
if (!isRecord(lock))
|
|
8
|
+
throw new Error('The npm lockfile must be a JSON object');
|
|
9
|
+
if (lock.lockfileVersion !== 2 && lock.lockfileVersion !== 3)
|
|
10
|
+
return undefined;
|
|
11
|
+
if (!isRecord(lock.packages))
|
|
12
|
+
throw new Error('The npm lockfile must contain a packages object');
|
|
13
|
+
const dependencies = Object.entries(lock.packages).flatMap(([path, entry]) => {
|
|
14
|
+
const prefix = path.lastIndexOf('node_modules/');
|
|
15
|
+
if (prefix === -1 || !isRecord(entry) || entry.link === true)
|
|
16
|
+
return [];
|
|
17
|
+
const name = (typeof entry.name === 'string' ? entry.name.trim() : '') || path.slice(prefix + 'node_modules/'.length);
|
|
18
|
+
if (!name)
|
|
19
|
+
return [];
|
|
20
|
+
const version = typeof entry.version === 'string' ? entry.version : undefined;
|
|
21
|
+
const license = declaredLicense(entry.license ?? entry.licenses);
|
|
22
|
+
return [{ at: pointer(['packages', path]), name, ...(version === undefined ? {} : { version }),
|
|
23
|
+
...(license === undefined ? {} : { license }), dev: entry.dev === true }];
|
|
24
|
+
});
|
|
25
|
+
if (!dependencies.length)
|
|
26
|
+
return [];
|
|
27
|
+
const spans = locateJsonSpans(text, new Set(dependencies.map(({ at }) => at)), 2);
|
|
28
|
+
return dependencies.map(({ at, ...dependency }) => ({ ...dependency, ...spans.get(at) ?? { line: 1, endLine: 1 } }));
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=npm-lock.js.map
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export declare const licenseListVersion: string;
|
|
2
|
+
export declare const presets: readonly ['open-source', 'weak-copyleft', 'permissive'];
|
|
3
|
+
type Preset = typeof presets[number];
|
|
4
|
+
type Decision = 'allowed' | 'denied' | 'unknown' | 'unclassified';
|
|
5
|
+
interface Exception {
|
|
6
|
+
readonly package: string;
|
|
7
|
+
readonly reason: string;
|
|
8
|
+
}
|
|
9
|
+
export interface PolicyOptions {
|
|
10
|
+
readonly policy?: Preset;
|
|
11
|
+
readonly allow?: readonly string[];
|
|
12
|
+
readonly deny?: readonly string[];
|
|
13
|
+
readonly exceptions?: readonly Exception[];
|
|
14
|
+
readonly dev?: boolean;
|
|
15
|
+
}
|
|
16
|
+
interface Policy {
|
|
17
|
+
readonly preset: Preset;
|
|
18
|
+
readonly allow: ReadonlySet<string>;
|
|
19
|
+
readonly deny: ReadonlySet<string>;
|
|
20
|
+
readonly exceptions: ReadonlyMap<string, string>;
|
|
21
|
+
readonly dev: boolean;
|
|
22
|
+
}
|
|
23
|
+
export declare const policySchema: {
|
|
24
|
+
readonly type: 'object';
|
|
25
|
+
readonly additionalProperties: false;
|
|
26
|
+
readonly properties: {
|
|
27
|
+
readonly policy: {
|
|
28
|
+
readonly enum: readonly ["open-source", "weak-copyleft", "permissive"];
|
|
29
|
+
};
|
|
30
|
+
readonly allow: {
|
|
31
|
+
readonly type: 'array';
|
|
32
|
+
readonly items: {
|
|
33
|
+
readonly type: 'string';
|
|
34
|
+
readonly minLength: 1;
|
|
35
|
+
};
|
|
36
|
+
readonly uniqueItems: true;
|
|
37
|
+
};
|
|
38
|
+
readonly deny: {
|
|
39
|
+
readonly type: 'array';
|
|
40
|
+
readonly items: {
|
|
41
|
+
readonly type: 'string';
|
|
42
|
+
readonly minLength: 1;
|
|
43
|
+
};
|
|
44
|
+
readonly uniqueItems: true;
|
|
45
|
+
};
|
|
46
|
+
readonly exceptions: {
|
|
47
|
+
readonly type: 'array';
|
|
48
|
+
readonly items: {
|
|
49
|
+
readonly type: 'object';
|
|
50
|
+
readonly required: readonly ['package', 'reason'];
|
|
51
|
+
readonly additionalProperties: false;
|
|
52
|
+
readonly properties: {
|
|
53
|
+
readonly package: {
|
|
54
|
+
readonly type: 'string';
|
|
55
|
+
readonly pattern: '\\S';
|
|
56
|
+
};
|
|
57
|
+
readonly reason: {
|
|
58
|
+
readonly type: 'string';
|
|
59
|
+
readonly pattern: '\\S';
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
readonly dev: {
|
|
65
|
+
readonly type: 'boolean';
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
export declare function licensePolicy({ policy: preset, allow, deny, exceptions, dev }?: PolicyOptions): Policy;
|
|
70
|
+
export declare function declaredLicense(value: unknown): string | undefined;
|
|
71
|
+
export declare function licenseDecision(expression: string | undefined, policy: Policy): Decision;
|
|
72
|
+
export {};
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { isRecord } from '@repometry/core/internal/engine/plugin.js';
|
|
2
|
+
import spdx from '../../../resources/licenses/spdx.json' with { type: 'json' };
|
|
3
|
+
import { maxLicenseLength } from './dependencies.js';
|
|
4
|
+
export const licenseListVersion = spdx.version;
|
|
5
|
+
export const presets = ['open-source', 'weak-copyleft', 'permissive'];
|
|
6
|
+
const catalog = new Map(Object.entries(spdx.licenses).map(([id, flags]) => [id.toLowerCase(),
|
|
7
|
+
{ id, open: flags[0] === true || flags[1] === true, kind: flags[2] }]));
|
|
8
|
+
const exceptions = new Set(spdx.exceptions.map(id => id.toLowerCase()));
|
|
9
|
+
const licenseReference = /^(?:DocumentRef-[A-Za-z0-9.-]+:)?LicenseRef-[A-Za-z0-9.-]+$/i;
|
|
10
|
+
const gnuLegacy = /^((?:AGPL|GFDL|GPL|LGPL)-\d\.\d)(\+)?$/i;
|
|
11
|
+
const legacyExceptions = {
|
|
12
|
+
'gpl-2.0-with-autoconf-exception': 'GPL-2.0-only',
|
|
13
|
+
'gpl-2.0-with-bison-exception': 'GPL-2.0-only',
|
|
14
|
+
'gpl-2.0-with-classpath-exception': 'GPL-2.0-only',
|
|
15
|
+
'gpl-2.0-with-font-exception': 'GPL-2.0-only',
|
|
16
|
+
'gpl-2.0-with-gcc-exception': 'GPL-2.0-only',
|
|
17
|
+
'gpl-3.0-with-autoconf-exception': 'GPL-3.0-only',
|
|
18
|
+
'gpl-3.0-with-gcc-exception': 'GPL-3.0-only',
|
|
19
|
+
};
|
|
20
|
+
const operators = new Set(['AND', 'OR', 'WITH', '(', ')']);
|
|
21
|
+
const expressionToken = /\s+|[()]|(?:[A-Za-z0-9.-]+:)?[A-Za-z0-9.-]+\+?/y;
|
|
22
|
+
export const policySchema = {
|
|
23
|
+
type: 'object', additionalProperties: false,
|
|
24
|
+
properties: {
|
|
25
|
+
policy: { enum: presets },
|
|
26
|
+
allow: { type: 'array', items: { type: 'string', minLength: 1 }, uniqueItems: true },
|
|
27
|
+
deny: { type: 'array', items: { type: 'string', minLength: 1 }, uniqueItems: true },
|
|
28
|
+
exceptions: { type: 'array', items: { type: 'object', required: ['package', 'reason'], additionalProperties: false,
|
|
29
|
+
properties: { package: { type: 'string', pattern: '\\S' }, reason: { type: 'string', pattern: '\\S' } } } },
|
|
30
|
+
dev: { type: 'boolean' },
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
export function licensePolicy({ policy: preset = 'open-source', allow = [], deny = [], exceptions = [], dev = false } = {}) {
|
|
34
|
+
return { preset, allow: policyIds(allow), deny: policyIds(deny),
|
|
35
|
+
exceptions: new Map(exceptions.map(entry => [entry.package.trim(), entry.reason.trim()])), dev };
|
|
36
|
+
}
|
|
37
|
+
function policyIds(ids) {
|
|
38
|
+
return new Set(ids.map(value => {
|
|
39
|
+
const id = canonicalLicense(value.trim());
|
|
40
|
+
if (!id || !catalog.has(id.toLowerCase()))
|
|
41
|
+
throw new Error('Expected an SPDX license identifier, received ' + JSON.stringify(value));
|
|
42
|
+
return id;
|
|
43
|
+
}));
|
|
44
|
+
}
|
|
45
|
+
function canonicalLicense(value) {
|
|
46
|
+
if (value === 'UNLICENSED' || value === 'proprietary')
|
|
47
|
+
return 'UNLICENSED';
|
|
48
|
+
if (licenseReference.test(value))
|
|
49
|
+
return value;
|
|
50
|
+
const normalized = value.toLowerCase();
|
|
51
|
+
const gnu = gnuLegacy.exec(value);
|
|
52
|
+
const historical = legacyExceptions[normalized] ?? (gnu ? gnu[1] + (gnu[2] ? '-or-later' : '-only') : undefined);
|
|
53
|
+
const id = historical?.toLowerCase() ?? normalized;
|
|
54
|
+
return catalog.get(id)?.id ?? (id.endsWith('+') ? catalog.get(id.slice(0, -1))?.id : undefined);
|
|
55
|
+
}
|
|
56
|
+
export function declaredLicense(value) {
|
|
57
|
+
if (typeof value === 'string')
|
|
58
|
+
return value.trim() || undefined;
|
|
59
|
+
if (isRecord(value) && typeof value.type === 'string')
|
|
60
|
+
return declaredLicense(value.type);
|
|
61
|
+
if (!Array.isArray(value) || !value.length)
|
|
62
|
+
return undefined;
|
|
63
|
+
const choices = value.map(entry => typeof entry === 'string' || isRecord(entry) ? declaredLicense(entry) : undefined);
|
|
64
|
+
return choices.every(choice => choice !== undefined) ? choices.map(choice => '(' + choice + ')').join(' OR ') : undefined;
|
|
65
|
+
}
|
|
66
|
+
export function licenseDecision(expression, policy) {
|
|
67
|
+
if (expression === undefined || expression.length > maxLicenseLength)
|
|
68
|
+
return 'unknown';
|
|
69
|
+
const parsed = tokenize(expression);
|
|
70
|
+
if (parsed === undefined)
|
|
71
|
+
return 'unknown';
|
|
72
|
+
const tokens = parsed;
|
|
73
|
+
let cursor = 0;
|
|
74
|
+
function operand(depth) {
|
|
75
|
+
if (depth > 64)
|
|
76
|
+
return undefined;
|
|
77
|
+
const token = tokens[cursor++];
|
|
78
|
+
if (token === '(') {
|
|
79
|
+
const result = composite(depth + 1, 'OR');
|
|
80
|
+
return tokens[cursor++] === ')' ? result : undefined;
|
|
81
|
+
}
|
|
82
|
+
if (token === undefined || operators.has(token))
|
|
83
|
+
return undefined;
|
|
84
|
+
if (tokens[cursor] === 'WITH') {
|
|
85
|
+
cursor++;
|
|
86
|
+
const exception = tokens[cursor++];
|
|
87
|
+
if (!exception || !exceptions.has(exception.toLowerCase()))
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
const id = canonicalLicense(token);
|
|
91
|
+
return id === undefined ? undefined : decide(id, policy);
|
|
92
|
+
}
|
|
93
|
+
function composite(depth, operator) {
|
|
94
|
+
const next = () => operator === 'AND' ? operand(depth) : composite(depth, 'AND');
|
|
95
|
+
let result = next();
|
|
96
|
+
while (result !== undefined && tokens[cursor] === operator) {
|
|
97
|
+
cursor++;
|
|
98
|
+
const right = next();
|
|
99
|
+
if (right === undefined)
|
|
100
|
+
return undefined;
|
|
101
|
+
result = combine(result, right, operator);
|
|
102
|
+
}
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
const decision = composite(0, 'OR');
|
|
106
|
+
return cursor === tokens.length ? decision ?? 'unknown' : 'unknown';
|
|
107
|
+
}
|
|
108
|
+
function tokenize(text) {
|
|
109
|
+
const tokens = [];
|
|
110
|
+
for (let index = 0; index < text.length; index = expressionToken.lastIndex) {
|
|
111
|
+
expressionToken.lastIndex = index;
|
|
112
|
+
const token = expressionToken.exec(text)?.[0];
|
|
113
|
+
if (!token)
|
|
114
|
+
return undefined;
|
|
115
|
+
if (token === 'WITH' && (!/\s/.test(text[index - 1] ?? '') || !/\s/.test(text[expressionToken.lastIndex] ?? '')))
|
|
116
|
+
return undefined;
|
|
117
|
+
if (token.trim())
|
|
118
|
+
tokens.push(token);
|
|
119
|
+
}
|
|
120
|
+
return tokens;
|
|
121
|
+
}
|
|
122
|
+
function decide(id, { preset, allow, deny }) {
|
|
123
|
+
if (id === 'UNLICENSED')
|
|
124
|
+
return 'denied';
|
|
125
|
+
const entry = catalog.get(id.toLowerCase());
|
|
126
|
+
if (entry === undefined)
|
|
127
|
+
return 'unknown';
|
|
128
|
+
const { open, kind } = entry;
|
|
129
|
+
if (kind === 'source-available' || deny.has(id))
|
|
130
|
+
return 'denied';
|
|
131
|
+
if (allow.has(id))
|
|
132
|
+
return 'allowed';
|
|
133
|
+
if (preset === 'open-source')
|
|
134
|
+
return open || kind !== undefined ? 'allowed' : 'denied';
|
|
135
|
+
if (kind === undefined)
|
|
136
|
+
return 'unclassified';
|
|
137
|
+
if (kind === 'permissive' || kind === 'public-domain' || (preset === 'weak-copyleft' && kind === 'weak-copyleft'))
|
|
138
|
+
return 'allowed';
|
|
139
|
+
return 'denied';
|
|
140
|
+
}
|
|
141
|
+
function combine(left, right, operator) {
|
|
142
|
+
const decisive = operator === 'AND' ? 'denied' : 'allowed';
|
|
143
|
+
if (left === decisive || right === decisive)
|
|
144
|
+
return decisive;
|
|
145
|
+
if (left === right)
|
|
146
|
+
return left;
|
|
147
|
+
return left === 'unknown' || right === 'unknown' ? 'unknown' : 'unclassified';
|
|
148
|
+
}
|
|
149
|
+
//# sourceMappingURL=policy.js.map
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { isRecord } from '@repometry/core/internal/engine/plugin.js';
|
|
2
|
+
import { locateJsonSpans, pointer } from '../json-spans.js';
|
|
3
|
+
export function readSbom(format, text) {
|
|
4
|
+
const value = JSON.parse(text);
|
|
5
|
+
if (!isRecord(value))
|
|
6
|
+
throw new Error('The SBOM must be a JSON object');
|
|
7
|
+
const dependencies = format === 'cyclonedx' ? cyclone(value) : spdx(value);
|
|
8
|
+
const wanted = new Set(dependencies.map(({ at }) => pointer(at)));
|
|
9
|
+
if (wanted.size === 0)
|
|
10
|
+
return [];
|
|
11
|
+
const depth = dependencies.reduce((max, { at }) => Math.max(max, at.length), 0);
|
|
12
|
+
const ranges = locateJsonSpans(text, wanted, depth);
|
|
13
|
+
return dependencies.map(({ at, ...entry }) => ({ ...entry, ...ranges.get(pointer(at)) ?? { line: 1, endLine: 1 } }));
|
|
14
|
+
}
|
|
15
|
+
function entries(value, name) {
|
|
16
|
+
if (value === undefined)
|
|
17
|
+
return [];
|
|
18
|
+
if (!Array.isArray(value))
|
|
19
|
+
throw new Error(name + ' must be an array');
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
function text(value) {
|
|
23
|
+
return typeof value === 'string' && value.trim() ? value.trim() : undefined;
|
|
24
|
+
}
|
|
25
|
+
function cyclone(value) {
|
|
26
|
+
if (value.bomFormat !== 'CycloneDX' || !['1.4', '1.5', '1.6'].some(version => version === value.specVersion)) {
|
|
27
|
+
throw new Error('Expected CycloneDX JSON version 1.4, 1.5 or 1.6');
|
|
28
|
+
}
|
|
29
|
+
const root = isRecord(value.metadata) && isRecord(value.metadata.component) ? value.metadata.component : undefined;
|
|
30
|
+
const rootId = text(root?.['bom-ref']);
|
|
31
|
+
const stack = [];
|
|
32
|
+
function add(components, prefix) {
|
|
33
|
+
const array = entries(components, 'CycloneDX components');
|
|
34
|
+
for (let index = array.length - 1; index >= 0; index--)
|
|
35
|
+
stack.push({ entry: array[index], at: [...prefix, index] });
|
|
36
|
+
}
|
|
37
|
+
add(root?.components, ['metadata', 'component', 'components']);
|
|
38
|
+
add(value.components, ['components']);
|
|
39
|
+
const dependencies = [];
|
|
40
|
+
const seen = new Set();
|
|
41
|
+
while (stack.length) {
|
|
42
|
+
const { entry, at } = stack.pop();
|
|
43
|
+
if (!isRecord(entry))
|
|
44
|
+
throw new Error('A CycloneDX component must be an object');
|
|
45
|
+
const name = text(entry.name);
|
|
46
|
+
if (name === undefined)
|
|
47
|
+
throw new Error('A CycloneDX component needs a name');
|
|
48
|
+
const id = text(entry['bom-ref']);
|
|
49
|
+
add(entry.components, [...at, 'components']);
|
|
50
|
+
if (id !== undefined && (id === rootId || seen.has(id)))
|
|
51
|
+
continue;
|
|
52
|
+
if (id !== undefined)
|
|
53
|
+
seen.add(id);
|
|
54
|
+
const group = text(entry.group);
|
|
55
|
+
const version = text(entry.version);
|
|
56
|
+
const license = cycloneLicense(entry.licenses);
|
|
57
|
+
dependencies.push({ at, name: group && !name.startsWith(group + '/') ? group + '/' + name : name,
|
|
58
|
+
...(version === undefined ? {} : { version }), ...(license === undefined ? {} : { license }), dev: entry.scope === 'excluded' });
|
|
59
|
+
}
|
|
60
|
+
return dependencies;
|
|
61
|
+
}
|
|
62
|
+
function cycloneLicense(value) {
|
|
63
|
+
const licenses = entries(value, 'CycloneDX licenses');
|
|
64
|
+
const declarations = new Set(licenses.flatMap(entry => {
|
|
65
|
+
if (!isRecord(entry))
|
|
66
|
+
return [];
|
|
67
|
+
if (isRecord(entry.license)) {
|
|
68
|
+
const license = entry.license;
|
|
69
|
+
const value = text(license.id) ?? text(license.name);
|
|
70
|
+
return license.acknowledgement !== 'concluded' && value !== undefined ? [value] : [];
|
|
71
|
+
}
|
|
72
|
+
const expression = text(entry.expression);
|
|
73
|
+
return entry.acknowledgement !== 'concluded' && expression !== undefined ? [expression] : [];
|
|
74
|
+
}));
|
|
75
|
+
return declarations.size ? [...declarations].join(', ') : undefined;
|
|
76
|
+
}
|
|
77
|
+
function spdx(value) {
|
|
78
|
+
if (value.spdxVersion !== 'SPDX-2.3')
|
|
79
|
+
throw new Error('Expected SPDX 2.3 JSON');
|
|
80
|
+
const roots = new Set(entries(value.documentDescribes, 'SPDX documentDescribes').filter(id => typeof id === 'string'));
|
|
81
|
+
const runtime = new Set();
|
|
82
|
+
const development = new Set();
|
|
83
|
+
const document = text(value.SPDXID) ?? 'SPDXRef-DOCUMENT';
|
|
84
|
+
for (const row of entries(value.relationships, 'SPDX relationships')) {
|
|
85
|
+
if (!isRecord(row))
|
|
86
|
+
continue;
|
|
87
|
+
const from = text(row.spdxElementId);
|
|
88
|
+
const to = text(row.relatedSpdxElement);
|
|
89
|
+
if (from === undefined || to === undefined)
|
|
90
|
+
continue;
|
|
91
|
+
switch (row.relationshipType) {
|
|
92
|
+
case 'DESCRIBES':
|
|
93
|
+
if (from === document)
|
|
94
|
+
roots.add(to);
|
|
95
|
+
break;
|
|
96
|
+
case 'DESCRIBED_BY':
|
|
97
|
+
if (to === document)
|
|
98
|
+
roots.add(from);
|
|
99
|
+
break;
|
|
100
|
+
case 'DEV_DEPENDENCY_OF':
|
|
101
|
+
case 'TEST_DEPENDENCY_OF':
|
|
102
|
+
development.add(from);
|
|
103
|
+
break;
|
|
104
|
+
case 'DEPENDENCY_OF':
|
|
105
|
+
case 'RUNTIME_DEPENDENCY_OF':
|
|
106
|
+
case 'OPTIONAL_DEPENDENCY_OF':
|
|
107
|
+
case 'PROVIDED_DEPENDENCY_OF':
|
|
108
|
+
case 'BUILD_DEPENDENCY_OF':
|
|
109
|
+
runtime.add(from);
|
|
110
|
+
break;
|
|
111
|
+
case 'DEPENDS_ON':
|
|
112
|
+
runtime.add(to);
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return entries(value.packages, 'SPDX packages').flatMap((entry, index) => {
|
|
117
|
+
if (!isRecord(entry))
|
|
118
|
+
throw new Error('An SPDX package must be an object');
|
|
119
|
+
const name = text(entry.name);
|
|
120
|
+
const id = text(entry.SPDXID);
|
|
121
|
+
if (name === undefined || id === undefined)
|
|
122
|
+
throw new Error('An SPDX package needs a name and SPDXID');
|
|
123
|
+
if (roots.has(id))
|
|
124
|
+
return [];
|
|
125
|
+
const version = text(entry.versionInfo);
|
|
126
|
+
const license = text(entry.licenseDeclared);
|
|
127
|
+
return [{ at: ['packages', index], name, ...(version === undefined ? {} : { version }),
|
|
128
|
+
...(license === undefined ? {} : { license }), dev: development.has(id) && !runtime.has(id) }];
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=sbom.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function listsDependencies(name: string, text: string): boolean;
|