diff --git a/README.md b/README.md index 1ed9c2c6..53bef801 100644 --- a/README.md +++ b/README.md @@ -863,7 +863,7 @@ The remediate command scans manifest files for known vulnerabilitie

Supported Manifest Formats

CLI Flags

@@ -943,7 +943,7 @@ When multiple vulnerability providers suggest different remediation versions for

Known Limitations

diff --git a/src/providers/java_gradle.js b/src/providers/java_gradle.js index 147b5203..5c9fbc71 100644 --- a/src/providers/java_gradle.js +++ b/src/providers/java_gradle.js @@ -19,8 +19,8 @@ import Base_java, { ecosystem_gradle } from "./base_java.js"; /** @typedef {import('../provider.js').Provided} Provided */ -const ROOT_PROJECT_KEY_NAME = "root-project"; - +const BUILD_FILE_NAMES = ['build.gradle.kts', 'build.gradle'] +const MAX_BUILD_FILE_SEARCH_DEPTH = 5 const TRUSTIFY_DA_IGNORE_REGEX_LINE = /.*\s?(exhortignore|trustify-da-ignore)\s*$/g const TRUSTIFY_DA_IGNORE_REGEX = /\/\/\s?(exhortignore|trustify-da-ignore)/ @@ -40,6 +40,28 @@ function stripString(depPart) { return depPart.replaceAll(/["']/g,"") } +/** + * Walk from `startDir` upward looking for a Gradle build file. Bounded so a scan + * starting outside a Gradle project cannot escape to the filesystem root. + * @param {string} startDir + * @returns {string|undefined} absolute path to the nearest build file, if any + */ +export function findNearestBuildFile(startDir) { + let dir = startDir + for (let depth = 0; depth < MAX_BUILD_FILE_SEARCH_DEPTH; depth++) { + for (const name of BUILD_FILE_NAMES) { + const candidate = path.join(dir, name) + if (fs.existsSync(candidate)) { + return candidate + } + } + const parent = path.dirname(dir) + if (parent === dir) { break } + dir = parent + } + return undefined +} + /** * This class provides common functionality for Groovy and Kotlin DSL files. */ @@ -75,30 +97,7 @@ export default class Java_gradle extends Base_java { * @returns {boolean} */ validateLockFile(manifestDir) { - return this.#findBuildFile(manifestDir) !== undefined - } - - /** - * Walk from `startDir` upward looking for a Gradle build file. - * Stops after 5 levels to avoid scanning to the filesystem root. - * @param {string} startDir - * @returns {string|undefined} absolute path to the build file, or undefined - */ - #findBuildFile(startDir) { - const maxDepth = 5 - let dir = startDir - for (let i = 0; i < maxDepth; i++) { - for (const name of ['build.gradle.kts', 'build.gradle']) { - const candidate = path.join(dir, name) - if (fs.existsSync(candidate)) { - return candidate - } - } - const parent = path.dirname(dir) - if (parent === dir) { break } - dir = parent - } - return undefined + return findNearestBuildFile(manifestDir) !== undefined } /** @@ -127,7 +126,7 @@ export default class Java_gradle extends Base_java { if (!path.basename(manifest).endsWith('.versions.toml')) { return manifest } - const found = this.#findBuildFile(path.dirname(manifest)) + const found = findNearestBuildFile(path.dirname(manifest)) if (!found) { throw new Error(`No parent build.gradle found for ${path.basename(manifest)}`) } @@ -200,7 +199,9 @@ export default class Java_gradle extends Base_java { line: `${processedLine}:compile`, depth: depth }; - }); + }) + // Depth -1 marks lines that are not part of a dependency tree (e.g. "No dependencies"). + .filter(entry => entry.depth > 0); } /** @@ -242,11 +243,8 @@ export default class Java_gradle extends Base_java { } } - #buildRootCoordinate(properties) { - const group = properties.group || 'unknown' - const version = properties.version || '0.0.0' - const rootName = properties[ROOT_PROJECT_KEY_NAME]?.match(/Root project '(.+)'/)?.[1] || 'unknown' - return `${group}:${rootName}:jar:${version}` + #buildRootCoordinate({ group, name, version }) { + return `${group || 'unknown'}:${name || 'unknown'}:jar:${version || '0.0.0'}` } /** @@ -294,24 +292,22 @@ export default class Java_gradle extends Base_java { } /** - * - * @param {string} manifestPath - path to build.gradle. - * @param {Object} opts - contains various options settings from client. - * @return {{Object}} an object that contains all gradle properties + * Reads the identity of the project that `gradle properties` was run in. + * `name` is the current project, so a subproject build file yields the + * subproject's identity rather than the root project's. + * @param {string} manifestPath - path to build.gradle[.kts] + * @param {Object} opts - client options + * @returns {{group: string|undefined, name: string|undefined, version: string|undefined}} */ #extractProperties(manifestPath, opts) { - let properties = {} - let propertiesContent = this.#getProperties(manifestPath, opts) - let regExpMatchArray = propertiesContent.match(/([^\n:]+):[\t ]*(.*)/g); - for (let i = 0; i < regExpMatchArray.length - 1; i++) { - let parts = regExpMatchArray[i].split(":"); - properties[parts[0].trim()] = parts[1].trim() - } - let regExpMatchArray1 = propertiesContent.match(/Root project '(.+)'/); - if (regExpMatchArray1[0]) { - properties[ROOT_PROJECT_KEY_NAME] = regExpMatchArray1[0] + const output = this.#getProperties(manifestPath, opts) + const readProperty = key => output.match(new RegExp(`^${key}:[ \\t]*(.*)$`, 'm'))?.[1].trim() + + return { + group: readProperty('group'), + name: readProperty('name'), + version: readProperty('version'), } - return properties; } /** diff --git a/src/remediate.js b/src/remediate.js index 37b4a1d2..ae71d6d5 100644 --- a/src/remediate.js +++ b/src/remediate.js @@ -6,6 +6,7 @@ import { PackageURL } from 'packageurl-js' import analysis from './analysis.js' import { availableProviders, match } from './provider.js' +import { discoverGradleSubprojects, findNearestBuildFile } from './providers/java_gradle.js' import { extractRemediations, compareVersions } from './remediation.js' import { mavenChangeKey, mavenDepsForEditSite, updateMavenVersions } from './updaters/maven_updater.js' import { tomlChangeKey, tomlDepsForEditSite, updateTomlVersions } from './updaters/toml_updater.js' @@ -203,6 +204,33 @@ export function findManifests(targetPath) { return manifests } +/** + * Resolves which manifests must be analyzed to produce remediations for `manifestPath`. + * + * Maven manifests are analyzed as-is. A Gradle version catalog is shared by every project + * in its build, while `gradle dependencies` only reports the project it runs in, so the + * catalog's owning build is expanded to all of its projects. Falls back to analyzing the + * catalog itself when the build cannot be discovered (no settings file) or has no projects. + * + * @param {string} manifestPath - the manifest being remediated + * @param {ManifestType} manifestType + * @param {Object} opts - client options + * @returns {Promise} manifests to analyze + */ +async function analysisPathsFor(manifestPath, manifestType, opts) { + if (manifestType.label !== 'toml') { + return [manifestPath] + } + + const buildFile = findNearestBuildFile(path.dirname(manifestPath)) + if (!buildFile) { + return [manifestPath] + } + + const projects = await discoverGradleSubprojects(path.dirname(buildFile), opts) + return projects.length > 0 ? projects : [manifestPath] +} + /** * Resolves a version conflict among candidates. Default: highest version wins. * @param {string[]} versions @@ -289,17 +317,29 @@ export async function runRemediation(targetPath, options = {}) { continue } - let provider - try { - provider = match(manifestPath, availableProviders, opts) - } catch { - continue - } + const providerPriority = providers ? providers.split(',').map(p => p.trim()).filter(Boolean) : undefined + const analysisPaths = await analysisPathsFor(manifestPath, manifestType, opts) + + // A dependency visible from several projects yields one remediation per analysis. + // Keep a single entry per purl so it is reported and edited once. + /** @type {Map} */ + const remediationsByPurl = new Map() + for (const analysisPath of analysisPaths) { + let provider + try { + provider = match(analysisPath, availableProviders, opts) + } catch { + continue + } - const analysisReport = await analysis.requestStack(provider, manifestPath, url, false, opts) - const extracted = extractRemediations(analysisReport, { - providerPriority: providers ? providers.split(',').map(p => p.trim()).filter(Boolean) : undefined, - }) + const analysisReport = await analysis.requestStack(provider, analysisPath, url, false, opts) + for (const remediation of extractRemediations(analysisReport, { providerPriority })) { + if (!remediationsByPurl.has(remediation.purl)) { + remediationsByPurl.set(remediation.purl, remediation) + } + } + } + const extracted = [...remediationsByPurl.values()].sort((a, b) => a.purl.localeCompare(b.purl)) let remediations = excludeMatchers.length === 0 ? extracted @@ -333,7 +373,9 @@ export async function runRemediation(targetPath, options = {}) { groupId: remediation.groupId, artifactId: remediation.artifactId, newVersion: remediation.fixedInVersion, - reason: 'Dependency not directly declared in the manifest', + reason: manifestType.label === 'toml' + ? 'Dependency not declared in the version catalog' + : 'Dependency not directly declared in the manifest', }) continue } diff --git a/test/providers/java_gradle_groovy.test.js b/test/providers/java_gradle_groovy.test.js index 5144b9bc..a0d22d10 100644 --- a/test/providers/java_gradle_groovy.test.js +++ b/test/providers/java_gradle_groovy.test.js @@ -139,6 +139,26 @@ suite('testing the java-gradle-groovy data provider', () => { }).timeout(process.env.GITHUB_ACTIONS ? 40000 : 10000) }) + suite('subproject and empty dependency trees', () => { + [ + { name: 'subproject build file', scenario: 'gradle_multi_project/app' }, + { name: 'empty dependency blocks', scenario: 'empty_dependencies' }, + ].forEach(({ name, scenario }) => { + test(`verify gradle data provided for stack analysis with scenario ${name}`, async () => { + const dir = `test/providers/tst_manifests/gradle/${scenario}` + const expectedSbom = fs.readFileSync(`${dir}/expected_stack_sbom.json`).toString().trim() + const depTree = fs.readFileSync(`${dir}/depTree.txt`).toString() + const props = fs.readFileSync(`${dir}/gradle.properties`).toString() + const provider = new Java_gradle_groovy() + mockInvokeCommand(provider, depTree, props, '') + + const provided = await provider.provideStack(`${dir}/build.gradle`) + + expect(JSON.parse(provided.content)).to.deep.equal(JSON.parse(expectedSbom)) + }).timeout(10000) + }) + }) + suite('artifact hash computation', () => { const testCase = 'deps_with_no_ignore_common_paths' const dir = `test/providers/tst_manifests/gradle/${testCase}` diff --git a/test/providers/tst_manifests/gradle/empty_dependencies/build.gradle b/test/providers/tst_manifests/gradle/empty_dependencies/build.gradle new file mode 100644 index 00000000..12ebf27d --- /dev/null +++ b/test/providers/tst_manifests/gradle/empty_dependencies/build.gradle @@ -0,0 +1,8 @@ +plugins { id 'java' } + +group = 'com.example' +version = '1.0.0' + +repositories { mavenCentral() } + +// Intentionally no dependencies: exercises empty configuration blocks. diff --git a/test/providers/tst_manifests/gradle/empty_dependencies/depTree.txt b/test/providers/tst_manifests/gradle/empty_dependencies/depTree.txt new file mode 100644 index 00000000..3a46d947 --- /dev/null +++ b/test/providers/tst_manifests/gradle/empty_dependencies/depTree.txt @@ -0,0 +1,8 @@ +> Task :dependencies + +compileClasspath - Compile classpath for source set 'main'. +No dependencies + +runtimeClasspath - Runtime classpath of source set 'main'. +No dependencies + diff --git a/test/providers/tst_manifests/gradle/empty_dependencies/expected_stack_sbom.json b/test/providers/tst_manifests/gradle/empty_dependencies/expected_stack_sbom.json new file mode 100644 index 00000000..7b4fd990 --- /dev/null +++ b/test/providers/tst_manifests/gradle/empty_dependencies/expected_stack_sbom.json @@ -0,0 +1,27 @@ +{ + "bomFormat": "CycloneDX", + "specVersion": "1.6", + "version": 1, + "metadata": { + "timestamp": "2023-08-07T00:00:00.000Z", + "component": { + "group": "com.example", + "name": "empty_dependencies", + "version": "1.0.0", + "purl": "pkg:maven/com.example/empty_dependencies@1.0.0", + "type": "application", + "bom-ref": "pkg:maven/com.example/empty_dependencies@1.0.0" + }, + "tools": { + "components": [ + { + "type": "application", + "name": "trustify-da-javascript-client", + "version": "0.4.0" + } + ] + } + }, + "components": [], + "dependencies": [] +} diff --git a/test/providers/tst_manifests/gradle/empty_dependencies/gradle.properties b/test/providers/tst_manifests/gradle/empty_dependencies/gradle.properties new file mode 100644 index 00000000..33c7ce66 --- /dev/null +++ b/test/providers/tst_manifests/gradle/empty_dependencies/gradle.properties @@ -0,0 +1,11 @@ + +> Task :properties + +------------------------------------------------------------ +Root project 'empty_dependencies' +------------------------------------------------------------ + +allprojects: [root project 'empty_dependencies'] +group: com.example +name: empty_dependencies +version: 1.0.0 diff --git a/test/providers/tst_manifests/gradle/gradle_multi_project/app/build.gradle b/test/providers/tst_manifests/gradle/gradle_multi_project/app/build.gradle index c6325b8f..9cc969e7 100644 --- a/test/providers/tst_manifests/gradle/gradle_multi_project/app/build.gradle +++ b/test/providers/tst_manifests/gradle/gradle_multi_project/app/build.gradle @@ -1 +1,11 @@ -// app build +plugins { id 'java' } + +group = 'com.example' +version = '1.0.0' + +repositories { mavenCentral() } + +dependencies { + implementation 'org.apache.commons:commons-lang3:3.16.0' + implementation 'commons-io:commons-io:2.16.1' +} diff --git a/test/providers/tst_manifests/gradle/gradle_multi_project/app/depTree.txt b/test/providers/tst_manifests/gradle/gradle_multi_project/app/depTree.txt new file mode 100644 index 00000000..c6198bd2 --- /dev/null +++ b/test/providers/tst_manifests/gradle/gradle_multi_project/app/depTree.txt @@ -0,0 +1,10 @@ +> Task :app:dependencies + +compileClasspath - Compile classpath for source set 'main'. ++--- org.apache.commons:commons-lang3:3.16.0 +\--- commons-io:commons-io:2.16.1 + +runtimeClasspath - Runtime classpath of source set 'main'. ++--- org.apache.commons:commons-lang3:3.16.0 +\--- commons-io:commons-io:2.16.1 + diff --git a/test/providers/tst_manifests/gradle/gradle_multi_project/app/expected_stack_sbom.json b/test/providers/tst_manifests/gradle/gradle_multi_project/app/expected_stack_sbom.json new file mode 100644 index 00000000..35b52066 --- /dev/null +++ b/test/providers/tst_manifests/gradle/gradle_multi_project/app/expected_stack_sbom.json @@ -0,0 +1,62 @@ +{ + "bomFormat": "CycloneDX", + "specVersion": "1.6", + "version": 1, + "metadata": { + "timestamp": "2023-08-07T00:00:00.000Z", + "component": { + "group": "com.example", + "name": "app", + "version": "1.0.0", + "purl": "pkg:maven/com.example/app@1.0.0", + "type": "application", + "bom-ref": "pkg:maven/com.example/app@1.0.0" + }, + "tools": { + "components": [ + { + "type": "application", + "name": "trustify-da-javascript-client", + "version": "0.4.0" + } + ] + } + }, + "components": [ + { + "group": "org.apache.commons", + "name": "commons-lang3", + "version": "3.16.0", + "purl": "pkg:maven/org.apache.commons/commons-lang3@3.16.0", + "type": "library", + "bom-ref": "pkg:maven/org.apache.commons/commons-lang3@3.16.0", + "scope": "required" + }, + { + "group": "commons-io", + "name": "commons-io", + "version": "2.16.1", + "purl": "pkg:maven/commons-io/commons-io@2.16.1", + "type": "library", + "bom-ref": "pkg:maven/commons-io/commons-io@2.16.1", + "scope": "required" + } + ], + "dependencies": [ + { + "ref": "pkg:maven/com.example/app@1.0.0", + "dependsOn": [ + "pkg:maven/org.apache.commons/commons-lang3@3.16.0", + "pkg:maven/commons-io/commons-io@2.16.1" + ] + }, + { + "ref": "pkg:maven/org.apache.commons/commons-lang3@3.16.0", + "dependsOn": [] + }, + { + "ref": "pkg:maven/commons-io/commons-io@2.16.1", + "dependsOn": [] + } + ] +} diff --git a/test/providers/tst_manifests/gradle/gradle_multi_project/app/gradle.properties b/test/providers/tst_manifests/gradle/gradle_multi_project/app/gradle.properties new file mode 100644 index 00000000..f57d89f1 --- /dev/null +++ b/test/providers/tst_manifests/gradle/gradle_multi_project/app/gradle.properties @@ -0,0 +1,11 @@ + +> Task :app:properties + +------------------------------------------------------------ +Project ':app' +------------------------------------------------------------ + +allprojects: [project ':app'] +group: com.example +name: app +version: 1.0.0 diff --git a/test/remediate.test.js b/test/remediate.test.js index c59c9e7e..5a3133a4 100644 --- a/test/remediate.test.js +++ b/test/remediate.test.js @@ -108,10 +108,14 @@ suite('remediate — runRemediation', () => { let runRemediation let requestStackStub let matchStub + let discoverGradleSubprojectsStub + let findNearestBuildFileStub setup(async () => { requestStackStub = stub() matchStub = stub() + discoverGradleSubprojectsStub = stub().resolves([]) + findNearestBuildFileStub = stub().returns(undefined) const mod = await esmock('../src/remediate.js', { '../src/analysis.js': { @@ -126,6 +130,10 @@ suite('remediate — runRemediation', () => { '../src/index.js': { selectTrustifyDABackend: () => 'https://da.example.com', }, + '../src/providers/java_gradle.js': { + discoverGradleSubprojects: discoverGradleSubprojectsStub, + findNearestBuildFile: findNearestBuildFileStub, + }, }) runRemediation = mod.runRemediation }) @@ -270,6 +278,107 @@ suite('remediate — runRemediation', () => { }) }) + suite('Gradle version catalog fan-out', () => { + const CATALOG = `[versions] +commons = "3.16.0" + +[libraries] +commons-lang3 = { module = "org.apache.commons:commons-lang3", version.ref = "commons" } +` + + const SUBPROJECT_REPORT = buildAnalysisReport({ + depRef: 'pkg:maven/org.apache.commons/commons-lang3@3.16.0', + fixedIn: ['pkg:maven/org.apache.commons/commons-lang3@3.17.0'], + }) + + /** + * Creates a multi-project Gradle layout and wires the catalog to it. + * @returns {{rootBuild: string, appBuild: string, tomlPath: string, cleanup: function}} + */ + function createMultiProject() { + const { dir, cleanup } = createTempDir({ + 'build.gradle': '// root', + 'app/build.gradle': '// app', + 'gradle/libs.versions.toml': CATALOG, + }) + const rootBuild = path.join(dir, 'build.gradle') + const appBuild = path.join(dir, 'app', 'build.gradle') + findNearestBuildFileStub.returns(rootBuild) + discoverGradleSubprojectsStub.resolves([rootBuild, appBuild]) + matchStub.returns({ provideStack: stub().resolves({ content: '{}', contentType: 'application/json', ecosystem: 'gradle' }) }) + return { rootBuild, appBuild, tomlPath: path.join(dir, 'gradle', 'libs.versions.toml'), cleanup } + } + + test('analyzes every project in the build and applies subproject remediations to the catalog', async () => { + const { rootBuild, appBuild, tomlPath, cleanup } = createMultiProject() + try { + requestStackStub.callsFake(async (_provider, manifestPath) => + manifestPath === appBuild ? SUBPROJECT_REPORT : { providers: {} }) + + const result = await runRemediation(tomlPath, {}) + + expect(requestStackStub.callCount).to.equal(2) + expect(requestStackStub.firstCall.args[1]).to.equal(rootBuild) + expect(result.remediations.map(r => r.artifactId)).to.deep.equal(['commons-lang3']) + expect(fs.readFileSync(tomlPath, 'utf-8')).to.include('3.17.0') + } finally { + cleanup() + } + }) + + test('reports a dependency found in several projects once', async () => { + const { tomlPath, cleanup } = createMultiProject() + try { + requestStackStub.resolves(SUBPROJECT_REPORT) + + const result = await runRemediation(tomlPath, { dryRun: true }) + + expect(result.remediations).to.have.lengthOf(1) + expect(result.groups).to.have.lengthOf(1) + } finally { + cleanup() + } + }) + + test('reports a subproject dependency absent from the catalog as skipped', async () => { + const { tomlPath, cleanup } = createMultiProject() + try { + requestStackStub.resolves(buildMultiDepReport([ + { depRef: 'pkg:maven/org.apache.commons/commons-lang3@3.16.0', fixedIn: ['pkg:maven/org.apache.commons/commons-lang3@3.17.0'], issueId: 'CVE-2024-0001' }, + { depRef: 'pkg:maven/com.example/direct-dep@1.0', fixedIn: ['pkg:maven/com.example/direct-dep@1.1'], issueId: 'CVE-2024-0002' }, + ])) + + const result = await runRemediation(tomlPath, { dryRun: true }) + + expect(result.skipped).to.deep.include({ + groupId: 'com.example', + artifactId: 'direct-dep', + newVersion: '1.1', + reason: 'Dependency not declared in the version catalog', + }) + } finally { + cleanup() + } + }) + + test('falls back to the catalog itself when no Gradle build is discovered', async () => { + const { dir, cleanup } = createTempDir({ 'gradle/libs.versions.toml': CATALOG }) + try { + const tomlPath = path.join(dir, 'gradle', 'libs.versions.toml') + matchStub.returns({ provideStack: stub().resolves({ content: '{}', contentType: 'application/json', ecosystem: 'gradle' }) }) + requestStackStub.resolves(SUBPROJECT_REPORT) + + const result = await runRemediation(tomlPath, { dryRun: true }) + + expect(requestStackStub.callCount).to.equal(1) + expect(requestStackStub.firstCall.args[1]).to.equal(tomlPath) + expect(result.remediations).to.have.lengthOf(1) + } finally { + cleanup() + } + }) + }) + suite('directory mode', () => { /** Verifies that directory mode discovers and processes all manifest files. */ test('discovers and processes pom.xml and TOML files', async () => {