From 6859887a1b844f9351bb166cb57c6cb0d5219c8b Mon Sep 17 00:00:00 2001 From: Viacheslav Klimov Date: Mon, 26 Oct 2026 14:36:16 +0200 Subject: [PATCH] Tighten device claiming permission checks --- .../permission/CustomerUserPermissions.java | 7 ++- .../controller/DeviceControllerTest.java | 57 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/thingsboard/server/service/security/permission/CustomerUserPermissions.java b/application/src/main/java/org/thingsboard/server/service/security/permission/CustomerUserPermissions.java index bf55bc66a1..d9ae780daa 100644 --- a/application/src/main/java/org/thingsboard/server/service/security/permission/CustomerUserPermissions.java +++ b/application/src/main/java/org/thingsboard/server/service/security/permission/CustomerUserPermissions.java @@ -8,6 +8,7 @@ import org.thingsboard.server.common.data.HasCustomerId; import org.thingsboard.server.common.data.HasTenantId; import org.thingsboard.server.common.data.TbResourceInfo; import org.thingsboard.server.common.data.User; +import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.DashboardId; import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.TbResourceId; @@ -68,7 +69,11 @@ public class CustomerUserPermissions extends AbstractPermissions { if (!(entity instanceof HasCustomerId)) { return false; } - return operation.equals(Operation.CLAIM_DEVICES) || user.getCustomerId().equals(((HasCustomerId) entity).getCustomerId()); + CustomerId customerId = ((HasCustomerId) entity).getCustomerId(); + if (operation.equals(Operation.CLAIM_DEVICES) && (customerId == null || customerId.isNullUid())) { + return true; + } + return user.getCustomerId().equals(customerId); } }; diff --git a/application/src/test/java/org/thingsboard/server/controller/DeviceControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/DeviceControllerTest.java index d0aa60e131..3edfaf36c3 100644 --- a/application/src/test/java/org/thingsboard/server/controller/DeviceControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/DeviceControllerTest.java @@ -25,6 +25,7 @@ import org.springframework.test.context.ContextConfiguration; import org.testcontainers.shaded.org.awaitility.Awaitility; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.ThingsBoardExecutors; +import org.thingsboard.server.common.data.ClaimRequest; import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.DeviceInfo; @@ -1638,6 +1639,62 @@ public class DeviceControllerTest extends AbstractControllerTest { assertThat(device.getVersion()).isEqualTo(3); } + @Test + public void testReclaimDeviceByCustomerUser() throws Exception { + Device device = createCustomerDevice("Reclaim own device"); + + loginCustomerUser(); + doDeleteAsync("/api/customer/device/" + device.getName() + "/claim", String.class); + + loginTenantAdmin(); + Device foundDevice = doGet("/api/device/" + device.getUuidId(), Device.class); + Assert.assertEquals(NULL_UUID, foundDevice.getCustomerId().getId()); + } + + @Test + public void testReclaimDeviceOfDifferentCustomerByCustomerUser() throws Exception { + Device device = createCustomerDevice("Reclaim device of different customer"); + + loginDifferentCustomer(); + doDelete("/api/customer/device/" + device.getName() + "/claim") + .andExpect(status().isForbidden()) + .andExpect(statusReason(containsString(msgErrorPermission))); + + loginTenantAdmin(); + Device foundDevice = doGet("/api/device/" + device.getUuidId(), Device.class); + Assert.assertEquals(customerId, foundDevice.getCustomerId()); + } + + @Test + public void testClaimDeviceOfDifferentCustomerByCustomerUser() throws Exception { + Device device = createCustomerDevice("Claim device of different customer"); + + loginDifferentCustomer(); + doPost("/api/customer/device/" + device.getName() + "/claim", new ClaimRequest(null)) + .andExpect(status().isForbidden()) + .andExpect(statusReason(containsString(msgErrorPermission))); + + loginTenantAdmin(); + Device foundDevice = doGet("/api/device/" + device.getUuidId(), Device.class); + Assert.assertEquals(customerId, foundDevice.getCustomerId()); + } + + @Test + public void testReclaimDeviceByTenantAdmin() throws Exception { + Device device = createCustomerDevice("Reclaim device by tenant admin"); + + doDeleteAsync("/api/customer/device/" + device.getName() + "/claim", String.class); + + Device foundDevice = doGet("/api/device/" + device.getUuidId(), Device.class); + Assert.assertEquals(NULL_UUID, foundDevice.getCustomerId().getId()); + } + + private Device createCustomerDevice(String name) throws Exception { + loginTenantAdmin(); + Device device = createDevice(name); + return doPost("/api/customer/" + customerId.getId() + "/device/" + device.getUuidId(), Device.class); + } + private Device createDevice(String name) { Device device = new Device(); device.setName(name);