Skip to content

Commit

Permalink
sanitize msg in output logger (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
kat-statsig authored Jun 27, 2024
1 parent e477619 commit 65247f2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
5 changes: 1 addition & 4 deletions error_boundary.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package statsig
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"runtime"
"strconv"
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -148,8 +146,7 @@ func (e *errorBoundary) logExceptionWithOptions(exception error, options logExce
}

if options.LogToOutput {
filteredError := strings.Replace(exceptionString, e.sdkKey, "******", -1)
Logger().LogError(errors.New(filteredError))
Logger().LogError(exception)
}
if !options.BypassDedupe && e.checkSeen(exceptionString) {
return
Expand Down
19 changes: 15 additions & 4 deletions output_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"regexp"
"time"
)

Expand All @@ -20,16 +21,20 @@ type OutputLogger struct {

func (o *OutputLogger) Log(msg string, err error) {
if o.isInitialized() && o.options.LogCallback != nil {
o.options.LogCallback(msg, err)
o.options.LogCallback(sanitize(msg), err)
} else {
timestamp := time.Now().Format(time.RFC3339)

formatted := fmt.Sprintf("[%s][Statsig] %s", timestamp, msg)

sanitized := ""
if err != nil {
formatted += err.Error()
fmt.Fprintln(os.Stderr, formatted)
sanitized = sanitize(formatted)
fmt.Fprintln(os.Stderr, sanitized)
} else if msg != "" {
fmt.Println(formatted)
sanitized = sanitize(formatted)
fmt.Println(sanitized)
}
}
}
Expand Down Expand Up @@ -60,10 +65,16 @@ func (o *OutputLogger) LogError(err interface{}) {
case error:
o.Log("", errTyped)
default:
fmt.Fprintln(os.Stderr, err)
sanitized := sanitize(fmt.Sprintf("%+v", err))
fmt.Fprintln(os.Stderr, sanitized)
}
}

func (o *OutputLogger) isInitialized() bool {
return o != nil
}

func sanitize(string string) string {
keyPattern := regexp.MustCompile(`secret-[a-zA-Z0-9]+`)
return keyPattern.ReplaceAllString(string, "secret-****")
}

0 comments on commit 65247f2

Please sign in to comment.