fix(parser): derive component version from the purl when the version field is absent#38
Open
dmchaledev wants to merge 1 commit into
Open
fix(parser): derive component version from the purl when the version field is absent#38dmchaledev wants to merge 1 commit into
dmchaledev wants to merge 1 commit into
Conversation
… absent A component's dedicated version field is optional in both CycloneDX (component.version) and SPDX (package.versionInfo), but the purl encodes the version authoritatively (e.g. pkg:npm/lodash@4.17.21). When the field was missing the parser left Component.version undefined, so reports rendered `name@unknown` and JSON/programmatic consumers got no version even though it was present in the purl. Extract the version from the purl as a fallback for both formats. The version follows the last unescaped `@` (namespace/name segments must percent-encode literal `@`, so scoped npm purls like pkg:npm/%40angular/core@17.0.0 resolve correctly) and is cut at any `?qualifiers` / `#subpath` and URL-decoded. An explicit version field still takes precedence, and purls without a version leave it undefined. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Xj1u5UNm7YzrnpJshe2fuV
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
A component's dedicated version field is optional in both CycloneDX (
component.version) and SPDX (package.versionInfo), but thepurlencodes the version authoritatively (e.g.pkg:npm/lodash@4.17.21). Today, when that field is missing, the parser leavesComponent.versionasundefinedeven though the version is right there in the purl. As a result:name@unknown, andsbom.components[i].version) getundefined.This PR extracts the version from the purl as a fallback for both formats.
Reproduction (before)
After this change it resolves to
4.17.21.What changed
src/parser.ts— newextractVersionFromPurl()helper, used as a fallback in bothparseCycloneDXandparseSPDX. The version follows the last unescaped@(namespace/name segments must percent-encode literal@, so scoped npm purls likepkg:npm/%40angular/core@17.0.0resolve correctly), is cut at any?qualifiers/#subpath, and is URL-decoded.src/types.ts— documented the fallback onComponent.version.src/__tests__/parser.test.ts— added coverage: purl-only version (incl. scoped + qualifiers/subpath), explicit field wins over purl, versionless purl / bare component staysundefined, and the SPDX externalRef case.An explicit
version/versionInfofield still takes precedence, so nothing changes for SBOMs that already carry it — the fix is purely additive and only fills a previously-empty field.Scope / conflicts
Confined to
parser.ts(component mapping + one helper). It does not touchdiff.ts,reporter.ts, orcli.ts, so it is orthogonal to the in-flight purl/diff PRs (#20/#31/#37, which rework diff keying) and to the reporter/CLI PRs.Verification
npm run lint,npm run typecheck,npm test(33 passing, +4 new), andnpm run buildall pass.🤖 Generated with Claude Code
https://claude.ai/code/session_01Xj1u5UNm7YzrnpJshe2fuV
Generated by Claude Code