-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
fix(cli): Handle empty ignore files more gracefully #7962
base: main
Are you sure you want to change the base?
Conversation
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.
It looks good! I left small comments.
log.Debug("Found an ignore yaml", log.FilePath(ignoreFile)) | ||
|
||
// Parse the YAML content | ||
var ignoreConfig IgnoreConfig | ||
if err = yaml.NewDecoder(f).Decode(&ignoreConfig); err != nil { | ||
if err = yaml.Unmarshal(b, &ignoreConfig); err != nil { |
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.
Decode
is better in terms of memory efficiency. We should leave a comment explaining why we use Unmarshal
here. Otherwise, someone may change it back in the future for performance reasons.
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.
added b304f64
Co-authored-by: Teppei Fukuda <knqyf263@gmail.com>
}) | ||
|
||
// TODO(simar7): This test currently fails as we don't validate | ||
// the correctness of ignore file when not in YAML format |
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.
If the test file ends with .yaml
, does this test work as expected?
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.
Yes I added a test for it
trivy/pkg/result/ignore_test.go
Lines 45 to 54 in e12cb88
t.Run("invalid YAML file passed", func(t *testing.T) { | |
f, err := os.CreateTemp("", "TestParseIgnoreFile-*.yaml") | |
require.NoError(t, err) | |
defer os.Remove(f.Name()) | |
_, _ = f.WriteString("this file is not a yaml file") | |
got, err := ParseIgnoreFile(context.TODO(), f.Name()) | |
assert.Contains(t, err.Error(), "yaml decode error") | |
assert.Empty(t, got) | |
}) |
We don't validate the correctness of non yaml ignore files. We silently return with no error.
Description
Also adds a log message to the debug stream when such a case occurs.
Related issues
Checklist