From 2c75a274822f679209589371882db89f5451f6db Mon Sep 17 00:00:00 2001 From: grumbulon Date: Wed, 3 May 2023 08:32:49 -0400 Subject: [PATCH] better and safer type assertions --- internal/api/helpers.go | 27 +++++++++++++++++++++------ internal/api/zone.go | 16 +++++++++++++--- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/internal/api/helpers.go b/internal/api/helpers.go index 750a2f4..c08eb9b 100644 --- a/internal/api/helpers.go +++ b/internal/api/helpers.go @@ -62,6 +62,11 @@ func (e *Response[T]) apiError(w http.ResponseWriter, r *http.Request) { "error": e.Err, } + message, ok := v["message"].(string) + if !ok { + return + } + w.Header().Set("X-Content-Type-Options", "nosniff") w.Header().Set("Content-Type", "application/json; charset=utf-8") @@ -72,7 +77,7 @@ func (e *Response[T]) apiError(w http.ResponseWriter, r *http.Request) { fallthrough case nil: - w.Header().Add("API Error", v["message"].(string)) + w.Header().Add("API Error", message) } w.WriteHeader(v["status"].(int)) @@ -87,21 +92,31 @@ func (e *Response[T]) apiError(w http.ResponseWriter, r *http.Request) { // writeLogEntry takes a response struct and writes info and error level logs // todo: make it write to file maybe func (e *Response[T]) writeLogEntry() { + logger := httplog.NewLogger("Pomme", httplog.Options{ + JSON: true, + }) + v := map[string]any{ "message": e.Message, "error": e.Err, } - logger := httplog.NewLogger("Pomme", httplog.Options{ - JSON: true, - }) + errorString, ok := v["error"].(string) + if !ok { + logger.Error().Msg("unable to convert any type 'error' to string") + } + + message, ok := v["message"].(string) + if !ok { + logger.Error().Msg("unable to convert any type 'message' to string") + } switch v["error"] { default: - logger.Error().Msg(v["error"].(string)) + logger.Error().Msg(errorString) fallthrough case nil: - logger.Info().Msg(v["message"].(string)) + logger.Info().Msg(message) } } diff --git a/internal/api/zone.go b/internal/api/zone.go index 68906e7..27f4292 100644 --- a/internal/api/zone.go +++ b/internal/api/zone.go @@ -83,7 +83,17 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) { return } - zoneFile := newDNSRequest(header.Filename, claims["username"].(string), b) + stringClaimsUName, ok := claims["username"].(string) + if !ok { + logger := newResponder(Response[any]{ + Message: "unable to convert any type claim to string", + Status: http.StatusInternalServerError, + }) + logger.apiError(w, r) + logger.writeLogEntry() + } + + zoneFile := newDNSRequest(header.Filename, stringClaimsUName, b) if err := zoneFile.parse(); err != nil { logger := newResponder(Response[any]{ @@ -111,7 +121,7 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) { } // check if request is coming from user not in the DB but has a valid JWT - db.Where("username = ?", claims["username"].(string)).First(&result) + db.Where("username = ?", stringClaimsUName).First(&result) if result.Username == "" { logger := newResponder(Response[any]{ @@ -125,7 +135,7 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) { db.Create( &ZoneRequest{ - User: claims["username"].(string), + User: stringClaimsUName, Zone: &Zone{ FileName: header.Filename, },