Skip to content

Commit

Permalink
fix: check the type before to cast it to string on filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Matrix86 committed Oct 1, 2020
1 parent 7c51eaa commit f6ff33d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion filters/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (f *Cache) DoFilter(msg *data.Message) (bool, error) {
var text interface{}

if f.target == "main" {
text = msg.GetMessage().(string)
text = msg.GetMessage()
} else if v, ok := msg.GetExtra()[f.target]; ok {
text = v
} else {
Expand Down
5 changes: 3 additions & 2 deletions filters/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ func NewEchoFilter(p map[string]string) (Filter, error) {

// DoFilter is the mandatory method used to "filter" the input data.Message
func (f *Echo) DoFilter(msg *data.Message) (bool, error) {
text := msg.GetMessage()
var text string
data := msg.GetMessage()
if f.printExtra {
for k, v := range msg.GetExtra() {
text = fmt.Sprintf("%s [%s: %s] ", text, k, v)
text = fmt.Sprintf("%#v [%#v: %#v] ", data, k, v)
}
}
log.Info("%s", text)
Expand Down
12 changes: 11 additions & 1 deletion filters/hash.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package filters

import (
"fmt"
"regexp"

"github.com/Matrix86/driplane/data"
Expand Down Expand Up @@ -63,7 +64,16 @@ func NewHashFilter(p map[string]string) (Filter, error) {

// DoFilter is the mandatory method used to "filter" the input data.Message
func (f *Hash) DoFilter(msg *data.Message) (bool, error) {
text := msg.GetTarget(f.target).(string)
var text string

if v, ok := msg.GetTarget(f.target).(string); ok {
text = v
} else if v, ok := msg.GetTarget(f.target).([]byte); ok {
text = string(v)
} else {
// ERROR this filter can't be used with different types
return false, fmt.Errorf("received data is not a string")
}
match := f.regex.FindAllStringSubmatch(text, -1)
if match != nil {
for _, m := range match {
Expand Down
13 changes: 12 additions & 1 deletion filters/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package filters

import (
"crypto/tls"
"fmt"
"html/template"
"strconv"
"strings"
Expand Down Expand Up @@ -81,7 +82,17 @@ func NewMailFilter(p map[string]string) (Filter, error) {
// DoFilter is the mandatory method used to "filter" the input data.Message
func (f *Mail) DoFilter(msg *data.Message) (bool, error) {
var err error
text := msg.GetMessage().(string)
var text string

if v, ok := msg.GetMessage().(string); ok {
text = v
} else if v, ok := msg.GetMessage().([]byte); ok {
text = string(v)
} else {
// ERROR this filter can't be used with different types
return false, fmt.Errorf("received data is not a string")
}

if f.template != nil {
text, err = msg.ApplyPlaceholder(f.template)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion filters/pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (f *PDF) DoFilter(msg *data.Message) (bool, error) {
} else {
if _, ok := msg.GetTarget(f.target).([]byte); !ok {
// ERROR this filter can't be used with different types
return false, fmt.Errorf("received data is not a string")
return false, fmt.Errorf("received data is not a []byte")
}

buf := bytes.NewBuffer(msg.GetTarget(f.target).([]byte))
Expand Down
12 changes: 11 additions & 1 deletion filters/url.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package filters

import (
"fmt"
"regexp"
"strings"

Expand Down Expand Up @@ -58,7 +59,16 @@ func NewURLFilter(p map[string]string) (Filter, error) {

// DoFilter is the mandatory method used to "filter" the input data.Message
func (f *URL) DoFilter(msg *data.Message) (bool, error) {
text := msg.GetTarget(f.target).(string)
var text string

if v, ok := msg.GetTarget(f.target).(string); ok {
text = v
} else if v, ok := msg.GetTarget(f.target).([]byte); ok {
text = string(v)
} else {
// ERROR this filter can't be used with different types
return false, fmt.Errorf("received data is not a string")
}

found := false
match := f.rURL.FindAllStringSubmatch(text, -1)
Expand Down

0 comments on commit f6ff33d

Please sign in to comment.