cover photo

RESOURCE · 6/7/2024

Securing APIs

An article which briefs us about API security. It includes the need for API security and some methods by which we can secure our APIs.

Sumanth G
Sumanth G
OP
Securing APIs

Securing APIs

Application Programming Interfaces (APIs) serve as the links between interconnected systems and services throughout the digital ecosystem, which enable data to flow, systems to communicate, and applications to connect. APIs play a vital role in modern software development, enabling the integration of services and facilitating the exchange of information. The ubiquity of APIs is a testament to their success in supporting a vast number of functions in modern software development. However, their prominence has also made APIs a target for cyberattacks.


API security amounts to a number of measures and practices designed to safeguard APIs from unauthorized access, data breaches, and cyber threats. It encompasses protecting the confidentiality, integrity, and availability of data transmitted and accessed through APIs. Managing API security is instrumental in preserving sensitive information, ensuring compliance with data privacy regulations, and upholding an organization’s reputation.


API security considerations extend to various types of APIs, including Representational State Transfer (REST) APIs and Simple Object Access Protocol (SOAP) APIs. Although both serve as intermediaries for communication between software components, they have distinct security considerations.


What is API security?

API security amounts to a number of measures and practices designed to safeguard APIs from unauthorized access, data breaches, and cyber threats. It encompasses protecting the confidentiality, integrity, and availability of data transmitted and accessed through APIs. Managing API security is instrumental in preserving sensitive information, ensuring compliance with data privacy regulations, and upholding an organization’s reputation.


Injection attacks: Injection attacks, such as SQL injection and XML injection, occur when attackers inject malicious code into API requests. This code can manipulate an API’s behavior and potentially grant access to sensitive data. Some common SQL injection examples include: Retrieving hidden data, where you can modify a SQL query to return additional results.


Authentication challenges: Weak or insufficient authentication mechanisms can lead to unauthorized API access. Attackers may employ techniques like brute force attacks or session hijacking to circumvent authentication.


Data Protection: APIs often handle sensitive data, such as personal information or financial data, which must be protected from unauthorized access or interception. Ensuring the confidentiality and integrity of data transmitted over APIs requires implementing encryption techniques.


Insecure Data transmission: APIs transmitting data without encryption are susceptible to eavesdropping. Data transferred unencrypted can be sniffed. This can not only give an attacker valuable information, but also the content of session cookies, allowing him to hijack a session.



Some methods to ensure secure APIs:


1. Always Use a Gateway


An API Gateway acts as a mediator between client applications and backend services in microservices architecture. It is a software layer that functions as a single endpoint for various APIs performing tasks such as request composition, routing, and protocol translation. The API gateway controls requests and responses by managing the traffic of APIs while enforcing security policies.


APIs should be always put behind a gateway. API gateways centralize traffic features and apply them to every request that hits your API. These features may be security-related, like rate limiting, blocking malicious clients, and proper logging. Or, they may be more practical and business-related, like path and headers rewriting, gathering business metrics, and so on.


Not having these controls could easily result in a serious security threat. Without a gateway, API providers would have to reinforce each endpoint with these features one-by-one.


2. Always Use a Central OAuth Server


OAuth (Open Authorization) is a protocol that allows a user to grant limited access to their resources on one site to another site without sharing their credentials. An OAuth server is responsible for managing and issuing access tokens that allow third-party applications to access protected resources on behalf of a user. It's a crucial component in enabling secure and controlled access to APIs and services.


Do not let your APIs or gateways issue access or refresh tokens. A centralized OAuth server should always issue such tokens. Issuing tokens requires many complex processes: authenticating the client, authenticating the user, authorizing the client, signing the tokens, and other operations. All these functions require access to different data, such as client information or the preferred authentication mechanism. Furthermore, if many entities issue and sign tokens, it becomes increasingly challenging to manage all the credentials used for signing. Only one entity can safely handle these processes — an OAuth server.


Example of OAuth server:

When using an app that connects to Google services like Drive, users are redirected to Google's login page. After granting permission for the app to access their Drive files, Google issues an access token. This token enables the app to securely upload data without requiring the user's password, ensuring both convenience and security.


3.Use JSON Web Tokens Internally


JSON Web Tokens (JWTs) are compact tokens used for securely transmitting information between parties in web applications. They contain a header, payload, and signature, making them ideal for authentication and authorization purposes due to their ease of use and security features.


When APIs are concerned, using JSON Web Tokens (JWTs) as access and refresh tokens is a good practice. Services that receive JWTs can leverage claim information to make informed business decisions: Is the caller allowed to access this resource? What data can the caller retrieve?


However, when tokens are exposed outside your infrastructure and especially when exposed to third-party clients, you should use opaque tokens instead of JWTs. Information in a JWT is easy to decode and thus available to everyone. If JWT data is public, privacy becomes a concern. You must ensure that no sensitive data ends up in the JWT's claims.


4. Use Scopes for Coarse-Grained Access Control


API scopes represent an authorization from the owner of a business resource for a client application to call a particular business resource API. They are used by API gateways to determine the authorized scope of client access to the API. They are sometimes extended to provide additional first-pass role-based end-user access control providing client applications with visibility of API operations available to users.


Scopes allow the authorization server to control what resource an external application has access to. It limits the "scope" of a token issued by the service.


OAuth scopes limit the capabilities of an access token. If stolen client credentials have limited scopes, an attacker will have much less power. Therefore, you should always issue tokens with limited capabilities. Verification of token scopes can be done at the API gateway to limit the malicious traffic reaching your API. You should use scopes during coarse-grained access control. This control could include checking whether a request with a given access token can query a given resource or verifying the client can use a given Content-Type.



5.Encryption


APIs are often used to transmit sensitive data, such as personal information, financial transactions, or health records. If these data are not encrypted, they can be intercepted, modified, or stolen by malicious actors who can access the network or the API server. This can result in data breaches, identity theft, fraud, or legal liabilities.


To prevent these scenarios, you need to encrypt your data at rest and in transit. Data at rest refers to data that are stored on physical or virtual media, such as disks or databases. Data in transit refers to data that are moving between components, locations, or programs, such as over the network or across a service bus. By encrypting your data at both states, you can ensure that only authorized parties can access and understand your data.


Ensuring the confidentiality and integrity of data transmitted over APIs requires implementing encryption techniques like TLS (Transport Layer Security) for data in transit and secure storage solutions for data at rest. Additionally, securely managing encryption keys and implementing access controls are essential for data protection.



Conclusion:


Keeping your APIs secure is crucial for protecting important data and keeping your systems safe. By setting up strong security measures like user authentication, authorization, and encryption, you can lower the risk of attacks like hacking or data theft. It's also important to watch out for new threats, update security regularly, and check for any weaknesses. Prioritizing API security not only keeps your information safe but also builds trust with users and helps your business run smoothly.

UVCE,
K. R Circle,
Bengaluru 01