github.com/quay/claircore@v1.5.28/debian/matcher.go (about)

     1  package debian
     2  
     3  import (
     4  	"context"
     5  
     6  	version "github.com/knqyf263/go-deb-version"
     7  
     8  	"github.com/quay/claircore"
     9  	"github.com/quay/claircore/libvuln/driver"
    10  )
    11  
    12  // Matcher is a [driver.Matcher] for Debian distributions.
    13  type Matcher struct{}
    14  
    15  var _ driver.Matcher = (*Matcher)(nil)
    16  
    17  // Name implements [driver.Matcher].
    18  func (*Matcher) Name() string {
    19  	return "debian-matcher"
    20  }
    21  
    22  // Filter implements [driver.Matcher].
    23  func (*Matcher) Filter(record *claircore.IndexRecord) bool {
    24  	if record.Distribution == nil {
    25  		return false
    26  	}
    27  
    28  	switch {
    29  	case record.Distribution.DID == "debian":
    30  		return true
    31  	case record.Distribution.Name == "Debian GNU/Linux":
    32  		return true
    33  	default:
    34  		return false
    35  	}
    36  }
    37  
    38  // Query implements [driver.Matcher].
    39  func (*Matcher) Query() []driver.MatchConstraint {
    40  	return []driver.MatchConstraint{
    41  		driver.DistributionDID,
    42  		driver.DistributionName,
    43  		driver.DistributionVersion,
    44  	}
    45  }
    46  
    47  // Vulnerable implements [driver.Matcher].
    48  func (*Matcher) Vulnerable(ctx context.Context, record *claircore.IndexRecord, vuln *claircore.Vulnerability) (bool, error) {
    49  	if vuln.FixedInVersion == "" {
    50  		return true, nil
    51  	}
    52  	// If Debian reports fixed_version is 0,
    53  	// the package is unaffected.
    54  	if vuln.FixedInVersion == "0" {
    55  		return false, nil
    56  	}
    57  
    58  	v1, err := version.NewVersion(record.Package.Version)
    59  	if err != nil {
    60  		return false, err
    61  	}
    62  	v2, err := version.NewVersion(vuln.FixedInVersion)
    63  	if err != nil {
    64  		return false, err
    65  	}
    66  
    67  	if v1.LessThan(v2) {
    68  		return true, nil
    69  	}
    70  
    71  	return false, nil
    72  }