Browse Source

Tighten device claiming permission checks

pull/16434/head
Viacheslav Klimov 1 month ago
parent
commit
6859887a1b
Failed to extract signature
  1. 7
      application/src/main/java/org/thingsboard/server/service/security/permission/CustomerUserPermissions.java
  2. 57
      application/src/test/java/org/thingsboard/server/controller/DeviceControllerTest.java

7
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);
}
};

57
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);

Loading…
Cancel
Save