{"id":315,"date":"2021-11-07T12:43:50","date_gmt":"2021-11-07T12:43:50","guid":{"rendered":"https:\/\/apimike.com\/?p=315"},"modified":"2022-12-12T22:00:26","modified_gmt":"2022-12-12T22:00:26","slug":"how-to-secure-rest-api","status":"publish","type":"post","link":"https:\/\/apimike.com\/how-to-secure-rest-api","title":{"rendered":"How to secure REST API"},"content":{"rendered":"\n

You’re a hardworking dev and you build an outstanding application, you spend gazillion hours upon hours designing the structure to make it easy to use by other developers and clients.<\/p>\n\n\n\n

Your API successfully interacts between your microservices and then a fear rises and you confront your colleagues and superiors with the dilemma of \u201cAt the end I expose my application on the web which means my data will not be safe!\u201d.<\/p>\n\n\n\n

In this article we will explain how to secure REST API in order to reduce the attack scenarios.<\/p>\n\n\n\n

What\u2019s a REST API?<\/h2>\n\n\n\n

A REST API is an API that conforms to the architectural style of REST and allows interaction with RESTful web services. <\/p>\n\n\n\n

REST stands for RE<\/strong>presentational S<\/strong>tate T<\/strong>ransfer.<\/p>\n\n\n\n

APIs (application programming interfaces) provide a way for app developers to \u201ccall\u201d information from outside sources into an application they build.<\/p>\n\n\n\n

First steps for a secure REST API.<\/h2>\n\n\n\n
\n
\n
\"API<\/figure>\n<\/div>\n\n\n\n
\n

USING THE RIGHT API TOOL FROM THE TOOLBOX<\/h3>\n\n\n\n

????Always consider a CI\/CD solution as your first steps for a more secure REST API, one of the common solutions is BLST Security Cherrybomb<\/a><\/p>\n<\/div>\n<\/div>\n\n\n\n

First of all RESTful APIs must be stateless \u2013 so request authentication\/authorization should not depend on sessions.<\/p>\n\n\n\n

Stateless in software means that it does not keep track of configuration settings, transaction information or any other data for the next session.<\/p>\n\n\n\n

There are multiple ways to secure a RESTful API e.g. basic auth, OAuth, etc.<\/p>\n\n\n\n

Each API request should come with some sort of authentication credentials that must be validated on the server for every request.<\/p>\n\n\n\n

 There are two types of api, public API and private API two of them do not require the same level of attentiveness.<\/p>\n\n\n\n

 Public APi:<\/strong><\/p>\n\n\n\n

 As the name indicates, public API allows the resources to be public to the internet.<\/p>\n\n\n\n

For example \/prices or \/product are resources available in your website for users without authentication. <\/p>\n\n\n\n

Even though the resource is public I strongly recommend using an API key (API Key is a single token string) to limit the chance of abuse.<\/p>\n\n\n\n

Private API:<\/strong><\/p>\n\n\n\n

An internal (or private) API is intended only for use within the company, to connect systems and data within the business. Internal API can provide information about users on the platform and that data has to be accessible only to the owners.<\/p>\n\n\n\n

Fu<\/strong>ndamentals for a secure REST API the AAA:<\/h2>\n\n\n\n

 Authentication.<\/strong><\/p>\n\n\n\n

Authentication is used to know who called the endpoint of the Rest api.<\/p>\n\n\n\n

Authentication proves that you are who you say you are.<\/p>\n\n\n\n

Authorization<\/p>\n\n\n\n

Authorization occurs after the user authenticates himself, authorization deals with which resources are permitted to be used by the caller.<\/p>\n\n\n\n

Since each HTTP call is handled independently, each time your application receives a call, you will need to decide whether or not the caller is allowed to consume that particular resource.<\/p>\n\n\n\n

Accountability.<\/p>\n\n\n\n

Accountability (also traceability) is about who did what, when and with what resources.<\/p>\n\n\n\n

To accurately track what is happening in your system, you need to note each HTTP call the 3 W\u2019s, what, who and when:<\/p>\n\n\n\n