Skip to content

Commit

Permalink
Compute log sanitization (#1000)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinkaseman authored Jan 21, 2025
1 parent 3e179a7 commit 95c9b2d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
18 changes: 11 additions & 7 deletions pkg/workflows/wasm/host/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io"
"math"
"regexp"

"strings"
"sync"
Expand Down Expand Up @@ -611,21 +612,24 @@ func createLogFn(logger logger.Logger) func(caller *wasmtime.Caller, ptr int32,
args = append(args, k, v)
}

reg, _ := regexp.Compile(`[\[\]\\/\\>\<\&\{\}\:\;]`)
sanitizedMsg := reg.ReplaceAllString(msg, "*")

switch level {
case "debug":
logger.Debugw(msg, args...)
logger.Debugw(sanitizedMsg, args...)
case "info":
logger.Infow(msg, args...)
logger.Infow(sanitizedMsg, args...)
case "warn":
logger.Warnw(msg, args...)
logger.Warnw(sanitizedMsg, args...)
case "error":
logger.Errorw(msg, args...)
logger.Errorw(sanitizedMsg, args...)
case "panic":
logger.Panicw(msg, args...)
logger.Panicw(sanitizedMsg, args...)
case "fatal":
logger.Fatalw(msg, args...)
logger.Fatalw(sanitizedMsg, args...)
default:
logger.Infow(msg, args...)
logger.Infow(sanitizedMsg, args...)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/workflows/wasm/host/test/log/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func BuildWorkflow(config []byte) *sdk.WorkflowSpecFactory {
"test-string-field-key", "this is a test field content",
"test-numeric-field-key", 6400000,
}...)
rsdk.Logger().Infow("Sanitized symbols []\\/><&{}:; Not sanitized symbols ッÖжγ", []interface{}{
"test-string-field-key", "this is a test field content",
"test-numeric-field-key", 6400000,
}...)
return false, nil
})

Expand Down
9 changes: 8 additions & 1 deletion pkg/workflows/wasm/host/wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func Test_Compute_Logs(t *testing.T) {
_, err = m.Run(ctx, req)
assert.Nil(t, err)

require.Len(t, logs.AllUntimed(), 1)
require.Len(t, logs.AllUntimed(), 2)
expectedEntries := []Entry{
{
Log: zapcore.Entry{Level: zapcore.InfoLevel, Message: "building workflow..."},
Expand All @@ -217,6 +217,13 @@ func Test_Compute_Logs(t *testing.T) {
zap.Float64("test-numeric-field-key", 6400000),
},
},
{
Log: zapcore.Entry{Level: zapcore.InfoLevel, Message: "Sanitized symbols *********** Not sanitized symbols ッÖжγ"},
Fields: []zapcore.Field{
zap.String("test-string-field-key", "this is a test field content"),
zap.Float64("test-numeric-field-key", 6400000),
},
},
}
for i := range expectedEntries {
assert.Equal(t, expectedEntries[i].Log.Level, logs.AllUntimed()[i].Entry.Level)
Expand Down

0 comments on commit 95c9b2d

Please sign in to comment.