Secure Your RESTful APIs

Creating an API is simple, the next thing that comes along with it is securing it. If API is vulnerable, the users’ sensitive information will be at risk

some of the Attacks that hackers can use against your API:

  • Code injection

  • Cross-site scripting

  • Distributed Denial of Service (DDoS)

  • Man-in-the-middle

  • Credential Stuffing

API securing Methods :

  1. Rate limiting - rate-limiting by setting a threshold above which requests will be rejected

  2. Authentication and Authorization

  3. Least Privileges to user

  4. Validate Input (as attackers can send malicious code to the server that can crash it)

  5. Failsafe (user’s default access level to any resource in the system should be denied unless permitted)

Various ways you can make your APIs more secure against external attacks

Rating Limiting (rate-limiting by setting a threshold above which requests will be rejected) - DDoS attack can bring down your API and, with it, crashes every application that relies on your API. To prevent this, you can implement rate-limiting by setting a threshold above which requests will be rejected.

Authentication And Authorization -- It is standard way to authenticate and authorize users to its resources. For this, you should use the OAuth2.0 standard to ensure only authorized users are accessing the data. If not implemented, your API data will be vulnerable.

Least Privileges When a user or entity accessing your API should only be provided the least amount of access to the API to perform their actions

Input validate Always validate the input that the user is sending along with the API. If you don’t, the attackers can send malicious code to the server that can, in turn, crash it.

Fail-Safe Implement a fail-safe. The user’s default access level to any resource in the system should be denied unless they have been granted a permit explicitly.

Practices for Securing API Keys

API authentication -- Validating the clients of an API to identify who is calling Authentication allows an API to restrict access to its endpoints, which is essential for securing an API

with the help of API keys, you can verify the identity of each app or user and mitigate the risks of unauthorized access.

API HUB generates secret keys for implementing API Authentication by using secure key. API keys allow APIs to limit access to the API endpoints based on the requirements and implement rate and usage limits

How to secure your API keys Don't Embed in Code

Don't Embed in Code

Don't do hardcoding the API keys in their applications, Always use environment variable (.env) files or secrets to store API keys. Don't forget to add the .env files to gitignore before pushing your code to a GitHub repository so that the file never gets exposed.

Regenerate Keys Regenerate your API keys regularly and update the keys in your applications. Doing so will render the exposed key useless.

Restrict access Avoiding the dangers of key exposure is to restrict what the key can do. APIs should allow the end-users to limit the API key access to specific actions. They can create separate keys for separate actions like GitHub does with their tokens.

Scenarios where you can start testing your API Testing cases And Securing Your API

Testing API security regularly ensures its integrity. It will also help you find loopholes that can bring down your API.

Areas To Test In An API

  1. Request Body

  2. Handling of code injections

  3. Data Handling

  4. Payload Size

1.RequestBody: Users can send more data to your API by intercepting the request in the middle. This can lead to severe repercussions. This is one area where you can test your API security via an API Client to check how it performs.

Ideally, you should never store the request body as it is in your database. You may have added some checks that reject the API call if a particular value is not present inside the request body. This check doesn’t validate if the request body contains more data than expected.

2.Handling of code injections An API developer should never rely on the end user to provide accurate data. They should always validate the data provided by the user against the defined standards. If the user has provided a description, it should also be checked against a regex. This is another area where we can test our API security.

Due to a lack of API security, malicious code often gets to the server. If the code is executed, it can affect server files.

  1. Data handling Under-fetching and over-fetching are most common in REST APIs. But it is also possible that you handle it on the server. Sending excessive amounts of data to the client is a bad practice and another area where we can test our API.

  2. Payload Size In POST/PUT requests, we take data from the user and send it to the server as a payload. The size of the payload can significantly affect your API performance. If the user uploads a huge file, it can ultimately bring the API down and the web app. This is another area where we can test our API.

5.Prevention against Denial of Service Attack Denial of service is one of the most common ways to bring an API down. Implementing rate limiting in your API is a good way to take care of it. Otherwise, even an authenticated user can call your API multiple times in a minute and take it down.

Authentication

Different ways you can implement authentication in your applications. For instance,

you can implement sign-up functionality for new users. If a user comes back to your application, they would have to sign in again using their email and password.

You can also set a one-time password to authenticate a user for only a single session or transaction.

Another way is using biometric authentication, where the user presents their fingerprint or retina scan to gain access to your application.

you can also add a two-factor authentication layer to your system where the user has to provide the code sent to their mobile or email after giving the username or password.

Authorization

Authorization always comes after authentication. It is the process of permitting users to access different resources from the server, and it’s not visible and changeable by the user.

Authorization works through settings that are implemented and maintained by the organization.

An application may be designed for different kinds of users - admin, customer

Authentication Methods There are many authentication methods that you can use with your REST APIs. Let’s discuss the three most common methods among the lot.

HTTP Authentication Schemes

There are various HTTP security schemes that you can use with your REST APIs for authentication. For instance:

Basic: With this, the sender places the username and password in the request header. Both the username and password are encrypted with Base64. The server decrypts the data and sends back a response of whether the user is authenticated or not.

**Bearer: ** An HTTP authentication scheme where the server generates a token and provides it to the client. The client then has to send this token in the Authorization header when making requests to protected resources.

**Digest: ** This type of authentication does not need a password to be transmitted. The client takes the username and password and uses the MD5 hashing algorithm to create a hash that is then sent to the SQL server.

**OAuth: ** It is an authorization protocol that provides applications the ability to secure designated access.

API Keys Another authentication method widely used with REST APIs is API keys. It provides first-time users with a unique generated key. When the user tries to access the requested resources, they use their API key. The API key tells the server this is the same user as before.

API keys must not be sent to the server as query parameters. Instead, they should be sent in the Authorization header using REST APIs.

OAuth 2.0 OAuth 2.0 (Open Authorization) is a standard developed to allow a user access to resources from a third-party application. It is an authorization protocol designed only to grant access to resources, and it works by using access tokens.

The access token is information that provides authorization to access resources on behalf of the user. Usually, the JSON Web Token (JWT) format is used for the access token.

They also may have an expiration date because of security reasons.

API Gateway supports multiple authentication methods that are suited to different applications and use cases. API Gateway uses the authentication method that you specify in your service configuration to validate incoming requests before passing them to your API backend.

Did you find this article valuable?

Support Ashok V by becoming a sponsor. Any amount is appreciated!