Skip to content

Commit

Permalink
trim whitespace from wordpress values (#2945)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
  • Loading branch information
wagoodman committed Jun 10, 2024
1 parent c43f4fb commit f966bcf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
22 changes: 14 additions & 8 deletions syft/pkg/cataloger/wordpress/parse_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"path/filepath"
"regexp"
"strings"

"github.com/anchore/syft/internal"
"github.com/anchore/syft/syft/artifact"
Expand Down Expand Up @@ -39,21 +40,14 @@ type pluginData struct {

func parseWordpressPluginFiles(_ context.Context, _ file.Resolver, _ *generic.Environment, reader file.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
var pkgs []pkg.Package
var fields = make(map[string]interface{})
buffer := make([]byte, contentBufferSize)

_, err := reader.Read(buffer)
if err != nil {
return nil, nil, fmt.Errorf("failed to read %s file: %w", reader.Location.Path(), err)
}

fileContent := string(buffer)
for field, pattern := range patterns {
matchMap := internal.MatchNamedCaptureGroups(pattern, fileContent)
if value := matchMap[field]; value != "" {
fields[field] = value
}
}
fields := extractFields(string(buffer))

name, nameOk := fields["name"]
version, versionOk := fields["version"]
Expand Down Expand Up @@ -96,3 +90,15 @@ func parseWordpressPluginFiles(_ context.Context, _ file.Resolver, _ *generic.En

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
}
27 changes: 27 additions & 0 deletions syft/pkg/cataloger/wordpress/parse_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package wordpress
import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/pkg/cataloger/internal/pkgtest"
Expand Down Expand Up @@ -30,3 +32,28 @@ func TestParseWordpressPluginFiles(t *testing.T) {

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))
})
}
}

0 comments on commit f966bcf

Please sign in to comment.