Covert ID (uint64) values before JSON encode
This is problematic in two places: - accessing JSON serialized meta data on action log - serializing Corredor script argument
This commit is contained in:
@@ -3,6 +3,8 @@ package automation
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/corredor"
|
||||
"github.com/cortezaproject/corteza-server/pkg/eventbus"
|
||||
"github.com/cortezaproject/corteza-server/pkg/expr"
|
||||
@@ -51,7 +53,10 @@ func (h corredorHandler) exec(ctx context.Context, args *corredorExecArgs) (r *c
|
||||
}
|
||||
|
||||
func makeScriptArgs(in *expr.Vars) *scriptArgs {
|
||||
return &scriptArgs{payload: in.Dict()}
|
||||
payload := in.Dict()
|
||||
sanitizeMapStringInterface(payload)
|
||||
|
||||
return &scriptArgs{payload: payload}
|
||||
}
|
||||
|
||||
// mimic onManual event on system:
|
||||
@@ -85,3 +90,18 @@ func (a *scriptArgs) Decode(enc map[string][]byte) (err error) {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Sanitizing all uint64 values that are encoded into JSON
|
||||
func sanitizeMapStringInterface(m map[string]interface{}) {
|
||||
for k := range m {
|
||||
switch v := m[k].(type) {
|
||||
case uint64:
|
||||
// make sure uint64 values on fields ending with ID
|
||||
// are properly encoded as strings
|
||||
m[k] = strconv.FormatUint(v, 10)
|
||||
|
||||
case map[string]interface{}:
|
||||
sanitizeMapStringInterface(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,12 +121,17 @@ func (ctrl Actionlog) makeFilterPayload(ctx context.Context, ee []*actionlog.Act
|
||||
return &actionlogPayload{Filter: f, Set: pp}, nil
|
||||
}
|
||||
|
||||
// Making sure JS can read our values
|
||||
// Making sure JS can read old entries that were encoded
|
||||
// as numeric. When decoded from the JSON stored in the DB
|
||||
// they are of type float64. We'll cast them and store as uint64
|
||||
//
|
||||
// We do not want to do that to any numeric values just for the keys ending with
|
||||
// "ID" or "By"
|
||||
func sanitizeMapStringInterface(m map[string]interface{}) {
|
||||
for k := range m {
|
||||
switch v := m[k].(type) {
|
||||
case float64:
|
||||
if strings.HasSuffix(k, "ID") {
|
||||
if strings.HasSuffix(k, "ID") || strings.HasSuffix(k, "By") {
|
||||
// make sure uint64 values on fields ending with ID
|
||||
// are properly encoded as strings
|
||||
m[k] = strconv.FormatUint(uint64(v), 10)
|
||||
|
||||
Reference in New Issue
Block a user