@trustify-da/trustify-da-javascript-client 0.3.0-ea.e12bc82 → 0.3.0-ea.e258fba

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.
Files changed (97) hide show
  1. package/README.md +367 -13
  2. package/dist/package.json +48 -28
  3. package/dist/src/analysis.d.ts +28 -5
  4. package/dist/src/analysis.js +99 -89
  5. package/dist/src/batch_opts.d.ts +24 -0
  6. package/dist/src/batch_opts.js +35 -0
  7. package/dist/src/cli.js +346 -9
  8. package/dist/src/cyclone_dx_sbom.d.ts +30 -3
  9. package/dist/src/cyclone_dx_sbom.js +95 -9
  10. package/dist/src/index.d.ts +202 -12
  11. package/dist/src/index.js +365 -30
  12. package/dist/src/license/index.d.ts +28 -0
  13. package/dist/src/license/index.js +103 -0
  14. package/dist/src/license/license_utils.d.ts +40 -0
  15. package/dist/src/license/license_utils.js +137 -0
  16. package/dist/src/license/licenses_api.d.ts +34 -0
  17. package/dist/src/license/licenses_api.js +98 -0
  18. package/dist/src/license/project_license.d.ts +20 -0
  19. package/dist/src/license/project_license.js +62 -0
  20. package/dist/src/oci_image/images.d.ts +4 -5
  21. package/dist/src/oci_image/images.js +15 -1
  22. package/dist/src/oci_image/utils.d.ts +7 -7
  23. package/dist/src/oci_image/utils.js +11 -2
  24. package/dist/src/package_version.d.ts +8 -0
  25. package/dist/src/package_version.js +31 -0
  26. package/dist/src/provider.d.ts +18 -5
  27. package/dist/src/provider.js +33 -5
  28. package/dist/src/providers/base_java.d.ts +68 -15
  29. package/dist/src/providers/base_java.js +82 -61
  30. package/dist/src/providers/base_javascript.d.ts +99 -12
  31. package/dist/src/providers/base_javascript.js +243 -27
  32. package/dist/src/providers/base_pyproject.d.ts +158 -0
  33. package/dist/src/providers/base_pyproject.js +322 -0
  34. package/dist/src/providers/containerfile_parser.d.ts +5 -0
  35. package/dist/src/providers/containerfile_parser.js +20 -0
  36. package/dist/src/providers/golang_gomodules.d.ts +30 -13
  37. package/dist/src/providers/golang_gomodules.js +219 -122
  38. package/dist/src/providers/gomod_parser.d.ts +4 -0
  39. package/dist/src/providers/gomod_parser.js +16 -0
  40. package/dist/src/providers/java_gradle.d.ts +76 -3
  41. package/dist/src/providers/java_gradle.js +323 -21
  42. package/dist/src/providers/java_gradle_groovy.d.ts +1 -1
  43. package/dist/src/providers/java_gradle_kotlin.d.ts +1 -1
  44. package/dist/src/providers/java_maven.d.ts +45 -10
  45. package/dist/src/providers/java_maven.js +300 -14
  46. package/dist/src/providers/javascript_bun.d.ts +22 -0
  47. package/dist/src/providers/javascript_bun.js +141 -0
  48. package/dist/src/providers/javascript_npm.d.ts +19 -0
  49. package/dist/src/providers/javascript_npm.js +61 -1
  50. package/dist/src/providers/javascript_pnpm.d.ts +14 -1
  51. package/dist/src/providers/javascript_pnpm.js +55 -5
  52. package/dist/src/providers/javascript_yarn.d.ts +12 -0
  53. package/dist/src/providers/javascript_yarn.js +75 -2
  54. package/dist/src/providers/manifest.d.ts +2 -0
  55. package/dist/src/providers/manifest.js +28 -7
  56. package/dist/src/providers/marker_evaluator.d.ts +14 -0
  57. package/dist/src/providers/marker_evaluator.js +191 -0
  58. package/dist/src/providers/oci_dockerfile.d.ts +51 -0
  59. package/dist/src/providers/oci_dockerfile.js +177 -0
  60. package/dist/src/providers/processors/yarn_berry_processor.d.ts +6 -2
  61. package/dist/src/providers/processors/yarn_berry_processor.js +91 -7
  62. package/dist/src/providers/processors/yarn_classic_processor.d.ts +6 -2
  63. package/dist/src/providers/processors/yarn_classic_processor.js +8 -6
  64. package/dist/src/providers/python_controller.d.ts +10 -3
  65. package/dist/src/providers/python_controller.js +122 -63
  66. package/dist/src/providers/python_pip.d.ts +16 -4
  67. package/dist/src/providers/python_pip.js +51 -58
  68. package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
  69. package/dist/src/providers/python_pip_pyproject.js +146 -0
  70. package/dist/src/providers/python_poetry.d.ts +98 -0
  71. package/dist/src/providers/python_poetry.js +286 -0
  72. package/dist/src/providers/python_uv.d.ts +46 -0
  73. package/dist/src/providers/python_uv.js +269 -0
  74. package/dist/src/providers/requirements_parser.d.ts +6 -0
  75. package/dist/src/providers/requirements_parser.js +24 -0
  76. package/dist/src/providers/rust_cargo.d.ts +56 -0
  77. package/dist/src/providers/rust_cargo.js +682 -0
  78. package/dist/src/providers/tree-sitter-containerfile.wasm +0 -0
  79. package/dist/src/providers/tree-sitter-gomod.wasm +0 -0
  80. package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
  81. package/dist/src/remediate.d.ts +21 -0
  82. package/dist/src/remediate.js +149 -0
  83. package/dist/src/remediation.d.ts +57 -0
  84. package/dist/src/remediation.js +436 -0
  85. package/dist/src/remediation_report.d.ts +40 -0
  86. package/dist/src/remediation_report.js +159 -0
  87. package/dist/src/sbom.d.ts +28 -2
  88. package/dist/src/sbom.js +26 -4
  89. package/dist/src/tools.d.ts +48 -6
  90. package/dist/src/tools.js +138 -2
  91. package/dist/src/updaters/maven_updater.d.ts +26 -0
  92. package/dist/src/updaters/maven_updater.js +322 -0
  93. package/dist/src/updaters/toml_updater.d.ts +33 -0
  94. package/dist/src/updaters/toml_updater.js +244 -0
  95. package/dist/src/workspace.d.ts +70 -0
  96. package/dist/src/workspace.js +256 -0
  97. package/package.json +49 -29
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Generates a formatted report from an array of remediation entries.
3
+ *
4
+ * @param {Array<{purl: string, groupId: string, artifactId: string, currentVersion: string,
5
+ * fixedInVersion: string, fixedInPurl: string, provider: string, source: string,
6
+ * advisories: Array<{id: string, url: string}>, severity: string, cves: string[]}>} remediations
7
+ * @param {object} [options]
8
+ * @param {'dependency'|'bundle'} [options.groupBy='dependency'] - grouping strategy
9
+ * @param {'markdown'|'json'} [options.format='markdown'] - output format
10
+ * @param {boolean} [options.dryRun=false] - when true, produces tabular summary
11
+ * @returns {string}
12
+ */
13
+ export function generateReport(remediations: Array<{
14
+ purl: string;
15
+ groupId: string;
16
+ artifactId: string;
17
+ currentVersion: string;
18
+ fixedInVersion: string;
19
+ fixedInPurl: string;
20
+ provider: string;
21
+ source: string;
22
+ advisories: Array<{
23
+ id: string;
24
+ url: string;
25
+ }>;
26
+ severity: string;
27
+ cves: string[];
28
+ }>, options?: {
29
+ groupBy?: "bundle" | "dependency" | undefined;
30
+ format?: "markdown" | "json" | undefined;
31
+ dryRun?: boolean | undefined;
32
+ }): string;
33
+ /**
34
+ * Generates a deterministic deduplication key for a remediation entry.
35
+ * Suitable for checking existing PR titles or branch names.
36
+ *
37
+ * @param {object} remediation - a single remediation entry
38
+ * @returns {string} stable key in the form `groupId:artifactId:newVersion`
39
+ */
40
+ export function generateDeduplicationKey(remediation: object): string;
@@ -0,0 +1,159 @@
1
+ /**
2
+ * Report generator that transforms remediation extractor output into structured
3
+ * markdown for PR bodies, CLI dry-run output, and JSON.
4
+ */
5
+ import { SEVERITY_ORDER } from './remediation.js';
6
+ /**
7
+ * Generates a formatted report from an array of remediation entries.
8
+ *
9
+ * @param {Array<{purl: string, groupId: string, artifactId: string, currentVersion: string,
10
+ * fixedInVersion: string, fixedInPurl: string, provider: string, source: string,
11
+ * advisories: Array<{id: string, url: string}>, severity: string, cves: string[]}>} remediations
12
+ * @param {object} [options]
13
+ * @param {'dependency'|'bundle'} [options.groupBy='dependency'] - grouping strategy
14
+ * @param {'markdown'|'json'} [options.format='markdown'] - output format
15
+ * @param {boolean} [options.dryRun=false] - when true, produces tabular summary
16
+ * @returns {string}
17
+ */
18
+ export function generateReport(remediations, options = {}) {
19
+ const { groupBy = 'dependency', format = 'markdown', dryRun = false } = options;
20
+ if (!remediations || remediations.length === 0) {
21
+ return format === 'json' ? '[]' : 'No remediations found.';
22
+ }
23
+ if (format === 'json') {
24
+ return JSON.stringify(remediations, null, '\t');
25
+ }
26
+ if (dryRun) {
27
+ return generateDryRunReport(remediations);
28
+ }
29
+ if (groupBy === 'bundle') {
30
+ return generateBundledReport(remediations);
31
+ }
32
+ return generatePerDependencyReport(remediations);
33
+ }
34
+ /**
35
+ * Generates a per-dependency markdown report with one section per remediation entry.
36
+ * @param {Array<object>} remediations
37
+ * @returns {string}
38
+ */
39
+ function generatePerDependencyReport(remediations) {
40
+ const sections = remediations.map(rem => {
41
+ const depName = rem.groupId
42
+ ? `${rem.groupId}:${rem.artifactId}`
43
+ : rem.artifactId;
44
+ const lines = [
45
+ `## Security Update: ${depName} ${rem.currentVersion} → ${rem.fixedInVersion}`,
46
+ '',
47
+ `**Provider:** ${rem.provider} | **Source:** ${rem.source}`,
48
+ '',
49
+ ];
50
+ if (rem.cves && rem.cves.length > 0) {
51
+ lines.push('### Vulnerabilities resolved');
52
+ lines.push('');
53
+ lines.push('| CVE | Severity | Advisory |');
54
+ lines.push('| --- | --- | --- |');
55
+ const advisoryLinks = formatAdvisoryLinks(rem.advisories);
56
+ for (const cve of rem.cves) {
57
+ lines.push(`| ${cve} | ${rem.severity} | ${advisoryLinks} |`);
58
+ }
59
+ }
60
+ return lines.join('\n');
61
+ });
62
+ return sections.join('\n\n');
63
+ }
64
+ /**
65
+ * Generates a bundled markdown report grouping all remediations by severity.
66
+ * @param {Array<object>} remediations
67
+ * @returns {string}
68
+ */
69
+ function generateBundledReport(remediations) {
70
+ const lines = ['# Security Update Summary', ''];
71
+ const bySeverity = groupBySeverity(remediations);
72
+ for (const severity of [...SEVERITY_ORDER].reverse()) {
73
+ const group = bySeverity.get(severity);
74
+ if (!group || group.length === 0) {
75
+ continue;
76
+ }
77
+ lines.push(`## ${severity}`);
78
+ lines.push('');
79
+ lines.push('| Dependency | Current | Fixed | Provider | CVEs | Advisory |');
80
+ lines.push('| --- | --- | --- | --- | --- | --- |');
81
+ for (const rem of group) {
82
+ const depName = rem.groupId
83
+ ? `${rem.groupId}:${rem.artifactId}`
84
+ : rem.artifactId;
85
+ const cves = (rem.cves || []).join(', ');
86
+ const advisoryLinks = formatAdvisoryLinks(rem.advisories);
87
+ lines.push(`| ${depName} | ${rem.currentVersion} | ${rem.fixedInVersion}`
88
+ + ` | ${rem.provider} | ${cves} | ${advisoryLinks} |`);
89
+ }
90
+ lines.push('');
91
+ }
92
+ return lines.join('\n').trimEnd();
93
+ }
94
+ /**
95
+ * Generates a tabular dry-run summary of proposed changes.
96
+ * @param {Array<object>} remediations
97
+ * @returns {string}
98
+ */
99
+ function generateDryRunReport(remediations) {
100
+ const lines = [
101
+ 'Proposed dependency updates:',
102
+ '',
103
+ '| Dependency | Current | Fixed | Severity | Provider |',
104
+ '| --- | --- | --- | --- | --- |',
105
+ ];
106
+ for (const rem of remediations) {
107
+ const depName = rem.groupId
108
+ ? `${rem.groupId}:${rem.artifactId}`
109
+ : rem.artifactId;
110
+ lines.push(`| ${depName} | ${rem.currentVersion} | ${rem.fixedInVersion}`
111
+ + ` | ${rem.severity} | ${rem.provider} |`);
112
+ }
113
+ return lines.join('\n');
114
+ }
115
+ /**
116
+ * Groups remediations by their severity.
117
+ * @param {Array<object>} remediations
118
+ * @returns {Map<string, Array<object>>}
119
+ */
120
+ function groupBySeverity(remediations) {
121
+ const map = new Map();
122
+ for (const severity of SEVERITY_ORDER) {
123
+ map.set(severity, []);
124
+ }
125
+ for (const rem of remediations) {
126
+ const sev = rem.severity || 'UNKNOWN';
127
+ if (!map.has(sev)) {
128
+ map.set(sev, []);
129
+ }
130
+ map.get(sev).push(rem);
131
+ }
132
+ return map;
133
+ }
134
+ /**
135
+ * Formats advisory entries into markdown links or plain text.
136
+ * @param {Array<{id: string, url: string}>} advisories
137
+ * @returns {string}
138
+ */
139
+ function formatAdvisoryLinks(advisories) {
140
+ if (!advisories || advisories.length === 0) {
141
+ return '-';
142
+ }
143
+ return advisories
144
+ .map(adv => adv.url ? `[${adv.id}](${adv.url})` : adv.id)
145
+ .join(', ');
146
+ }
147
+ /**
148
+ * Generates a deterministic deduplication key for a remediation entry.
149
+ * Suitable for checking existing PR titles or branch names.
150
+ *
151
+ * @param {object} remediation - a single remediation entry
152
+ * @returns {string} stable key in the form `groupId:artifactId:newVersion`
153
+ */
154
+ export function generateDeduplicationKey(remediation) {
155
+ const group = remediation.groupId || '';
156
+ const artifact = remediation.artifactId || '';
157
+ const version = remediation.fixedInVersion || '';
158
+ return `${group}:${artifact}:${version}`;
159
+ }
@@ -2,9 +2,10 @@ export default class Sbom {
2
2
  sbomModel: CycloneDxSbom;
3
3
  /**
4
4
  * @param {PackageURL} root - add main/root component for sbom
5
+ * @param {string|Array} [licenses] - optional license(s) for the root component
5
6
  * @return Sbom
6
7
  */
7
- addRoot(root: PackageURL): CycloneDxSbom;
8
+ addRoot(root: PackageURL, licenses?: string | any[]): CycloneDxSbom;
8
9
  /**
9
10
  * @return {{{"bom-ref": string, name, purl: string, type, version}}} root component of sbom.
10
11
  */
@@ -24,9 +25,25 @@ export default class Sbom {
24
25
  /**
25
26
  * @param {component} sourceRef current source Component ( Starting from root component by clients)
26
27
  * @param {PackageURL} targetRef current dependency to add to Dependencies list of component sourceRef
28
+ * @param {string} [scope] - Scope of the dependency
29
+ * @param {Array<{alg: string, content: string}>} [targetHashes] - Optional hashes for the target component
27
30
  * @return Sbom
28
31
  */
29
- addDependency(sourceRef: component, targetRef: PackageURL, scope: any): CycloneDxSbom;
32
+ addDependency(sourceRef: component, targetRef: PackageURL, scope?: string, targetHashes?: Array<{
33
+ alg: string;
34
+ content: string;
35
+ }>): CycloneDxSbom;
36
+ /**
37
+ * Attach hashes to existing components by matching their PURL. Post-processing
38
+ * step used by ecosystem-specific providers (e.g. Maven) to enrich the SBOM
39
+ * with artifact hashes without leaking their concern into the shared parser.
40
+ * @param {Map<string, Array<{alg: string, content: string}>>} hashMap - PURL→hashes map
41
+ * @return {Sbom}
42
+ */
43
+ attachHashes(hashMap: Map<string, Array<{
44
+ alg: string;
45
+ content: string;
46
+ }>>): Sbom;
30
47
  /**
31
48
  * @return String sbom json in a string format
32
49
  */
@@ -43,6 +60,8 @@ export default class Sbom {
43
60
  type: any;
44
61
  version: any;
45
62
  scope: any;
63
+ licenses?: any;
64
+ hashes?: any;
46
65
  };
47
66
  /** This method gets a component object, and a string name, and checks if the name is a substring of the component' purl.
48
67
  * @param {} component to search in its dependencies
@@ -51,6 +70,13 @@ export default class Sbom {
51
70
  * @return {boolean}
52
71
  */
53
72
  checkIfPackageInsideDependsOnList(component: any, name: string): boolean;
73
+ /**
74
+ * Checks if any entry in the dependsOn list of sourceRef starts with the given purl prefix.
75
+ * @param {PackageURL} sourceRef - The source component to check
76
+ * @param {string} purlPrefix - The purl prefix to match (e.g. "pkg:npm/minimist@")
77
+ * @return {boolean}
78
+ */
79
+ checkDependsOnByPurlPrefix(sourceRef: PackageURL, purlPrefix: string): boolean;
54
80
  /** Removes the root component from the sbom
55
81
  */
56
82
  removeRootComponent(): void;
package/dist/src/sbom.js CHANGED
@@ -12,10 +12,11 @@ export default class Sbom {
12
12
  }
13
13
  /**
14
14
  * @param {PackageURL} root - add main/root component for sbom
15
+ * @param {string|Array} [licenses] - optional license(s) for the root component
15
16
  * @return Sbom
16
17
  */
17
- addRoot(root) {
18
- return this.sbomModel.addRoot(root);
18
+ addRoot(root, licenses) {
19
+ return this.sbomModel.addRoot(root, licenses);
19
20
  }
20
21
  /**
21
22
  * @return {{{"bom-ref": string, name, purl: string, type, version}}} root component of sbom.
@@ -42,10 +43,22 @@ export default class Sbom {
42
43
  /**
43
44
  * @param {component} sourceRef current source Component ( Starting from root component by clients)
44
45
  * @param {PackageURL} targetRef current dependency to add to Dependencies list of component sourceRef
46
+ * @param {string} [scope] - Scope of the dependency
47
+ * @param {Array<{alg: string, content: string}>} [targetHashes] - Optional hashes for the target component
45
48
  * @return Sbom
46
49
  */
47
- addDependency(sourceRef, targetRef, scope) {
48
- return this.sbomModel.addDependency(sourceRef, targetRef, scope);
50
+ addDependency(sourceRef, targetRef, scope, targetHashes) {
51
+ return this.sbomModel.addDependency(sourceRef, targetRef, scope, targetHashes);
52
+ }
53
+ /**
54
+ * Attach hashes to existing components by matching their PURL. Post-processing
55
+ * step used by ecosystem-specific providers (e.g. Maven) to enrich the SBOM
56
+ * with artifact hashes without leaking their concern into the shared parser.
57
+ * @param {Map<string, Array<{alg: string, content: string}>>} hashMap - PURL→hashes map
58
+ * @return {Sbom}
59
+ */
60
+ attachHashes(hashMap) {
61
+ return this.sbomModel.attachHashes(hashMap);
49
62
  }
50
63
  /**
51
64
  * @return String sbom json in a string format
@@ -76,6 +89,15 @@ export default class Sbom {
76
89
  checkIfPackageInsideDependsOnList(component, name) {
77
90
  return this.sbomModel.checkIfPackageInsideDependsOnList(component, name);
78
91
  }
92
+ /**
93
+ * Checks if any entry in the dependsOn list of sourceRef starts with the given purl prefix.
94
+ * @param {PackageURL} sourceRef - The source component to check
95
+ * @param {string} purlPrefix - The purl prefix to match (e.g. "pkg:npm/minimist@")
96
+ * @return {boolean}
97
+ */
98
+ checkDependsOnByPurlPrefix(sourceRef, purlPrefix) {
99
+ return this.sbomModel.checkDependsOnByPurlPrefix(sourceRef, purlPrefix);
100
+ }
79
101
  /** Removes the root component from the sbom
80
102
  */
81
103
  removeRootComponent() {
@@ -1,12 +1,10 @@
1
- /// <reference types="node" />
2
- /// <reference types="packageurl-js/src/package-url" />
3
1
  /**
4
2
  *
5
3
  * @param {string} key to log its value from environment variables and from opts, if it exists
6
4
  * @param {{}} [opts={}] different options of application, if key in it, log it.
7
5
  * @param {string }defValue default value of key in case there is no option and environment variable values for key
8
6
  */
9
- export function logValueFromObjects(key: string, opts?: {} | undefined, defValue: string): void;
7
+ export function logValueFromObjects(key: string, opts?: {}, defValue: string): void;
10
8
  /**
11
9
  * Utility function will return the value for key from the environment variables,
12
10
  * if not present will return the value for key from the opts objects only if it's a string,
@@ -17,7 +15,7 @@ export function logValueFromObjects(key: string, opts?: {} | undefined, defValue
17
15
  * @returns {string|null} the value of the key found in the environment, options object, or the
18
16
  * default supplied
19
17
  */
20
- export function getCustom(key: string, def?: string | null | undefined, opts?: {} | undefined): string | null;
18
+ export function getCustom(key: string, def?: string | null, opts?: {}): string | null;
21
19
  /**
22
20
  * Utility function for looking up custom variable for a binary path.
23
21
  * Will look in the environment variables (1) or in opts (2) for a key with TRUSTIFY_DA_x_PATH, x is an
@@ -28,7 +26,7 @@ export function getCustom(key: string, def?: string | null | undefined, opts?: {
28
26
  * @returns {string|null} the value of the key found in the environment, options object, or the
29
27
  * original name supplied
30
28
  */
31
- export function getCustomPath(name: any, opts?: {} | undefined): string | null;
29
+ export function getCustomPath(name: any, opts?: {}): string | null;
32
30
  /**
33
31
  * Utility function for determining whether wrappers for build tools such as gradlew/mvnw should be
34
32
  * preferred over invoking the binary directly.
@@ -63,12 +61,56 @@ export function toPurlFromString(strPurl: any): PackageURL | null;
63
61
  * @param {string} cwd - directory for which to find the root of the git repository.
64
62
  */
65
63
  export function getGitRootDir(cwd: string): string | undefined;
64
+ /**
65
+ * Normalize a filesystem path, lowercasing on Windows for case-insensitive comparison.
66
+ *
67
+ * @param {string} thePath
68
+ * @returns {string}
69
+ */
70
+ export function normalizePath(thePath: string): string;
71
+ /**
72
+ * Walk up from `startDir` to `repoRoot` looking for an executable wrapper script.
73
+ *
74
+ * @param {string} startDir - Absolute directory to start from
75
+ * @param {string} wrapperName - Wrapper filename (e.g. `mvnw`, `gradlew`)
76
+ * @param {string} [repoRoot] - Stop boundary (defaults to git root or filesystem root)
77
+ * @returns {string | undefined}
78
+ */
79
+ export function traverseForWrapper(startDir: string, wrapperName: string, repoRoot?: string): string | undefined;
80
+ /**
81
+ * Resolve a build-tool binary, preferring a wrapper when configured.
82
+ *
83
+ * @param {string} globalBinary - Global binary name (e.g. `mvn`, `gradle`)
84
+ * @param {string} localWrapper - Wrapper filename (e.g. `mvnw`, `gradlew.bat`)
85
+ * @param {string} startDir - Directory from which to start the wrapper search
86
+ * @param {import('./index.js').Options} [opts={}]
87
+ * @returns {string} Path to the resolved binary
88
+ */
89
+ export function resolveBinary(globalBinary: string, localWrapper: string, startDir: string, opts?: import("./index.js").Options): string;
66
90
  /** this method invokes command string in a process in a synchronous way.
67
91
  * @param {string} bin - the command to be invoked
68
92
  * @param {Array<string>} args - the args to pass to the binary
69
93
  * @param {import('child_process').ExecFileOptionsWithStringEncoding} [opts={}]
70
94
  * @returns {string}
71
95
  */
72
- export function invokeCommand(bin: string, args: Array<string>, opts?: import("child_process").ExecFileOptionsWithStringEncoding | undefined): string;
96
+ export function invokeCommand(bin: string, args: Array<string>, opts?: import("child_process").ExecFileOptionsWithStringEncoding): string;
97
+ /**
98
+ * Adds proxy agent configuration to fetch options if a proxy URL is specified
99
+ * @param {RequestInit} options - The base fetch options
100
+ * @param {import("index.js").Options} opts - The trustify DA options that may contain proxy configuration
101
+ * @returns {RequestInit} The fetch options with proxy agent if applicable
102
+ */
103
+ export function addProxyAgent(options: RequestInit, opts: any): RequestInit;
104
+ /**
105
+ * Utility function for fetching vendor tokens
106
+ * @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
107
+ * @returns {{}}
108
+ */
109
+ export function getTokenHeaders(opts?: any): {};
73
110
  export const RegexNotToBeLogged: RegExp;
111
+ export const TRUSTIFY_DA_TOKEN_HEADER: "trust-da-token";
112
+ export const TRUSTIFY_DA_TELEMETRY_ID_HEADER: "telemetry-anonymous-id";
113
+ export const TRUSTIFY_DA_SOURCE_HEADER: "trust-da-source";
114
+ export const TRUSTIFY_DA_OPERATION_TYPE_HEADER: "trust-da-operation-type";
115
+ export const TRUSTIFY_DA_PACKAGE_MANAGER_HEADER: "trust-da-pkg-manager";
74
116
  import { PackageURL } from "packageurl-js";
package/dist/src/tools.js CHANGED
@@ -1,7 +1,10 @@
1
1
  import { execFileSync } from "child_process";
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
2
4
  import { EOL } from "os";
5
+ import { HttpsProxyAgent } from "https-proxy-agent";
3
6
  import { PackageURL } from "packageurl-js";
4
- export const RegexNotToBeLogged = /TRUSTIFY_DA_.*_TOKEN|ex-.*-token/;
7
+ export const RegexNotToBeLogged = /TRUSTIFY_DA_(.*_)?TOKEN|ex-.*-token|trust-.*-token/;
5
8
  /**
6
9
  *
7
10
  * @param {string} key to log its value from environment variables and from opts, if it exists
@@ -39,6 +42,28 @@ export function getCustom(key, def = null, opts = {}) {
39
42
  }
40
43
  return key in process.env ? process.env[key] : key in opts && typeof opts[key] === 'string' ? opts[key] : def;
41
44
  }
45
+ /**
46
+ * Validates that an executable path does not use directory traversal or relative segments.
47
+ * @param {string} binPath - The executable path to validate.
48
+ * @returns {string} The validated path.
49
+ * @throws {Error} If the path contains '..' segments or is a relative path with separators.
50
+ */
51
+ function validateExecutablePath(binPath) {
52
+ if (typeof binPath !== 'string' || binPath.length === 0) {
53
+ throw new Error('Executable path rejected: expected a non-empty string');
54
+ }
55
+ if (binPath.startsWith('./') || binPath.startsWith('.\\')) {
56
+ throw new Error(`Executable path rejected: relative paths starting with './' are not allowed: ${binPath}`);
57
+ }
58
+ const segments = binPath.split(/[/\\]/);
59
+ if (segments.includes('..')) {
60
+ throw new Error(`Executable path rejected: path contains directory traversal segment (..): ${binPath}`);
61
+ }
62
+ if ((binPath.includes('/') || binPath.includes('\\')) && !path.isAbsolute(binPath)) {
63
+ throw new Error(`Executable path rejected: relative paths are not allowed, use an absolute path or a bare command name: ${binPath}`);
64
+ }
65
+ return binPath;
66
+ }
42
67
  /**
43
68
  * Utility function for looking up custom variable for a binary path.
44
69
  * Will look in the environment variables (1) or in opts (2) for a key with TRUSTIFY_DA_x_PATH, x is an
@@ -50,7 +75,8 @@ export function getCustom(key, def = null, opts = {}) {
50
75
  * original name supplied
51
76
  */
52
77
  export function getCustomPath(name, opts = {}) {
53
- return getCustom(`TRUSTIFY_DA_${name.toUpperCase()}_PATH`, name, opts);
78
+ const resolvedPath = getCustom(`TRUSTIFY_DA_${name.toUpperCase()}_PATH`, name, opts);
79
+ return validateExecutablePath(resolvedPath);
54
80
  }
55
81
  /**
56
82
  * Utility function for determining whether wrappers for build tools such as gradlew/mvnw should be
@@ -127,6 +153,62 @@ export function getGitRootDir(cwd) {
127
153
  return undefined;
128
154
  }
129
155
  }
156
+ /**
157
+ * Normalize a filesystem path, lowercasing on Windows for case-insensitive comparison.
158
+ *
159
+ * @param {string} thePath
160
+ * @returns {string}
161
+ */
162
+ export function normalizePath(thePath) {
163
+ const normalized = path.resolve(thePath).normalize();
164
+ return process.platform === 'win32' ? normalized.toLowerCase() : normalized;
165
+ }
166
+ /**
167
+ * Walk up from `startDir` to `repoRoot` looking for an executable wrapper script.
168
+ *
169
+ * @param {string} startDir - Absolute directory to start from
170
+ * @param {string} wrapperName - Wrapper filename (e.g. `mvnw`, `gradlew`)
171
+ * @param {string} [repoRoot] - Stop boundary (defaults to git root or filesystem root)
172
+ * @returns {string | undefined}
173
+ */
174
+ export function traverseForWrapper(startDir, wrapperName, repoRoot = undefined) {
175
+ const currentDir = normalizePath(startDir);
176
+ repoRoot = repoRoot || getGitRootDir(currentDir) || path.parse(currentDir).root;
177
+ const wrapperPath = path.join(currentDir, wrapperName);
178
+ try {
179
+ fs.accessSync(wrapperPath, fs.constants.X_OK);
180
+ return wrapperPath;
181
+ }
182
+ catch {
183
+ const rootDir = path.parse(currentDir).root;
184
+ if (currentDir === repoRoot || currentDir === rootDir) {
185
+ return undefined;
186
+ }
187
+ const parentDir = path.dirname(currentDir);
188
+ if (parentDir === currentDir || parentDir === rootDir) {
189
+ return undefined;
190
+ }
191
+ return traverseForWrapper(parentDir, wrapperName, repoRoot);
192
+ }
193
+ }
194
+ /**
195
+ * Resolve a build-tool binary, preferring a wrapper when configured.
196
+ *
197
+ * @param {string} globalBinary - Global binary name (e.g. `mvn`, `gradle`)
198
+ * @param {string} localWrapper - Wrapper filename (e.g. `mvnw`, `gradlew.bat`)
199
+ * @param {string} startDir - Directory from which to start the wrapper search
200
+ * @param {import('./index.js').Options} [opts={}]
201
+ * @returns {string} Path to the resolved binary
202
+ */
203
+ export function resolveBinary(globalBinary, localWrapper, startDir, opts = {}) {
204
+ if (getWrapperPreference(globalBinary, opts)) {
205
+ const wrapper = traverseForWrapper(startDir, localWrapper);
206
+ if (wrapper !== undefined) {
207
+ return wrapper;
208
+ }
209
+ }
210
+ return getCustomPath(globalBinary, opts);
211
+ }
130
212
  /** this method invokes command string in a process in a synchronous way.
131
213
  * @param {string} bin - the command to be invoked
132
214
  * @param {Array<string>} args - the args to pass to the binary
@@ -157,3 +239,57 @@ export function invokeCommand(bin, args, opts = {}) {
157
239
  };
158
240
  return execFileSync(bin, args, { ...{ stdio: 'pipe', encoding: 'utf-8' }, ...opts });
159
241
  }
242
+ export const TRUSTIFY_DA_TOKEN_HEADER = "trust-da-token";
243
+ export const TRUSTIFY_DA_TELEMETRY_ID_HEADER = "telemetry-anonymous-id";
244
+ export const TRUSTIFY_DA_SOURCE_HEADER = "trust-da-source";
245
+ export const TRUSTIFY_DA_OPERATION_TYPE_HEADER = "trust-da-operation-type";
246
+ export const TRUSTIFY_DA_PACKAGE_MANAGER_HEADER = "trust-da-pkg-manager";
247
+ /**
248
+ * Adds proxy agent configuration to fetch options if a proxy URL is specified
249
+ * @param {RequestInit} options - The base fetch options
250
+ * @param {import("index.js").Options} opts - The trustify DA options that may contain proxy configuration
251
+ * @returns {RequestInit} The fetch options with proxy agent if applicable
252
+ */
253
+ export function addProxyAgent(options, opts) {
254
+ const proxyUrl = getCustom('TRUSTIFY_DA_PROXY_URL', null, opts);
255
+ if (proxyUrl) {
256
+ options.agent = new HttpsProxyAgent(proxyUrl);
257
+ }
258
+ return options;
259
+ }
260
+ /**
261
+ * Utility function for fetching vendor tokens
262
+ * @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
263
+ * @returns {{}}
264
+ */
265
+ export function getTokenHeaders(opts = {}) {
266
+ let headers = {};
267
+ setCustomHeader(TRUSTIFY_DA_TOKEN_HEADER, headers, 'TRUSTIFY_DA_TOKEN', opts);
268
+ setCustomHeader(TRUSTIFY_DA_SOURCE_HEADER, headers, 'TRUSTIFY_DA_SOURCE', opts);
269
+ setCustomHeader(TRUSTIFY_DA_OPERATION_TYPE_HEADER, headers, TRUSTIFY_DA_OPERATION_TYPE_HEADER.toUpperCase().replaceAll("-", "_"), opts);
270
+ setCustomHeader(TRUSTIFY_DA_PACKAGE_MANAGER_HEADER, headers, TRUSTIFY_DA_PACKAGE_MANAGER_HEADER.toUpperCase().replaceAll("-", "_"), opts);
271
+ setCustomHeader(TRUSTIFY_DA_TELEMETRY_ID_HEADER, headers, 'TRUSTIFY_DA_TELEMETRY_ID', opts);
272
+ if (getCustom("TRUSTIFY_DA_DEBUG", null, opts) === "true") {
273
+ console.log("Headers Values to be sent to Trustify DA backend:" + EOL);
274
+ for (const headerKey in headers) {
275
+ if (!headerKey.match(RegexNotToBeLogged)) {
276
+ console.log(`${headerKey}: ${headers[headerKey]}`);
277
+ }
278
+ }
279
+ }
280
+ return headers;
281
+ }
282
+ /**
283
+ *
284
+ * @param {string} headerName - the header name to populate in request
285
+ * @param headers
286
+ * @param {string} optsKey - key in the options object to use the value for
287
+ * @param {import("index.js").Options} [opts={}] - options input object to fetch header values from
288
+ * @private
289
+ */
290
+ function setCustomHeader(headerName, headers, optsKey, opts) {
291
+ let customHeaderValue = getCustom(optsKey, null, opts);
292
+ if (customHeaderValue) {
293
+ headers[headerName] = customHeaderValue;
294
+ }
295
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Updates dependency versions in a Maven pom.xml while preserving file formatting.
3
+ *
4
+ * Uses `fast-xml-parser` to understand the XML structure (dependencies and properties),
5
+ * then performs position-based string splice to replace version strings without altering
6
+ * any other formatting, comments, whitespace, or indentation.
7
+ *
8
+ * Supports `${property}` indirection: when a dependency's `<version>` is a property
9
+ * reference, the updater resolves the property chain (including recursive references)
10
+ * and updates the terminal property value in `<properties>` instead.
11
+ *
12
+ * @param {string} pomContent - the raw pom.xml file content
13
+ * @param {Array<{groupId: string, artifactId: string, newVersion: string}>} versionChanges -
14
+ * list of version changes to apply
15
+ * @returns {{content: string, applied: Array, skipped: Array}} the updated content and
16
+ * lists of applied and skipped changes
17
+ */
18
+ export function updateMavenVersions(pomContent: string, versionChanges: Array<{
19
+ groupId: string;
20
+ artifactId: string;
21
+ newVersion: string;
22
+ }>): {
23
+ content: string;
24
+ applied: any[];
25
+ skipped: any[];
26
+ };