From 34a1a6d13d5acbeeb841aa5cf540cdd80e94e785 Mon Sep 17 00:00:00 2001 From: Mitsu <124433727+Mizuw@users.noreply.github.com> Date: Wed, 9 Jul 2025 20:42:30 +0200 Subject: [PATCH] validate provided HEX-Code --- cmd/internal/db/queries.go | 6 ++++++ cmd/utils/utils.go | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/cmd/internal/db/queries.go b/cmd/internal/db/queries.go index 9bbbedf..0811fa4 100644 --- a/cmd/internal/db/queries.go +++ b/cmd/internal/db/queries.go @@ -1,8 +1,11 @@ package db import ( + "errors" + "github.com/siestaw/laterna/server/cmd/internal/logger" "github.com/siestaw/laterna/server/cmd/internal/models" + "github.com/siestaw/laterna/server/cmd/utils" ) func ViewColor(id int) (*models.LampState, error) { @@ -19,6 +22,9 @@ func ViewColor(id int) (*models.LampState, error) { } func SetColor(id int, color string) error { + if !utils.IsValidHexColor(color) { + return errors.New("invalid HEX Code: %s") + } currentColor, err := ViewColor(id) if err != nil { return err diff --git a/cmd/utils/utils.go b/cmd/utils/utils.go index 3321457..d0a2ddc 100644 --- a/cmd/utils/utils.go +++ b/cmd/utils/utils.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "net/http" + "regexp" "strconv" "time" @@ -20,6 +21,11 @@ func IDtoInt(id string) (int, error) { } return idInt, nil } +func IsValidHexColor(color string) bool { + pattern := `^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$` + match, _ := regexp.MatchString(pattern, color) + return match +} func HTTPErrorHandling(w http.ResponseWriter, r *http.Request, status int, message string) { timestamp := time.Now().Format("2006-01-02_15-04-05")