Skip to content

Commit 5c030e3

Browse files
iansltxgeorgekarrv
authored andcommitted
Revise auth requirements for debug endpoints (#38173)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves # # Checklist for submitter If some of the following don't apply, delete the relevant line. - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - [x] Input data is properly validated, `SELECT *` is avoided, SQL injection is prevented (using placeholders for values in statements) ## Testing - [x] Added/updated automated tests - [x] QA'd all new/changed functionality manually
1 parent 9c51cd8 commit 5c030e3

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

changes/pprof-tweaks

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Revised auth requirements for /debug endpoints

server/service/debug_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (m *debugAuthenticationMiddleware) Middleware(next http.Handler) http.Handl
3939
return
4040
}
4141

42-
if !v.CanPerformActions() {
42+
if !v.CanPerformActions() || v.User.GlobalRole == nil || *v.User.GlobalRole != fleet.RoleAdmin {
4343
http.Error(w, "Unauthorized", http.StatusForbidden)
4444
return
4545
}

server/service/debug_handler_test.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/fleetdm/fleet/v4/server/config"
1111
"github.com/fleetdm/fleet/v4/server/fleet"
12+
"github.com/fleetdm/fleet/v4/server/ptr"
1213
"github.com/stretchr/testify/assert"
1314
"github.com/stretchr/testify/mock"
1415
)
@@ -66,7 +67,39 @@ func TestDebugHandlerAuthenticationSessionInvalid(t *testing.T) {
6667
assert.Equal(t, http.StatusUnauthorized, res.Code)
6768
}
6869

69-
func TestDebugHandlerAuthenticationSuccess(t *testing.T) {
70+
func TestDebugHandlerAuthenticationFailsDueToRole(t *testing.T) {
71+
for test, user := range map[string]fleet.User{
72+
"no role": {},
73+
"global observer role": {GlobalRole: ptr.String(fleet.RoleObserver)},
74+
"global maintainer role": {GlobalRole: ptr.String(fleet.RoleMaintainer)},
75+
"non-global role": {Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1, Name: "foo"}, Role: fleet.RoleAdmin}}},
76+
} {
77+
t.Run(test, func(t *testing.T) {
78+
svc := &mockService{}
79+
svc.On(
80+
"GetSessionByKey",
81+
mock.Anything,
82+
"fake_session_key",
83+
).Return(&fleet.Session{UserID: 42, ID: 1}, nil)
84+
svc.On(
85+
"UserUnauthorized",
86+
mock.Anything,
87+
uint(42),
88+
).Return(&user, nil)
89+
90+
handler := MakeDebugHandler(svc, testConfig, nil, nil, nil)
91+
92+
req := httptest.NewRequest(http.MethodGet, "https://fleetdm.com/debug/pprof/cmdline", nil)
93+
req.Header.Add("Authorization", "BEARER fake_session_key")
94+
res := httptest.NewRecorder()
95+
96+
handler.ServeHTTP(res, req)
97+
assert.Equal(t, http.StatusForbidden, res.Code)
98+
})
99+
}
100+
}
101+
102+
func TestDebugHandlerAuthenticationSucceeds(t *testing.T) {
70103
svc := &mockService{}
71104
svc.On(
72105
"GetSessionByKey",
@@ -77,7 +110,7 @@ func TestDebugHandlerAuthenticationSuccess(t *testing.T) {
77110
"UserUnauthorized",
78111
mock.Anything,
79112
uint(42),
80-
).Return(&fleet.User{}, nil)
113+
).Return(&fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)}, nil)
81114

82115
handler := MakeDebugHandler(svc, testConfig, nil, nil, nil)
83116

0 commit comments

Comments
 (0)