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

Trim whitespace from wordpress values #2945

Merged
merged 1 commit into from
Jun 10, 2024
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
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))
})
}
}
Loading