From 5b5bec976757bfa8b74c351d9d2bc3041c4766b5 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Mon, 16 Aug 2021 22:49:27 +0200 Subject: [PATCH] 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 --- automation/automation/corredor_handler.go | 22 +++++++++++++++++++++- system/rest/actionlog.go | 9 +++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/automation/automation/corredor_handler.go b/automation/automation/corredor_handler.go index 79432a89f..cb88d119f 100644 --- a/automation/automation/corredor_handler.go +++ b/automation/automation/corredor_handler.go @@ -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) + } + } +} diff --git a/system/rest/actionlog.go b/system/rest/actionlog.go index d811462d5..dbefebd28 100644 --- a/system/rest/actionlog.go +++ b/system/rest/actionlog.go @@ -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)