Skip to content

Lightweight server monitoring with historical data, docker stats, and alerts.

fix(hub): verify write access to target system when moving a network monitor (#2714)

duongynhi000005-oss2de112436d99
diff --git a/internal/hub/network_monitors.go b/internal/hub/network_monitors.go
index 15eff1793..c67dde9ae 100644
--- a/internal/hub/network_monitors.go
+++ b/internal/hub/network_monitors.go
@@ -59,6 +59,23 @@ func bindNetworkMonitorsEvents(hub *Hub) {
// record with the new ID and delete the old one. Otherwise, just update the existing monitor on the agent.
hub.OnRecordUpdateRequest("network_monitors").BindFunc(func(e *core.RecordRequestEvent) error {
systemID := e.Record.GetString("system")
+ if systemID != e.Record.Original().GetString("system") {
+ requestInfo, err := e.RequestInfo()
+ if err != nil {
+ return err
+ }
+ targetSystem, err := e.App.FindRecordById("systems", systemID)
+ if err != nil {
+ return e.ForbiddenError("", nil)
+ }
+ canAccess, err := e.App.CanAccessRecord(targetSystem, requestInfo, targetSystem.Collection().UpdateRule)
+ if err != nil {
+ return err
+ }
+ if !canAccess {
+ return e.ForbiddenError("", nil)
+ }
+ }
protocol := e.Record.GetString("protocol")
// only tcp uses port - set other protocols port to zero
if protocol != "tcp" {
diff --git a/internal/hub/network_monitors_test.go b/internal/hub/network_monitors_test.go
index 28b7a0842..7040c251f 100644
--- a/internal/hub/network_monitors_test.go
+++ b/internal/hub/network_monitors_test.go
@@ -329,3 +329,91 @@ func TestCopyMonitorToNewRecordDropsResultFields(t *testing.T) {
assert.Zero(t, newRecord.GetFloat("loss1h"))
assert.Equal(t, "", newRecord.GetString("updated"))
}
+
+func TestUpdateNetworkMonitorSystemAuthorization(t *testing.T) {
+ hub, testApp, err := createTestHub(t)
+ require.NoError(t, err)
+ defer cleanupTestHub(hub, testApp)
+ require.NoError(t, setCollectionAuthSettings(hub))
+ bindNetworkMonitorsEvents(hub)
+
+ alice, err := createTestUser(hub)
+ require.NoError(t, err)
+ bob, err := createTestRecord(hub, "users", map[string]any{
+ "email": "[email protected]",
+ "password": "password123",
+ })
+ require.NoError(t, err)
+
+ aliceSystemA, err := createTestRecord(hub, "systems", map[string]any{
+ "name": "AliceSystemA", "host": "localhost", "port": "45876",
+ "status": "paused", "users": []string{alice.Id},
+ })
+ require.NoError(t, err)
+ aliceSystemC, err := createTestRecord(hub, "systems", map[string]any{
+ "name": "AliceSystemC", "host": "localhost", "port": "45877",
+ "status": "paused", "users": []string{alice.Id},
+ })
+ require.NoError(t, err)
+ bobSystemB, err := createTestRecord(hub, "systems", map[string]any{
+ "name": "BobSystemB", "host": "localhost", "port": "45878",
+ "status": "paused", "users": []string{bob.Id},
+ })
+ require.NoError(t, err)
+
+ monitorRecord, err := createTestRecord(hub, "network_monitors", map[string]any{
+ "system": aliceSystemA.Id, "target": "1.1.1.1", "protocol": "icmp",
+ "interval": 60, "enabled": true,
+ })
+ require.NoError(t, err)
+
+ aliceToken, err := alice.NewAuthToken()
+ require.NoError(t, err)
+ router, err := apis.NewRouter(hub)
+ require.NoError(t, err)
+ handler, err := router.BuildMux()
+ require.NoError(t, err)
+
+ // 1. Alice cannot move her monitor to Bob's system B
+ patchBobBody, err := json.Marshal(map[string]any{
+ "system": bobSystemB.Id,
+ "target": "http://127.0.0.1:12345/probe",
+ "protocol": "http",
+ "interval": 60,
+ "enabled": true,
+ })
+ require.NoError(t, err)
+ req := httptest.NewRequest(http.MethodPatch, "/api/collections/network_monitors/records/"+monitorRecord.Id, bytes.NewReader(patchBobBody))
+ req.Header.Set("Content-Type", "application/json")
+ req.Header.Set("Authorization", aliceToken)
+ resp := httptest.NewRecorder()
+ handler.ServeHTTP(resp, req)
+ assert.Equal(t, http.StatusForbidden, resp.Code, resp.Body.String())
+
+ records, err := hub.FindAllRecords("network_monitors")
+ require.NoError(t, err)
+ require.Len(t, records, 1)
+ assert.Equal(t, monitorRecord.Id, records[0].Id)
+ assert.Equal(t, aliceSystemA.Id, records[0].GetString("system"))
+
+ // 2. Alice can move her monitor to her own system C
+ patchAliceBody, err := json.Marshal(map[string]any{
+ "system": aliceSystemC.Id,
+ "target": "1.1.1.1",
+ "protocol": "icmp",
+ "interval": 60,
+ "enabled": true,
+ })
+ require.NoError(t, err)
+ req2 := httptest.NewRequest(http.MethodPatch, "/api/collections/network_monitors/records/"+monitorRecord.Id, bytes.NewReader(patchAliceBody))
+ req2.Header.Set("Content-Type", "application/json")
+ req2.Header.Set("Authorization", aliceToken)
+ resp2 := httptest.NewRecorder()
+ handler.ServeHTTP(resp2, req2)
+ assert.Equal(t, http.StatusOK, resp2.Code, resp2.Body.String())
+
+ records, err = hub.FindAllRecords("network_monitors")
+ require.NoError(t, err)
+ require.Len(t, records, 1)
+ assert.Equal(t, aliceSystemC.Id, records[0].GetString("system"))
+}