polycratia

Not checked is a verdict: five ways a version comparison fails

· 11 min read

A matcher that measures an inventory against a set of advisories will meet components it cannot compare at all, and the cheapest thing to do with them is nothing. The report that comes out is shorter, and a shorter report reads as a cleaner one. That is the most expensive defect I know of in this class of tool, because nobody ever files it.

I hit this while building vexdesk, which reads a CycloneDX SBOM, measures the components in it against OSV advisories sitting on disk, and turns the decisions a person makes into an OpenVEX document. It does not scan and it ships no vulnerability database; it consumes what syft, trivy and cdxgen already produce. The matching itself is the easy half. The hard half is all the bookkeeping around every comparison that could not be made.

The shape of the lie#

Take a matcher written the obvious way. For each component, derive a package identifier, look up the advisories that name it, compare versions, emit a finding. Now feed it an inventory with a vendored binary blob that carries no package URL, a Maven artifact whose advisories are indexed under a groupId:artifactId name you never built, and a Python package whose advisory bounds its versions with an ECOSYSTEM range in PEP 440 order.

Each of those three falls out of the loop at a different place. In the obvious implementation all three fall out the same way, through a continue. The output is a table of the components that survived the gauntlet. Nothing in it says the gauntlet existed.

I have spent years on payment reconciliation, and the discipline there is identical. A transaction you could not attribute does not balance. It becomes an exception with a reason code, and the reason code is the whole product. A reconciliation report that drops unattributable rows balances perfectly and tells you nothing. A security report that drops unidentifiable components clears perfectly and tells you nothing. The difference is that in payments somebody notices by end of month.

Four refusals, recorded before any advisory is read#

So match.Run checks four things in order for every component, and at the first one that fails it records a Skip carrying the reason, then moves on. The counter of compared components goes up only after all four pass. Only then is the advisory set walked.

What is missing Recorded reason
no package URL at all no package URL: nothing to look up
a package URL ParsePURL rejects the parser's own error
a purl type absent from the ecosystem table that type is not mapped to an advisory ecosystem
no version on the component and none in the purl no version: cannot compare against any range

The order is not arbitrary. The version check comes last because a component can carry no version field of its own and still be perfectly comparable (the version is in the package URL, and that has to be parsed before you can know). And the empty-purl case is split from the unparseable-purl case on purpose. "Your SBOM generator emitted nothing here" and "your SBOM generator emitted something I could not read" send a reader to two different places. One is a gap in the build. The other is a bug in a tool, probably mine.

In the report those four land under their own heading, with the count in it, and they are printed whether or not anybody asked.

console
$ vexdesk match -sbom sbom.cyclonedx.json -advisories ./advisories
4 component(s) compared against the advisory set

STATUS    ADVISORY      COMPONENT        VERSION  REASON
affected  FIXTURE-0001  widget           1.2.3    version falls inside the advisory's affected range
affected  FIXTURE-0004  cogwheel         4.0.0    version is in the advisory's affected version list

Not checked (1):
  vendored-blob  no package URL: nothing to look up

Five components went in. Four were compared. The line that says so is the first line of the output, where you cannot miss it, and the component nobody looked at has a name and a sentence next to it.

The fifth failure cannot be a skip#

The four above are all failures of identification. The matcher never got far enough to read an advisory. There is a fifth kind, and it is categorically different. Here the component is identified, the advisory names it, the claim is about the thing you actually ship, and the comparison still cannot be made, because the range is expressed in an ordering nobody implemented.

That cannot be filed as "not checked" alongside a nameless blob. It is a live claim against a known component, so it becomes a verdict in the findings table, unknown, with every reason that produced it.

go
    var unknown []string
    for _, r := range aff.Ranges {
        switch r.Type {
        case osv.RangeSemver:
            // fall through to comparison
        case osv.RangeEcosystem:
            if !eco.semverOrdered {
                unknown = append(unknown, fmt.Sprintf(
                    "ECOSYSTEM range for %s needs that ecosystem's own version ordering, which is not implemented",
                    eco.name))
                continue
            }
        case osv.RangeGit:
            unknown = append(unknown, "GIT range: commit ranges cannot be compared against a version string")
            continue
        default:
            unknown = append(unknown, fmt.Sprintf("range type %q is not recognised", r.Type))
            continue
        }

        hit, err := inSemverRange(version, r)
        if err != nil {
            unknown = append(unknown, err.Error())
            continue
        }
        if hit {
            return Affected, "version falls inside the advisory's affected range"
        }
    }

    if len(unknown) > 0 {
        // Every reason survives: the person clearing this finding needs the
        // whole list, not the first item of it.
        return Unknown, strings.Join(unknown, "; ")
    }

Two things in there are load-bearing. First, the accumulator. An affected entry can have several ranges, and a reader clearing this finding by hand needs the full list of reasons it stayed undecided. Second, the position of the len(unknown) > 0 check. It sits before the fallbacks that would return NotAffected. If any range in the entry could not be ordered, the entry cannot clear the version, no matter how many sibling ranges were evaluated cleanly and missed. An unordered range is an unread one.

And unknown is a hard outcome. Unknown findings are kept by Attention, so cmdMatch returns exitCode(1) on them unless -exit-zero is passed. A comparison that could not be made stops the pipeline exactly like an affected one does. That is the whole bet. A human looking at a build that failed for a stupid reason is cheaper than a human not looking at a build that passed for a stupid reason.

Refusing to guess the ordering is the point#

The obvious objection is that ECOSYSTEM ranges are implementable. PEP 440 is written down. Debian epochs are written down. Maven's ordering is written down. Implement them and the unknown class mostly disappears.

It disappears into whichever answer the implementation happens to produce, and that is the problem. Each of those schemes has rules semver lacks, like epochs, post-releases, a different notion of what sorts before what. A semver comparator pointed at them still returns an answer, with no sign of any trouble. Half the time it answers correctly. The other half it writes not_affected into a document that a person signs.

So the ecosystem table carries ordering as an explicit, per-ecosystem claim, and only three ecosystems make it.

go
// ecosystem describes how an OSV ecosystem names its packages and whether its
// versions can be ordered with semver rules.
type ecosystem struct {
    name string
    // semverOrdered marks ecosystems whose own version ordering is semver, so
    // an ECOSYSTEM range can be evaluated with semver comparison. Ecosystems
    // with their own ordering rules (PEP 440, Debian epochs, Maven) are left
    // out on purpose: guessing their order would produce confident wrong
    // answers, which is worse than saying "unknown".
    semverOrdered bool
    // pkgName builds the package name as the ecosystem's advisories spell it.
    pkgName func(p inventory.PURL) string
}

var byPURLType = map[string]ecosystem{
    "golang":   {name: "Go", semverOrdered: true, pkgName: joinSlash},
    "npm":      {name: "npm", semverOrdered: true, pkgName: joinSlash},
    "cargo":    {name: "crates.io", semverOrdered: true, pkgName: nameOnly},
    "pypi":     {name: "PyPI", pkgName: normalizedPyPI},
    "maven":    {name: "Maven", pkgName: joinColon},
    "gem":      {name: "RubyGems", pkgName: nameOnly},
    "nuget":    {name: "NuGet", pkgName: nameOnly},
    "composer": {name: "Packagist", pkgName: joinSlash},
    "hex":      {name: "Hex", pkgName: joinSlash},
    "pub":      {name: "Pub", pkgName: nameOnly},
}

Note what the same table does commit to for every row, which is naming. pkgName is how that ecosystem's advisories spell a package. joinSlash for Go, npm, Packagist and Hex, joinColon for Maven, nameOnly for crates.io, RubyGems, NuGet and Pub, and normalizedPyPI for PyPI, which applies PEP 503 normalization so that Flask_Login and flask-login are the same package. samePackage then applies the same normalization to the advisory's side, after cutting any suffix off the ecosystem string, because OSV writes Alpine:v3.19 and means the Alpine family.

That asymmetry is deliberate and worth stating plainly. Name normalization is total and reversible-in-the-small (fold the separators, lowercase, compare). Getting it wrong produces a missed match, which shows up as a component no advisory names, visible, and nothing is asserted about it. Version ordering is a total order over an infinite set, and getting it wrong produces an assertion. Where a wrong answer looks exactly like a right one, I will take the gap. -json carries the reason string out to whatever consumes it, so the gap is machine-visible too.

Three kinds of silence, deliberately not merged#

Once you start naming the ways a comparison fails, you find that "nothing to report" is three different statements.

  • Not checked. The component never reached the advisory set. Printed with its reason, every time.
  • Named by no advisory. The component was compared against every record that was read, and none of them mentioned it. Counted on its own line always, listed only under -all, because on a real advisory tree this is most of the inventory and would bury the findings. The full list goes into -json as unmatched regardless. This says something about the records that were read, and it gives no verdict on the component. An advisory tree a day out of date produces exactly this line.
  • not_affected. A real comparison happened and placed the version outside every affected range.

Only the third is a security claim. Collapsing the first two into it is what produces a clean report out of an empty one.

What I would do differently#

Two things.

The reasons are English sentences. They read well in a terminal and they survive into the JSON output, but a consumer that wants to gate on "skipped because the purl type is unmapped" has to match on prose I might rewrite. I would carry a stable code next to the sentence from the start, the sentence for the human and the code for the pipeline, and so avoid retrofitting one after something downstream starts pattern-matching my wording.

And semverOrdered is a boolean standing in for a comparator. It is honest today, because the answer really is "semver or nothing", but it shapes how the gap gets closed. Adding PEP 440 support should mean adding a comparator to one row of that table, with no flag to flip and no hoping the semver path copes. I would have made the field a function the day I wrote it.

The design itself I would keep unchanged. The report is longer than it needs to be, and every extra line in it is a thing somebody did not check.

react

$ new-project --brief

or email hey@polycratia.com