-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(scanner/redhatbase): don't return error when parse failure of source file #2092
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3d8f471
to
04e158a
Compare
I suggest that splitFileName follow the rpm implementation and also add a test case. :100644 100644 05debc1 1b749f3 M scanner/redhatbase.go
:100644 100644 28399d4 19e6d5c M scanner/redhatbase_test.go
diff --git a/scanner/redhatbase.go b/scanner/redhatbase.go
index 05debc1..1b749f3 100644
--- a/scanner/redhatbase.go
+++ b/scanner/redhatbase.go
@@ -582,7 +582,7 @@ func (o *redhatBase) parseInstalledPackagesLine(line string) (*models.Package, *
case "(none)":
return nil, nil
default:
- n, v, r, err := splitFileName(fields[5])
+ n, v, r, _, _, err := splitFileName(fields[5])
if err != nil {
o.warns = append(o.warns, xerrors.Errorf("Failed to parse source rpm file. err: %w", err))
return nil, nil
@@ -638,7 +638,7 @@ func (o *redhatBase) parseInstalledPackagesLineFromRepoquery(line string) (*mode
case "(none)":
return nil, nil
default:
- n, v, r, err := splitFileName(fields[5])
+ n, v, r, _, _, err := splitFileName(fields[5])
if err != nil {
o.warns = append(o.warns, xerrors.Errorf("Failed to parse source rpm file. err: %w", err))
return nil, nil
@@ -688,29 +688,41 @@ func (o *redhatBase) parseInstalledPackagesLineFromRepoquery(line string) (*mode
}
}
-// https://github.com/aquasecurity/trivy/blob/51f2123c5ccc4f7a37d1068830b6670b4ccf9ac8/pkg/fanal/analyzer/pkg/rpm/rpm.go#L212-L241
-func splitFileName(filename string) (name, ver, rel string, err error) {
+// splitFileName returns a name, version, release, epoch, arch:
+//
+// e.g.
+// foo-1.0-1.i386.rpm => foo, 1.0, 1, i386
+// 1:bar-9-123a.ia64.rpm => bar, 9, 123a, 1, ia64
+//
+// https://github.com/rpm-software-management/yum/blob/043e869b08126c1b24e392f809c9f6871344c60d/rpmUtils/miscutils.py#L301
+func splitFileName(filename string) (name, ver, rel, epoch, arch string, err error) {
basename := strings.TrimSuffix(filename, ".rpm")
archIndex := strings.LastIndex(basename, ".")
if archIndex == -1 {
- return "", "", "", xerrors.Errorf("unexpected file name. expected: %q, actual: %q", "<name>-<version>-<release>.<arch>.rpm", filename)
+ return "", "", "", "", "", xerrors.Errorf("unexpected file name. expected: %q, actual: %q", "<name>-<version>-<release>.<arch>.rpm", fmt.Sprintf("%s.rpm", filename))
}
+ arch = basename[archIndex+1:]
relIndex := strings.LastIndex(basename[:archIndex], "-")
if relIndex == -1 {
- return "", "", "", xerrors.Errorf("unexpected file name. expected: %q, actual: %q", "<name>-<version>-<release>.<arch>.rpm", filename)
+ return "", "", "", "", "", xerrors.Errorf("unexpected file name. expected: %q, actual: %q", "<name>-<version>-<release>.<arch>.rpm", fmt.Sprintf("%s.rpm", filename))
}
rel = basename[relIndex+1 : archIndex]
verIndex := strings.LastIndex(basename[:relIndex], "-")
if verIndex == -1 {
- return "", "", "", xerrors.Errorf("unexpected file name. expected: %q, actual: %q", "<name>-<version>-<release>.<arch>.rpm", filename)
+ return "", "", "", "", "", xerrors.Errorf("unexpected file name. expected: %q, actual: %q", "<name>-<version>-<release>.<arch>.rpm", fmt.Sprintf("%s.rpm", filename))
}
ver = basename[verIndex+1 : relIndex]
- name = basename[:verIndex]
- return name, ver, rel, nil
+ epochIndex := strings.Index(basename, ":")
+ if epochIndex != -1 {
+ epoch = basename[:epochIndex]
+ }
+
+ name = basename[epochIndex+1 : verIndex]
+ return name, ver, rel, epoch, arch, nil
}
func (o *redhatBase) parseRpmQfLine(line string) (pkg *models.Package, ignored bool, err error) {
diff --git a/scanner/redhatbase_test.go b/scanner/redhatbase_test.go
index 28399d4..19e6d5c 100644
--- a/scanner/redhatbase_test.go
+++ b/scanner/redhatbase_test.go
@@ -342,6 +342,22 @@ func Test_redhatBase_parseInstalledPackagesLine(t *testing.T) {
},
wantsp: nil,
},
+ {
+ name: "epoch in source package",
+ args: args{line: "bar 1 9 123a ia64 1:bar-9-123a.src.rpm"},
+ wantbp: &models.Package{
+ Name: "bar",
+ Version: "1:9",
+ Release: "123a",
+ Arch: "ia64",
+ },
+ wantsp: &models.SrcPackage{
+ Name: "bar",
+ Version: "1:9-123a",
+ Arch: "src",
+ BinaryNames: []string{"bar"},
+ },
+ },
{
name: "new: package 1",
args: args{line: "gpg-pubkey 0 f5282ee4 58ac92a3 (none) (none)"},
@@ -402,6 +418,17 @@ func Test_redhatBase_parseInstalledPackagesLine(t *testing.T) {
BinaryNames: []string{"community-mysql"},
},
},
+ {
+ name: "invalid source package",
+ args: args{line: "elasticsearch 0 8.17.0 1 x86_64 elasticsearch-8.17.0-1-src.rpm (none)"},
+ wantbp: &models.Package{
+ Name: "elasticsearch",
+ Version: "8.17.0",
+ Release: "1",
+ Arch: "x86_64",
+ },
+ wantsp: nil,
+ },
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
|
MaineK00n
reviewed
Dec 18, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #2092 (comment)
04e158a
to
f0ae212
Compare
Applied! |
MaineK00n
approved these changes
Dec 18, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
If this Pull Request is work in progress, Add a prefix of “[WIP]” in the title.
What did you implement:
Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context.
Before this PR, some rpms with not-very-good source file name format let vuls scan failed with errors like:
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Preparation:
Install elasticsearch with the steps described in https://www.elastic.co/guide/en/elasticsearch/reference/current/rpm.html
Package list output from rpm command is:
The filename column value is
-src.rpm
, not.src.rpm
.Before:
After:
Checklist:
You don't have to satisfy all of the following.
make fmt
make test
Reference