|
| 1 | +import { expect } from 'chai' |
| 2 | +import { PackageURL } from 'packageurl-js' |
| 3 | + |
| 4 | +import CycloneDxSbom from '../src/cyclone_dx_sbom.js' |
| 5 | + |
| 6 | +const sampleHashes = [{ alg: 'SHA-256', content: 'abc123def456' }] |
| 7 | + |
| 8 | +suite('CycloneDX SBOM hash support', () => { |
| 9 | + |
| 10 | + /** Verifies that addDependency with hashes produces a component containing the hashes array. */ |
| 11 | + test('getComponent with hashes includes hashes array in component', () => { |
| 12 | + // Given an SBOM with a root and a dependency with hashes |
| 13 | + const sbom = new CycloneDxSbom() |
| 14 | + const root = new PackageURL('pypi', undefined, 'my-app', '1.0.0', undefined, undefined) |
| 15 | + const purl = new PackageURL('pypi', undefined, 'requests', '2.33.1', undefined, undefined) |
| 16 | + sbom.addRoot(root) |
| 17 | + |
| 18 | + // When adding a dependency with hashes |
| 19 | + sbom.addDependency(root, purl, undefined, sampleHashes) |
| 20 | + |
| 21 | + // Then the target component should include the hashes |
| 22 | + const targetComponent = sbom.components.find(c => c.name === 'requests') |
| 23 | + expect(targetComponent).to.exist |
| 24 | + expect(targetComponent.hashes).to.deep.equal(sampleHashes) |
| 25 | + }) |
| 26 | + |
| 27 | + /** Verifies that addDependency without hashes produces a component with no hashes field. */ |
| 28 | + test('getComponent without hashes does not include hashes field', () => { |
| 29 | + // Given an SBOM with a root and a dependency without hashes |
| 30 | + const sbom = new CycloneDxSbom() |
| 31 | + const root = new PackageURL('pypi', undefined, 'my-app', '1.0.0', undefined, undefined) |
| 32 | + const dep = new PackageURL('pypi', undefined, 'requests', '2.33.1', undefined, undefined) |
| 33 | + sbom.addRoot(root) |
| 34 | + |
| 35 | + // When adding a dependency without hashes |
| 36 | + sbom.addDependency(root, dep) |
| 37 | + |
| 38 | + // Then the target component should not have a hashes property |
| 39 | + const targetComponent = sbom.components.find(c => c.name === 'requests') |
| 40 | + expect(targetComponent).to.exist |
| 41 | + expect(targetComponent).to.not.have.property('hashes') |
| 42 | + }) |
| 43 | + |
| 44 | + /** Verifies that hashes are attached only to the target component, not the source. */ |
| 45 | + test('addDependency forwards hashes only to target component', () => { |
| 46 | + // Given an SBOM with a chain: root -> dep1 -> dep2 (only dep2 has hashes) |
| 47 | + const sbom = new CycloneDxSbom() |
| 48 | + const root = new PackageURL('pypi', undefined, 'my-app', '1.0.0', undefined, undefined) |
| 49 | + const dep1 = new PackageURL('pypi', undefined, 'requests', '2.33.1', undefined, undefined) |
| 50 | + const dep2 = new PackageURL('pypi', undefined, 'numpy', '1.24.0', undefined, undefined) |
| 51 | + const dep2Hashes = [{ alg: 'SHA-256', content: '789xyz' }] |
| 52 | + sbom.addRoot(root) |
| 53 | + sbom.addDependency(root, dep1) |
| 54 | + |
| 55 | + // When adding dep2 as a dependency of dep1 with hashes |
| 56 | + sbom.addDependency(dep1, dep2, undefined, dep2Hashes) |
| 57 | + |
| 58 | + // Then dep1 (source) should have no hashes, dep2 (target) should have hashes |
| 59 | + const dep1Component = sbom.components.find(c => c.name === 'requests') |
| 60 | + expect(dep1Component).to.not.have.property('hashes') |
| 61 | + const dep2Component = sbom.components.find(c => c.name === 'numpy') |
| 62 | + expect(dep2Component.hashes).to.deep.equal(dep2Hashes) |
| 63 | + }) |
| 64 | + |
| 65 | + /** Verifies that hashes are included in the serialized CycloneDX JSON output. */ |
| 66 | + test('hashes appear in serialized SBOM JSON', () => { |
| 67 | + // Given an SBOM with a hashed dependency |
| 68 | + const sbom = new CycloneDxSbom() |
| 69 | + const root = new PackageURL('pypi', undefined, 'my-app', '1.0.0', undefined, undefined) |
| 70 | + const dep = new PackageURL('pypi', undefined, 'requests', '2.33.1', undefined, undefined) |
| 71 | + sbom.addRoot(root) |
| 72 | + sbom.addDependency(root, dep, undefined, sampleHashes) |
| 73 | + |
| 74 | + // When serializing to JSON |
| 75 | + const json = JSON.parse(sbom.getAsJsonString({})) |
| 76 | + |
| 77 | + // Then the component in JSON should contain hashes |
| 78 | + const comp = json.components.find(c => c.name === 'requests') |
| 79 | + expect(comp.hashes).to.deep.equal(sampleHashes) |
| 80 | + }) |
| 81 | + |
| 82 | + /** Verifies that components without hashes have no hashes field in serialized JSON. */ |
| 83 | + test('component without hashes has no hashes in serialized SBOM JSON', () => { |
| 84 | + // Given an SBOM with a dependency without hashes |
| 85 | + const sbom = new CycloneDxSbom() |
| 86 | + const root = new PackageURL('pypi', undefined, 'my-app', '1.0.0', undefined, undefined) |
| 87 | + const dep = new PackageURL('pypi', undefined, 'requests', '2.33.1', undefined, undefined) |
| 88 | + sbom.addRoot(root) |
| 89 | + sbom.addDependency(root, dep) |
| 90 | + |
| 91 | + // When serializing to JSON |
| 92 | + const json = JSON.parse(sbom.getAsJsonString({})) |
| 93 | + |
| 94 | + // Then the component in JSON should not have a hashes property |
| 95 | + const comp = json.components.find(c => c.name === 'requests') |
| 96 | + expect(comp).to.not.have.property('hashes') |
| 97 | + }) |
| 98 | + |
| 99 | + /** Verifies that hashes are applied to an existing component that was first created without them as a source. */ |
| 100 | + test('hashes are updated on component first seen as source without hashes', () => { |
| 101 | + // Given a component first created as a source (without hashes) |
| 102 | + const sbom = new CycloneDxSbom() |
| 103 | + const root = new PackageURL('pypi', undefined, 'my-app', '1.0.0', undefined, undefined) |
| 104 | + const mid = new PackageURL('pypi', undefined, 'requests', '2.33.1', undefined, undefined) |
| 105 | + const leaf = new PackageURL('pypi', undefined, 'urllib3', '2.0.0', undefined, undefined) |
| 106 | + const midHashes = [{ alg: 'SHA-256', content: 'abc123' }] |
| 107 | + sbom.addRoot(root) |
| 108 | + |
| 109 | + // When mid is first seen as a source (no hashes), then as a target with hashes |
| 110 | + sbom.addDependency(mid, leaf) |
| 111 | + sbom.addDependency(root, mid, undefined, midHashes) |
| 112 | + |
| 113 | + // Then mid should have hashes despite being created first without them |
| 114 | + const midComponent = sbom.components.find(c => c.name === 'requests') |
| 115 | + expect(midComponent.hashes).to.deep.equal(midHashes) |
| 116 | + }) |
| 117 | + |
| 118 | + /** Verifies that hashes are applied when a component already exists as a target without hashes. */ |
| 119 | + test('hashes are updated on component first seen as target without hashes', () => { |
| 120 | + // Given a component first added as a target without hashes |
| 121 | + const sbom = new CycloneDxSbom() |
| 122 | + const root = new PackageURL('pypi', undefined, 'my-app', '1.0.0', undefined, undefined) |
| 123 | + const dep = new PackageURL('pypi', undefined, 'requests', '2.33.1', undefined, undefined) |
| 124 | + const depHashes = [{ alg: 'SHA-256', content: 'abc123' }] |
| 125 | + sbom.addRoot(root) |
| 126 | + sbom.addDependency(root, dep) |
| 127 | + |
| 128 | + // When the same component is added again as a target with hashes |
| 129 | + const other = new PackageURL('pypi', undefined, 'other', '1.0.0', undefined, undefined) |
| 130 | + sbom.addDependency(other, dep, undefined, depHashes) |
| 131 | + |
| 132 | + // Then the component should have hashes and not be duplicated |
| 133 | + const depComponents = sbom.components.filter(c => c.name === 'requests') |
| 134 | + expect(depComponents).to.have.lengthOf(1) |
| 135 | + expect(depComponents[0].hashes).to.deep.equal(depHashes) |
| 136 | + }) |
| 137 | + |
| 138 | + /** Verifies that passing an empty hashes array is treated the same as no hashes. */ |
| 139 | + test('empty hashes array does not add hashes field', () => { |
| 140 | + // Given an SBOM with a dependency with an empty hashes array |
| 141 | + const sbom = new CycloneDxSbom() |
| 142 | + const root = new PackageURL('pypi', undefined, 'my-app', '1.0.0', undefined, undefined) |
| 143 | + const dep = new PackageURL('pypi', undefined, 'requests', '2.33.1', undefined, undefined) |
| 144 | + sbom.addRoot(root) |
| 145 | + |
| 146 | + // When adding a dependency with empty hashes |
| 147 | + sbom.addDependency(root, dep, undefined, []) |
| 148 | + |
| 149 | + // Then the component should not have a hashes property |
| 150 | + const targetComponent = sbom.components.find(c => c.name === 'requests') |
| 151 | + expect(targetComponent).to.not.have.property('hashes') |
| 152 | + }) |
| 153 | +}) |
0 commit comments