From 85a70bd00677106b06a2b266158536dcd6fb19f9 Mon Sep 17 00:00:00 2001 From: Felix <23635466+its-felix@users.noreply.github.com> Date: Fri, 26 Jul 2024 21:20:40 +0200 Subject: [PATCH] fix: harden deserialization of gw2eff api status --- web/notifications.go | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/web/notifications.go b/web/notifications.go index cbb39d5..c5465a0 100644 --- a/web/notifications.go +++ b/web/notifications.go @@ -8,16 +8,29 @@ import ( "time" ) +type gw2EffApiStatusElement struct { + Name string `json:"name"` + Status int `json:"status"` + Duration int `json:"duration"` + Error any `json:"error"` +} + +func (e gw2EffApiStatusElement) CheckError() bool { + switch v := e.Error.(type) { + case bool: + return v + + case string: + return v != "" + + default: + return false + } +} + type gw2EffApiStatusResponse struct { - Data []struct { - Name string `json:"name"` - Url string `json:"url"` - Status int `json:"status"` - Duration int `json:"duration"` - Error bool `json:"error"` - SchemaValid bool `json:"schemaValid"` - } `json:"data"` - UpdatedAt time.Time `json:"updated_at"` + Data []gw2EffApiStatusElement `json:"data"` + UpdatedAt time.Time `json:"updated_at"` } type notificationType string @@ -72,7 +85,7 @@ func NotificationsEndpoint(httpClient *http.Client) echo.HandlerFunc { var endpointsWithIssues []string for _, element := range gw2EffStatus.Data { - if slices.Contains(relevantEndpoints, element.Name) && (element.Status != http.StatusOK || element.Error || element.Duration >= 15_000) { + if slices.Contains(relevantEndpoints, element.Name) && (element.Status != http.StatusOK || element.CheckError() || element.Duration >= 15_000) { endpointsWithIssues = append(endpointsWithIssues, element.Name) } }