API9:2023 - Improper Inventory Management چیست

به واسطه عدم مدیریت نسخه های API ها و همچنین لیست قابلیت ها و عملکرد های نمونه موردی برای تمام توابع، امکان بهره برداری مهاجم از عملکرد های متفاوت در نسخه های گوناگون اپلیکیشن وجود دارد.


مثال:

درخواست GET برای دریافت لیست نسخه‌های موجود API:


GET /api/versions


کد آسیب پذیر(NET.):


[ApiController]

[Route("api/inventory")]

public class InventoryController : ControllerBase

{

    private readonly IInventoryService _inventoryService;



    public InventoryController(IInventoryService inventoryService)

    {

        _inventoryService = inventoryService;

    }



    // GET api/inventory/{productId}

    [HttpGet("{productId}")]

    public IActionResult GetProductInventory(int productId)

    {

        // Fetch inventory data directly from the database

        var inventory = _inventoryService.GetInventoryByProductId(productId);

        return Ok(inventory);

    }



    // POST api/inventory

    [HttpPost]

    public IActionResult UpdateProductInventory(InventoryModel inventory)

    {

        // Update inventory directly in the database

        _inventoryService.UpdateInventory(inventory);

        return Ok();

    }



    // Other methods...

}


پیشگیری (NET.):


[ApiController]

[Route("api/inventory")]

public class InventoryController : ControllerBase

{

    private readonly IInventoryService _inventoryService;



    public InventoryController(IInventoryService inventoryService)

    {

        _inventoryService = inventoryService;

    }



    // GET api/inventory/{productId}

    [HttpGet("{productId}")]

    public IActionResult GetProductInventory(int productId)

    {

        // Fetch inventory data through the inventory service

        var inventory = _inventoryService.GetProductInventory(productId);

        

        if (inventory == null)

            return NotFound();



        return Ok(inventory);

    }



    // POST api/inventory

    [HttpPost]

    [Authorize(Roles = "Admin")] // Restrict access to authorized users with the "Admin" role

    public IActionResult UpdateProductInventory(InventoryModel inventory)

    {

        // Update inventory through the inventory service

        _inventoryService.UpdateProductInventory(inventory);

        return Ok();

    }



    // Other methods...

}



کد آسیب پذیر (جاوا):


@RestController

@RequestMapping("/api/inventory")

public class InventoryController {

    

    private final InventoryService inventoryService;

    

    public InventoryController(InventoryService inventoryService) {

        this.inventoryService = inventoryService;

    }

    

    // GET /api/inventory/{productId}

    @GetMapping("/{productId}")

    public ResponseEntity<Inventory> getProductInventory(@PathVariable int productId) {

        // Fetch inventory data directly from the database

        Inventory inventory = inventoryService.getInventoryByProductId(productId);

        return ResponseEntity.ok(inventory);

    }

    

    // POST /api/inventory

    @PostMapping

    public ResponseEntity<?> updateProductInventory(@RequestBody Inventory inventory) {

        // Update inventory directly in the database

        inventoryService.updateInventory(inventory);

        return ResponseEntity.ok().build();

    }

    

    // Other methods...

}


پیشگیری (جاوا):


@RestController

@RequestMapping("/api/inventory")

public class InventoryController {

    

    private final InventoryService inventoryService;

    

    public InventoryController(InventoryService inventoryService) {

        this.inventoryService = inventoryService;

    }

    

    // GET /api/inventory/{productId}

    @GetMapping("/{productId}")

    public ResponseEntity<Inventory> getProductInventory(@PathVariable int productId) {

        // Fetch inventory data through the inventory service

        Inventory inventory = inventoryService.getProductInventory(productId);

        

        if (inventory == null) {

            return ResponseEntity.notFound().build();

        }

        

        return ResponseEntity.ok(inventory);

    }

    

    // POST /api/inventory

    @PostMapping

    @PreAuthorize("hasRole('ADMIN')") // Restrict access to authorized users with the "ADMIN" role

    public ResponseEntity<?> updateProductInventory(@RequestBody Inventory inventory) {

        // Update inventory through the inventory service

        inventoryService.updateProductInventory(inventory);

        return ResponseEntity.ok().build();

    }

    

    // Other methods...

}


پیشنهادات کلی جلوگیری:

مستندسازی کامل و دقیق برای API، شامل نسخه های موجود و نسخه های قبلی.

ایجاد یک سیستم مدیریت نسخه که به‌روز رسانی و مدیریت نسخه‌های API را ساده می‌کند.

معرفی یک سیاست ازادسازی نسخه که شامل دوره زمانی و پشتیبانی نسخه‌های قدیمی شده باشد.

استفاده از روش‌های خودکار برای بررسی نسخه API مورد استفاده توسط مشتریان و هشدار دادن در صورت استفاده از نسخه‌های قدیمی.

مانیتورینگ و پایش مداوم برای تشخیص و رفع مشکلاتی مانند نسخه‌های قدیمی شده API و نقاط پایانی اشکال‌زده.

استفاده از روش‌های اتوماسیون برای بررسی و به‌روزرسانی خودکار نسخه‌های API و میزبان‌ها.

تعیین سیاست های به‌روزرسانی برای نسخه های API قدیمی و عدم پشتیبانی از آنها.

برچسب خورده:
جهت ارسال ديدگاه وارد شويد و يا ثبت نام كنيد.