Hardening E-Commerce APIs to Secure Billion-Dollar Retailers
Tags: apisecurity, ecommerce, b2b, onlineshopping
Published: 2021-09-12 20:02:36

![Alt text of image] (https://resources.fabric.inc/hubfs/retail-ecommerce-api-security.png)

At fabric we are building a [modular](https://resources.fabric.inc/blog/glossary/modular-commerce) and [headless](https://resources.fabric.inc/blog/headless-commerce) commerce platform with the vision of becoming for commerce services what AWS is for web services, and due to the headless nature of our platform, APIs run the show. So much so that a multi-billion dollar equipment supplier just signed a multi-million dollar contract with us to consume our commerce APIs.

API security was a concern before I joined fabric in January 2021, but as we attract more billion-dollar retailers such as Restoration Hardware, GNC, and Staples—including more general market interest with our recent $100M Series B—API security is now a high priority.

In this post, I will show you our general approach for securing APIs to support these major retailers. But first, let’s briefly review why API security matters.

# Why is API Security Important in Retail?

At fabric we use APIs to connect internal platform services such as cart, checkout, and pricing, and external services such as Stripe (payments), Alavara (tax), and Algolia (search). These connections mean data transfer and compromised APIs are behind [major data breaches](https://www.cloudvector.com/api-data-breaches-in-2020/). With mainstream retail, personally identifiable and payment information is widely required to conduct trade and, as e-commerce grows, the attack surface grows with it.

The [API Security project by OWASP](https://owasp.org/www-project-api-security/) supports this:

“A foundational element of innovation in today’s app-driven world is the API. From banks, retail and transportation to IoT, autonomous vehicles and smart cities, APIs can be found in customer-facing, partner-facing and internal applications. By nature, APIs expose application logic and sensitive data such as Personally Identifiable Information (PII) and because of this have increasingly become a target for attackers. Without secure APIs, rapid innovation would be impossible.”

Below I will describe the tactics we use to secure our APIs. With these tactics, the [top ten API security threats](https://owasp.org/www-project-api-security/) top ten API security threats identified by OWASP are directed. __Note__: The OWASP API security project is nascent and needs contributors!

#How We Secure APIs

These are the different [RESTful APIs we use](https://api.fabric.inc/):

__Customer storefront APIs:__ Our Storefront API protects e-commerce store frontends (including mobile apps). It is a modular, customizable, and secure API gateway that provides a single data source for all your UIs.

__Third-party APIs:__ These are APIs provided by fabric to third-party vendors like Zendesk or RevCascade to access order and inventory modules. For example, mapping APIs are used to update inventory with new products from merchants.

__Merchant application APIs:__ This is a customer API that is unique for each customer.

All of our API traffic is securely transmitted using HTTPS and modern TLS protocols. In addition to applying security best practices for data in transit via APIs, we take these security measurements:

#Internal Vulnerability Testing

Using vulnerability assessment tools (e.g. [QRadar](https://www.ibm.com/security/security-intelligence/qradar), [Qualys](qualys.com), and [Nessus](https://www.tenable.com/products/nessus)) and considering new security enhancements throughout the software development life cycle (SDLC) is essential for keeping APIs secure. At fabric, our continuous vulnerability management process makes security a key aspect of API development.

Security tooling is part of our CI/CD pipeline and automated security-focused unit tests run against every deployment. Additionally, we run monthly vulnerability scans to identify and categorize API security issues for all of our customers. If we identify any critical security vulnerabilities, they’re patched within a week.

#Third-Party Pen Tests

Third-party penetration tests reputable firms (e.g. [SpiderLabs](https://www.trustwave.com/en-us/company/about-us/spiderlabs/), [Cobalt](https://cobalt.io/), and [FireEye](https://www.fireeye.com/)) provide an objective expert analysis of a system’s security. A third-party pen test can expose vulnerabilities an internal audit and automated testing alone can miss.

In addition to leveraging automated scanning tools, third-party pen testers provide the human expertise necessary to identify misconfigurations and exploits that could otherwise go unnoticed. 

For example, automated tooling is great at identifying common vulnerabilities and exposures (CVEs), but an attack that exploits business logic in an e-commerce site’s frontend APIs may go undetected. Simply put: third-party testing manipulates and attempts to hack an API in the same ways a real attacker would. This human perspective makes it possible to detect and correct misconfigurations and logic flaws before an attacker exploits them.

At fabric, we conduct third-party pen tests for our entire API annually and before every major release.

#Authentication and Authorization

Validating users are who they say they are (authentication) and only allowing access to resources they have permissions for (authorization) is fundamental to API security. fabric authenticates users and then provides them with a [JSON Web Token (JWT)](https://jwt.io/introduction).  Authorization of those requests is secured using Okta’s implementation of the industry-standard [OAuth 2.0 protocol](https://developer.okta.com/docs/reference/api/oidc/).

End-to-end, the process works like this:

A client sends credentials (username and password) to the fabric login API.

The credentials are verified by comparing what the client provides to a salted hash. (Credentials are encrypted using a robust hashing algorithm.) 

If the credentials are valid, the API signs and returns a JWT. If the credentials are invalid, the login request is rejected. Rate-limiting on the login API prevents brute force attacks.
 
The client verifies the token and sends the signed token with every subsequent request. This eliminates the need for the client to include credentials for any other requests in the session.

OAuth 2.0 authorization ensures that the client can only access API endpoints they have adequate permissions for. 

#API Throttling

DDoS attacks can prevent an e-commerce site from processing legitimate orders and are one of the [biggest threats facing large retailers](https://www.infosecurity-magazine.com/news/ddos-online-four/). API throttling allows us to limit the number of API requests per second and prevent DDoS from bringing a site down. 

Of course, with any throttling, there is the risk of false positives. To mitigate this risk, we use automation to optimize the throttling of APIs and reduce false positives while still providing protection from malicious attacks. Additionally, limits are based on both steady-state rates and bursts to account for different behaviour patterns. 

To implement API throttling, we use [AWS API Gateway](https://aws.amazon.com/api-gateway/). At a high level, the throttling process works like this:

Clients call an API.

If the number of requests is less than the throttling limit, the request is processed normally.

If the number of requests exceeds the throttling limit, the user receives an error and must wait to send additional requests.

#API Keys

Like API throttling, API keys help limit abuse of our API and protect e-commerce sites that depend on it. API keys include a unique identifier (key ID) that allows us to meter access and prevent attacks. 

Keys are created for each fabric customer and tied to specific entities. Clients must include a key ID and secret key in their requests. By tracking key IDs, we can hone in on malicious behaviour and block it at the source. 

#CORS Support

By default, modern web clients only allow servers to execute scripts from their own [origin](https://developer.mozilla.org/en-US/docs/Glossary/Origin) (an origin is a domain, scheme, or port). This is known as the [same-origin policy](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy). From a security perspective, the same-origin policy has obvious benefits. For example, it prevents a malicious site from executing JavaScript that reads data from another webpage a user is logged into. 

Of course, there are plenty of legitimate reasons a site or API may need to call resources with a different origin. [Cross-Origin Resource Sharing (CORS)](https://blog.stackpath.com/glossary-cross-origin-resource-sharing/) solves this problem and allows servers to securely access resources from specific, pre-defined domains.

To implement CORS, we use the [AWS CloudFront CDN](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/example-function-add-cors-header-response.html) and [AWS CORS](https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html) for our REST APIs. Our implementation ensures only authorized cross-origin scripts can be executed.

Here’s a simple example of how we use CORS in practice:

A client uses the fabric Commerce API gateway to load their website from an S3 Bucket at example.website.s3.us-east-1.amazonaws.com.

To load resources and allow users to interact with the online store and shop, the site uses JavaScript to make authenticated HTTP GET and PUT requests against the same example.website.s3.us-east-1.amazonaws.com bucket.

Typically, a modern web browser would block the requests to example.website.s3.us-east-1.amazonaws.com because they’re from a different origin. However, with CORS, we can explicitly allow specific cross-origin requests (in this case, example.website.s3.us-east-1.amazonaws.com), and the site will work as intended.  


#Conclusion

What we’ve discussed here covers a majority of the API attack surface. Still, we also perform regular code security audits; maintain compliance with standards including [PCI DSS](https://resources.fabric.inc/blog/ecommerce-pci-compliance), SOC2 Type II, NIST 800, and OWASP Top 10; and implement robust identity and access management to secure our APIs.

In addition to directing common API security issues, [we document our security approach](https://fabric.inc/knowledgebase/start/security-compliance) to help customers understand how their data and the data of their customers is protected when using our commerce platform.

If you’re developing an API-based platform for retailers (or for users within another industry), implementing the tactics described in this blog post will harden the platform instances you deploy for your customers. It will also secure shopping-related data at scale and help your customers earn trust among shoppers in this evolving digital landscape.