From b78e83b05563417464db8f49a2ae64ff62cb0bc7 Mon Sep 17 00:00:00 2001 From: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> Date: Thu, 8 Dec 2022 14:15:56 +0100 Subject: [PATCH] Do not allow unknown fields in JSON configuration file --- receivers/ipmiReceiver.go | 5 +++-- receivers/redfishReceiver.go | 9 +++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/receivers/ipmiReceiver.go b/receivers/ipmiReceiver.go index 7eecba1..e3d544a 100644 --- a/receivers/ipmiReceiver.go +++ b/receivers/ipmiReceiver.go @@ -352,8 +352,9 @@ func NewIPMIReceiver(name string, config json.RawMessage) (Receiver, error) { // Read the IPMI receiver specific JSON config if len(config) > 0 { - err := json.Unmarshal(config, &configJSON) - if err != nil { + d := json.NewDecoder(bytes.NewReader(config)) + d.DisallowUnknownFields() + if err := d.Decode(&configJSON); err != nil { cclog.ComponentError(r.name, "Error reading config:", err.Error()) return nil, err } diff --git a/receivers/redfishReceiver.go b/receivers/redfishReceiver.go index 72141c5..767a2de 100644 --- a/receivers/redfishReceiver.go +++ b/receivers/redfishReceiver.go @@ -353,10 +353,10 @@ func (r *RedfishReceiver) readProcessorMetrics( if err != nil { return fmt.Errorf("unable to read JSON for processor metrics: %+w", err) } - err = json.NewDecoder(bytes.NewReader(body)).Decode(&processorMetrics) + err = json.Unmarshal(body, &processorMetrics) if err != nil { return fmt.Errorf( - "unable to decode JSON='%s' for processor metrics: %+w", + "unable to unmarshal JSON='%s' for processor metrics: %+w", string(body), err, ) @@ -673,8 +673,9 @@ func NewRedfishReceiver(name string, config json.RawMessage) (Receiver, error) { // Read the redfish receiver specific JSON config if len(config) > 0 { - err := json.Unmarshal(config, &configJSON) - if err != nil { + d := json.NewDecoder(bytes.NewReader(config)) + d.DisallowUnknownFields() + if err := d.Decode(&configJSON); err != nil { cclog.ComponentError(r.name, "Error reading config:", err.Error()) return nil, err }