sanitize log output

This commit is contained in:
Dimitri Herzog 2021-12-20 22:37:32 +01:00
parent 69dc38308c
commit 61ca25c64c
4 changed files with 18 additions and 7 deletions

View File

@ -6,10 +6,10 @@ import (
"strings"
"time"
"github.com/0xERR0R/blocky/log"
"github.com/0xERR0R/blocky/util"
"github.com/go-chi/chi"
log "github.com/sirupsen/logrus"
)
// BlockingControl interface to control the blocking status
@ -76,7 +76,7 @@ func registerBlockingEndpoints(router chi.Router, control BlockingControl) {
// @Success 200 "Blocking is enabled"
// @Router /blocking/enable [get]
func (s *BlockingEndpoint) apiBlockingEnable(_ http.ResponseWriter, _ *http.Request) {
log.Info("enabling blocking...")
log.Log().Info("enabling blocking...")
s.control.EnableBlocking()
}
@ -103,7 +103,7 @@ func (s *BlockingEndpoint) apiBlockingDisable(rw http.ResponseWriter, req *http.
if len(durationParam) > 0 {
duration, err = time.ParseDuration(durationParam)
if err != nil {
log.Errorf("wrong duration format '%s'", durationParam)
log.Log().Errorf("wrong duration format '%s'", log.EscapeInput(durationParam))
rw.WriteHeader(http.StatusBadRequest)
return
@ -117,7 +117,7 @@ func (s *BlockingEndpoint) apiBlockingDisable(rw http.ResponseWriter, req *http.
err = s.control.DisableBlocking(duration, groups)
if err != nil {
log.Error("can't disable the blocking: ", err)
log.Log().Error("can't disable the blocking: ", log.EscapeInput(err.Error()))
rw.WriteHeader(http.StatusBadRequest)
}
}

View File

@ -3,6 +3,8 @@ package log
//go:generate go-enum -f=$GOFILE --marshal --names
import (
"strings"
"github.com/sirupsen/logrus"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
)
@ -44,6 +46,14 @@ func PrefixedLog(prefix string) *logrus.Entry {
return logger.WithField("prefix", prefix)
}
// EscapeInput removes line breaks from input
func EscapeInput(input string) string {
result := strings.ReplaceAll(input, "\n", "")
result = strings.ReplaceAll(result, "\r", "")
return result
}
// ConfigureLogger applies configuration to the global logger
func ConfigureLogger(logLevel Level, formatType FormatType, logTimestamp bool) {
if level, err := logrus.ParseLevel(logLevel.String()); err != nil {

View File

@ -195,9 +195,10 @@ func (r *BlockingResolver) DisableBlocking(duration time.Duration, disableGroups
s.disableEnd = time.Now().Add(duration)
if duration == 0 {
log.Log().Infof("disable blocking for group(s) '%s'", strings.Join(s.disabledGroups, "; "))
log.Log().Infof("disable blocking for group(s) '%s'", log.EscapeInput(strings.Join(s.disabledGroups, "; ")))
} else {
log.Log().Infof("disable blocking for %s for group(s) '%s'", duration, strings.Join(s.disabledGroups, "; "))
log.Log().Infof("disable blocking for %s for group(s) '%s'", duration,
log.EscapeInput(strings.Join(s.disabledGroups, "; ")))
s.enableTimer = time.AfterFunc(duration, func() {
r.EnableBlocking()
log.Log().Info("blocking enabled again")

View File

@ -260,7 +260,7 @@ func configureRootHandler(cfg *config.Config, router *chi.Mux) {
func logAndResponseWithError(err error, message string, writer http.ResponseWriter) {
if err != nil {
log.Log().Error(message, err)
log.Log().Error(message, log.EscapeInput(err.Error()))
http.Error(writer, err.Error(), http.StatusInternalServerError)
}
}