From 2810edca613e662caa836e52c651a842cbe9bda2 Mon Sep 17 00:00:00 2001 From: dashevchenko Date: Mon, 1 Jun 2026 11:37:26 +0300 Subject: [PATCH] fixed alarm comment permission bug --- .../controller/AlarmCommentController.java | 10 +++++ .../AlarmCommentControllerTest.java | 37 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/application/src/main/java/org/thingsboard/server/controller/AlarmCommentController.java b/application/src/main/java/org/thingsboard/server/controller/AlarmCommentController.java index 8a113fb424..90a20880f6 100644 --- a/application/src/main/java/org/thingsboard/server/controller/AlarmCommentController.java +++ b/application/src/main/java/org/thingsboard/server/controller/AlarmCommentController.java @@ -31,6 +31,7 @@ import org.thingsboard.server.common.data.alarm.Alarm; import org.thingsboard.server.common.data.alarm.AlarmComment; import org.thingsboard.server.common.data.alarm.AlarmCommentInfo; import org.thingsboard.server.common.data.alarm.AlarmCommentType; +import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; import org.thingsboard.server.common.data.exception.ThingsboardException; import org.thingsboard.server.common.data.id.AlarmCommentId; import org.thingsboard.server.common.data.id.AlarmId; @@ -77,6 +78,7 @@ public class AlarmCommentController extends BaseController { checkParameter(ALARM_ID, strAlarmId); AlarmId alarmId = new AlarmId(toUUID(strAlarmId)); Alarm alarm = checkAlarmInfoId(alarmId, Operation.WRITE); + checkUserCommentOwnership(alarmComment, Operation.WRITE); alarmComment.setAlarmId(alarmId); alarmComment.setType(AlarmCommentType.OTHER); return tbAlarmCommentService.saveAlarmComment(alarm, alarmComment, getCurrentUser()); @@ -93,6 +95,7 @@ public class AlarmCommentController extends BaseController { AlarmCommentId alarmCommentId = new AlarmCommentId(toUUID(strCommentId)); AlarmComment alarmComment = checkAlarmCommentId(alarmCommentId, alarmId); + checkUserCommentOwnership(alarmComment, Operation.DELETE); tbAlarmCommentService.deleteAlarmComment(alarm, alarmComment, getCurrentUser()); } @@ -120,4 +123,11 @@ public class AlarmCommentController extends BaseController { return checkNotNull(alarmCommentService.findAlarmComments(alarm.getTenantId(), alarmId, pageLink)); } + private void checkUserCommentOwnership(AlarmComment alarmComment, Operation operation) throws ThingsboardException { + if (alarmComment.getUserId() != null && !alarmComment.getUserId().equals(getCurrentUser().getId())) { + throw new ThingsboardException("User is not allowed to " + operation.name().toLowerCase() + " other user's comment", + ThingsboardErrorCode.PERMISSION_DENIED); + } + } + } diff --git a/application/src/test/java/org/thingsboard/server/controller/AlarmCommentControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/AlarmCommentControllerTest.java index ba86d60852..f2599d3adf 100644 --- a/application/src/test/java/org/thingsboard/server/controller/AlarmCommentControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/AlarmCommentControllerTest.java @@ -160,6 +160,26 @@ public class AlarmCommentControllerTest extends AbstractControllerTest { testLogEntityActionEntityEqClass(alarm, alarm.getId(), tenantId, customerId, tenantAdminUserId, TENANT_ADMIN_EMAIL, ActionType.UPDATED_COMMENT, 1, updatedAlarmComment); } + @Test + public void testUpdateOthersAlarmCommentByTenantAdmin() throws Exception { + // Tenant admins are NOT exempt from the ownership rule — even with full tenant-level + // privileges they cannot rewrite a comment authored by a different user. + loginCustomerUser(); + AlarmComment alarmComment = createAlarmComment(alarm.getId()); + + loginTenantAdmin(); + Mockito.reset(tbClusterService, auditLogService); + + JsonNode newComment = JacksonUtil.newObjectNode().set("text", new TextNode("Tenant rewrite attempt")); + alarmComment.setComment(newComment); + + doPost("/api/alarm/" + alarm.getId() + "/comment", alarmComment) + .andExpect(status().isForbidden()) + .andExpect(statusReason(containsString("User is not allowed to write other user's comment"))); + + testNotifyEntityNever(alarm.getId(), alarmComment); + } + @Test public void testUpdateAlarmViaDifferentTenant() throws Exception { loginTenantAdmin(); @@ -215,6 +235,23 @@ public class AlarmCommentControllerTest extends AbstractControllerTest { testLogEntityActionEntityEqClass(alarm, alarm.getId(), tenantId, customerId, customerUserId, CUSTOMER_USER_EMAIL, ActionType.DELETED_COMMENT, 1, expectedAlarmComment); } + @Test + public void testDeleteOthersAlarmCommentByTenantAdmin() throws Exception { + // Tenant admins are NOT exempt from the ownership rule on delete either — even with full + // tenant-level privileges they cannot delete a comment authored by a different user. + loginCustomerUser(); + AlarmComment alarmComment = createAlarmComment(alarm.getId()); + + loginTenantAdmin(); + Mockito.reset(tbClusterService, auditLogService); + + doDelete("/api/alarm/" + alarm.getId() + "/comment/" + alarmComment.getId()) + .andExpect(status().isForbidden()) + .andExpect(statusReason(containsString("User is not allowed to delete other user's comment"))); + + testNotifyEntityNever(alarm.getId(), alarmComment); + } + @Test public void testDeleteAlarmViaTenant() throws Exception { loginTenantAdmin();