@trustify-da/trustify-da-javascript-client 0.3.0-ea.f2d5d72 → 0.3.0-ea.fa16f06
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 +2 -2
- package/dist/package.json +26 -23
- package/dist/src/analysis.d.ts +14 -7
- package/dist/src/analysis.js +32 -15
- package/dist/src/cli.js +156 -3
- package/dist/src/cyclone_dx_sbom.d.ts +7 -1
- package/dist/src/cyclone_dx_sbom.js +29 -8
- package/dist/src/index.d.ts +84 -11
- package/dist/src/index.js +95 -8
- package/dist/src/license/index.d.ts +3 -3
- package/dist/src/license/index.js +4 -1
- package/dist/src/license/license_utils.js +4 -1
- package/dist/src/license/licenses_api.d.ts +2 -2
- package/dist/src/oci_image/images.d.ts +1 -1
- package/dist/src/oci_image/images.js +15 -1
- package/dist/src/oci_image/utils.d.ts +5 -5
- package/dist/src/oci_image/utils.js +11 -2
- package/dist/src/provider.d.ts +4 -3
- package/dist/src/provider.js +8 -2
- package/dist/src/providers/base_java.d.ts +14 -12
- package/dist/src/providers/base_java.js +18 -44
- package/dist/src/providers/base_javascript.d.ts +20 -9
- package/dist/src/providers/base_javascript.js +10 -3
- package/dist/src/providers/base_pyproject.d.ts +42 -31
- package/dist/src/providers/base_pyproject.js +121 -78
- package/dist/src/providers/containerfile_parser.d.ts +5 -0
- package/dist/src/providers/containerfile_parser.js +20 -0
- package/dist/src/providers/golang_gomodules.d.ts +11 -1
- package/dist/src/providers/golang_gomodules.js +65 -8
- package/dist/src/providers/java_gradle.d.ts +19 -0
- package/dist/src/providers/java_gradle.js +116 -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 +10 -2
- package/dist/src/providers/java_maven.js +165 -1
- package/dist/src/providers/javascript_bun.d.ts +10 -0
- package/dist/src/providers/javascript_bun.js +100 -0
- package/dist/src/providers/javascript_npm.d.ts +6 -0
- package/dist/src/providers/javascript_npm.js +21 -0
- package/dist/src/providers/javascript_pnpm.js +6 -2
- package/dist/src/providers/marker_evaluator.d.ts +14 -0
- package/dist/src/providers/marker_evaluator.js +191 -0
- package/dist/src/providers/oci_dockerfile.d.ts +51 -0
- package/dist/src/providers/oci_dockerfile.js +177 -0
- package/dist/src/providers/processors/yarn_berry_processor.d.ts +2 -2
- package/dist/src/providers/processors/yarn_berry_processor.js +6 -2
- package/dist/src/providers/processors/yarn_classic_processor.d.ts +2 -2
- package/dist/src/providers/python_controller.d.ts +5 -1
- package/dist/src/providers/python_controller.js +69 -8
- package/dist/src/providers/python_pip.d.ts +5 -0
- package/dist/src/providers/python_pip.js +5 -5
- package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
- package/dist/src/providers/python_pip_pyproject.js +146 -0
- package/dist/src/providers/python_poetry.d.ts +64 -8
- package/dist/src/providers/python_poetry.js +165 -25
- package/dist/src/providers/python_uv.d.ts +22 -2
- package/dist/src/providers/python_uv.js +157 -6
- package/dist/src/providers/rust_cargo.d.ts +6 -2
- package/dist/src/providers/rust_cargo.js +105 -19
- package/dist/src/providers/tree-sitter-containerfile.wasm +0 -0
- package/dist/src/remediate.d.ts +21 -0
- package/dist/src/remediate.js +149 -0
- package/dist/src/remediation.d.ts +57 -0
- package/dist/src/remediation.js +436 -0
- package/dist/src/remediation_report.d.ts +40 -0
- package/dist/src/remediation_report.js +159 -0
- package/dist/src/sbom.d.ts +7 -1
- package/dist/src/sbom.js +4 -2
- package/dist/src/tools.d.ts +28 -2
- package/dist/src/tools.js +58 -0
- package/dist/src/updaters/maven_updater.d.ts +26 -0
- package/dist/src/updaters/maven_updater.js +322 -0
- package/dist/src/updaters/toml_updater.d.ts +33 -0
- package/dist/src/updaters/toml_updater.js +244 -0
- package/dist/src/workspace.d.ts +9 -0
- package/dist/src/workspace.js +1 -1
- package/package.json +27 -24
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps Node.js/OS values to PEP 508 marker variables.
|
|
3
|
+
* Example: on Linux, sys_platform='linux', platform_system='Linux', os_name='posix'
|
|
4
|
+
* @returns {Record<string, string>}
|
|
5
|
+
*/
|
|
6
|
+
export function getEnvironmentMarkers(): Record<string, string>;
|
|
7
|
+
/**
|
|
8
|
+
* Evaluates a full PEP 508 marker expression against the current platform.
|
|
9
|
+
* Example: "sys_platform == 'win32' and python_version >= '3.8'" → false on Linux
|
|
10
|
+
* Empty/missing markers return true (unconditional dependency).
|
|
11
|
+
* @param {string} markerExpr
|
|
12
|
+
* @returns {boolean}
|
|
13
|
+
*/
|
|
14
|
+
export function evaluateMarker(markerExpr: string): boolean;
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// PEP 508 environment marker evaluator.
|
|
2
|
+
// Filters Python dependencies by platform/version markers so that e.g.
|
|
3
|
+
// "pywin32 ; sys_platform == 'win32'" is excluded on Linux/macOS.
|
|
4
|
+
// See https://peps.python.org/pep-0508/#environment-markers
|
|
5
|
+
import os from 'node:os';
|
|
6
|
+
import { getCustomPath, invokeCommand } from '../tools.js';
|
|
7
|
+
let cachedPythonVersions = undefined;
|
|
8
|
+
function getPythonVersions() {
|
|
9
|
+
if (cachedPythonVersions !== undefined) {
|
|
10
|
+
return cachedPythonVersions;
|
|
11
|
+
}
|
|
12
|
+
try {
|
|
13
|
+
let python = getCustomPath('python3');
|
|
14
|
+
let out = invokeCommand(python, ['-c', "import sys; v=sys.version_info; print(f'{v.major}.{v.minor} {v.major}.{v.minor}.{v.micro}')"], { timeout: 5000 }).toString().trim();
|
|
15
|
+
let [short, full] = out.split(' ');
|
|
16
|
+
cachedPythonVersions = { short, full };
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
cachedPythonVersions = null;
|
|
20
|
+
}
|
|
21
|
+
return cachedPythonVersions;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Maps Node.js/OS values to PEP 508 marker variables.
|
|
25
|
+
* Example: on Linux, sys_platform='linux', platform_system='Linux', os_name='posix'
|
|
26
|
+
* @returns {Record<string, string>}
|
|
27
|
+
*/
|
|
28
|
+
export function getEnvironmentMarkers() {
|
|
29
|
+
let platform = process.platform;
|
|
30
|
+
let systemMap = { win32: 'Windows', linux: 'Linux', darwin: 'Darwin' };
|
|
31
|
+
let machine = typeof os.machine === 'function' ? os.machine() : process.arch;
|
|
32
|
+
let pyVer = getPythonVersions();
|
|
33
|
+
return {
|
|
34
|
+
sys_platform: platform,
|
|
35
|
+
platform_system: systemMap[platform] || platform,
|
|
36
|
+
os_name: platform === 'win32' ? 'nt' : 'posix',
|
|
37
|
+
platform_machine: machine,
|
|
38
|
+
platform_release: os.release(),
|
|
39
|
+
platform_version: os.version?.() || '',
|
|
40
|
+
python_version: pyVer?.short || '',
|
|
41
|
+
python_full_version: pyVer?.full || '',
|
|
42
|
+
implementation_name: 'cpython',
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function compareVersions(left, right) {
|
|
46
|
+
let lParts = left.split('.').map(Number);
|
|
47
|
+
let rParts = right.split('.').map(Number);
|
|
48
|
+
let len = Math.max(lParts.length, rParts.length);
|
|
49
|
+
for (let i = 0; i < len; i++) {
|
|
50
|
+
let l = lParts[i] || 0;
|
|
51
|
+
let r = rParts[i] || 0;
|
|
52
|
+
if (l < r) {
|
|
53
|
+
return -1;
|
|
54
|
+
}
|
|
55
|
+
if (l > r) {
|
|
56
|
+
return 1;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return 0;
|
|
60
|
+
}
|
|
61
|
+
// Evaluates a single comparison like sys_platform == 'win32' or python_version >= '3.8'.
|
|
62
|
+
// Version-bearing variables (python_version, python_full_version) use numeric comparison;
|
|
63
|
+
// all others use string equality. Returns false when the env value is missing.
|
|
64
|
+
function evaluateComparison(variable, op, value, env) {
|
|
65
|
+
let envVal = env[variable];
|
|
66
|
+
if (envVal === undefined || envVal === '') {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
let isVersion = variable.includes('version');
|
|
70
|
+
if (isVersion) {
|
|
71
|
+
let cmp = compareVersions(envVal, value);
|
|
72
|
+
switch (op) {
|
|
73
|
+
case '==': return cmp === 0;
|
|
74
|
+
case '!=': return cmp !== 0;
|
|
75
|
+
case '>=': return cmp >= 0;
|
|
76
|
+
case '<=': return cmp <= 0;
|
|
77
|
+
case '>': return cmp > 0;
|
|
78
|
+
case '<': return cmp < 0;
|
|
79
|
+
case '~=': {
|
|
80
|
+
let parts = value.split('.');
|
|
81
|
+
parts.pop();
|
|
82
|
+
let prefix = parts.join('.');
|
|
83
|
+
return envVal.startsWith(prefix) && cmp >= 0;
|
|
84
|
+
}
|
|
85
|
+
default: return true;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
switch (op) {
|
|
89
|
+
case '==': return envVal === value;
|
|
90
|
+
case '!=': return envVal !== value;
|
|
91
|
+
case 'in': return value.includes(envVal);
|
|
92
|
+
case 'not in': return !value.includes(envVal);
|
|
93
|
+
default: return envVal === value;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// Parses a single marker comparison into {variable, op, value}.
|
|
97
|
+
// Handles both normal and reversed forms:
|
|
98
|
+
// "sys_platform == 'linux'" → { variable: 'sys_platform', op: '==', value: 'linux' }
|
|
99
|
+
// "'linux' == sys_platform" → { variable: 'sys_platform', op: '==', value: 'linux' }
|
|
100
|
+
function parseAtom(expr) {
|
|
101
|
+
// Normal form: variable op 'value'
|
|
102
|
+
let m = expr.match(/^\s*([\w.]+)\s*(~=|!=|==|>=|<=|>|<|not\s+in|in)\s*["']([^"']*)["']\s*$/);
|
|
103
|
+
if (m) {
|
|
104
|
+
return { variable: m[1], op: m[2].replace(/\s+/g, ' '), value: m[3] };
|
|
105
|
+
}
|
|
106
|
+
// Reversed form: 'value' op variable — reverse directional operators
|
|
107
|
+
let mReverse = expr.match(/^\s*["']([^"']*)['"]\s*(~=|!=|==|>=|<=|>|<|not\s+in|in)\s*([\w.]+)\s*$/);
|
|
108
|
+
if (mReverse) {
|
|
109
|
+
let reverseOp = { '<': '>', '>': '<', '<=': '>=', '>=': '<=' };
|
|
110
|
+
let op = mReverse[2].replace(/\s+/g, ' ');
|
|
111
|
+
return { variable: mReverse[3], op: reverseOp[op] || op, value: mReverse[1] };
|
|
112
|
+
}
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Evaluates a full PEP 508 marker expression against the current platform.
|
|
117
|
+
* Example: "sys_platform == 'win32' and python_version >= '3.8'" → false on Linux
|
|
118
|
+
* Empty/missing markers return true (unconditional dependency).
|
|
119
|
+
* @param {string} markerExpr
|
|
120
|
+
* @returns {boolean}
|
|
121
|
+
*/
|
|
122
|
+
export function evaluateMarker(markerExpr) {
|
|
123
|
+
if (!markerExpr || !markerExpr.trim()) {
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
let env = getEnvironmentMarkers();
|
|
127
|
+
return evaluateExpr(markerExpr.trim(), env);
|
|
128
|
+
}
|
|
129
|
+
function evaluateExpr(expr, env) {
|
|
130
|
+
let orParts = splitLogical(expr, ' or ');
|
|
131
|
+
if (orParts.length > 1) {
|
|
132
|
+
return orParts.some(part => evaluateExpr(part, env));
|
|
133
|
+
}
|
|
134
|
+
let andParts = splitLogical(expr, ' and ');
|
|
135
|
+
if (andParts.length > 1) {
|
|
136
|
+
return andParts.every(part => evaluateExpr(part, env));
|
|
137
|
+
}
|
|
138
|
+
let trimmed = expr.trim();
|
|
139
|
+
if (trimmed.startsWith('(') && trimmed.endsWith(')')) {
|
|
140
|
+
return evaluateExpr(trimmed.slice(1, -1), env);
|
|
141
|
+
}
|
|
142
|
+
let atom = parseAtom(trimmed);
|
|
143
|
+
if (!atom) {
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
return evaluateComparison(atom.variable, atom.op, atom.value, env);
|
|
147
|
+
}
|
|
148
|
+
// Splits an expression by " and " or " or " at the top level, skipping
|
|
149
|
+
// separators inside parentheses or quoted strings.
|
|
150
|
+
// Example: splitLogical("a == 'x' and (b == 'y' or c == 'z')", " and ")
|
|
151
|
+
// → ["a == 'x'", "(b == 'y' or c == 'z')"]
|
|
152
|
+
function splitLogical(expr, sep) {
|
|
153
|
+
let parts = [];
|
|
154
|
+
let depth = 0;
|
|
155
|
+
let current = '';
|
|
156
|
+
let i = 0;
|
|
157
|
+
let quoteChar = null;
|
|
158
|
+
while (i < expr.length) {
|
|
159
|
+
let ch = expr[i];
|
|
160
|
+
if (quoteChar) {
|
|
161
|
+
if (ch === quoteChar) {
|
|
162
|
+
quoteChar = null;
|
|
163
|
+
}
|
|
164
|
+
current += ch;
|
|
165
|
+
i++;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (ch === '"' || ch === "'") {
|
|
169
|
+
quoteChar = ch;
|
|
170
|
+
current += ch;
|
|
171
|
+
i++;
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
if (ch === '(') {
|
|
175
|
+
depth++;
|
|
176
|
+
}
|
|
177
|
+
if (ch === ')') {
|
|
178
|
+
depth--;
|
|
179
|
+
}
|
|
180
|
+
if (depth === 0 && expr.substring(i, i + sep.length) === sep) {
|
|
181
|
+
parts.push(current);
|
|
182
|
+
current = '';
|
|
183
|
+
i += sep.length;
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
current += ch;
|
|
187
|
+
i++;
|
|
188
|
+
}
|
|
189
|
+
parts.push(current);
|
|
190
|
+
return parts.filter(p => p.trim());
|
|
191
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse all FROM instructions from a Dockerfile using tree-sitter to extract base image references.
|
|
3
|
+
* In multi-stage builds, every FROM line is returned so each image can be analyzed independently.
|
|
4
|
+
* ARG substitutions are resolved using declared default values. FROM lines with unresolvable
|
|
5
|
+
* variables (ARGs without defaults) are silently skipped.
|
|
6
|
+
* @param {string} manifestContent the content of the Dockerfile
|
|
7
|
+
* @returns {Promise<string[]>} array of image references from all resolvable FROM instructions
|
|
8
|
+
* @throws {Error} when no FROM instruction is found or when no FROM lines can be resolved
|
|
9
|
+
*/
|
|
10
|
+
export function parseAllFromImages(manifestContent: string): Promise<string[]>;
|
|
11
|
+
declare namespace _default {
|
|
12
|
+
export { isSupported };
|
|
13
|
+
export { validateLockFile };
|
|
14
|
+
export { provideComponent };
|
|
15
|
+
export { provideStack };
|
|
16
|
+
export { readLicenseFromManifest };
|
|
17
|
+
export function packageManagerName(): string;
|
|
18
|
+
}
|
|
19
|
+
export default _default;
|
|
20
|
+
export type Provided = any;
|
|
21
|
+
/**
|
|
22
|
+
* Check if the given manifest name is a Dockerfile or Containerfile.
|
|
23
|
+
* Supports dot-suffixed variants such as Dockerfile.dev or Containerfile.prod.
|
|
24
|
+
* @param {string} manifestName the manifest file name to check
|
|
25
|
+
* @returns {boolean} true if the manifest is a Dockerfile or Containerfile
|
|
26
|
+
*/
|
|
27
|
+
declare function isSupported(manifestName: string): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Dockerfiles have no lock file, so validation always passes.
|
|
30
|
+
* @returns {boolean} always true
|
|
31
|
+
*/
|
|
32
|
+
declare function validateLockFile(): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Provide content and content type for Dockerfile component analysis.
|
|
35
|
+
* @param {string} manifest path to the Dockerfile
|
|
36
|
+
* @param {{}} [opts={}] optional various options to pass along the application
|
|
37
|
+
* @returns {Promise<Provided>}
|
|
38
|
+
*/
|
|
39
|
+
declare function provideComponent(manifest: string, opts?: {}): Promise<Provided>;
|
|
40
|
+
/**
|
|
41
|
+
* Provide content and content type for Dockerfile stack analysis.
|
|
42
|
+
* @param {string} manifest path to the Dockerfile
|
|
43
|
+
* @param {{}} [opts={}] optional various options to pass along the application
|
|
44
|
+
* @returns {Promise<Provided>}
|
|
45
|
+
*/
|
|
46
|
+
declare function provideStack(manifest: string, opts?: {}): Promise<Provided>;
|
|
47
|
+
/**
|
|
48
|
+
* Dockerfiles contain no license information.
|
|
49
|
+
* @returns {null} always null
|
|
50
|
+
*/
|
|
51
|
+
declare function readLicenseFromManifest(): null;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import { generateImageSBOM, parseImageRef } from '../oci_image/utils.js';
|
|
3
|
+
import { getArgQuery, getFromQuery, getParser } from './containerfile_parser.js';
|
|
4
|
+
export default { isSupported, validateLockFile, provideComponent, provideStack, readLicenseFromManifest, packageManagerName() { return 'oci'; } };
|
|
5
|
+
/** @typedef {import('../provider').Provider} */
|
|
6
|
+
/** @typedef {import('../provider').Provided} Provided */
|
|
7
|
+
/**
|
|
8
|
+
* @type {string} ecosystem identifier for OCI image packages
|
|
9
|
+
* @private
|
|
10
|
+
*/
|
|
11
|
+
const ecosystem = 'oci';
|
|
12
|
+
/**
|
|
13
|
+
* Check if the given manifest name is a Dockerfile or Containerfile.
|
|
14
|
+
* Supports dot-suffixed variants such as Dockerfile.dev or Containerfile.prod.
|
|
15
|
+
* @param {string} manifestName the manifest file name to check
|
|
16
|
+
* @returns {boolean} true if the manifest is a Dockerfile or Containerfile
|
|
17
|
+
*/
|
|
18
|
+
function isSupported(manifestName) {
|
|
19
|
+
return /^(Dockerfile|Containerfile)(\..+)?$/.test(manifestName);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Dockerfiles have no lock file, so validation always passes.
|
|
23
|
+
* @returns {boolean} always true
|
|
24
|
+
*/
|
|
25
|
+
function validateLockFile() { return true; }
|
|
26
|
+
/**
|
|
27
|
+
* Check whether a syntax node contains any expansion (variable substitution) children.
|
|
28
|
+
* @param {import('web-tree-sitter').SyntaxNode} node
|
|
29
|
+
* @returns {boolean}
|
|
30
|
+
* @private
|
|
31
|
+
*/
|
|
32
|
+
function containsExpansion(node) {
|
|
33
|
+
if (node.type === 'expansion') {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
37
|
+
if (containsExpansion(node.child(i))) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Strip surrounding single or double quotes from a string.
|
|
45
|
+
* @param {string} text
|
|
46
|
+
* @returns {string}
|
|
47
|
+
* @private
|
|
48
|
+
*/
|
|
49
|
+
function stripQuotes(text) {
|
|
50
|
+
if ((text.startsWith('"') && text.endsWith('"')) || (text.startsWith("'") && text.endsWith("'"))) {
|
|
51
|
+
return text.slice(1, -1);
|
|
52
|
+
}
|
|
53
|
+
return text;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Collect ARG key-value pairs from the Dockerfile AST.
|
|
57
|
+
* Only ARGs with a default value are collected (ARGs without defaults cannot be resolved statically).
|
|
58
|
+
* @param {import('web-tree-sitter').Tree} tree the parsed Dockerfile tree
|
|
59
|
+
* @param {import('web-tree-sitter').Query} argQuery the tree-sitter query for arg_instruction nodes
|
|
60
|
+
* @returns {Map<string, string>} map of ARG names to their default values
|
|
61
|
+
* @private
|
|
62
|
+
*/
|
|
63
|
+
function collectArgs(tree, argQuery) {
|
|
64
|
+
const args = new Map();
|
|
65
|
+
for (const match of argQuery.matches(tree.rootNode)) {
|
|
66
|
+
const name = match.captures.find(c => c.name === 'name')?.node.text;
|
|
67
|
+
const defaultValue = match.captures.find(c => c.name === 'default')?.node.text;
|
|
68
|
+
if (name && defaultValue) {
|
|
69
|
+
args.set(name, stripQuotes(defaultValue));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return args;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Resolve ARG substitutions in an image spec text using the collected ARG map.
|
|
76
|
+
* Replaces ${VAR} and $VAR patterns with their ARG default values.
|
|
77
|
+
* @param {string} text the image spec text potentially containing variable references
|
|
78
|
+
* @param {Map<string, string>} args map of ARG names to their default values
|
|
79
|
+
* @returns {string|null} the resolved text, or null if any variable could not be resolved
|
|
80
|
+
* @private
|
|
81
|
+
*/
|
|
82
|
+
function resolveArgs(text, args) {
|
|
83
|
+
let resolved = text;
|
|
84
|
+
let hasUnresolved = false;
|
|
85
|
+
resolved = resolved.replace(/\$\{([^}]+)\}|\$([A-Za-z_]\w*)/g, (_match, braced, plain) => {
|
|
86
|
+
const key = braced || plain;
|
|
87
|
+
if (args.has(key)) {
|
|
88
|
+
return args.get(key);
|
|
89
|
+
}
|
|
90
|
+
hasUnresolved = true;
|
|
91
|
+
return _match;
|
|
92
|
+
});
|
|
93
|
+
return hasUnresolved ? null : resolved;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Parse all FROM instructions from a Dockerfile using tree-sitter to extract base image references.
|
|
97
|
+
* In multi-stage builds, every FROM line is returned so each image can be analyzed independently.
|
|
98
|
+
* ARG substitutions are resolved using declared default values. FROM lines with unresolvable
|
|
99
|
+
* variables (ARGs without defaults) are silently skipped.
|
|
100
|
+
* @param {string} manifestContent the content of the Dockerfile
|
|
101
|
+
* @returns {Promise<string[]>} array of image references from all resolvable FROM instructions
|
|
102
|
+
* @throws {Error} when no FROM instruction is found or when no FROM lines can be resolved
|
|
103
|
+
*/
|
|
104
|
+
export async function parseAllFromImages(manifestContent) {
|
|
105
|
+
const [parser, fromQuery, argQuery] = await Promise.all([getParser(), getFromQuery(), getArgQuery()]);
|
|
106
|
+
const tree = parser.parse(manifestContent);
|
|
107
|
+
const matches = fromQuery.matches(tree.rootNode);
|
|
108
|
+
if (matches.length === 0) {
|
|
109
|
+
throw new Error('No FROM line found in Dockerfile');
|
|
110
|
+
}
|
|
111
|
+
const args = collectArgs(tree, argQuery);
|
|
112
|
+
const images = [];
|
|
113
|
+
for (const match of matches) {
|
|
114
|
+
const imageSpec = match.captures.find(c => c.name === 'image').node;
|
|
115
|
+
if (containsExpansion(imageSpec)) {
|
|
116
|
+
const resolved = resolveArgs(imageSpec.text, args);
|
|
117
|
+
if (resolved != null) {
|
|
118
|
+
images.push(resolved);
|
|
119
|
+
}
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
images.push(imageSpec.text);
|
|
123
|
+
}
|
|
124
|
+
if (images.length === 0) {
|
|
125
|
+
throw new Error('Dockerfile uses ARG substitution in all FROM lines — cannot resolve variable references');
|
|
126
|
+
}
|
|
127
|
+
return images;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Generate image SBOMs for all FROM instructions in a Dockerfile manifest using syft.
|
|
131
|
+
* Returns a batch result with a purl-to-SBOM map suitable for the batch-analysis endpoint.
|
|
132
|
+
* @param {string} manifest path to the Dockerfile
|
|
133
|
+
* @param {{}} [opts={}] optional various options to pass along the application
|
|
134
|
+
* @returns {Promise<{ecosystem: string, content: string, contentType: string, batch: boolean}>}
|
|
135
|
+
* @private
|
|
136
|
+
*/
|
|
137
|
+
async function getImageSBOM(manifest, opts = {}) {
|
|
138
|
+
const manifestContent = fs.readFileSync(manifest, 'utf-8');
|
|
139
|
+
const images = await parseAllFromImages(manifestContent);
|
|
140
|
+
const sbomByPurl = {};
|
|
141
|
+
for (const image of images) {
|
|
142
|
+
const imageRef = parseImageRef(image, opts);
|
|
143
|
+
const purl = imageRef.getPackageURL().toString();
|
|
144
|
+
if (purl in sbomByPurl)
|
|
145
|
+
continue;
|
|
146
|
+
sbomByPurl[purl] = generateImageSBOM(imageRef, opts);
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
ecosystem,
|
|
150
|
+
content: JSON.stringify(sbomByPurl),
|
|
151
|
+
contentType: 'application/vnd.cyclonedx+json',
|
|
152
|
+
batch: true
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Provide content and content type for Dockerfile component analysis.
|
|
157
|
+
* @param {string} manifest path to the Dockerfile
|
|
158
|
+
* @param {{}} [opts={}] optional various options to pass along the application
|
|
159
|
+
* @returns {Promise<Provided>}
|
|
160
|
+
*/
|
|
161
|
+
async function provideComponent(manifest, opts = {}) {
|
|
162
|
+
return getImageSBOM(manifest, opts);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Provide content and content type for Dockerfile stack analysis.
|
|
166
|
+
* @param {string} manifest path to the Dockerfile
|
|
167
|
+
* @param {{}} [opts={}] optional various options to pass along the application
|
|
168
|
+
* @returns {Promise<Provided>}
|
|
169
|
+
*/
|
|
170
|
+
async function provideStack(manifest, opts = {}) {
|
|
171
|
+
return getImageSBOM(manifest, opts);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Dockerfiles contain no license information.
|
|
175
|
+
* @returns {null} always null
|
|
176
|
+
*/
|
|
177
|
+
function readLicenseFromManifest() { return null; }
|
|
@@ -29,13 +29,13 @@ export default class Yarn_berry_processor extends Yarn_processor {
|
|
|
29
29
|
* @param {Object} depTree - The dependency tree object
|
|
30
30
|
* @returns {Map<string, PackageURL>} Map of dependency names to their PackageURL objects
|
|
31
31
|
*/
|
|
32
|
-
getRootDependencies(depTree:
|
|
32
|
+
getRootDependencies(depTree: Object): Map<string, PackageURL>;
|
|
33
33
|
/**
|
|
34
34
|
* Adds dependencies to the SBOM
|
|
35
35
|
* @param {Sbom} sbom - The SBOM object to add dependencies to
|
|
36
36
|
* @param {Object} depTree - The dependency tree object
|
|
37
37
|
*/
|
|
38
|
-
addDependenciesToSbom(sbom: Sbom, depTree:
|
|
38
|
+
addDependenciesToSbom(sbom: Sbom, depTree: Object): void;
|
|
39
39
|
#private;
|
|
40
40
|
}
|
|
41
41
|
import Yarn_processor from "./yarn_processor.js";
|
|
@@ -15,7 +15,10 @@ export default class Yarn_berry_processor extends Yarn_processor {
|
|
|
15
15
|
* @returns {string[]} Command arguments for listing dependencies
|
|
16
16
|
*/
|
|
17
17
|
listCmdArgs(includeTransitive) {
|
|
18
|
-
|
|
18
|
+
// --all is needed to include workspace members in the output
|
|
19
|
+
return includeTransitive
|
|
20
|
+
? ['info', '--recursive', '--all', '--json']
|
|
21
|
+
: ['info', '--all', '--json'];
|
|
19
22
|
}
|
|
20
23
|
/**
|
|
21
24
|
* Returns the command arguments for updating the lock file
|
|
@@ -68,7 +71,8 @@ export default class Yarn_berry_processor extends Yarn_processor {
|
|
|
68
71
|
if (!name) {
|
|
69
72
|
return false;
|
|
70
73
|
}
|
|
71
|
-
|
|
74
|
+
// Workspace members use paths like "member-a@workspace:packages/member-a", not just "@workspace:."
|
|
75
|
+
return name.startsWith(`${this._manifest.name}@workspace:`);
|
|
72
76
|
}
|
|
73
77
|
/**
|
|
74
78
|
* Adds dependencies to the SBOM
|
|
@@ -25,13 +25,13 @@ export default class Yarn_classic_processor extends Yarn_processor {
|
|
|
25
25
|
* @param {Object} depTree - The dependency tree object
|
|
26
26
|
* @returns {Map<string, PackageURL>} Map of dependency names to their PackageURL objects
|
|
27
27
|
*/
|
|
28
|
-
getRootDependencies(depTree:
|
|
28
|
+
getRootDependencies(depTree: Object): Map<string, PackageURL>;
|
|
29
29
|
/**
|
|
30
30
|
* Adds dependencies to the SBOM
|
|
31
31
|
* @param {Sbom} sbom - The SBOM object to add dependencies to
|
|
32
32
|
* @param {Object} depTree - The dependency tree object
|
|
33
33
|
*/
|
|
34
|
-
addDependenciesToSbom(sbom: Sbom, depTree:
|
|
34
|
+
addDependenciesToSbom(sbom: Sbom, depTree: Object): void;
|
|
35
35
|
#private;
|
|
36
36
|
}
|
|
37
37
|
import Yarn_processor from "./yarn_processor.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** @typedef {{name: string, version: string, dependencies: DependencyEntry[]}} DependencyEntry */
|
|
1
|
+
/** @typedef {{name: string, version: string, dependencies: DependencyEntry[], hashes?: Array<{alg: string, content: string}>}} DependencyEntry */
|
|
2
2
|
export default class Python_controller {
|
|
3
3
|
/**
|
|
4
4
|
* Constructor to create new python controller instance to interact with pip package manager
|
|
@@ -31,4 +31,8 @@ export type DependencyEntry = {
|
|
|
31
31
|
name: string;
|
|
32
32
|
version: string;
|
|
33
33
|
dependencies: DependencyEntry[];
|
|
34
|
+
hashes?: Array<{
|
|
35
|
+
alg: string;
|
|
36
|
+
content: string;
|
|
37
|
+
}>;
|
|
34
38
|
};
|
|
@@ -19,7 +19,54 @@ function getPipShowOutput(depNames) {
|
|
|
19
19
|
throw new Error('fail invoking \'pip show\' to fetch metadata for all installed packages in environment', { cause: error });
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* Get pip inspect output from env var override or by invoking pip inspect.
|
|
24
|
+
* Returns null if pip inspect is unavailable (pip < 22.2) or if freeze/show
|
|
25
|
+
* env vars are set without an inspect override (to avoid inconsistent data).
|
|
26
|
+
* @return {string|null} pip inspect JSON output, or null on failure
|
|
27
|
+
*/
|
|
28
|
+
function getPipInspectOutput() {
|
|
29
|
+
if (environmentVariableIsPopulated("TRUSTIFY_DA_PIP_INSPECT")) {
|
|
30
|
+
return new Buffer.from(process.env["TRUSTIFY_DA_PIP_INSPECT"], 'base64').toString('ascii');
|
|
31
|
+
}
|
|
32
|
+
if (environmentVariableIsPopulated("TRUSTIFY_DA_PIP_FREEZE") || environmentVariableIsPopulated("TRUSTIFY_DA_PIP_SHOW")) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
return invokeCommand(this.pathToPipBin, ['inspect']).toString();
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
console.warn('pip inspect is not available (requires pip 22.2+), SBOM will be generated without hashes');
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Parse pip inspect JSON output and build a hash lookup map.
|
|
45
|
+
* @param {string|null} inspectOutput - raw pip inspect JSON string
|
|
46
|
+
* @return {Map<string, Array<{alg: string, content: string}>>} map of lowercase package name to hashes
|
|
47
|
+
*/
|
|
48
|
+
function parsePipInspectHashes(inspectOutput) {
|
|
49
|
+
if (!inspectOutput) {
|
|
50
|
+
return new Map();
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
let inspectData = JSON.parse(inspectOutput);
|
|
54
|
+
let hashMap = new Map();
|
|
55
|
+
for (let pkg of (inspectData.installed || [])) {
|
|
56
|
+
let name = pkg.metadata?.name;
|
|
57
|
+
let sha256 = pkg.download_info?.archive_info?.hashes?.sha256;
|
|
58
|
+
if (name && sha256) {
|
|
59
|
+
hashMap.set(name.toLowerCase(), [{ alg: "SHA-256", content: sha256 }]);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return hashMap;
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
console.warn('Failed to parse pip inspect output, SBOM will be generated without hashes');
|
|
66
|
+
return new Map();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** @typedef {{name: string, version: string, dependencies: DependencyEntry[], hashes?: Array<{alg: string, content: string}>}} DependencyEntry */
|
|
23
70
|
export default class Python_controller {
|
|
24
71
|
pythonEnvDir;
|
|
25
72
|
pathToPipBin;
|
|
@@ -95,7 +142,7 @@ export default class Python_controller {
|
|
|
95
142
|
}
|
|
96
143
|
/**
|
|
97
144
|
* Parse the requirements.txt file using tree-sitter and return structured requirement data.
|
|
98
|
-
* @return {Promise<{name: string, version: string|null}[]>}
|
|
145
|
+
* @return {Promise<{name: string, version: string|null, hasMarker: boolean}[]>}
|
|
99
146
|
*/
|
|
100
147
|
async #parseRequirements() {
|
|
101
148
|
const content = fs.readFileSync(this.pathToRequirements).toString();
|
|
@@ -107,7 +154,8 @@ export default class Python_controller {
|
|
|
107
154
|
const version = versionMatches.length > 0
|
|
108
155
|
? versionMatches[0].captures.find(c => c.name === 'version').node.text
|
|
109
156
|
: null;
|
|
110
|
-
|
|
157
|
+
const hasMarker = reqNode.children.some(c => c.type === 'marker_spec');
|
|
158
|
+
return { name, version, hasMarker };
|
|
111
159
|
}));
|
|
112
160
|
}
|
|
113
161
|
#decideIfWindowsOrLinuxPath(fileName) {
|
|
@@ -130,8 +178,11 @@ export default class Python_controller {
|
|
|
130
178
|
startingTime = new Date();
|
|
131
179
|
console.log("Starting time to get requirements.txt dependency tree = " + startingTime);
|
|
132
180
|
}
|
|
181
|
+
let installBestEfforts = getCustom("TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS", "false", this.options);
|
|
182
|
+
if (installBestEfforts === "true" && this.realEnvironment) {
|
|
183
|
+
throw new Error("Conflicting settings, TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS=true requires TRUSTIFY_DA_PYTHON_VIRTUAL_ENV=true");
|
|
184
|
+
}
|
|
133
185
|
if (!this.realEnvironment) {
|
|
134
|
-
let installBestEfforts = getCustom("TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS", "false", this.options);
|
|
135
186
|
if (installBestEfforts === "false") {
|
|
136
187
|
try {
|
|
137
188
|
invokeCommand(this.pathToPipBin, ['install', '-r', this.pathToRequirements]);
|
|
@@ -224,7 +275,12 @@ export default class Python_controller {
|
|
|
224
275
|
CachedEnvironmentDeps[packageName.replace("_", "-")] = pipDepTreeEntryForCache;
|
|
225
276
|
});
|
|
226
277
|
}
|
|
227
|
-
|
|
278
|
+
const inspectOutput = getPipInspectOutput.call(this);
|
|
279
|
+
const hashMap = parsePipInspectHashes(inspectOutput);
|
|
280
|
+
parsedRequirements.forEach(({ name: depName, version: manifestVersion, hasMarker }) => {
|
|
281
|
+
if (hasMarker && CachedEnvironmentDeps[depName.toLowerCase()] === undefined) {
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
228
284
|
if (matchManifestVersions === "true" && manifestVersion != null) {
|
|
229
285
|
let installedVersion;
|
|
230
286
|
if (CachedEnvironmentDeps[depName.toLowerCase()] !== undefined) {
|
|
@@ -243,7 +299,7 @@ export default class Python_controller {
|
|
|
243
299
|
}
|
|
244
300
|
let path = [];
|
|
245
301
|
path.push(depName.toLowerCase());
|
|
246
|
-
bringAllDependencies(dependencies, depName, CachedEnvironmentDeps, includeTransitive, path, usePipDepTree);
|
|
302
|
+
bringAllDependencies(dependencies, depName, CachedEnvironmentDeps, includeTransitive, path, usePipDepTree, hashMap);
|
|
247
303
|
});
|
|
248
304
|
dependencies.sort((dep1, dep2) => {
|
|
249
305
|
const DEP1 = dep1.name.toLowerCase();
|
|
@@ -323,8 +379,9 @@ function getDepsList(record) {
|
|
|
323
379
|
* @param includeTransitive
|
|
324
380
|
* @param usePipDepTree
|
|
325
381
|
* @param {[string]}path array representing the path of the current branch in dependency tree, starting with a root dependency - that is - a given dependency in requirements.txt
|
|
382
|
+
* @param {Map<string, Array<{alg: string, content: string}>>} hashMap - map of lowercase package name to hashes
|
|
326
383
|
*/
|
|
327
|
-
function bringAllDependencies(dependencies, dependencyName, cachedEnvironmentDeps, includeTransitive, path, usePipDepTree) {
|
|
384
|
+
function bringAllDependencies(dependencies, dependencyName, cachedEnvironmentDeps, includeTransitive, path, usePipDepTree, hashMap) {
|
|
328
385
|
if (dependencyName?.trim() === "") {
|
|
329
386
|
return;
|
|
330
387
|
}
|
|
@@ -346,7 +403,11 @@ function bringAllDependencies(dependencies, dependencyName, cachedEnvironmentDep
|
|
|
346
403
|
directDeps = record.dependencies;
|
|
347
404
|
}
|
|
348
405
|
let targetDeps = [];
|
|
406
|
+
let hashes = hashMap?.get(depName.toLowerCase());
|
|
349
407
|
let entry = { "name": depName, "version": version, "dependencies": [] };
|
|
408
|
+
if (hashes) {
|
|
409
|
+
entry.hashes = hashes;
|
|
410
|
+
}
|
|
350
411
|
dependencies.push(entry);
|
|
351
412
|
directDeps.forEach((dep) => {
|
|
352
413
|
let depArray = [];
|
|
@@ -356,7 +417,7 @@ function bringAllDependencies(dependencies, dependencyName, cachedEnvironmentDep
|
|
|
356
417
|
depArray.push(dep.toLowerCase());
|
|
357
418
|
if (includeTransitive) {
|
|
358
419
|
// send to recurrsion the array of all deps in path + the current dependency name which is not on the path.
|
|
359
|
-
bringAllDependencies(targetDeps, dep, cachedEnvironmentDeps, includeTransitive, path.concat(depArray), usePipDepTree);
|
|
420
|
+
bringAllDependencies(targetDeps, dep, cachedEnvironmentDeps, includeTransitive, path.concat(depArray), usePipDepTree, hashMap);
|
|
360
421
|
}
|
|
361
422
|
}
|
|
362
423
|
// sort ra
|
|
@@ -4,12 +4,17 @@ declare namespace _default {
|
|
|
4
4
|
export { provideComponent };
|
|
5
5
|
export { provideStack };
|
|
6
6
|
export { readLicenseFromManifest };
|
|
7
|
+
export function packageManagerName(): string;
|
|
7
8
|
}
|
|
8
9
|
export default _default;
|
|
9
10
|
export type DependencyEntry = {
|
|
10
11
|
name: string;
|
|
11
12
|
version: string;
|
|
12
13
|
dependencies: DependencyEntry[];
|
|
14
|
+
hashes?: Array<{
|
|
15
|
+
alg: string;
|
|
16
|
+
content: string;
|
|
17
|
+
}>;
|
|
13
18
|
};
|
|
14
19
|
/**
|
|
15
20
|
* @param {string} manifestName - the subject manifest name-type
|