API10:2023 - Unsafe Consumption of APIs چیست

به واسطه آسیب پذیری فوق مهاجم قادر است با ارسال و یا دریافت اطلاعات از منابع زنجیره تامین، اطلاعات و یا درخواست های مورد نظر خود را در گروه مشخص اجرا نماید.



مثال:

درخواست GET برای دریافت اطلاعات هواشناسی از سرویس شخص ثالث:


GET /api/weather?location=New+York


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


[ApiController]

[Route("api/weather")]

public class WeatherController : ControllerBase

{

    private readonly IWeatherService weatherService;



    public WeatherController(IWeatherService weatherService)

    {

        this.weatherService = weatherService;

    }



    // GET /api/weather

    [HttpGet]

    public IActionResult GetWeather(string location)

    {

        // Make a direct call to the third-party weather API

        WeatherData weatherData = weatherService.GetWeatherData(location);

        return Ok(weatherData);

    }



    // Other methods...

}


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


[ApiController]

[Route("api/weather")]

public class WeatherController : ControllerBase

{

    private readonly IWeatherService weatherService;



    public WeatherController(IWeatherService weatherService)

    {

        this.weatherService = weatherService;

    }



    // GET /api/weather

    [HttpGet]

    public IActionResult GetWeather(string location)

    {

        // Validate the location parameter and restrict access to trusted sources

        if (!IsValidLocation(location))

        {

            return BadRequest();

        }



        // Make a call to the third-party weather API through the weather service

        WeatherData weatherData = weatherService.GetWeatherData(location);



        if (weatherData == null)

        {

            return NotFound();

        }



        return Ok(weatherData);

    }



    private bool IsValidLocation(string location)

    {

        // Implement validation logic to ensure the location is safe and trusted

        // This could involve white-listing trusted sources or validating against a known set of safe locations

        // Return true if the location is valid, false otherwise

        // Example: return Regex.IsMatch(location, "^[a-zA-Z]+(,[a-zA-Z]+)*$");

        // Implement your validation logic here



        // For simplicity, assuming any location is valid

        return true;

    }



    // Other methods...

}



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


@RestController

@RequestMapping("/api/weather")

public class WeatherController {

    

    private final ThirdPartyWeatherService weatherService;

    

    public WeatherController(ThirdPartyWeatherService weatherService) {

        this.weatherService = weatherService;

    }

    

    // GET /api/weather

    @GetMapping

    public ResponseEntity<WeatherData> getWeather(@RequestParam String location) {

        // Make a direct call to the third-party weather API

        WeatherData weatherData = weatherService.getWeatherData(location);

        return ResponseEntity.ok(weatherData);

    }

    

    // Other methods...

}


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


@RestController

@RequestMapping("/api/weather")

public class WeatherController {

    

    private final ThirdPartyWeatherService weatherService;

    

    public WeatherController(ThirdPartyWeatherService weatherService) {

        this.weatherService = weatherService;

    }

    

    // GET /api/weather

    @GetMapping

    public ResponseEntity<WeatherData> getWeather(@RequestParam String location) {

        // Validate the location parameter and restrict access to trusted sources

        if (!isValidLocation(location)) {

            return ResponseEntity.badRequest().build();

        }

        

        // Make a call to the third-party weather API through the weather service

        WeatherData weatherData = weatherService.getWeatherData(location);

        

        if (weatherData == null) {

            return ResponseEntity.notFound().build();

        }

        

        return ResponseEntity.ok(weatherData);

    }

    

    private boolean isValidLocation(String location) {

        // Implement validation logic to ensure the location is safe and trusted

        // This could involve white-listing trusted sources or validating against a known set of safe locations

        // Return true if the location is valid, false otherwise

        // Example: return location.matches("^[a-zA-Z]+(,[a-zA-Z]+)*$");

        // Implement your validation logic here

        

        // For simplicity, assuming any location is valid

        return true;

    }

    

    // Other methods...

}


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

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

بررسی و تحقق از امنیت و استانداردهای امنیتی سرویس شخص ثالث قبل از اتصال به آن.

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

محدود کردن دسترسی و سطوح اجازه داده شده به سرویس‌های شخص ثالث و تنظیم محدودیت‌های مناسب.

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

مانیتورینگ و پایش مداوم برای تشخیص و رفع هرگونه نقص در امنیت سرویس‌های خارجی.

آموزش توسعه‌دهندگان در خصوص اصول امنیتی و استفاده صحیح از API های خارجی.

 

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