diff --git a/api/badge.go b/api/badge.go index e2ed3f46..8fa95696 100644 --- a/api/badge.go +++ b/api/badge.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "net/url" "strconv" "strings" "time" @@ -53,7 +54,10 @@ func UptimeBadge(c *fiber.Ctx) error { default: return c.Status(400).SendString("Durations supported: 30d, 7d, 24h, 1h") } - key := c.Params("key") + key, err := url.QueryUnescape(c.Params("key")) + if err != nil { + return c.Status(400).SendString("invalid key encoding") + } uptime, err := store.Get().GetUptimeByKey(key, from, time.Now()) if err != nil { if errors.Is(err, common.ErrEndpointNotFound) { @@ -88,7 +92,10 @@ func ResponseTimeBadge(cfg *config.Config) fiber.Handler { default: return c.Status(400).SendString("Durations supported: 30d, 7d, 24h, 1h") } - key := c.Params("key") + key, err := url.QueryUnescape(c.Params("key")) + if err != nil { + return c.Status(400).SendString("invalid key encoding") + } averageResponseTime, err := store.Get().GetAverageResponseTimeByKey(key, from, time.Now()) if err != nil { if errors.Is(err, common.ErrEndpointNotFound) { @@ -107,7 +114,10 @@ func ResponseTimeBadge(cfg *config.Config) fiber.Handler { // HealthBadge handles the automatic generation of badge based on the group name and endpoint name passed. func HealthBadge(c *fiber.Ctx) error { - key := c.Params("key") + key, err := url.QueryUnescape(c.Params("key")) + if err != nil { + return c.Status(400).SendString("invalid key encoding") + } pagingConfig := paging.NewEndpointStatusParams() status, err := store.Get().GetEndpointStatusByKey(key, pagingConfig.WithResults(1, 1)) if err != nil { @@ -133,7 +143,10 @@ func HealthBadge(c *fiber.Ctx) error { } func HealthBadgeShields(c *fiber.Ctx) error { - key := c.Params("key") + key, err := url.QueryUnescape(c.Params("key")) + if err != nil { + return c.Status(400).SendString("invalid key encoding") + } pagingConfig := paging.NewEndpointStatusParams() status, err := store.Get().GetEndpointStatusByKey(key, pagingConfig.WithResults(1, 1)) if err != nil { diff --git a/api/chart.go b/api/chart.go index 81a4d11d..79eda349 100644 --- a/api/chart.go +++ b/api/chart.go @@ -4,6 +4,7 @@ import ( "errors" "math" "net/http" + "net/url" "sort" "time" @@ -45,7 +46,11 @@ func ResponseTimeChart(c *fiber.Ctx) error { default: return c.Status(400).SendString("Durations supported: 30d, 7d, 24h") } - hourlyAverageResponseTime, err := store.Get().GetHourlyAverageResponseTimeByKey(c.Params("key"), from, time.Now()) + key, err := url.QueryUnescape(c.Params("key")) + if err != nil { + return c.Status(400).SendString("invalid key encoding") + } + hourlyAverageResponseTime, err := store.Get().GetHourlyAverageResponseTimeByKey(key, from, time.Now()) if err != nil { if errors.Is(err, common.ErrEndpointNotFound) { return c.Status(404).SendString(err.Error()) diff --git a/api/endpoint_status.go b/api/endpoint_status.go index 4d8777d8..4be1ad93 100644 --- a/api/endpoint_status.go +++ b/api/endpoint_status.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "net/url" "github.com/TwiN/gatus/v5/client" "github.com/TwiN/gatus/v5/config" @@ -86,7 +87,12 @@ func getEndpointStatusesFromRemoteInstances(remoteConfig *remote.Config) ([]*end func EndpointStatus(cfg *config.Config) fiber.Handler { return func(c *fiber.Ctx) error { page, pageSize := extractPageAndPageSizeFromRequest(c, cfg.Storage.MaximumNumberOfResults) - endpointStatus, err := store.Get().GetEndpointStatusByKey(c.Params("key"), paging.NewEndpointStatusParams().WithResults(page, pageSize).WithEvents(1, cfg.Storage.MaximumNumberOfEvents)) + key, err := url.QueryUnescape(c.Params("key")) + if err != nil { + logr.Errorf("[api.EndpointStatus] Failed to decode key: %s", err.Error()) + return c.Status(400).SendString("invalid key encoding") + } + endpointStatus, err := store.Get().GetEndpointStatusByKey(key, paging.NewEndpointStatusParams().WithResults(page, pageSize).WithEvents(1, cfg.Storage.MaximumNumberOfEvents)) if err != nil { if errors.Is(err, common.ErrEndpointNotFound) { return c.Status(404).SendString(err.Error()) @@ -95,7 +101,7 @@ func EndpointStatus(cfg *config.Config) fiber.Handler { return c.Status(500).SendString(err.Error()) } if endpointStatus == nil { // XXX: is this check necessary? - logr.Errorf("[api.EndpointStatus] Endpoint with key=%s not found", c.Params("key")) + logr.Errorf("[api.EndpointStatus] Endpoint with key=%s not found", key) return c.Status(404).SendString("not found") } output, err := json.Marshal(endpointStatus)