Repository navigation
fix(vulnfeeds): only fuzzy-match versions on component boundaries - #6482
AnujYamdagni wants to merge 2 commits into
Conversation
VersionToCommit falls back to a fuzzy tag match when there is no exact one, and the match uses a plain string prefix. "1.2" (normalized "1-2") can therefore resolve to the commit of a lone v1.20.0 tag. Accept a fuzzy match only when the tag extends the version by whole components, so "1-2" still matches "1-2-0". NormalizeVersion returns an empty string and no error for a version that is only a pre-release label, such as "rc1". The empty version can fuzzy- match a lone tag without a pre-release component, and label-only tags collapse onto one empty key in the tag map. Return an error instead, so these versions and tags are skipped.
| if v.MatchesVersionText == normalizedVersionMatchesText && strings.HasPrefix(k, normalizedVersion) { | ||
| // Only extend the version by whole components: "1-2" may match "1-2-0", but not "1-20-0". | ||
| rest, isPrefix := strings.CutPrefix(k, normalizedVersion) | ||
| if v.MatchesVersionText == normalizedVersionMatchesText && isPrefix && (rest == "" || rest[0] == '-') { |
There was a problem hiding this comment.
Non-blocking note: this looks clean and fixes the component-boundary issue well. For future reference, the len(candidateTags) == 1 branch below will still let e.g. "1.2" fuzzy-match a lone v1.2.5 tag (unlike the multi-candidate -0 fallback)—fine to keep out of scope for this PR.
There was a problem hiding this comment.
Thanks. Agreed, the single-candidate branch still accepts a lone tag that extends the version by any number of components, so "1.2" can match a lone v1.2.5. I left that out of this PR.
| @@ -133,6 +135,10 @@ func NormalizeVersion(version string) (normalizedVersion string, e error) { | |||
| if validVersionText.MatchString(components[0]) { | |||
There was a problem hiding this comment.
If an input has multiple label matches and no version number (e.g. "alpha-beta", "rc1-beta2", or "source-archive" matching "rc" twice), only the first component is dropped and NormalizeVersion still returns a bare label without error.
Looping here closes that gap (and also handles multi-word prefixes like "source-archive-1.0" → "1-0"), guaranteeing any non-error result starts with a numeric component:
| if validVersionText.MatchString(components[0]) { | |
| for len(components) > 0 && validVersionText.MatchString(components[0]) { |
If we make this change, could we also add a test case like "alpha-beta" or "source-archive" to TestNormalizeVersion?
There was a problem hiding this comment.
Done in 22c1200. It now drops every leading label component, and TestNormalizeVersion has "alpha-beta" (error) and "source-archive-1.0" ("1-0"). Both fail without the loop and pass with it.
NormalizeVersion dropped only the first component that matched a pre-release label, so "alpha-beta" normalized to "beta" without an error, and "source-archive-1.0" (both words contain "rc") to "rc-1-0". Drop leading label components until a numeric one, so a non-error result always starts with a number.
Overview
VersionToCommitcould attach the wrong tag's commit to a version: "1.2" could match a lone v1.20.0 tag, and "rc1" could match a lone tag without a pre-release component.Fixes #6462
Details
findFuzzyCommitaccepts a tag only when it extends the version by whole components (rest == "" || rest[0] == '-'), so "1-2" still matches "1-2-0".NormalizeVersionreturns an error when nothing is left after dropping the leading pre-release labels. Label-only versions and tags are skipped instead of mapping to"".normalize_tagkeepsrc1instead; please say if you'd prefer parity.Testing
Go 1.27.4, linux/amd64, master at 507dc98:
go test -count=1 -run 'TestVersionToCommit|TestNormalizeVersion' ./git/(invulnfeeds/): on master, 6 new cases fail ("Only a pre-release label", "Only pre-release labels", "Several leading words that match a label", "Fuzzy match does not cross a component boundary", "Fuzzy match does not extend a pre-release label", "Pre-release label alone does not match a lone release tag"); with this change all pass.go test -count=1 ./git/ ./conversion/... ./cmd/converters/...: ok.go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.14.0 run ./git/...: 0 issues.Not tested: a full conversion run against real CVE data.
This PR was AI-assisted.