API3:2023 - Broken Object Property Level Authorization چیست
در این آسیب پذیری مهاجم به واسطه عدم بررسی مدل های داده در درخواست و پاسخ امکان استخراج و یا عملیات CRUD را بر روی متد های مربوطه دارد این موضوع به سبب عدم صحت اعتبارسنجی مجوز دسترسی به ویژگیهای اشیا و در نتیجه افشا اطلاعات یا اختلال در درخواست ها می گردد.
مثال:
درخواست PUT برای به روزرسانی یک ویژگی از یک مورد:
PUT /api/items/{item_id} Body: { "name": "Updated Item Name", "price": 10.99, "is_available": true }
کد آسیب پذیر(NET.):
[Route("api/items")] public class ItemController : ControllerBase { private readonly IItemService _itemService; public ItemController(IItemService itemService) { _itemService = itemService; } [HttpGet("{itemId}")] public IActionResult GetItem(int itemId) { // Retrieve the item from the database Item item = _itemService.GetItem(itemId); // Return the item without any authorization check return Ok(item); } [HttpPut("{itemId}")] public IActionResult UpdateItem(int itemId, [FromBody] Item updatedItem) { // Retrieve the existing item from the database Item existingItem = _itemService.GetItem(itemId); // Update only the allowed properties existingItem.Name = updatedItem.Name; existingItem.Price = updatedItem.Price; existingItem.IsAvailable = updatedItem.IsAvailable; // Save the changes to the database _itemService.UpdateItem(existingItem); // Return a success response return Ok(); } // Other methods... }
پیشگیری (NET.):
[Route("api/items")] public class ItemController : ControllerBase { private readonly IItemService _itemService; public ItemController(IItemService itemService) { _itemService = itemService; } [HttpGet("{itemId}")] public IActionResult GetItem(int itemId) { // Retrieve the item from the database Item item = _itemService.GetItem(itemId); // Check if the user is authorized to access the item if (!IsUserAuthorized(item)) { return Forbid(); } // Return the item return Ok(item); } [HttpPut("{itemId}")] public IActionResult UpdateItem(int itemId, [FromBody] Item updatedItem) { // Retrieve the existing item from the database Item existingItem = _itemService.GetItem(itemId); // Check if the user is authorized to update the item properties if (!IsUserAuthorized
کد آسیب پذیر (جاوا):
@RestController @RequestMapping("/api/items") public class ItemController { private final ItemService itemService; public ItemController(ItemService itemService) { this.itemService = itemService; } @GetMapping("/{itemId}") public Item getItem(@PathVariable int itemId) { // Retrieve the item from the database Item item = itemService.getItem(itemId); // Return the item without any authorization check return item; } @PutMapping("/{itemId}") public void updateItem(@PathVariable int itemId, @RequestBody Item updatedItem) { // Retrieve the existing item from the database Item existingItem = itemService.getItem(itemId); // Update only the allowed properties existingItem.setName(updatedItem.getName()); existingItem.setPrice(updatedItem.getPrice()); existingItem.setAvailable(updatedItem.isAvailable()); // Save the changes to the database itemService.updateItem(existingItem); } // Other methods... }
پیشگیری (جاوا):
@RestController @RequestMapping("/api/items") public class ItemController { private final ItemService itemService; public ItemController(ItemService itemService) { this.itemService = itemService; } @GetMapping("/{itemId}") public ResponseEntity<Item> getItem(@PathVariable int itemId) { // Retrieve the item from the database Item item = itemService.getItem(itemId); // Check if the user is authorized to access the item if (!isUserAuthorized(item)) { return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); } // Return the item return ResponseEntity.ok(item); } @PutMapping("/{itemId}") public ResponseEntity<Void> updateItem(@PathVariable int itemId, @RequestBody Item updatedItem) { // Retrieve the existing item from the database Item existingItem = itemService.getItem(itemId); // Check if the user is authorized to update the item properties if (!isUserAuthorized(existingItem)) { return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); } // Only update the allowed properties existingItem.setName(updatedItem.getName()); existingItem.setPrice(updatedItem.getPrice()); existingItem.setAvailable(updatedItem.isAvailable()); // Save the changes to the database itemService.updateItem(existingItem); // Return a success response return ResponseEntity.ok().build(); } private boolean isUserAuthorized(Item item) { // Implement your authorization logic here // Check if the user has the necessary permissions to access the item // Return true if authorized, false otherwise } // Other methods... }
پیشنهادات کلی جلوگیری:
در هنگام ایجاد یا به روزرسانی اشیا، اطمینان حاصل کنید که مجوز دسترسی به ویژگیها در سطح صحیح تنظیم شده است.
اعتبارسنجی دادههای ورودی کاربران و فقط قبول دادن آنها در صورتی که دسترسی مجاز به ویژگیهای مربوطه را دارند.
استفاده از مکانیزمهای قوی و امن برای تعیین و مدیریت مجوزها و نقشها در سیستم، مانند RBAC (Role-Based Access Control).
محدود کردن دسترسی کاربران به ویژگیهای اشیا بر اساس نیازهای کسب و کار و اصول حداقل اصول (Principle of Least Privilege).
اجرای آزمون های امنیتی منظم بر روی API ها و سیستم اطمینان حاصل کنید که تمامی مجوزها و اعتبارسنجیهای مورد نیاز به درستی پیاده سازی شدهاند.