Skip to content
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

Allow CPE parsing failures #425

Merged
merged 2 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions grype/cpe/cpe.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package cpe

import (
"github.com/anchore/grype/internal/log"
"github.com/anchore/syft/syft/pkg"
)

func NewSlice(cpeStrs ...string) ([]pkg.CPE, error) {
ret := make([]pkg.CPE, len(cpeStrs))
for idx, c := range cpeStrs {
var cpes []pkg.CPE
for _, c := range cpeStrs {
value, err := pkg.NewCPE(c)
if err != nil {
return nil, err
log.Warnf("unable to hydrate CPE for string %q, omitting from result CPE slice: %v", c, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe "hydrate" -> "parse" ? or less verbose: "Excluding invalid CPE %q: %v"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kzantow I like that! I'll create a PR for this

continue
}
ret[idx] = value

cpes = append(cpes, value)
}
return ret, nil
return cpes, nil
}

func MatchWithoutVersion(c pkg.CPE, candidates []pkg.CPE) []pkg.CPE {
Expand Down
16 changes: 15 additions & 1 deletion grype/pkg/syft_json_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"os"
"testing"

"github.com/stretchr/testify/assert"

"github.com/anchore/syft/syft/source"

"github.com/anchore/syft/syft/distro"
Expand Down Expand Up @@ -156,7 +158,19 @@ func TestParseSyftJSON(t *testing.T) {
}
}

// Note that the fixture has been modified from the real syft output to include less packages, CPEs, layers,
func TestParseSyftJSON_BadCPEs(t *testing.T) {
const testFixture = "test-fixtures/syft-java-bad-cpes.json"
fh, err := os.Open(testFixture)
if err != nil {
t.Fatalf("unable to open fixture: %+v", err)
}

pkgs, _, err := parseSyftJSON(fh)
assert.NoError(t, err)
assert.Len(t, pkgs, 1)
}

// Note that the fixture has been modified from the real syft output to include fewer packages, CPEs, layers,
// and package IDs are removed so that the test case variable isn't unwieldingly huge.
var springImageTestCase = struct {
Fixture string
Expand Down
104 changes: 104 additions & 0 deletions grype/pkg/test-fixtures/syft-java-bad-cpes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"artifacts": [
{
"id": "da8f3d4c-fc48-4a79-a775-31c730ce5f97",
"name": "wstx-asl",
"version": "3.2.7",
"type": "java-archive",
"foundBy": "java-cataloger",
"locations": [
{
"path": "/hudson.war",
"layerID": "sha256:540ce9731a52867a29db67bc9b7ead11db04918f00a6e432627d21e1160ed92b"
}
],
"licenses": [],
"language": "java",
"cpes": [
"cpe:2.3:a:http://jcp_org/en/jsr/detail?id=173:wstx_asl:3.2.7:*:*:*:*:*:*:*",
"cpe:2.3:a:http://jcp_org/en/jsr/detail?id=173:wstx-asl:3.2.7:*:*:*:*:*:*:*",
"cpe:2.3:a:http://jcp-org/en/jsr/detail?id=173:wstx-asl:3.2.7:*:*:*:*:*:*:*",
"cpe:2.3:a:http://jcp-org/en/jsr/detail?id=173:wstx_asl:3.2.7:*:*:*:*:*:*:*",
"cpe:2.3:a:woodstox_codehaus_org:wstx-asl:3.2.7:*:*:*:*:*:*:*",
"cpe:2.3:a:woodstox-codehaus-org:wstx-asl:3.2.7:*:*:*:*:*:*:*",
"cpe:2.3:a:woodstox-codehaus-org:wstx_asl:3.2.7:*:*:*:*:*:*:*",
"cpe:2.3:a:woodstox_codehaus_org:wstx_asl:3.2.7:*:*:*:*:*:*:*",
"cpe:2.3:a:wstx-asl:wstx-asl:3.2.7:*:*:*:*:*:*:*",
"cpe:2.3:a:wstx-asl:wstx_asl:3.2.7:*:*:*:*:*:*:*",
"cpe:2.3:a:wstx_asl:wstx-asl:3.2.7:*:*:*:*:*:*:*",
"cpe:2.3:a:wstx_asl:wstx_asl:3.2.7:*:*:*:*:*:*:*",
"cpe:2.3:a:wstx:wstx-asl:3.2.7:*:*:*:*:*:*:*",
"cpe:2.3:a:wstx:wstx_asl:3.2.7:*:*:*:*:*:*:*"
],
"purl": "",
"metadataType": "JavaMetadata",
"metadata": {
"virtualPath": "/hudson.war:WEB-INF/lib/wstx-asl-3.2.7.jar",
"manifest": {
"main": {
"Ant-Version": "Apache Ant 1.6.5",
"Built-By": "tatu",
"Created-By": "1.4.2_03-b02 (Sun Microsystems Inc.)",
"Implementation-Title": "WoodSToX XML-processor",
"Implementation-Vendor": "woodstox.codehaus.org",
"Implementation-Version": "3.2.7",
"Manifest-Version": "1.0",
"Specification-Title": "StAX 1.0 API",
"Specification-Vendor": "http://jcp.org/en/jsr/detail?id=173",
"Specification-Version": "1.0"
}
}
}
}
],
"artifactRelationships": [],
"source": {
"type": "image",
"target": {
"userInput": "docker.io/anchore/test_images:java",
"imageID": "sha256:55125c059cf3d9e8a179c3b543deeaf8613b1aa3101515cd51a7918b857b8eea",
"manifestDigest": "sha256:64dc1086a990469c65bf65c28c8d2d9062dd32aa566d045e1fe71e32e6b0d600",
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"tags": [
"anchore/test_images:java"
],
"imageSize": 39383202,
"layers": [
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"digest": "sha256:8ea3b23f387bedc5e3cee574742d748941443c328a75f511eb37b0d8b6164130",
"size": 5608905
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"digest": "sha256:540ce9731a52867a29db67bc9b7ead11db04918f00a6e432627d21e1160ed92b",
"size": 33772593
},
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"digest": "sha256:a155363a67edf6e6462aa47445c3d900b84cec3320b62405b495ddf65035c039",
"size": 1704
}
],
"manifest": "eyJzY2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mplbmtpbnMtY2kub3JnL2Rvd25sb2FkL3dhci8xLjM5MC9odWRzb24ud2FyIn0seyJjcmVhdGVkIjoiMjAyMS0wNC0wMVQxNToyODoyNy4zOTE0OTA5NjFaIiwiY3JlYXRlZF9ieSI6Ii9iaW4vc2ggLWMgIyhub3ApIENPUFkgZmlsZToyZGQ4NTczMWE0MDU2OWJmZWMwNDg4MGU0NTg5ODc0OGM0MjAyMGUyMzg2ZGExZWJmMDk0OGJmNDhiYjBmZGZmIGluIC8gIn1dLCJvcyI6ImxpbnV4Iiwicm9vdGZzIjp7InR5cGUiOiJsYXllcnMiLCJkaWZmX2lkcyI6WyJzaGEyNTY6OGVhM2IyM2YzODdiZWRjNWUzY2VlNTc0NzQyZDc0ODk0MTQ0M2MzMjhhNzVmNTExZWIzN2IwZDhiNjE2NDEzMCIsInNoYTI1Njo1NDBjZTk3MzFhNTI4NjdhMjlkYjY3YmM5YjdlYWQxMWRiMDQ5MThmMDBhNmU0MzI2MjdkMjFlMTE2MGVkOTJiIiwic2hhMjU2OmExNTUzNjNhNjdlZGY2ZTY0NjJhYTQ3NDQ1YzNkOTAwYjg0Y2VjMzMyMGI2MjQwNWI0OTVkZGY2NTAzNWMwMzkiXX19",
"repoDigests": [
"anchore/test_images@sha256:4f6203a146e4c056f09fd72c687adfb23e75b18b58e3ea7c9a25a8af6699a381"
],
"scope": "Squashed"
}
},
"distro": {
"name": "alpine",
"version": "3.13.4",
"idLike": ""
},
"descriptor": {
"name": "syft",
"version": "0.23.0"
},
"schema": {
"version": "1.1.0",
"url": "https://raw.githubusercontent.com/anchore/syft/main/schema/json/schema-1.1.0.json"
}
}