From 4028af83179f70fd866af02e4212beee82361cfc Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Wed, 18 Oct 2023 23:44:33 +0300 Subject: [PATCH] fix: check A record in update api, move check to func --- internal/api/update.go | 10 ++++++++++ internal/handlers/domains.go | 6 +++--- internal/util/check.go | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 internal/util/check.go diff --git a/internal/api/update.go b/internal/api/update.go index 8d79a5b..564edfb 100644 --- a/internal/api/update.go +++ b/internal/api/update.go @@ -10,6 +10,7 @@ import ( "net/http" "git.src.quest/~skye/felu-ddns/internal/db" + "git.src.quest/~skye/felu-ddns/internal/util" "github.com/gin-gonic/gin" ) @@ -40,6 +41,15 @@ func UpdateA() gin.HandlerFunc { aRecord = c.ClientIP() } + if err := util.CheckARecord(aRecord); err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "status": "error", + "error": err.Error(), + }) + c.Abort() + return + } + err := db.UpdateDomainARecord(domain, apiKey, aRecord) if err != nil { // FIXME: Handle better, "bad api key" is just the most likely scenario diff --git a/internal/handlers/domains.go b/internal/handlers/domains.go index faf7a9e..0242a48 100644 --- a/internal/handlers/domains.go +++ b/internal/handlers/domains.go @@ -7,12 +7,12 @@ package handlers import ( - "net" "net/http" "regexp" "strings" "git.src.quest/~skye/felu-ddns/internal/db" + "git.src.quest/~skye/felu-ddns/internal/util" "github.com/gin-gonic/gin" ) @@ -46,8 +46,8 @@ func PostDomain() gin.HandlerFunc { c.Abort() return } - if net.ParseIP(data.ARecord).To4() == nil { - c.String(http.StatusBadRequest, "The A record is invalid") + if err := util.CheckARecord(data.ARecord); err != nil { + c.String(http.StatusBadRequest, err.Error()) c.Abort() return } diff --git a/internal/util/check.go b/internal/util/check.go new file mode 100644 index 0000000..de46633 --- /dev/null +++ b/internal/util/check.go @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2023 Jonni Liljamo + * + * This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for + * more information. + */ +package util + +import ( + "errors" + "net" +) + +func CheckARecord(aRecord string) error { + if net.ParseIP(aRecord).To4() == nil { + return errors.New("Invalid A record") + } + return nil +} -- 2.44.1