From 29e305393ec480b7c34e954283780ac159e6f428 Mon Sep 17 00:00:00 2001 From: Viacheslav Klimov Date: Mon, 26 Oct 2026 14:45:41 +0200 Subject: [PATCH 1/2] 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 cd9f1b2c66..0544bdc75a 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 @@ -9,6 +9,7 @@ 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.ApiKeyId; +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; @@ -71,7 +72,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 52e16ca1eb..0ef72ae19e 100644 --- a/application/src/test/java/org/thingsboard/server/controller/DeviceControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/DeviceControllerTest.java @@ -26,6 +26,7 @@ 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.AttributeScope; +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; @@ -1695,6 +1696,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); + } + @Test public void testSaveDeviceWithUniquifyStrategy() throws Exception { Device device = new Device(); From 33c59ded38d27fd829526d203c334bf93cba8391 Mon Sep 17 00:00:00 2001 From: Viacheslav Klimov Date: Mon, 26 Oct 2026 14:45:41 +0200 Subject: [PATCH 2/2] Fix repeated login in different customer test helpers --- .../server/controller/AbstractWebTest.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/AbstractWebTest.java b/application/src/test/java/org/thingsboard/server/controller/AbstractWebTest.java index a97f5d5b9d..9168908040 100644 --- a/application/src/test/java/org/thingsboard/server/controller/AbstractWebTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/AbstractWebTest.java @@ -533,10 +533,12 @@ public abstract class AbstractWebTest extends AbstractInMemoryStorageTest { } protected void loginDifferentCustomer() throws Exception { - if (savedDifferentCustomer != null) { - login(savedDifferentCustomer.getEmail(), CUSTOMER_USER_PASSWORD); + if (differentCustomerUser != null) { + login(DIFFERENT_CUSTOMER_USER_EMAIL, DIFFERENT_CUSTOMER_USER_PASSWORD); } else { - createDifferentCustomer(); + if (savedDifferentCustomer == null) { + createDifferentCustomer(); + } loginTenantAdmin(); differentCustomerUser = new User(); @@ -551,10 +553,12 @@ public abstract class AbstractWebTest extends AbstractInMemoryStorageTest { } protected void loginDifferentTenantCustomer() throws Exception { - if (savedDifferentTenantCustomer != null) { - login(savedDifferentTenantCustomer.getEmail(), CUSTOMER_USER_PASSWORD); + if (differentTenantCustomerUser != null) { + login(DIFFERENT_TENANT_CUSTOMER_USER_EMAIL, DIFFERENT_CUSTOMER_USER_PASSWORD); } else { - createDifferentTenantCustomer(); + if (savedDifferentTenantCustomer == null) { + createDifferentTenantCustomer(); + } loginDifferentTenant(); differentTenantCustomerUser = new User(); @@ -597,6 +601,8 @@ public abstract class AbstractWebTest extends AbstractInMemoryStorageTest { loginSysAdmin(); deleteTenant(savedDifferentTenant.getId()); savedDifferentTenant = null; + savedDifferentTenantCustomer = null; + differentTenantCustomerUser = null; } }