Secure Your Python API. Protect your API from three dimensions | by Xiaoxu Gao | Jun, 2022

Defend your API from three dimensions

Photograph by Tobias Tullius on unsplash

API has change into a foundational ingredient of at this time’s app-driven world. Many firms together with Amazon have adopted the API-first strategy. It views the position of APIs as unbiased merchandise, reasonably than integration options with different techniques.

By design, APIs expose helpful info to the world comparable to public climate information and personal delicate Personally Identifiable Data (PII) information, and due to this, APIs have become new targets for hackers. With out safe APIs, constructing profitable merchandise could be unattainable.

This text is meant to be a wake-up name for those who haven’t utilized any safety technique to your API but. As regular, I might be offering Python implementation for a number of the finest practices. Securing your API could be seen as a non-functional requirement (NFR). In widespread with different NFRs like efficiency and reliability, it’s onerous to outline when precisely the API is safe sufficient.

That’s why many firms would rent a 3rd get together to execute an API penetration check to have a good judgment. Briefly, the pen tester will attempt to hack towards your API to examine for any exploitable vulnerability.

Context of API Safety

First issues first. Why can we even have to care about API safety? API defines a set of operations that the caller can use. If I don’t need this operation to be known as, then I can merely exclude it or put it behind a firewall.

To reply this query, we have to know what precisely we have to safe. API safety entails a number of safety disciplines: info safety, community safety, and software safety.

Data safety is worried with the safety of delicate information. Every endpoint is an operation utilized to a number of objects. The operation of an object could be accessible to solely a gaggle of customers with a particular position.

Individuals with totally different roles can sit in the identical room sharing the identical wifi, thus merely blocking the community just isn’t an optimum resolution. A kind of authentication and authorization must be performed when API receives requests from the shopper.

The distinction between authentication and authorization. Merely put, authentication is the method of verifying who you’re. For instance, there’s an inner endpoint inside an organization that exposes workers’ salaries.

Authentication verifies you’re the worker of this firm. Authorization is the method of verifying what particular objects you’ve entry to. On this case, in case your determine examine passes, it verifies in case you have permission to examine firm PII information.

Community safety protects the community and information from breaches and different threats. There are an entire lot of issues round this subject, however within the context of API, probably the most relevant part is TLS protocol (Transport Layer Safety) which is to supply communication safety.

That’s mainly the ‘S’ a part of HTTPS protocol. In the course of the TLS handshake, the 2 events alternate messages to acknowledge one another and agree on the encryption algorithm and session key they may use. From then on, the communication might be encrypted utilizing the session key.

That is to keep away from the man-in-the-middle assault.

It’s price noting that TLS is used to encrypt information in transit not information at relaxation. That’s past the scope of this text.

Supply: TLS handshake steps

Utility safety ensures that the software program is designed to resist assaults and misuse. For instance, hackers may inject script or code into the request and the malicious information can trick the software program into executing unintended instructions. Making use of enter validation turns into essential.

In the remainder of the article, I’ll checklist just a few sensible safety suggestions in Python to safeguard the appliance.

After understanding what we need to safe within the context of API, let’s soar into some examples that are impressed by this OWASP API Security Top 10 checklist.

Authentication and Authorization

That is in all probability crucial precept that we have to comply with in terms of API safety. There are a number of authentication and authorization strategies they usually range in complexity and safety. However they attain a consensus — the shopper should present some sort of credentials associated to its id and the server will evaluate what was despatched to what has been saved.

If the credentials match, the server will create a consumer session and challenge a cookie to the shopper.

Fundamental Authentication

Fundamental authentication is, nicely, very fundamental. The shopper merely contains their username and password within the request header. It might be in plaintext or encoded into base 64 to save lots of house.

requests.get('https://httpbin.org/basic-auth/foo/bar', auth=('foo', 'bar'))
Encoded utilizing base64 (Created by creator)

Fundamental authentication is the least authentication technique as a result of if the visitors is hijacked by a hacker, the hacker can simply get credentials from the request header or carry out a brute-force assault the place they fight totally different usernames and passwords till they discover one thing works.

API key

An API secret’s a novel string generated by the server to authenticate shoppers. It’s totally different from the base64 string in fundamental authentication wherein the string is generated by the shopper themselves. As soon as the API shopper will get the important thing from the server, they’ll embody it someplace within the request specified by the supplier, which could be question string parameters, request header, or request physique.

The method of key technology will depend on the supplier. For instance, NASA Open API requires shoppers to supply their title and e mail. And the secret’s purported to be handed as a URL parameter.

Key technology of NASA API
Utilizing API key in Python

An API secret’s safer than fundamental authentication. First, it’s lengthy, complicated, and randomly generated which makes it tougher for hackers to brute-force assault. Apart from, the important thing can include extra info comparable to expiration time. Even when the secret’s in some way disclosed, it’s going to quickly change into invalid after the expiration time.

Nevertheless, API keys are nonetheless not thought-about safe. Normally, the software program makes use of one algorithm to generate the important thing and the important thing may include consumer info. Hackers could guess the algorithm by studying the API shoppers.

In accordance with the recommendation from Google, it is sensible to require API keys if you wish to perceive utilization patterns in your API visitors, block nameless visitors, or management the variety of calls, however don’t use API keys to authenticate or authorize customers.

JWT (JSON Net Token)

JWT is a token-based authentication. The shopper first authenticates to the API supplier with a username and password. The supplier generates a JWT and sends it again to the shopper. Then the shopper provides the token to the authorization header within the API request.

JWT token in Python
JWT auth stream (Created by creator)

The token itself has three components: the Header part accommodates the algorithm used for the signature and in addition to the token kind. The Payload part accommodates the token information just like the username, token technology date, and expiration date. The Signature part is the results of Header and Payload, concatenated and encrypted with a non-public key.

When the server receives the token, it’s going to decrypt the signature with the non-public key and evaluate it with the header and payload. JWT is safer than API key as a result of 1) the token doesn’t include delicate information like passwords, and a pair of) it’s method tougher to decrypt the signature which is signed utilizing HMAC-SHA256.

PyJWT is a Python library that means that you can encode and decode JSON Net Tokens (JWT). It’s largely used on the server facet.

Nevertheless, there are nonetheless some drawbacks to JWT. If a consumer needs to vary their password and if authentication has been carried out beforehand, then the token with the earlier password will nonetheless be legitimate till expiry. Apart from, the consumer must reauthenticate after the token will get expired.

To cope with these challenges, some JWT libraries permit the refresh token mechanisms or power a consumer to reauthenticate in some instances.

OAuth 2.0

OAuth 2.0 is an authorization normal that permits totally different providers to entry one another’s information. A use case is that my software can log in to a consumer’s Twitter account and retrieve the consumer’s tweets with out understanding the consumer’s Twitter credential.

Earlier than OAuth, an software would simply retrieve your Twitter credentials by means of the shape and do the login in your behalf, which is usually dangerous as a result of the appliance can retailer all of the consumer’s Twitter credentials. With OAuth, the communication between providers is token-based.

Right here is the summary Oauth2.0 stream. Typically, there are six steps. The appliance first requests authorization to entry Twitter information from the consumer. If the consumer authorizes the request, that means efficiently login into Twitter, the appliance will obtain an authorization grant.

Later, the appliance requests the entry token from Twitter by presenting authentication of its personal id and authorization grant. If all the things is verified, the appliance retrieves consumer assets by presenting an entry token.

Summary Oauth 2.0 stream (Created by creator)

The precise implementation varies from supplier to supplier. I wrote an article about building a Twitter login component using NextJS and Flask the place I defined the Twitter Oauth stream intimately.

A price restrict is the variety of API calls a consumer could make inside a given time interval. If this restrict is exceeded, the consumer could also be throttled. Any public API is topic to a price restrict as a result of it protects API from extreme use to keep away from issues like DDoS assaults.

Right here is the vulnerability from Zoom that allows an attacker to attempt 1 million Zoom passwords in a matter of minutes as a result of a scarcity of a price restrict. In a nutshell, rate-limiting limits entry for customers to entry API primarily based on the brink set by the API’s supplier.

Apart from defending useful resource utilization, price limiting could be applied to regulate information stream. In a distributed system, API hundreds may not be evenly distributed throughout processors. Having a price restrict avoids the state of affairs the place one processor is overloaded whereas the opposite is idle.

Fee limits can be applied to regulate prices. Each request will at all times generate a value, and the extra requests an API will get, the extra prices it’s going to accumulate. Fee limits can play an especially necessary position to save lots of prices. And the identical goes for the API customers.

Many trendy API providers present totally different subscription fashions primarily based on the speed restrict. In that case, totally different price limiting could be utilized to particular person customers primarily based on their wants and it’ll guarantee honest use with out disrupting others’ entry.

One of many Python libraries that implements price limiting is ratelimiter. In reality, this library applies a price restrict on any perform, not simply particular to APIs. You possibly can select to make use of it as a decorator or context supervisor.

from ratelimiter import RateLimiter

@RateLimiter(max_calls=10, interval=1)
def api_call():
go

rate_limiter = RateLimiter(max_calls=10, interval=1)

for i in vary(100):
with rate_limiter:
do_something()

When a price restrict is in place and triggered, your API ought to return the next HTTP standing code: 429 Too Many Requests . Though there is no such thing as a normal title for the HTTP headers to tell customers of the speed restrict, many trendy API providers together with GitHub and Twitter use the next headers.

The outline of every header can range from supplier to supplier. However usually, they imply:

  • X-RateLimit-Restrict : The utmost variety of calls allowed per restrict window (could be 1 hour/minute).
  • X-RateLimit-Remaning : The variety of requests remaining within the present restrict window.
  • X-RateLimit-Reset : The time at which the present price restrict window resets in UTC.

Some APIs additionally use headers like Retry-After to inform customers after they can name it once more. So, please discover this info within the API documentation.

As a consumer, what ought to we do when receiving a 429 standing code? We shouldn’t dodge it, however as an alternative, deal with it accordingly. There are a number of choices: 1) The response often accommodates one of many above headers with the variety of seconds it is best to wait till the following request. Remember the fact that ready for too lengthy in a job queue just isn’t thought-about apply.

It’s best to as an alternative retry the request at a later time to unlock the queue for different issues. 2) If the server doesn’t inform you how lengthy to attend, you should use exponential backoff. The shopper periodically retries a failed request with rising delays between requests. The delay will increase from one, two, 4, eight seconds, and longer. Google gives a Retry class in google-api-core which is a helper for retrying capabilities with exponential back-off.

One other side of securing your API is to have full visibility on “who did what, the place, and when”? Many enterprises like Google and Slack present Audit Log API which gives evaluation of how your assets are being accessed.

The thought is to provide useful resource house owners the flexibility to question consumer motion. With this API, the useful resource proprietor may feed useful resource entry information into an auditing device. The proprietor may also monitor for potential safety points or malicious entry makes an attempt.

So, if you’re a shopper, take into consideration leveraging Audit log API to construct monitoring and alerting techniques. In case you are the service supplier, take into consideration offering such API to your shoppers and assist them pay attention to any malicious consumer patterns.

Enter validation is a part of the appliance safety. It’s to make sure solely correctly shaped information is coming into the system, stopping malformed information from persisting within the database or triggering malicious scripts. Enter validation ought to occur as early as doable within the information stream. Within the context of API, it ought to occur as quickly as the information is acquired from the shopper.

It’s usually apply to use automated schema validation as quickly as the information arrives. Many API frameworks like connexion assist OpenAPI specs. The specification file contains every kind of validations that might be robotically utilized to the request physique by the framework.

The returned standing code is generally 400: Dangerous Request with a descriptive error message like The worth is predicted to be a optimistic integer. It helps the shopper to grasp what goes mistaken and alter their request accordingly.

When an error happens on both the shopper’s facet or the server’s facet, the API response ought to give some hints concerning the error cause. However API ought to by no means leak an excessive amount of details about the inner server error. For instance, standing 500 means an inner server error. And that is sufficient.

Don’t put the precise error cause within the response physique, for example, a selected database error message or reminiscence error. In contrast to 400 Dangerous Request error, such a message gained’t assist the shopper as a result of they’ll’t do something with it. The rule of thumb is to make sure that APIs solely return as a lot info as is critical to meet their perform.

I hope you discover this text helpful! The checklist can positively go on and on.

Be at liberty to remark under if you already know different API safety objects everyone wants to pay attention to.

Cheers!

https://brightsec.com/blog/api-security/

More Posts