API Security Best Practices
Tags: api, security, design, webdev
Published: 2020-01-14 19:19:55


Application Programming Interface (API) Security is the design, processes, and systems that keep a web-based API responding to requests, securely processing data and functioning as intended.

API Security is an incredibly broad topic as the many different API frameworks, and systems all operate differently. This guide will help you build better, more secure APIs and help you assess the security footing of your current API implementations.

<center>
<img src='https://expeditedsecurity.com/img/blog/undraw_security_o890.svg' style='width: 75%;' class='buffer-top buffer-bottom'>
</center>


## Why is API Security Important?

Modern web programming has split the front end (HTML/CSS/JS) of applications from the backend (Server-side processing and storage). Similarly, mobile applications often handle user-facing functions within the app but pass data back to a centralized server. These trends have sparked a massive increase in the number of web APIs that are now in production. 

## What differentiates API security from general Web security?

Web/HTTP Application Programming Interfaces (API) have unique threat models, security concerns, and authentication modes that are distinct from standard web applications. Much of this difference is because standalone APIs cannot rely on basic browser security features to help limit the scope of actions an attacker could do. 

## How to Secure an API 

This guide below is a mix of techniques and recommendations that:

- You can directly implement in your application
- Design choices you can make to strengthen your security model
- Features of Expedited WAF that can make the whole process easier


<span id='table-of-contents' class='toc-anchor'></span>

## TABLE OF CONTENTS

<ol>
    <li><a href='#denial-of-service'>Distributed Denial of Service Attacks on APIs</a>
        <ul>
            <li><a href='#classic-ddos'>Classic DDoS Attacks</a></li>
            <li><a href='#modern-ddos'>Modern Denial of Service Attacks</a></li>
            <li><a href='#out-of-control-client'>Out of Control API Client [Case Study]</a></li>
        </ul>
    </li>
    <li>
        <a href='#data-breach-attacks'>Data Breach Attacks</a>
        <ul>
            <li><a href='#sql-injection'>SQL Injection (SQLi)</a></li>
            <li><a href='#json-sql-injection'>JSON SQL Injection [Case Study]</a></li>
            <li><a href='#non-sql-injection'>Non SQL Query Injection (NSQLi)</a></li>
            <li><a href='#social-search-problems'>Social Search Problems [Case Study]</a></li>
            <li><a href='#xss-attacks'>XSS & Code Injection API Attacks</a></li>
        </ul>
    </li>
    <li>
        <a href='#functionality-attacks'>Functionality and Resource Attacks</a>    
        <ul>
            <li><a href='#email-sending'>Email Sending</a></li>
            <li><a href='#location-tracking'>Location Tracking</a></li>
            <li><a href='#link-publishing'>Link and File Publishing</a></li>
            <li><a href='#processing'>Processing</a></li>
            <li><a href='#phishing'>Phishing</a></li>
        </ul>
    </li>
    <li><a href='#owasp'>OWASP Top 10 Vulnerabilities</a>
        <ul>
            <li><a href='#why-care'>Why care about the OWASP Top 10?</a></li>
            <li><a href='#is-top-10'>Is the OWASP Top 10 for APIs?</a></li>
        </ul>
        <ol>
            <li><a href='#code-injection'>Code Injection</a></li>
            <li><a href='#broken-authentication'>Broken Authentication</a></li>
            <li><a href='#sensitive-data-exposure'>Sensitive Data Exposure</a>
                <ul>
                    <li><a href='#devise-auth-ip'>Devise Auth IP [Case Study]</a></li>
                </ul>
            </li>
            <li><a href='#xxe'>XML External Entities (XXE)</a></li>
            <li><a href='#broken-access-controls'>Broken Access Controls</a></li>
            <li><a href='#security-misconfiguration'>Security Misconfigurations</a></li>
            <li><a href='#xss'>Cross Site Scripting</a></li>
            <li><a href='#insecure-deserialization'>Insecure Deserialization</a></li>
            <li><a href='#vulnerable-components'>Using Components with Vulnerabilities</a></li>
            <li><a href='#insufficient-logging'>Insufficient Logging and Monitoring</a></li>
        </ol>
    </li>
    <li>
        <a href='#api-security-controls'>API Security Controls</a>
        <ul>
            <li><a href='#block-ddos-probes'>Block DDos Probes</a></li>
            <li><a href='#stop-anonymous-proxy-networks'>Stop Anonymous Proxy Networks</a></li>
            <li><a href='#designate-allowed-ip-ranges'>Designate Allowed IP Ranges</a>
                <ul>
                    <li><a href='#bots-in-production'>Bots In Production [Case Study]</a></li>
                </ul>
            </li>
            <li><a href='#geofiltering'>Geo Filtering
                <ul>
                    <li><a href='#south-african-nsfw'>South African NSFW Links [Case Study]</a></li>
                </ul>
            </li>
            <li><a href='#filter-bots'>Filter Bots</a>
                <ul>
                    <li><a href='#auto-scaling-bots'>Auto Scaling SEO Bots [Case Study]</a></li>
                </ul>            
            </li>
        </ul>
    </li>
    <li><a href='#api-security-in-transit'>API Security in Transit (SSL/TLS/HTTPS)</a>
        <ul>
            <li><a href='#https-enforcement'>HTTPS Enforcement for APIs</a></li>
            <li><a href='#tls-version-enforcement'>TLS Version Enforcement</a></li>
        </ul>
    </li>
    <li><a href='#api-authentication-methods'>API Authentication Methods</a>
        <ul>
            <li><a href='#api-keys'>API Keys</a></li>
            <li><a href='#basic-auth'>Basic Auth</a></li>
            <li><a href='#client-certificates'>Client Certificate API Authentication</a></li>
            <li><a href='#oauth'>OAuth 2.0 for APIs</a></li>
        </ul>
    </li>
    <li><a href='#api-authorization'>Authorization</a>
        <ul>
            <li><a href='#rbac'>Role Based Access Control</a></li>
            <li><a href='#abac'>Attribute Based Access Control</a></li>
        </ul>
    </li>
    <li><a href='#secure-api-design'>Secure API Design Guidelines</a>
        <ul>
            <li><a href='#simple-beats-complicated'>Simple Beats Complicated</a></li>
            <li><a href='#least-privilege'>Allow Least Privilege</a></li>
            <li><a href='#least-data-access'>Be Conservative in Exposing Data</a></li>
            <li><a href='#least-data-collected'>Least Data Collected</a></li>
            <li><a href='#versioning'>Versioning</a></li>
            <li><a href='#logging'>Logging Requests and Responses</a></li>
        </ul>
    </li>
    <li><a href='#security-procedures'>Security Procedures</a>
        <ul>
            <li><a href='#handling-security-reports'>Handling Security Reports</a></li>
            <li><a href='#data-breach-handling'>Data Breach Incident Planning</a></li>
        </ul>
    </li>
    

</ol>


<span id='denial-of-service' class='toc-anchor'></span>

<h2>1. Distributed Denial of Service (DDoS) Attacks on APIs</h2>

<a href='#table-of-contents'>⇑ Back to Table of Contents</a>

<center>
<img src='https://expeditedsecurity.com/img/blog/undraw_safe_bnk7.svg' style='width: 55%;' class='buffer-top-2x buffer-bottom-2x'>
</center>


Any action taken against your API that results in disruption of service to legitimate requests is a denial of service attack.

<h4>Defending API's from DDOS attacks is a challenge for two main reasons:</h4>

<p></p>

1. API clients tend to consume both more and more expensive requests than web users.
2. Some of the most common DDoS prevention mechanisms (like CAPTCHAs) don't make sense in an API context. 

Even worse, external (non-accidental) attacks are typically more serious as they're typically initiated by:

<ul>
    <li>Competitors who are trying to take you offline.</li>
    <li>Disgruntled former employees (with a deep knowledge of your systems).</li>
    <li>Cyber-criminals that are seeking to extort you to call off the attack.</li>
</ul>

No magic bullet will stop all attacks against your API, but the following approaches form a "recipe" that will blunt most attacks. 

Which one to apply depends upon how clients authenticate to your API, whether clients operate from within a browser a dedicated client app or a hybrid system and the overall number of clients that you need to support.

<p id='classic-ddos' class='toc-anchor'></p>
<h4 class='buffer-top-2x'>Classic Distributed Denial of Service (DDoS) Attacks</h4>


Classic Distributed Denial of Service (DDoS) Attacks rely upon low-level network tricks like <a href='https://www.usenix.org/system/files/conference/woot14/woot14-kuhrer.pdf'>TCP Amplification Attacks</a> that do things like send one packet but that require the server to send ten packets in response.

Network and cloud operations have become adept at identifying and stopping these types of attacks.

<p id='modern-ddos' class='toc-anchor'></p>
<h4 class='buffer-top-2x'>Modern Denial of Service Attacks (HTTP Flood)</h4>

Modern denial of service attacks employ full GET/POST HTTP Requests that mirror legitimate traffic (<a href='https://en.wikipedia.org/wiki/HTTP_Flood'>HTTP Floods</a>).

Web API's that work at the HTTP level are vulnerable to these attacks as each API request:
Takes a larger slice of server resources to parse and respond.
Holds a connection open for longer.
It is more difficult to determine if it is legitimate or something deliberately meant to be disruptive.

What's worse is that the complex interaction of API providers and API clients can easily result in an unintentional Denial of Service attack on the API. 

<span id='out-of-control-client' class='toc-anchor'></span>
<div class='case-study buffer-top-2x'>
<strong>CASE STUDY: The Out of Control API Client</strong>
<p/>
An Expedited WAF customer had developed an API used by hundreds of high volume e-commerce sellers, each interacting with the API via a customized client. 
<p/>
A bug in a single seller's client code caused a massive spike in API requests. The requests were legitimate calls, and the client was fully authenticated and authorized to make the requests.
<p/>


The wave of calls was massive enough to start knocking off other clients and slowing their entire system, despite their efforts to add more resources to deal with the issue. 
<p/>


<blockquote>We couldn't identify and kill connections fast enough within our app to keep up.</blockquote>


Blocking the client's API key was ineffective as:
<p/>
<ul>
 <li>Blocked HTTP requests still ate up app resources that they were knocking legitimate requests offline.</li> 
 <li>The API client retried failed requests and kept hammering the API.</li>
</ul>
 
<p/>
Unable to contact the client and with support requests pouring in, they blocked the client's IP address in Expedited WAF. This action immediately stopped the requests at the network level, and the properly configured clients were able to use the API again successfully.
</div>



<span id='data-breach-attacks' class='toc-anchor'></span>

<h2>2. Data Breach Attacks on APIs</h2>

<a href='#table-of-contents'>⇑ Back to Table of Contents</a>

<center>
<img src='https://expeditedsecurity.com/img/blog/undraw_code_review_l1q9.svg' style='width: 45%;' class='buffer-top buffer-bottom-2x'>
</center>


Data breach attacks seek to extract information from your API beyond what the user is authorized to access. These attacks range from subtle (manipulating search filters to return normally out of bounds records) to bots brute force trying "unguessable" URLs as they trawl for data. 

APIs are particularly vulnerable to data breach attacks as they are designed to be consumed programmatically. An attacker doesn't have to reverse engineer endpoints from your web app or try and work around authenticity tokens in web forms. 

Note: we're classifying these attacks as "Data Breach" attacks as from our perspective that's the worst-case scenario (that all the )


<span id='sql-injection' class='toc-anchor'></span>

### SQL Injection (SQLi) in APIs
SQL Injection is the most well-known data exploit. The classic version of the attack features an attacker changing an API URL from requesting a single ID value like:

<h4>Standard Request</h4>

Request: `https://api.example.com/?user_id=1234`

Generated SQL: `SELECT * FROM users WHERE id = 1234`


<h4>Injected Request</h4>

Request: `https://api.example.com/?user_id=1234%20OR%20*`

Generated SQL: `SELECT * FROM USERS WHERE id = 1234 OR *`

This enables them to pull the entire Users table from the application.

These types of attacks are now generally blocked as part of the security mechanisms of web frameworks.

However:

<ol>
    <li class='buffer-bottom'>Perfect implementations don't exist, web frameworks still rely on developers properly implementing protection mechanisms perfectly every time, even in situations where time and resources are tight.</li>
    <li>The one area developers are most likely to stray from standard out of the box framework querying features is in reporting. Many applications try to expose a "Report Builder" interface that lets users specify columns, filters, and joins to pull their data. 
    <p/>
    These queries back to your API deserve special attention for proper escaping as they're more complex and more vulnerable to manipulation.
    </li>
</ol>

<span id='json-sql-injection' class='toc-anchor'></span>
<div class='case-study buffer-top-2x'>
<strong>CASE STUDY: JSON SQL Injection</strong>

In the course of an investigation on behalf of a client, we found that a developer had built a custom "REST Query Interface" that they were incredibly proud of where the front end of the application would create a SQL Query from a visual drag and drop query builder. 

<blockquote>
{query: 'SELECT * FROM any_table WITH no_restrictions'}
</blockquote>

The resulting SQL statement was then bundled up into a JSON Object and POST'd back to the API for raw execution, completely bypassing their web framework's built-in SQL Injection Protection. 

</div>

<span id='non-sql-injection' class='toc-anchor'></span>

### Non-SQL Query Injection (NSQLi)

Datastores have gotten more diverse as application needs have changed, and modern web applications often use a mix of full-text search applications, No-SQL data stores, and caching servers to deliver data back to clients. 

<div style='padding-top: 40px; padding-bottom: 40px; width: 80%; text-align:center; margin: auto;'>

<style>.embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; } .embed-container iframe, .embed-container object, .embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }</style><div class='embed-container'><iframe src='https://www.youtube.com/embed/HdnDXsqiPYo' frameborder='0' allowfullscreen></iframe></div>


</div>

These datastores, each with their own custom query language, are just as vulnerable to injection attacks as traditional SQL servers ( Like <a href='https://www.postgresql.org/'>Postgres</a> and <a href='https://www.mysql.com/'>MySQL</a>).

But the web frameworks that most developers rely on to help protect them don't have equally mature built-in sanitization mechanisms.

This is not theoretical, practical examples of these attacks are currently in the wild. One solid example is of the SOLR search engine and the corresponding <a href='https://github.com/veracode-research/solr-injection'>Apache Solr Injection</a> project which demonstrates how trivial it is for improperly escaped SOLR query to return out of bounds data or to enable remote code execution (as seen in the screenshot below).

<center>
<p class='buffer-top-2x'/>
<strong>SOLR Query with Injected Script from the SOLR Injection Project</strong>
<a href='https://github.com/veracode-research/solr-injection'> 
<img src='https://expeditedsecurity.com/img/blog/solr-injection.png' style='width: 75%; text-align: center;' class='buffer-top-2x buffer-bottom-2x'></a>
</center>

<div class='case-study buffer-top-2x buffer-bottom-2x'>
<span id='social-search-problems' class='toc-anchor'></span>
<strong>CASE STUDY: Social Search Problems</strong>
<p/>
An Expedited WAF customer had an API that was used to provide search functionality via a Javascript client on an otherwise static site. 
<p/>
Each search result called the API once, and there was a relatively small amount of traffic using the search function as they were a boutique seller of multimedia resources. 
<p/>

<blockquote>Generating a search result page is the most execution heavy action on our site and it looked like half of China was querying simultaneously.</blockquote> 

One of their resources went virally popular on a Chinese social network, but what was passed around was the results URL of the search page. Each new preview, click, and share resulted in a new API call that quickly brought down the site. 
<p/>
Using Expedited WAF, the team temporarily blocked access to the site from China and was able to remain online to their business customers.
</div>


<span id='xss-attacks' class='toc-anchor'></span>

### XSS (Cross Site Scripting) & Code Injection API Attacks

Classically an XSS attack occurred when one user of a site was able to upload some code (typically Javascript) into a field that would later be displayed to other users on a site. 

Code Injection is a more encompassing term that better represents the scope of challenges an API faces in keeping scripts and executable content out of their system.

Most app frameworks have some limited ability to filter HTML and Javascript from form inputs; however, this gets complicated when payloads are wrapped in additional layers of abstraction and encoding. 

Counters to this will be covered in the <a href='#data-validation'>Data Validation</a> section.


<span id='functionality-attacks' class='toc-anchor'></span>

<h2>3. Functionality and Resource Attacks on APIs</h2>

<a href='#table-of-contents'>⇑ Back to Table of Contents</a>

<center>
<img src='https://expeditedsecurity.com/img/blog/undraw_Map_dark_k9pw.svg' style='width: 45%;' class='buffer-top-2x'>
</center>


APIs are built to expose the underlying functionality of a service. This underlying functionality can be in and of itself valuable to cybercriminals. These resource and functionality attacks may be launched via other vulnerabilities but often are simply unexpected uses of otherwise innocent features.

The following are general areas of functionality that are often abused and deserve special monitoring for abuse.

<span id='email-sending' class='toc-anchor'></span>

<h3>Email Sending</h3>

Spammers are constantly trying to break into legitimate APIs that have some communications mechanisms. Most often, it's email spammers selling shifty pharmaceuticals, but any type of unfettered email access is likely to be exploited. To the greatest extent possible, you should lock down email subjects and content to predefined messages that can't be customized. 

<span id='location-tracking' class='toc-anchor'></span>

<h3>Location Tracking</h3>

Location tracking (and especially real-time location tracking) can be exploited by stalkers and other criminals. This doesn't need to be a set of Lat/Long coordinates either, like:


<blockquote>IP Addresses and Image Uploads both leak location data unless properly filtered.
</blockquote>


<ul>
<li>IP Addresses which can be run through services like <a href='https://elements.heroku.com/addons/iptoearth'>IP to Earth</a> to determine (rough) location.</li>
<li>EXIF data in uploaded images which often contains location information unless deliberately stripped, see <a href='https://www.pic2map.com/'>Pic2Map</a></li>
</ul>


<span id='link-publishing class='toc-anchor'></span>

<h2>Link and File Publishing</h2>

Letting users modify content via an API is a dicey proposition (even if everything is functioning in a secure and error free way) as there may be unintended consequences.

<h3>Link Publishing</h3>
A page's rank in search engine results is, in large part, based upon how many other sites are linking to it. If there's any functionality within your API to add links (or freeform text with links in it), then it's quite likely that you'll be exploited by those seeking to inflate their search engine results. 

<h4>File Uploads</h4>
For reasons similar to link publishing, file uploading is often exploited by those seeking to distribute unethical content or just looking to boost their search rankings. For years a popular black hat SEO technique was to attempt to upload HTML files full with links pointing to their clients' sites to any location that failed to filter their uploads properly. Often even a "failed" upload via the API and validation mechanisms still left a file in a web-accessible location. 





<span id='phishing' class='toc-anchor'></span>
<h3>Phishing</h3> 

One on one messaging within apps has been abused for Phishing and other scams to the point where if you include this functionality within your API, you should build in some protection mechanisms for your users. 

To get a sense of how bad it's gotten, check out the <a href='https://github.com/DarkSecDevelopers/HiddenEye'>HiddenEye</a> tool from DarkSecDevelopers. 

<center>
<a href='https://github.com/DarkSecDevelopers/HiddenEye'>
<img src='https://expeditedsecurity.com/img/blog/phishing-bot.png' style='width: 65%;' class='buffer-top '></a>
<p><strong>HiddenEye will generate phishing assets for Facebook, Google, Twitch...</strong></p>
</center>


<span id='owasp' class='toc-anchor'></span>

<h2>4. OWASP Top 10 API Vulnerabilities</h2>
<a href='#table-of-contents'>⇑ Back to Table of Contents</a>


<center>
<img src='https://expeditedsecurity.com/img/blog/undraw_to_do_xvvc.svg' style='width: 45%;' class='buffer-top-2x buffer-bottom-2x' >
</center>


The <a href='https://owasp.org'>OWASP</a> (Open Web Application Security Project) Top 10 vulnerabilities are best thought of like a real-world survey of the attacks that are most often being seen by sysadmins and security professionals that are working to secure APIs day in and day out.

Released each year, they're a general guide to which threats you should be paying the most attention to countering in your API security processes.

<span id='why-care' class='toc-anchor'></span>

<h3>Why care about the OWASP Top 10</h3>

We've covered most of this list elsewhere in the guide, but the OWASP Top 10 often comes up in security discussions, and it can be helpful to see it organized in this form as it specifically applies to API security.

<blockquote>
The OWASP list is at its heart a method of prioritization. It's your peers saying that they have been affected by these very specific categories of attacks and that you should prepare your systems to defend against them as well.</blockquote>

There will always be more you could be doing to secure your systems, the OWASP list is at its heart a method of prioritization. It's your peers saying that they have been affected by these very specific categories of attacks and that you should prepare your systems to defend against them as well.

<span id='is-top-10' class='toc-anchor'></span>

<h3>Is the OWASP Top 10 for APIs?</h3>

No, but it is still mostly applicable, we've tweaked the items below to better represent the state of the industry with regards to API Security.

<span id='code-injection' class='toc-anchor'></span>

<h4>1) Code Injection</h4>
<strong>What is it</strong><br/>
Any attack where code is injected to extract data spread malware or otherwise disturb the normal functioning of your API.

<p>
<strong>What you should do</strong><br/>
Filter all inbound inputs. Limit the amount of customizability and separate free-form entry from structured data storage.

<span id='broken-authentication' class='toc-anchor'></span>

<h4>2) Broken Authentication</h4>

<strong>What is it</strong><br/>
Insufficiently strong authentication (either in the cryptographic sense of "weak" ciphers) or improperly chosen or applied authentication mechanisms. 
<p/>
<strong>What you should do</strong><br/>
Choose the Authentication pattern that best matches your API needs, layer additional authentication methods, and encryption on your base authentication scheme until it matches your level of paranoia.


<span id='sensitive-data-exposure' class='toc-anchor'></span>

<h4>3) Sensitive Data Exposure</h4>

<strong>What is it</strong><br/>
The unintentional revealing of data from the API that could be impactful in some way. Examples of sensitive data are things like physical location, gender, medical history, financial standing, etc.

<strong>What you should do</strong><br/>
Carefully audit all data returns to make sure that only the proper data fields are exposed. Avoid coding defaults that publish all data fields of an object by default. Deliberately choose which fields are displayed in all cases.

<p>
<div class='case-study buffer-top-2x buffer-bottom-2x'>
<span id='devise-auth-ip' class='toc-anchor'></span>
<strong>CASE STUDY: Devise Auth IP</strong><p/>
<a href='https://github.com/plataformatec/devise'>Devise</a> is an excellent user authentication system for <a href='https://rubyonrails.org/'>Rails applications</a> that among other things will log the IP address used by the client at login. A typical implementation would add that to the main User model in the application. 
<p/>
A naive API implementation of user profiles might then inadvertendly expose that IP address.
</div>
</p>

<span id='xxe' class='toc-anchor'></span>

<h4>4) XML External Entities (XXE)</h4>

<strong>What is it</strong><br/>
An attack against XML parsing engines used to process XML based requests. 

<strong>What you should do</strong><br/>
Keep your XML parsing libraries up to date. 


<span id='broken-access-controls' class='toc-anchor'></span>

<h4>5) Broken Access Controls</h4>

<strong>What is it</strong><br/>
Insufficient bounds being placed on users with legitimate access. Examples would be ordinary users that can access administrative functions in an API.

<strong>What you should do</strong><br/>
Audit your system for out of bounds access.


<span id='security-misconfiguration' class='toc-anchor'></span>

<h4>6) Security Misconfiguration</h4>
<strong>What is it</strong><br/>
Today's systems are complex enough that it's not always clear what the end result may be and who can access it. 
<br/>
A common example is that many APIs using Amazon Web Services will output data processing jobs to S3, but the S3 bucket itself will be improperly permissioned.

<blockquote>If the API is secure, but the output is written to S3 where due to a misconfiguration, the data is not secure, then the API is actually not secure.
</blockquote>

<strong>What you should do</strong><br/>
Test and document both internal and external systems for for misconfigurations. 


<span id='xss' class='toc-anchor'></span>

<h4>7) Cross Site Scripting (XSS)</h4>
<strong>What is it</strong><br/>
Manipulation of your API to further spread a malicious script.

<strong>What you should do</strong><br/>
Strongly filter all inputs for both corectness and script components.


<center>
<a href='https://owasp.org'>
<img src='https://expeditedsecurity.com/img/blog/owasp-logo.png' style='width: 45%;' class='buffer-top-2x buffer-bottom-2x '></a>
</center>



<span id='insecure-deserialization' class='toc-anchor'></span>

<h4>8) Insecure Deserialization</h4>
<strong>What is it</strong><br/>
Any time that a data object is transformed from one notation system to another, there is a danger that the system doing the deserialization could itself have vulnerabilities that could be exploited. 



<strong>What you should do</strong><br/>
Keep your serialization and deserialization libraries up to date, filter inputs for malicious inputs.


<span id='vulnerable-components' class='toc-anchor'></span>


<h4>9) Vulnerable Components</h4>
<strong>What is it</strong><br/>
Modern web development is, in many ways, a system of plugging many different libraries and services together to create the desired result. While this is great for programmer productivity, it implicitly requires that all components are tested and updated in perpetuity.

<strong>What you should do</strong><br/>
Use one of the many services that automatically tracks the packages included in your framework and alerts you to insecure versions.

<span id='insufficient-logging' class='toc-anchor'></span>

<h4>10) Insufficient Logging and Monitoring</h4>

<strong>What is it</strong><br/>
Most security breaches are only discovered far after they've actually occurred. Determining what data was affected, what systems were touched, and what steps need to be done to remediate the situation all hinge on having good logs of access, security alerts, and user activity.

<strong>What you should do</strong><br/>
Define a data retention policy that includes how far back logs will be kept. Instrument your API access actions to record key metrics and events. Keep logs indexable and searchable.


<span id='api-security-controls' class='toc-anchor'></span>
<h2>5. API Security Controls</h2>
<a href='#table-of-contents'>⇑ Back to Table of Contents</a>

<center>
<img src='https://expeditedsecurity.com/img/blog/undraw_version_control_9bpv.svg' style='width: 45%;' class='buffer-top buffer-bottom'>
</center>

With respect to security functions, "controls" are any feature or process that lets you respond to a threat. Data privacy and security regulations will often invoke language like "...application should have controls in place to counter threats such as X, Y, and Z" where XYZ are cybercriminals setting your API on fire.

The following security controls can help to blunt Denial of Service, Data Breach, and other attacks aimed at your API. No control is 100% effective 100% of the time in countering a threat. For this reason, it's important to layer them along with your application-level security precautions to achieve security in depth. 

The following are general techniques and approaches (controls) to improving the overall security of your API.

Most tend to cross multiple different areas of threats. For example: blocking attack probes tends to stop both DDoS attack probes and framework vulnerability probes.

<span id='stop-anonymous-proxy-networks' class='toc-anchor'></span>

### Stop Anonymous Proxy Networks

An attack originating from a handful of IP addresses is trivially easy to stop with a solution like Expedited WAF. Attackers counter to this is to obscure the true origination point of attacks behind what are known as "anonymous proxies."

While there do exist legitimate anonymous proxy providers, most often "anonymous proxy" is a polite fiction for "a PC in someone's house that's been infected with malware and is being remotely manipulated." 

These networks are difficult to counter on your own, and it's only by continuously combing shared security vendor lists of identified anonymous proxies and our data from over 20,000 sites, that we're able to block anonymous proxies at the network level. 

Blocking anonymous proxies has the dual benefit of blocking both DDoS requests but also the source of many web attacks against applications.


## Designate Allowed IP Ranges
Depending upon the usage model of your API, you can further extend the security of your endpoints by only allowing designated IPs and IP ranges. Many popular API's use explicitly allowed IPs in conjunction with Tokens or API keys as a means of layering an additional level of security on their endpoints. A good example is the [Namecheap Domains API](https://www.namecheap.com/support/api/intro/), which is used by thousands of organizations. 

In a cloud environment like Heroku (where hosts are referenced by name and not IP), clients will need to use an add-on like one of the following to consistently make all API calls from the same IP address. There are a number of services designed to help with situation such as:

<ul>
<li><a href='https://elements.heroku.com/addons/quotaguardshield'>Fixie</a></li>
<li><a href='https://elements.heroku.com/addons/quotaguardstatic'>QuotaGuard Static</a></li>
<li><a href='https://elements.heroku.com/addons/proximo'>Proximo</a></li>
</ul>


<span id='bots-in-production' class='toc-anchor'></span>

<div class='case-study buffer-top-2x buffer-bottom-2x'>
<strong>CASE STUDY: Testing Bots In Production</strong>
<p/>
A newly launched API service had set up Expedited WAF in anticipation of meeting regulatory security requirements but was surprised when they were being hit multiple times a day by a bot that was methodically working through their entire set of API endpoints.
<p/>
They restricted allowed IP requests to a small set of known values. Immediately they got a call from one of their remote developers who had incorrectly configured their test suite to run against the production server instead of the unprotected WAF staging instance. 
</div>

<span id='geo-filtering' class='toc-anchor'></span>
### Geo Filtering 
While theoretically, attacks against your API can originate from anyplace on the Internet, practically, they often originate from a single country. The reasons for this vary, but it is a mix of certain countries having laxer cybersecurity regulations, other countries with a computer population more likely to be taken over for botnets or simply that attackers find it easier to break into computers in their native language. 

With that in mind, the ability to quickly block a country from GET/POST requests against your API may be the fastest way to stop an attack in progress. 

<div class='case-study buffer-top-2x buffer-bottom-2x'>
<span id='south-african-nsfw' class='toc-anchor'></span>
<strong>CASE STUDY: South African NSFW Links</strong>
<p/>
An Expedited WAF customer with a landing page creation service for mobile users had an API that was being exploited by unscrupulous people in South Africa to "launder links" to various NSFW sites. They'd sign up for new trial accounts, get fresh API keys, and then script the automatic creation of thousands of landing pages with their links embedded. 
<p/>
As a result, the landing page service itself was classified by Google as an <a href='https://support.google.com/chrome/answer/99020?co=GENIE.Platform%3DDesktop&hl=en'>"Unsafe Site"</a> and Chrome users started seeing red error messages the one shown below when browsing to the site.


<center>
<a href='https://support.google.com/chrome/answer/99020?co=GENIE.Platform%3DDesktop&hl=en'>
<img src='https://expeditedsecurity.com/img/blog/unsafe-site.png' style='width: 45%;' class='buffer-top '></a>
<p><strong>Failing to Kill NSFW Links may lead<br/> Google to block your site.</strong></p>
</center>

<p/>
As part of the cleanup effort, the customer was able to block the offending customers and regain their safe site status.

</div>



<span id='filter-bots' class='toc-anchor'></span>
### Filter Bots from your Single Page Applications and Browser APIs

Single Page Applications often are architected with a front end framework like React, Angular, or Vue calling a backend API on each interaction. 

By necessity, the API endpoints are readily discoverable by anyone with the technical ability to right-click on the page and open developer tools.

Attackers will take these endpoints and attempt to brute force or otherwise manipulate them with command-line tools.  

Expedited WAF can fingerprint bot activity and block these attempted endpoint enumerations.


<span id='auto-scaling-bots' class='toc-anchor'></span>
<div class='case-study buffer-top-2x buffer-bottom-2x'>
<strong>CASE STUDY: Auto Scaling for SEO Bots </strong></br/>
An Expedited WAF protected application was using an auto-scaling plugin to add additional resources when the performance of the application slowed under load. 
<p/>
Once a week like clockwork, they'd have a spike in load, and for several hours their system would burn 10x their standard workload.
<p/>

<blockquote>We didn't realize there were that many dumb bots out there.</blockquote>

However, this traffic spike wasn't accompanied by any of the user signup and sales metrics that they should have been seeing. They tracked it down to a bot from a known SEO spider that was aggressively indexing their site, disregarding their robots.txt directives and costing them thousands in server fees.
<p/>
Expedited WAF's bot blocking features were enabled, and they discovered that beyond the one bot, many were ripping through their site and that what they considered their "base" load was actually substantially less than they had thought.   
</div>

<!-- API SECURITY MECHANISMS -->
<span id='api-security-in-transit' class='toc-anchor'></span>
<h2>6. API Security in Transit (SSL/TLS/HTTPS)</h2>
<a href='#table-of-contents'>⇑ Back to Table of Contents</a>


<center>
<img src='https://expeditedsecurity.com/img/blog/undraw_hacker_mind_6y85.svg' style='width: 40%;' class='buffer-top buffer-bottom-2x'>
</center>

SSL/TLS (HTTPS) is by far the most commonly used in transit encryption mechanism used for web-based HTTP APIs. 

Secure API connections are not without challenges as much of the security provided by HTTPS connections is a result of web browser innovations like HSTS, protocol negotiations, and others that help enforce higher levels of security.

In contrast, most HTTP request/response libraries used by API clients either do not have or do not enable similar features by default. 

Expedited WAF's HTTPS enforcement features help reduce the chances of an insecure connection by eliminating insecure options and blocking circumvention attempts.


<span id='https-enforcement' class='toc-anchor'></span>
#### HTTPS Enforcement for APIs
All requests from clients to your API should be encrypted (HTTPS). Unfortunately many client HTTP libraries do not enable HTTPS/secure connections by default so it's necessary to enforce that from the server. 

<blockquote>Unfortunately many client HTTP libraries do not enable secure connections by default.
</blockquote>


Expedited WAF implements HTTPS enforcement as a standard (status code 301) redirect. Clients who attempt to connect via HTTP are seamlessly redirected to secure HTTPS connections with no opportunity to force an unsecured connection.


<span id='tls-version-enforcement' class='toc-anchor'></span>
#### TLS Version Enforcement
Most http client libraries are capable of interacting with multiple different version of the SSL/TLS specification. A modern web browser like Chrome will preferentially choose the most secure/latest version of TLS (1.3). 

Many HTTP libraries have a different handshake model that instead works UP from the oldest version of TLS to the latest (and the underlying SSL/TLS library in use and hasn't been updated). 

The result can be connections that are much more easily spoofed or broken. Expedited WAF enforces v1.2 and higher connections by default preventing downgrade attacks and poorly configured clients from connecting insecurely.



<!-- API Authentication -->
<span id='api-authentication-methods' class='toc-anchor'></span>
<h2>7. API Authentication Methods</h2>
<a href='#table-of-contents'>⇑ Back to Table of Contents</a>


<center>
<img src='https://expeditedsecurity.com/img/blog/undraw_sign_in_e6hj.svg' style='width: 40%;' class='buffer-top buffer-bottom-2x'>
</center>

There's no one "best" method of securing an API you need to choose a method based on:

<ol>
    <li>How many clients you need to support</li>
    <li>Sensitivity of data</li>
    <li>Process concerns (revokability, expirations)</li>
    <li>Platforms to support</li>
    <li>Developer experience</li>
    <li>User Enablement</li>
    <li>Regulatory Guidelines</li>
    <li>Logging/Tracking Needs</li>
</ol>



<span id='api-keys' class='toc-anchor'></span>
### API Key Authentication
Static API keys are one of the classic ways that API's have been accessed since web applications have been brought online.

##### When to Use API Keys
API Keys support a very simple interaction model that works well for API clients that:

- Directly access resources
- Work on platforms that can keep the key secret (server applications)

##### Allow for multiple keys
Users of your API should be able to generate multiple keys. This allows for more fine-grained access and tracking across development, testing, staging, and production environments. 

Depending on your system it may even make sense to allow for individual API creation for each developer or application consuming the API

##### API Key Storage
Client code examples should be generated with the assumption that the API keys are being loaded from either a secure key store. a non-source controlled environmental secrets file or in the case of Heroku their <a href='https://devcenter.heroku.com/articles/config-vars'>environmental variable configuration feature</a>.

##### Allow self-revocation
Users should have the ability to revoke the current authorization of an API key. Revocation is needed in situations where the key may have been exposed to unauthorized users. 

Example: A key is accidentally checked into and then deployed to a public Github repository.

##### Allow Expiration
In some cases allowing for expiring API Keys is advantageous to limit the total number of keys in use. 

##### API's Using API Keys for Authentication
The following prominent services use API Keys for aspects of their Authentication schemes.

<ul>
<li><a href='https://cloud.ibm.com/docs/iam?topic=iam-manapikey'>IBM Cloud API</a></li>
<li><a href='https://api.data.gov/docs/api-key/'>Data.gov API</a></li>
<li><a href='https://cloud.google.com/docs/authentication/api-keys'>Google Cloud API</a></li>
</ul>


<span id='basic-auth' class='toc-anchor'></span>
### Basic Auth API Authentication
Basic Auth passes unencrypted security credentials (username and password) in a standard HTTP header to the API endpoint. This allows it to function with almost every HTTP library and API consumption application that exists. 

Visit the Mozilla Developer site for a more in-depth explanation of <a href='https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication'>Web/Basic Authentication</a>.

<center>
<a href='https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication'>
<img src='https://expeditedsecurity.com/img/blog/HTTPAuth.png' style='width: 80%;' class='buffer-top buffer-bottom-2x'>
</a>
</center>

##### When to use Basic Auth
In modern web applications, Basic Auth is most often mixed with another API authentication scheme to create a more secure system. It's unlikely that standard basic auth is something that you should depend upon for your standard API Authentication.

##### Must use with HTTPS
Basic Auth encodes credentials in Base64, but as they are not encrypted in transit unless you are also enforcing HTTPS connections, the credentials can be read by any intermediate in their path to the server. If you're using Heroku, Expedited WAF will enforce <a href='/heroku/how-to-force-https-on-heroku/'>Full HTTPS compliance</a>.

##### Examples of Basic Auth combined with other schemes
There are a number of notable API's that use Basic Auth together with API Keys or generated Tokens to add more security depth to their application.


<ul>
<li><a href='https://devcenter.heroku.com/articles/legacy-building-an-add-on#step-2-integrate-with-the-add-on-partner-api'>Heroku Add-ons API</a></li>
<li><a href='https://developer.atlassian.com/cloud/jira/platform/jira-rest-api-basic-authentication/'>Jira Cloud</a></li>
</ul>


<span id='client-certificates' class='toc-anchor'></span>
### Client Certificate API Authentication (X.509)
Many developers are familiar with TLS/HTTPS certificates that are used to secure server communications. Lesser known is that you can issue Public Key Infrastructure (PKI) certificates to a client that are used for authentication.

##### When to use Client Certificates
Client certificate authentication adds another layer of cryptographic complexity to the authentication and transmission of API communications. The overhead of managing, distributing, and supporting client certificates is non-trivial.

This scheme is best used when you have a limited number of very high-value API clients and sufficient support resources to assist them.

##### How it Works
Client and Server certificates are generated with the same set of keys and need to remain in sync. When a connection is first made, both the client and server certificates are validated to each other, and if either has expired, been invalidated, or have mismatched information, the underlying API connection (HTTP request) won't be established. 


<center>
<a href='https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication'>
<img src='https://expeditedsecurity.com/img/blog/openssl-x509.jpg' style='width: 80%; border: 4px solid #ccc;' class='buffer-top'>
</a><br/>
<p class='buffer-bottom-2x'><strong>OpenSSL Generation of a x.509 certificate</strong>
</p>
</center>

Typically, at the same time, another identity service (such as Active Directory) authenticates the veracity of the client certificate. 

##### Examples of APIs with Client Certificate Authentication

<ul>
<li><a href='https://docs.microsoft.com/en-us/azure/api-management/api-management-howto-mutual-certificates'>Microsoft Azure API Management (support for)</a></li>
<li><a href='https://help.salesforce.com/articleView?id=security_keys_about.htm&type=5'>Salesforce API</a>
</ul>


<span id='oauth' class='toc-anchor'></span>
### OAuth 2.0 for APIs

Open Auth (OAuth) combines elements of both authentication (who is connecting to your API) with authorization (what the person connecting is allowed to access). OAuth 2 was designed to be an industry-standard protocol for authentication/authorization and shines when used for APIs with third-party access.

##### OAuth 1 vs OAuth 2
Despite the simple version bump, OAuth 2 is a much-simplified version of the protocol that's fairly easy to implement (even without a client library).

##### HTTPS Required
One of the main reasons that OAuth 2 is substantially simpler than OAuth 1 is that OAuth 2 relies upon TLS/SSL to secure transmissions. HTTPS connections are required for a secure OAuth 2 implementation.

##### When to use OAuth
OAuth is ideal for API ecosystems where there is a primary service that developers are building applications for end users. 

##### OAuth vs API Key
Consider a library that exposes an API for their patrons that a single endpoint that returns all books that a patron has ever checked out, and that lets you check out a book.

`GET  https://library-example.com/books`

<strong>API KEY:</strong> An API Key authentication model would allow a developer to create an application to consume the API and analyze the books for their own use.

<strong>OAUTH 2:</strong> An OAuth 2 authentication model would allow a developer to create an application "CheckApp" that other patrons of the library could then use, the Authentication/Authorization could include scoped limits on access, could be easily revoked from the main service and would limit reuse and distribution of usernames/passwords.

##### OpenID Connect (OIDC)

OpenID Connect is a further simplified subset of Open Auth 2.0 focused on requesting and returning a small set of user-specific information that could be used across services. While this may sound great for your needs:

<ol>
<li>As soon as you step outside the scopes of profile, email, address, and phone you're back to a full OAuth 2 implementation.</li>
<li>One of the promises of OIDC was that it would allow for easier implementations between services, but immediately each service using it opted to fragment their implementation.</li>
</ol>


##### APIs that use OAuth 2 for Authentication

<ul>
<li><a href='https://developer.github.com/apps/building-oauth-apps/'>Github</a></li>
<li><a href='https://developers.facebook.com/docs/facebook-login/access-tokens/'>Facebook</a></li>
<li><a href='https://dev.fitbit.com/build/reference/web-api/oauth2/'>Fitbit</a></li>
</ul>



<!-- API Authorization -->
<span id='api-authorization' class='toc-anchor'></span>
<h2>8. API Authorization Approaches</h2>
<a href='#table-of-contents'>⇑ Back to Table of Contents</a>

<center>
<img src='https://expeditedsecurity.com/img/blog/undraw_quiz_nlyh.svg' style='width: 40%;' class='buffer-top-2x buffer-bottom-2x'>
</center>


Authorization (if a user connected to your API has permissions to perform an action) is an important and often neglected aspect of API creation. 

By establishing a consistent model for handling authorizations within your API, you are much less likely to over permission users, allow unsafe defaults to persist, and as a result, you'll deliver an overall safer application.

<span id='rbac' class='toc-anchor'></span>
### Role Based Access Control (RBAC)

Roles are collections of authorizations granted to users of your API. As an example, consider Wordpress User Roles. By default, Wordpress creates multiple different roles such as Administrator (complete control of articles and users, Editor (control of all articles, but not users), Author (control of articles they've written but not others), etc.


<div style='padding-top: 40px; padding-bottom: 40px; width: 80%; text-align:center; margin: auto;'>

<style>.embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; } .embed-container iframe, .embed-container object, .embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }</style><div class='embed-container'><iframe src='https://www.youtube.com/embed/rGObWtjxGBc' frameborder='0' allowfullscreen></iframe></div>

</div>

Wordpress's <a href='https://developer.wordpress.org/rest-api'>REST API</a> extends these roles to calls against the API endpoints, so, for example, an Editor is not able to modify a User via the API. 

By shipping multiple roles with associated access out of the box with Wordpress, the developers are materially improving the security of the platform. 


<span id='abac' class='toc-anchor'></span>
### Attribute Based Access Control (ABAC)

Sometimes called <a href='https://csrc.nist.gov/glossary/term/policy-based-access-control'>Policy Based Access Control (PBAC)</a> the overall concept of PBAC/ABAC is that rigid access control based on roles is too inflexible and inefficient.

In this authorization model, what is important is both the context of the request and the context of what is being requested. 

##### ABAC Policy Example

Consider an API whose endpoints return a mix of public information (overall system status) and private information (a user's profile information), in an ABAC system two different policies could be applied to those endpoints. 

A policy for the public status endpoint might be:

`IF $client_api_key IS valid`<br/>`THEN return resource`

And the private user profile endpoint might be:

`IF $client_api_key IS valid`<br/>
`AND $client_api_ip_address IS WITHIN $allowed_ip_range`<br/>
`THEN return resource`

##### Attributes for Policies
Some common contextual attributes used in creating policies are:

<ul>
<li>Client IP</li>
<li>Time of Day</li>
<li>Client Request Country (Geo IP)</li>
<li>Rate Limiting</li>
<li>User Agent</li>
<li>VPN/Anonymous Proxy Detected</li>
<li>Sensitive of Request</li>

</ul>

Depending upon the resource being requested and the mix of the above factors, additional access requirements might be applied, or access denied.


<span id='secure-api-design' class='toc-anchor'></span>
<h2>9. Secure API Design</h2>
<a href='#table-of-contents'>⇑ Back to Table of Contents</a>


<center>
<img src='https://expeditedsecurity.com/img/blog/undraw_prioritise_tgdh.svg' style='width: 30%;' class='buffer-top buffer-bottom-2x'>
</center>

APIs are by their nature extremely diverse allowing developers to do everything from <a href='https://openweathermap.org/api'>finding out if it will be sunny tomorrow</a> to <a href='https://dougseven.com/2014/04/17/knightmare-a-devops-cautionary-tale/'>destroying 440 million dollars in less than 45 minutes</a>.

Despite this very disparate nature of APIs, there are still some fundamental approaches that can help you create a secure and solid API. 

<span id='simple-beats-complicated' class='toc-anchor'></span>
### Simple Means Secure

Static definitions of "simple" and "complicated" never really capture the feelings involved.

Simple is "what I would have done," and complicated is "what anybody else does," and on that scale, implementing APIs are always complicated because it's your thoughts and design that another person is consuming. 

<center>
<a href='https://twitter.com/dhh/status/1201870975228186624'>
<img src='https://expeditedsecurity.com/img/blog/galls-law.png' style='width: 60%; border: 4px solid #ccc;' class='buffer-top'>
</a><br/>
<p class='buffer-bottom-2x'><strong>Even DHH Agrees</strong>
</p>
</center>

Choose the simplest methods of authentication, authorization, and security controls you can for your API. Leverage industry-standard technologies and don't implement your own cryptographic solutions. Additionally, prize security over flexibility in your API. Set secure defaults and raise warnings when they are changed.


<span id='least-privilege' class='toc-anchor'></span>
### Allow Least Privilege
<a href='https://www.us-cert.gov/bsi/articles/knowledge/principles/least-privilege'>Least Privilege</a> as applied to APIs means allowing only enough access to API clients for them to perform what tasks they need to. Additional functionality should only be granted if necessary. 

Accomplish this with <a href='#rbac'>Role Based Access</a>, separate read/write API Keys, OAuth Scopes, and granular permissions systems. 


<span id='least-data-access' class='toc-anchor'></span>
### Be Conservative in Exposing Data
Similar to Least Privilege, you should strive to only include the absolute least amount of data necessary from any API call. 

This minimizes the chances that you'll accidentally expose a sensitive field but also prevents inadvertent disclosures of personal information that can be inferred by correlating data from different datasets (a particular vulnerability of APIs).

<center>
<a href='https://techcrunch.com/2019/12/24/twitter-android-bug-phone-numbers/'>
<img src='https://expeditedsecurity.com/img/blog/twitter-hack.png' style='width: 60%; border: 4px solid #ccc;' class='buffer-top'>
</a><br/>
<p class='buffer-bottom-2x'><strong>17 Million People had their data compromised.</strong>
</p>
</center>

In December of 2019, <a href='https://techcrunch.com/2019/12/24/twitter-android-bug-phone-numbers/'>a design flaw</a> in the Twitter Android API was disclosed. Twitter's API was allowing massive generated lists of phone numbers to be uploaded to their account matching API. Via this method, over 17 million phone numbers were matched to Twitter accounts.  


<span id='least-data-collected' class='toc-anchor'></span>
### Least Data Collected
Pre Data regulations like GDPR or CCPA and the rash of data breaches destroying people's privacy, there was a collective thought that having data in your system was a good thing.

Currently, holding user data is more like having a toxic asset.

<blockquote>Holding user data is like having a toxic asset.</blockquote>

Minimize the amount of data you collect. Document why you are collecting each piece of user data. Document their consent in giving you their data.

<span id='versioning' class='toc-anchor'></span>
### Versioning 
It's inevitable that at some point, you'll need to make potentially breaking changes to your API; at that moment, you'll thank yourself for having the foresight to build request versioning into your API from the start. 

Beyond being a nice to have feature for both clients and developers, versioning gives you a clear process to deprecate older API methods that may be less secure than you intended, lets you cleanly swap to stronger authentication methods, and communicates these changes in working code to your API clients.

<a href='https://stripe.com/blog/api-versioning'>Stripe's Engineering blog</a> lists some of the steps they've gone through and benefits from implementing versioning as a first-class feature of their APIs.


<span id='logging' class='toc-anchor'></span>
### Logging Requests and Responses
Earlier in this article, we discussed "logging" in the abstract and how many apps don't retain or structure logs sufficiently to investigate a data breach. Now we're focusing specifically on the request/response cycle and a feature you can add to each response to greatly simplify log searching and debugging: adding a request_id. 

Typically this is a cryptographic hash of the inputs (request parameters, JSON blog, SOAP Envelope, etc.) that is returned in the response and also logged. 

Later if a data dump of the API is discovered, a security researcher reports a problem, or even if you're just debugging, you have a very easy log parameter to search.


<span id='security-procedures' class='toc-anchor'></span>
<h2>10. Security Procedures</h2>
<a href='#table-of-contents'>⇑ Back to Table of Contents</a>


<center>
<img src='https://expeditedsecurity.com/img/blog/undraw_functions_egi3.svg' style='width: 45%;' class='buffer-top buffer-bottom-2x'>
</center>


<span id='handling-security-reports' class='toc-anchor'></span>
### Handling Security Reports
Even with the best of intentions, latest frameworks and stacks of security controls your API may still have security issues. 

As that's the case, it's very important that you have publicly established a contact point where security issues can be reported. Not having a public contact method risks losing security reports in support or customer service queues. The easiest thing is to add a `security@example.com` alias to your Contact Us page (here's <a href='/contact/'>ours</a>)

<blockquote>You can't walk into a Starbucks, order a Venti Pike Place and casually report that you have gained access into all of their internal administrative applications.</blockquote>


Alternatively you can add a 'security.txt' file to the root of your website, more information here: <a href='https://securitytxt.org/'>securitytxt.org</a>.


As an example of how this plays out in practice: <a href='https://portal.starbucks.com'>Starbucks</a> API developers accidentally left an <a href='https://www.bleepingcomputer.com/news/security/starbucks-devs-leave-api-key-in-github-public-repo/'>API key in a public Github repo</a> which granted access to internal Starbucks authentication and SSO services. An independent vulnerability hunter found the key, reported it to Starbucks via their <a href='https://hackerone.com/starbucks'>disclosure service</a> and the developers were able to resolve the issue before systems were affected.  


<center>
<img src='https://expeditedsecurity.com/img/blog/combination-logo.jpg' style='width: 85%;' class='buffer-top buffer-bottom-2x'>
</center>



Now, consider the case of not having those communications systems in place. You can't reasonably expect to walk into your corner Starbucks ask for a Venti Pike Place and casually mention that if you were so inclined that you could impersonate <a href='https://en.wikipedia.org/wiki/Howard_Schultz'>Howard Schultz</a> on their corporate network if you felt like it. 

Clear lines of security communication:

<ul>
<li>Reduce the time that vulnerabilities are open</li>
<li>Encourage threat researchers from going public with vulnerabilities before they're patched</li>
<li>Improve your overall security posture</li>
</ul>

Security communication processes are one of the most effective, speedy and cost effective ways to improve your API security.

<span id='data-breach-handling' class='toc-anchor'></span>
### Data Breach Handling

Depending on your jurisdiction if a data breach has occurred, you'll need to report it to various authorities such as your <a href='https://www.varonis.com/blog/data-breach-definition-by-state/'>US State's Attorney General</a> or a EU Country's <a href='https://ec.europa.eu/info/law/law-topic/data-protection/reform/what-are-data-protection-authorities-dpas_en'>Data Protection Authority</a>.



<center>
<img src='https://expeditedsecurity.com/img/blog/undraw_gdpr_3xfb.svg' style='width: 25%;' class='buffer-top-2x buffer-bottom-2x'>
</center>

In most cases, there are escalating levels of reporting requirements as the level of sensitivity rises (ex: medical data is treated more seriously in a data breach than local weather reports) and the overall number of people affected.