From 61a3facb64e37a05ef0918e9fdbca5f6c62ba6f8 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sun, 18 Dec 2022 14:48:57 +0100 Subject: [PATCH] [core] Add validation for rule UUIDs to be unique Signed-off-by: Knut Ahlers --- config.go | 37 +++++++++++++++++++++++++++---------- configEditor_rules.go | 6 ++++++ 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/config.go b/config.go index 9f0b6cc..1b2b5a4 100644 --- a/config.go +++ b/config.go @@ -15,6 +15,7 @@ import ( log "github.com/sirupsen/logrus" "gopkg.in/yaml.v2" + "github.com/Luzifer/go_helpers/v2/str" "github.com/Luzifer/twitch-bot/v3/plugins" ) @@ -104,16 +105,8 @@ func loadConfig(filename string) error { return errors.Wrap(err, "parsing config") } - if len(tmpConfig.Channels) == 0 { - log.Warn("Loaded config with empty channel list") - } - - if len(tmpConfig.Rules) == 0 { - log.Warn("Loaded config with empty ruleset") - } - - if err = tmpConfig.validateRuleActions(); err != nil { - return errors.Wrap(err, "validating rule actions") + if err = tmpConfig.runLoadChecks(); err != nil { + return errors.Wrap(err, "running load-checks on config") } configLock.Lock() @@ -324,6 +317,30 @@ func (c *configFile) fixMissingUUIDs() { } } +func (c *configFile) runLoadChecks() (err error) { + if len(c.Channels) == 0 { + log.Warn("Loaded config with empty channel list") + } + + if len(c.Rules) == 0 { + log.Warn("Loaded config with empty ruleset") + } + + var seen []string + for _, r := range c.Rules { + if r.UUID != "" && str.StringInSlice(r.UUID, seen) { + return errors.New("duplicate rule UUIDs found") + } + seen = append(seen, r.UUID) + } + + if err = c.validateRuleActions(); err != nil { + return errors.Wrap(err, "validating rule actions") + } + + return nil +} + func (c *configFile) updateAutoMessagesFromConfig(old *configFile) { for idx, nam := range c.AutoMessages { // By default assume last message to be sent now diff --git a/configEditor_rules.go b/configEditor_rules.go index b6c00a4..025df01 100644 --- a/configEditor_rules.go +++ b/configEditor_rules.go @@ -99,6 +99,12 @@ func configEditorRulesAdd(w http.ResponseWriter, r *http.Request) { } if err := patchConfig(cfg.Config, user, "", "Add rule", func(c *configFile) error { + for _, r := range c.Rules { + if r.UUID == msg.UUID { + return errors.New("rule already exists (UUID duplicate)") + } + } + c.Rules = append(c.Rules, msg) return nil }); err != nil {