From 12a6de7461037b51f807c1140b0c008930d377d9 Mon Sep 17 00:00:00 2001 From: Marcus Efraimsson Date: Mon, 12 Feb 2018 09:26:09 +0100 Subject: [PATCH] dashboard: always make sure dashboard exist in dashboard acl http api (#10856) --- pkg/api/dashboard_acl.go | 15 +++++++++++++ pkg/api/dashboard_acl_test.go | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/pkg/api/dashboard_acl.go b/pkg/api/dashboard_acl.go index b5d912d25f1..45f121dd0d0 100644 --- a/pkg/api/dashboard_acl.go +++ b/pkg/api/dashboard_acl.go @@ -13,6 +13,11 @@ import ( func GetDashboardAclList(c *middleware.Context) Response { dashId := c.ParamsInt64(":dashboardId") + _, rsp := getDashboardHelper(c.OrgId, "", dashId, "") + if rsp != nil { + return rsp + } + guardian := guardian.NewDashboardGuardian(dashId, c.OrgId, c.SignedInUser) if canAdmin, err := guardian.CanAdmin(); err != nil || !canAdmin { @@ -36,6 +41,11 @@ func GetDashboardAclList(c *middleware.Context) Response { func UpdateDashboardAcl(c *middleware.Context, apiCmd dtos.UpdateDashboardAclCommand) Response { dashId := c.ParamsInt64(":dashboardId") + _, rsp := getDashboardHelper(c.OrgId, "", dashId, "") + if rsp != nil { + return rsp + } + guardian := guardian.NewDashboardGuardian(dashId, c.OrgId, c.SignedInUser) if canAdmin, err := guardian.CanAdmin(); err != nil || !canAdmin { return dashboardGuardianResponse(err) @@ -79,6 +89,11 @@ func DeleteDashboardAcl(c *middleware.Context) Response { dashId := c.ParamsInt64(":dashboardId") aclId := c.ParamsInt64(":aclId") + _, rsp := getDashboardHelper(c.OrgId, "", dashId, "") + if rsp != nil { + return rsp + } + guardian := guardian.NewDashboardGuardian(dashId, c.OrgId, c.SignedInUser) if canAdmin, err := guardian.CanAdmin(); err != nil || !canAdmin { return dashboardGuardianResponse(err) diff --git a/pkg/api/dashboard_acl_test.go b/pkg/api/dashboard_acl_test.go index 467045e360a..e43e57ed5c0 100644 --- a/pkg/api/dashboard_acl_test.go +++ b/pkg/api/dashboard_acl_test.go @@ -23,6 +23,14 @@ func TestDashboardAclApiEndpoint(t *testing.T) { } dtoRes := transformDashboardAclsToDTOs(mockResult) + getDashboardQueryResult := m.NewDashboard("Dash") + var getDashboardNotFoundError error + + bus.AddHandler("test", func(query *m.GetDashboardQuery) error { + query.Result = getDashboardQueryResult + return getDashboardNotFoundError + }) + bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error { query.Result = dtoRes return nil @@ -60,6 +68,40 @@ func TestDashboardAclApiEndpoint(t *testing.T) { So(respJSON.GetIndex(0).Get("permission").MustInt(), ShouldEqual, m.PERMISSION_VIEW) }) }) + + loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/dashboards/id/2/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_ADMIN, func(sc *scenarioContext) { + getDashboardNotFoundError = m.ErrDashboardNotFound + sc.handlerFunc = GetDashboardAclList + sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec() + + Convey("Should not be able to access ACL", func() { + So(sc.resp.Code, ShouldEqual, 404) + }) + }) + + Convey("Should not be able to update permissions for non-existing dashboard", func() { + cmd := dtos.UpdateDashboardAclCommand{ + Items: []dtos.DashboardAclUpdateItem{ + {UserId: 1000, Permission: m.PERMISSION_ADMIN}, + }, + } + + postAclScenario("When calling POST on", "/api/dashboards/id/1/acl", "/api/dashboards/id/:dashboardId/acl", m.ROLE_ADMIN, cmd, func(sc *scenarioContext) { + getDashboardNotFoundError = m.ErrDashboardNotFound + CallPostAcl(sc) + So(sc.resp.Code, ShouldEqual, 404) + }) + }) + + loggedInUserScenarioWithRole("When calling DELETE on", "DELETE", "/api/dashboards/id/2/acl/6", "/api/dashboards/id/:dashboardId/acl/:aclId", m.ROLE_ADMIN, func(sc *scenarioContext) { + getDashboardNotFoundError = m.ErrDashboardNotFound + sc.handlerFunc = DeleteDashboardAcl + sc.fakeReqWithParams("DELETE", sc.url, map[string]string{}).exec() + + Convey("Should not be able to delete non-existing dashboard", func() { + So(sc.resp.Code, ShouldEqual, 404) + }) + }) }) Convey("When user is org editor and has admin permission in the ACL", func() {