3
0

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:
Denis Arh
2021-08-16 22:49:27 +02:00
parent ff06953813
commit 5b5bec9767
2 changed files with 28 additions and 3 deletions
+21 -1
View File
@@ -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)
}
}
}
+7 -2
View File
@@ -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)