Sobes.tech
Junior — Middle

Kuidas kaitsta API-d volitamata juurdepääsu ja rünnakute eest?

sobes.tech AI

Vastus AI-lt

To protect the API from unauthorized access and attacks, the following methods are applied:

  • Authentication and authorization: Using tokens (JWT, OAuth), API keys, Basic Auth to verify user rights.
  • Encryption: Transmitting data over HTTPS to prevent interception.
  • Rate limiting: Preventing DDoS attacks and abuse.
  • Validation and filtering of incoming data: Protecting against SQL injections, XSS, and other attacks.
  • Logging and monitoring: Tracking suspicious activity.
  • Using CORS: Controlling access from different domains.
  • Protection against CSRF: Using tokens or other mechanisms.

Example of simple authentication using an API key in Go:

func apiHandler(w http.ResponseWriter, r *http.Request) {
    apiKey := r.Header.Get("X-API-Key")
    if apiKey != "expected_api_key" {
        http.Error(w, "Unauthorized", http.StatusUnauthorized)
        return
    }
    // Processing the request
}