trim whitespace from wordpress values (#2945)

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
Alex Goodman 2024-06-10 13:35:01 -04:00 committed by GitHub
parent c43f4fb416
commit f966bcfd03
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 8 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings"
"github.com/anchore/syft/internal" "github.com/anchore/syft/internal"
"github.com/anchore/syft/syft/artifact" "github.com/anchore/syft/syft/artifact"
@ -39,7 +40,6 @@ type pluginData struct {
func parseWordpressPluginFiles(_ context.Context, _ file.Resolver, _ *generic.Environment, reader file.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) { func parseWordpressPluginFiles(_ context.Context, _ file.Resolver, _ *generic.Environment, reader file.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
var pkgs []pkg.Package var pkgs []pkg.Package
var fields = make(map[string]interface{})
buffer := make([]byte, contentBufferSize) buffer := make([]byte, contentBufferSize)
_, err := reader.Read(buffer) _, err := reader.Read(buffer)
@ -47,13 +47,7 @@ func parseWordpressPluginFiles(_ context.Context, _ file.Resolver, _ *generic.En
return nil, nil, fmt.Errorf("failed to read %s file: %w", reader.Location.Path(), err) return nil, nil, fmt.Errorf("failed to read %s file: %w", reader.Location.Path(), err)
} }
fileContent := string(buffer) fields := extractFields(string(buffer))
for field, pattern := range patterns {
matchMap := internal.MatchNamedCaptureGroups(pattern, fileContent)
if value := matchMap[field]; value != "" {
fields[field] = value
}
}
name, nameOk := fields["name"] name, nameOk := fields["name"]
version, versionOk := fields["version"] version, versionOk := fields["version"]
@ -96,3 +90,15 @@ func parseWordpressPluginFiles(_ context.Context, _ file.Resolver, _ *generic.En
return pkgs, nil, nil return pkgs, nil, nil
} }
func extractFields(in string) map[string]any {
var fields = make(map[string]interface{})
for field, pattern := range patterns {
matchMap := internal.MatchNamedCaptureGroups(pattern, in)
if value := matchMap[field]; value != "" {
fields[field] = strings.TrimSpace(value)
}
}
return fields
}

View File

@ -3,6 +3,8 @@ package wordpress
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/anchore/syft/syft/file" "github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/pkg/cataloger/internal/pkgtest" "github.com/anchore/syft/syft/pkg/cataloger/internal/pkgtest"
@ -30,3 +32,28 @@ func TestParseWordpressPluginFiles(t *testing.T) {
pkgtest.TestFileParser(t, fixture, parseWordpressPluginFiles, []pkg.Package{expectedPkg}, nil) pkgtest.TestFileParser(t, fixture, parseWordpressPluginFiles, []pkg.Package{expectedPkg}, nil)
} }
func Test_extractFields(t *testing.T) {
tests := []struct {
name string
in string
want map[string]any
}{
{
name: "carriage returns are stripped",
in: "Plugin Name: WP Migration\r\nVersion: 5.3\r\nLicense: GPLv3\r\nAuthor: MonsterInsights\r\nAuthor URI: https://servmask.com/\r\n",
want: map[string]any{
"name": "WP Migration",
"version": "5.3",
"license": "GPLv3",
"author": "MonsterInsights",
"author_uri": "https://servmask.com/",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, extractFields(tt.in))
})
}
}