From 73e89da81ce70b75baf9061936d9acf605d403d6 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Tue, 5 Oct 2021 14:04:26 -0400 Subject: [PATCH] Fix static path matching issue in macaron (#40023) (#40027) Co-authored-by: Malcolm Holmes (cherry picked from commit 329f96db1a507378a5d8ba0de685a0f0ae3ce0e0) Co-authored-by: Marcus Efraimsson --- pkg/api/dashboard_snapshot.go | 10 ++++++++++ pkg/macaron/router.go | 10 ++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/pkg/api/dashboard_snapshot.go b/pkg/api/dashboard_snapshot.go index b108a0b1141..7da37574fc0 100644 --- a/pkg/api/dashboard_snapshot.go +++ b/pkg/api/dashboard_snapshot.go @@ -147,6 +147,10 @@ func CreateDashboardSnapshot(c *models.ReqContext, cmd models.CreateDashboardSna // GET /api/snapshots/:key func GetDashboardSnapshot(c *models.ReqContext) response.Response { key := macaron.Params(c.Req)[":key"] + if len(key) == 0 { + return response.Error(404, "Snapshot not found", nil) + } + query := &models.GetDashboardSnapshotQuery{Key: key} err := bus.Dispatch(query) @@ -211,6 +215,9 @@ func deleteExternalDashboardSnapshot(externalUrl string) error { // GET /api/snapshots-delete/:deleteKey func DeleteDashboardSnapshotByDeleteKey(c *models.ReqContext) response.Response { key := macaron.Params(c.Req)[":deleteKey"] + if len(key) == 0 { + return response.Error(404, "Snapshot not found", nil) + } query := &models.GetDashboardSnapshotQuery{DeleteKey: key} @@ -241,6 +248,9 @@ func DeleteDashboardSnapshotByDeleteKey(c *models.ReqContext) response.Response // DELETE /api/snapshots/:key func DeleteDashboardSnapshot(c *models.ReqContext) response.Response { key := macaron.Params(c.Req)[":key"] + if len(key) == 0 { + return response.Error(404, "Snapshot not found", nil) + } query := &models.GetDashboardSnapshotQuery{Key: key} diff --git a/pkg/macaron/router.go b/pkg/macaron/router.go index 778bb99ff87..18a87ce9ea7 100644 --- a/pkg/macaron/router.go +++ b/pkg/macaron/router.go @@ -207,10 +207,12 @@ func (r *Router) NotFound(handlers ...Handler) { func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) { if t, ok := r.routers[req.Method]; ok { // Fast match for static routes - leaf := r.getLeaf(req.Method, req.URL.Path) - if leaf != nil { - leaf.handle(rw, req, nil) - return + if !strings.ContainsAny(req.URL.Path, ":*") { + leaf := r.getLeaf(req.Method, req.URL.Path) + if leaf != nil { + leaf.handle(rw, req, nil) + return + } } h, p, ok := t.Match(req.URL.EscapedPath())