-
Notifications
You must be signed in to change notification settings - Fork 0
release: fail the SBOM check unless every declared runtime dependency is enumerated #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yakimoto
wants to merge
1
commit into
main
Choose a base branch
from
fix/sbom-after-install
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,14 @@ | |
| * 1. package.json version equals the version encoded in the release tag. | ||
| * 2. The CycloneDX document really is CycloneDX, and its root component is THIS version. | ||
| * 3. The SPDX document lists at least one package. | ||
| * 4. EVERY runtime dependency package.json declares (`dependencies` keys) appears in the | ||
| * SPDX document with a real versionInfo AND as a CycloneDX component, and the SPDX | ||
| * document carries a versioned entry for the package itself. Both SBOMs are generated | ||
| * from the INSTALLED tree (`npm ci` then `npm sbom --omit dev`); a document produced | ||
| * from an uninstalled checkout has one self-describing entry and zero dependencies, | ||
| * which check 3 alone would wave through. The floor is derived from package.json, never | ||
| * hardcoded, so it tracks dependency changes on its own. A package that declares no | ||
| * runtime dependencies is refused outright (a floor of 0 validates nothing). | ||
| * | ||
| * Usage: TAG=sdk-v2.1.3 node scripts/supply-chain/validate-sbom.mjs | ||
| * node scripts/supply-chain/validate-sbom.mjs --tag sdk-v2.1.3 --dir . | ||
|
|
@@ -27,10 +35,42 @@ export function versionFromTag(tag) { | |
| return version; | ||
| } | ||
|
|
||
| const NO_VERSION = new Set(['', 'NOASSERTION']); | ||
|
|
||
| /** Sorted names of the runtime dependencies package.json declares (never devDependencies). */ | ||
| export function declaredDependencies(pkg) { | ||
| const deps = pkg && typeof pkg.dependencies === 'object' && pkg.dependencies !== null ? pkg.dependencies : {}; | ||
| return Object.keys(deps).sort(); | ||
| } | ||
|
|
||
| /** Names of SPDX packages[] entries that carry a real versionInfo (not missing/empty/NOASSERTION). */ | ||
| export function spdxVersionedNames(spdx) { | ||
| const names = new Set(); | ||
| for (const p of Array.isArray(spdx?.packages) ? spdx.packages : []) { | ||
| const version = typeof p?.versionInfo === 'string' ? p.versionInfo : ''; | ||
| if (typeof p?.name === 'string' && p.name && !NO_VERSION.has(version)) names.add(p.name); | ||
| } | ||
| return names; | ||
| } | ||
|
|
||
| /** | ||
| * Names of CycloneDX components, from `name` when present or else from the purl | ||
| * (`pkg:npm/%40scope%2Fname@1.2.3` -> `@scope/name`), since `npm sbom` emits both. | ||
| */ | ||
| export function cyclonedxComponentNames(cyclonedx) { | ||
| const names = new Set(); | ||
| for (const c of Array.isArray(cyclonedx?.components) ? cyclonedx.components : []) { | ||
| if (typeof c?.name === 'string' && c.name) names.add(c.name); | ||
| const m = typeof c?.purl === 'string' ? /^pkg:npm\/(.+?)(?:@[^@]*)?(?:\?.*)?$/.exec(c.purl) : null; | ||
| if (m) names.add(decodeURIComponent(m[1])); | ||
| } | ||
| return names; | ||
| } | ||
|
|
||
| /** | ||
| * Validate both SBOM documents against the version being released. | ||
| * @param {{ tag: string, pkg: any, cyclonedx: any, spdx: any }} input | ||
| * @returns {{ version: string, componentCount: number, spdxPackageCount: number, specVersion: string }} | ||
| * @returns {{ version: string, componentCount: number, spdxPackageCount: number, specVersion: string, declaredDependencyCount: number }} | ||
| */ | ||
| export function validateSboms({ tag, pkg, cyclonedx, spdx }) { | ||
| const version = versionFromTag(tag); | ||
|
|
@@ -54,11 +94,41 @@ export function validateSboms({ tag, pkg, cyclonedx, spdx }) { | |
| throw new Error('spdx: document lists no packages'); | ||
| } | ||
|
|
||
| // Every declared runtime dependency must actually be enumerated -- by name, with a real | ||
| // version -- in BOTH documents. This is the check that distinguishes an SBOM generated | ||
| // from the installed tree from one generated before install. | ||
| const declared = declaredDependencies(pkg); | ||
| if (declared.length === 0) { | ||
| throw new Error( | ||
| 'package.json declares no runtime dependencies -- refusing to validate against a floor of 0', | ||
| ); | ||
| } | ||
| const spdxNames = spdxVersionedNames(spdx); | ||
| if (typeof pkg?.name === 'string' && pkg.name && !spdxNames.has(pkg.name)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: When Prompt for AI agents |
||
| throw new Error(`spdx: no versioned package entry for the package itself, ${pkg.name}@${version}`); | ||
| } | ||
| const missingSpdx = declared.filter((dep) => !spdxNames.has(dep)); | ||
| if (missingSpdx.length > 0) { | ||
| throw new Error( | ||
| `spdx: no versioned package entry for declared runtime dependency(ies): ${missingSpdx.join(', ')} ` + | ||
| '-- the SBOM was likely generated from an uninstalled tree', | ||
| ); | ||
| } | ||
| const cdxNames = cyclonedxComponentNames(cyclonedx); | ||
| const missingCdx = declared.filter((dep) => !cdxNames.has(dep)); | ||
| if (missingCdx.length > 0) { | ||
| throw new Error( | ||
| `cyclonedx: no component for declared runtime dependency(ies): ${missingCdx.join(', ')} ` + | ||
| '-- the SBOM was likely generated from an uninstalled tree', | ||
| ); | ||
| } | ||
|
|
||
| return { | ||
| version, | ||
| componentCount: cyclonedx.components.length, | ||
| spdxPackageCount: spdx.packages.length, | ||
| specVersion: cyclonedx.specVersion ?? 'unknown', | ||
| declaredDependencyCount: declared.length, | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -78,7 +148,8 @@ function main(argv) { | |
| }); | ||
| process.stdout.write( | ||
| `cyclonedx ${summary.specVersion}: ${summary.componentCount} runtime components; ` + | ||
| `spdx: ${summary.spdxPackageCount} packages; root ${summary.version}\n`, | ||
| `spdx: ${summary.spdxPackageCount} packages; root ${summary.version}; ` + | ||
| `all ${summary.declaredDependencyCount} declared runtime dependencies enumerated in both\n`, | ||
| ); | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: A whitespace-only
versionInfois accepted as versioned, allowing a malformed SPDX entry to pass the fail-closed dependency check. TrimversionInfobefore testing it against the empty andNOASSERTIONvalues.Prompt for AI agents