morningconsult / go-elasticsearch-alerts

Elasticsearch Alerting Daemon
https://morningconsult.github.io/go-elasticsearch-alerts/
Apache License 2.0
92 stars 17 forks source link

segfault when opening rules file #124

Open jyoung15 opened 2 years ago

jyoung15 commented 2 years ago

When attempting to open a rules files that is unreadable (for example due to file permissions), the program crashes:

Started Go Elasticsearch Alerts.
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x713a16]
goroutine 1 [running]:
os.(*File).Name(...)
        /usr/lib/go/src/os/file.go:57
github.com/morningconsult/go-elasticsearch-alerts/config.ParseRules()
        /home/xxxxx/go-elasticsearch-alerts.git/config/parse.go:295 +0x476

This appears to be due to invalid use of the file pointer when os.Open returns an error. Suggested fix below. Can submit pull request upon request.

diff --git a/config/parse.go b/config/parse.go
index 9bb5d12..6b700b6 100644
--- a/config/parse.go
+++ b/config/parse.go
@@ -290,11 +290,11 @@ func ParseRules() ([]RuleConfig, error) {
                file, err := os.Open(filepath.Clean(ruleFile))
                if err != nil {
                        if os.IsNotExist(err) {
                                continue
                        }
-                       return nil, xerrors.Errorf("error opening file %s: %v", file.Name(), err)
+                       return nil, xerrors.Errorf("error opening file: %v", err)
                }

                dec := json.NewDecoder(file)
                dec.UseNumber()

With patch above applied, error message appears as expected:

error opening file: open rules/xxxxx.json: permission denied
alexdulin commented 2 years ago

Hi @jyoung15, thanks for pointing this out! Please feel free to open a pull request with your suggested fix. Contributions are welcome!