We are very much in the age of APIs. From widely-used single-purpose products like Slack to cloud-based solutions like Amazon Web Services (AWS) and Microsoft Azure, APIs are used to drive business processes in all kinds of industries, every day. For tech companies, whether you’re doing a monolithic back-end, containerized microservices, or serverless architecture, the most common way to build applications today is to create a backing API with a decoupled front-end. In some ways, as technologists, we have gotten quite good at securing APIs. With that said, certain aspects of them are still overlooked more often than not.
I’m using the term rate-limiting in a broad sense here, including any effective combinations of dynamic throttling, lockouts, and anti-automation (think CAPTCHAs). Traditional server-rendered web apps once had the same issue. A lack of rate-limiting on the login page was an uncommon find. We got increasingly consistent with this over the years. Eventually, we started rate-limiting login forms more often than not. It seems that we have forgotten these lessons in the translation to RESTful APIs though, as I rarely see it. This roughly separates APIs into three groups:
You don’t want your API to be in the latter group.
If you’re taking individual user credentials (username, email address, password) on your API, you should be using anti-automation countermeasures. CAPTCHAs can work just as well on a RESTful endpoint as they do on a traditional web app. It just means that your API needs to validate the CAPTCHA submission.
If you allow the Content-Type header on the request to be any valid mime type, there are a few things that can go wrong:
<form>
element to be submitted in such a way that the request body is a well-formed JSON object, which provides another way of achieving CSRF.The content-type header’s purpose is to tell the server what type of data is in the request body. For the majority of modern APIs, application/json is the only content type they intend to support. Manyf back-end frameworks don’t check this header at all. Some other frameworks support other types by default, even if the developer of the API never intended to support those inputs. Your API should only accept requests that have a content-type matching your expectation.
You should give the same consideration to the accepts header as well, since requesting other formats of output, such as text/html, can result in unexpected vulnerabilities. If the developer intends to return an error message in a JSON structure, they probably aren’t escaping or encoding HTML. So if an attacker can instruct the framework to return HTML instead of JSON, it would increase the potential for cross-site scripting flaws. This is uncommon enough that it doesn’t warrant a separate list item, but it’s worth mentioning.
API routes tend to be relatively intuitive to somebody with access to a few samples, and some knowledge of the entities that the API deals with. An extremely common pattern is to have /company/23/user/42 type patterns using route parameters. When the API uses guessable or sequential route parameters, they become easily enumerable. This makes it likely that an attacker will try enumerating other numbers in the sequence sooner or later. That shouldn’t matter, because you should have good authorization controls anyway.
A high-entropy value like a UUIDv4 or GUIDv4 provides a layer of obscurity. We know that obscurity alone is not sufficient for providing meaningful security. But that does not mean that obscurity has no value. There are several scenarios where a high-entropy identifier increases the difficulty of exploiting a flaw that is present. Here are a few examples:
These practices are not the answer to critical issues like injection flaws. However, they highlight some core principles that will strengthen your API’s security posture. Don’t forget the lessons we, as an industry, learned from over twenty years of web application development. Be certain to use the standard HTTP headers effectively. Be rigid in your expectations for request structure. And be aware that APIs are a prime target for enumeration.
Checkout our other API security content below.
The post Three Excellent API Security Practices Most People Neglect appeared first on Professionally Evil Insights.
Click to Open Code Editor