API5:2023 - Broken Function Level Authorization چیست

به واسطه عدم احراز سیاست‌های کنترل دسترسی با سلسله مراتب دسترسی مهاجم امکان فراخوانی و اجرا درخواست های غیرمجاز از Endpoint مجاز برای دسترسی به منابع دیگر کاربران و/یا عملکردهای مدیریتی دسترسی را دارد.


مثال:

درخواست DELETE برای حذف یک نظر با شناسه نظر:


DELETE /api/comments/{comment_id}


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


[ApiController]

[Route("api/data")]

public class DataController : ControllerBase

{

    private readonly DataService _dataService;



    public DataController(DataService dataService)

    {

        _dataService = dataService;

    }



    [HttpGet]

    public IActionResult GetData()

    {

        // Get data from the service

        var data = _dataService.GetData();



        // Return the data

        return Ok(data);

    }



    [HttpPost]

    public IActionResult UpdateData(DataModel data)

    {

        // Update the data using the service

        _dataService.UpdateData(data);



        // Return success response

        return Ok("Data updated successfully");

    }



    // Other methods...

}


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


[ApiController]

[Route("api/data")]

[Authorize]

public class DataController : ControllerBase

{

    private readonly DataService _dataService;



    public DataController(DataService dataService)

    {

        _dataService = dataService;

    }



    [HttpGet]

    [Authorize(Roles = "ReadAccess")]

    public IActionResult GetData()

    {

        // Get the user's identity

        var identity = HttpContext.User.Identity as ClaimsIdentity;



        // Get the user's role

        var role = identity.FindFirst(ClaimTypes.Role)?.Value;



        // Check if the user has the required role for reading data

        if (role != "ReadAccess")

        {

            return Forbid(); // Return 403 Forbidden if the user is not authorized

        }



        // Get data from the service

        var data = _dataService.GetData();



        // Return the data

        return Ok(data);

    }



    [HttpPost]

    [Authorize(Roles = "WriteAccess")]

    public IActionResult UpdateData(DataModel data)

    {

        // Get the user's identity

        var identity = HttpContext.User.Identity as ClaimsIdentity;



        // Get the user's role

        var role = identity.FindFirst(ClaimTypes.Role)?.Value;



        // Check if the user has the required role for updating data

        if (role != "WriteAccess")

        {

            return Forbid(); // Return 403 Forbidden if the user is not authorized

        }



        // Update the data using the service

        _dataService.UpdateData(data);



        // Return success response

        return Ok("Data updated successfully");

    }



    // Other methods...

}



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


@RestController

@RequestMapping("/api/data")

public class DataController {

    private final DataService dataService;



    public DataController(DataService dataService) {

        this.dataService = dataService;

    }



    @GetMapping

    public ResponseEntity<List<Data>> getData() {

        // Get data from the service

        List<Data> data = dataService.getData();



        // Return the data

        return ResponseEntity.ok(data);

    }



    @PostMapping

    public ResponseEntity<String> updateData(@RequestBody Data data) {

        // Update the data using the service

        dataService.updateData(data);



        // Return success response

        return ResponseEntity.ok("Data updated successfully");

    }



    // Other methods...

}


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


@RestController

@RequestMapping("/api/data")

public class DataController {

    private final DataService dataService;



    public DataController(DataService dataService) {

        this.dataService = dataService;

    }



    @GetMapping

    @PreAuthorize("hasRole('ROLE_READ')")

    public ResponseEntity<List<Data>> getData() {

        // Get data from the service

        List<Data> data = dataService.getData();



        // Return the data

        return ResponseEntity.ok(data);

    }



    @PostMapping

    @PreAuthorize("hasRole('ROLE_WRITE')")

    public ResponseEntity<String> updateData(@RequestBody Data data) {

        // Update the data using the service

        dataService.updateData(data);



        // Return success response

        return ResponseEntity.ok("Data updated successfully");

    }



    // Other methods...

}



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

اعتبارسنجی کامل در هر عملکرد API بر اساس سطوح دسترسی و نقش کاربران.

استفاده از سیستم‌های مجوز دسترسی چندسطحی و اعمال سطوح دسترسی به منابع مختلف.

جداسازی صحیح بین عملکردهای مدیریتی و عادی و اعمال سیاست‌های دسترسی مناسب برای هر کدام.

بررسی مجوزها در هر عملکرد و اعتبارسنجی دسترسی کاربر در زمان اجرا.

استفاده از فریمورک‌ها و کتابخانه‌های مدیریت دسترسی کاربران و اجرای سیاست‌های دسترسی پیچیده‌تر مانند RBAC (Role-Based Access Control) یا ABAC (Attribute-Based Access Control).

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