---
title: "WAF & API Gateway Integration: Protecting Your APIs - API7.ai"
description: "By following best practices and deploying the right tools, you can create a robust security layer that protects your APIs from a wide range of attacks."
url: "https://api7.ai/blog/waf-and-api-gateway-integration"
---

[Blog](https://api7.ai/blog)

[Products](https://api7.ai/category/products)[Technology](https://api7.ai/category/technology)[Ecosystem](https://api7.ai/category/ecosystem)[Case Study](https://api7.ai/category/usercase)[Team & Community](https://api7.ai/category/community)

Search

⌘ + K

[Blog](https://api7.ai/blog)

Ecosystem

# WAF & API Gateway Integration: Protecting Your APIs

January 30, 2024

[Ecosystem](https://api7.ai/category/ecosystem)

In today's digital age, APIs have become an integral part of our daily lives. They allow us to access and exchange data between different applications and services. However, with the increasing number of API endpoints exposed to the public, there is an ever-growing risk of cyberattacks and data breaches. This is why it is crucial to implement robust security measures to protect your APIs from malicious attacks.

## Benefits of Integrating WAF and API Gateway for API Protection

Web Application Firewall (WAF) and API Gateway technologies are two of the most effective ways to secure APIs from attacks. A WAF is a security solution that sits between the internet and your API server, analyzing incoming requests and blocking any malicious traffic. On the other hand, an API Gateway is a middleware layer that sits between your API server and the client, managing access control, traffic routing, and rate limiting.

```
sequenceDiagram
    participant Client
    participant APIGateway
    participant WAF
    participant BackendService

    Client->>APIGateway: Send API request
    activate APIGateway
    APIGateway->>WAF: Forward request for security check
    activate WAF

    alt Request is legitimate
        WAF-->>APIGateway: Return approval
        APIGateway->>BackendService: Forward request
        activate BackendService
        BackendService-->>APIGateway: Return response
        deactivate BackendService
        APIGateway-->>Client: Return response
    else Request is malicious
        WAF-->>APIGateway: Return denial
        APIGateway-->>Client: Return 403 Forbidden
    end

    deactivate WAF
    deactivate APIGateway
```

[Apache APISIX](https://api7.ai/apisix), a popular open-source API gateway, offers a robust set of built-in security plugins. However, in the face of increasingly sophisticated attacks like CVEs (Common Vulnerabilities and Exposures) and zero-day exploits, relying solely on these plugins can leave your APIs vulnerable. Integrating a professional Web Application Firewall (WAF) with APISIX provides a multi-layered defense strategy, ensuring comprehensive protection against modern threats.

### Understanding APISIX's Security Capabilities

1. Authentication and Authorization: APISIX supports plugins for [`JWT`](https://docs.api7.ai/hub/jwt-auth), [`basic auth`](https://docs.api7.ai/hub/basic-auth), [`key auth`](https://docs.api7.ai/hub/key-auth), and integration with [OpenID Connect](https://docs.api7.ai/hub/openid-connect) providers, enforcing access control.
2. Rate Limiting: Prevents malicious traffic spikes and DoS attacks through plugins like [`limit-conn`](https://docs.api7.ai/hub/limit-conn), [`limit-req`](https://docs.api7.ai/hub/limit-req), and [`limit-count`](https://docs.api7.ai/hub/limit-count).
3. IP Restriction and User-Agent Filtering: Allow granular control over incoming requests based on IP addresses and user agents.
4. CSRF Protection: Thwarts Cross-Site Request Forgery attacks.

### Limitations of API Gateway

1. Signature-Based Detection: APISIX plugins primarily rely on known attack signatures, leaving them ineffective against zero-day exploits that lack defined patterns.
2. Lack of Rule Updates: Security rules are constantly changing, which requires professional security experts and companies to maintain.
3. Limited Scope: While APISIX safeguards the gateway layer, WAFs provide broader protection across application layers.

### Benefits of WAF and API Gateway Integration

1. Proactive Threat Detection: Advanced WAFs leverage machine learning and behavioral analysis to detect anomalous traffic, even without prior knowledge of vulnerabilities.
2. Real-Time Rule Updates: Cloud-based WAFs can quickly update rules to address emerging threats, minimizing exposure windows.
3. Deeper Application Protection: WAFs can filter and block malicious traffic at the application layer, shielding against attacks that bypass API gateways.
4. Compliance and Regulatory Adherence: Certain industries mandate WAF usage for compliance with data security regulations.

## Deep Dive into the Integration Process

To integrate WAF and API Gateway, you need to choose the right tools for the job. Apache APISIX is a popular API Gateway solution that provides a scalable and flexible platform for managing your APIs. [Chaitin SafeLine](https://github.com/chaitin/SafeLine) and [Coraza](https://coraza.io/) are the WAF solutions that offer advanced security features and customizable rule sets.

### APISIX and Chaitin SafeLine

The Chaitin SafeLine WAF is a built-in plugin from APISIX 3.5. After the `chaitin-waf` plug-in is enabled, traffic will be forwarded to the Chaitin WAF service to detect and prevent various web application attacks to protect the security of applications and user data.

![WAF\_APISIX\_1](https://static.api7.ai/uploads/2024/01/23/Sy8ac32t_WAF_APISIX_1.png?imageMogr2/format/webp)

Assuming that you have installed Apache APISIX and SafeLine, the following command line can integrate the two:

```
curl http://127.0.0.1:9180/apisix/admin/plugin_metadata/chaitin-waf -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
  "nodes":[
     {
       "host": "192.168.99.11",
       "port": 8000
     }
  ]
}'
```

192.168.99.11 is the ip of the SafeLine service. Then we can create a route in APISIX:

```
curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
   "uri": "/*",
   "plugins": {
       "chaitin-waf": {}
    },
   "upstream": {
       "type": "roundrobin",
       "nodes": {
           "192.168.99.12:80": 1
       }
   }
}'
```

192.168.99.12 is the ip of the upstream service. The integration is complete.

Now let’s simulate SQL injection to see the effect:

```
curl http://127.0.0.1:9080 -d 'a=1 and 1=1'
```

An HTTP 403 error was returned, and as can be seen from the error message, Chaitin SafeLine successfully defended against the attack.

```
{"code":403,"success":false,"message":"blocked by Chaitin SafeLine Web Application Firewall","event_id":"18e0f220f7a94127acb21ad3c1b4ac47"}
```

```
sequenceDiagram
    participant Admin
    participant APISIX
    participant SafeLine
    participant Client

    Admin->>APISIX: Configure Chaitin WAF plugin
    activate APISIX
    APISIX->>SafeLine: Establish connection and configure nodes
    activate SafeLine
    SafeLine-->>APISIX: Confirm configuration
    deactivate SafeLine
    APISIX-->>Admin: Confirm plugin configuration
    deactivate APISIX

    Admin->>APISIX: Create route with WAF plugin enabled
    activate APISIX
    APISIX-->>Admin: Confirm route creation
    deactivate APISIX

    Client->>APISIX: Send API request (e.g., SQL injection)
    activate APISIX
    APISIX->>SafeLine: Forward request for security check
    activate SafeLine
    SafeLine-->>APISIX: Return denial (malicious request detected)
    deactivate SafeLine
    APISIX-->>Client: Return 403 Forbidden
    deactivate APISIX
```

### APISIX and Coraza-proxy-wasm

APISIX supports developing plugins with WebAssembly ([Wasm](https://api7.ai/blog/how-apisix-supports-wasm)), and [Coraza](https://docs.api7.ai/apisix/how-to-guide/security/waf/integrate-with-coraza) also provides Wasm plugins as an option. Therefore, integrating Coraza with APISIX incurs relatively low costs.

Wasm can be utilized cross-platform, allowing APISIX and Coraza to work without additional extensive modifications or adaptations. This eliminates extensive code modifications and adaptations.

Coraza is also a built-in plug-in of Apache APISIX, which can be enabled by modifying the configuration `file conf/config-default.yaml`:

```
wasm:
  plugins:
    - name: coraza-filter
      priority: 7999
      file: /home/ubuntu/coraza-proxy-wasm/build/main.wasm
```

Then create a route in APISIX with the rules of Coraza:

```
curl -i http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{
  "uri": "/anything",
  "plugins": {
    "coraza-filter": {
      "conf": {
        "directives_map": {
          "default": [
            "SecDebugLogLevel 9",
            "SecRuleEngine On",
            "SecRule REQUEST_URI \"@beginsWith /anything\" \"id:101,phase:1,t:lowercase,deny\""
          ]
        },
        "default_directives": "default"
      }
    }
  },
  "upstream": {
    "type": "roundrobin",
    "nodes": {
      "httpbin.org:80": 1
    }
  }
}'
```

Now let's send a request to see the effect:

```
curl http://localhost:9080/anything -v
```

Check logs in`logs/error.log`:

```
2023/08/31 09:20:39 [info] 126240#126240: *23933 Transaction interrupted tx_id="JVhHVfDuGjVbfgvDjik" context_id=2 action="deny" phase="http_request_headers", client: 127.0.0.1, server: _, request: "GET /anything HTTP/1.1", host: "localhost:9080"
2023/08/31 09:20:39 [debug] 126240#126240: *23933 Interruption already handled, sending downstream the local response tx_id="JVhHVfDuGjVbfgvDjik" context_id=2 interruption_handled_phase="http_request_headers"
```

## Best Practices for Securing APIs Using WAF and API Gateway Integration

To ensure the security of your APIs, you should follow these best practices:

1. Implement a defense-in-depth strategy that includes multiple layers of security controls;
2. Use SSL/TLS encryption to secure data in transit;
3. Regularly update your WAF rule sets to ensure they are up-to-date with the latest threats;
4. Monitor your API traffic and logs to quickly detect and respond to security incidents.

## Future Trends and Advancements in API Security and Protection

As the number of APIs in use grows, there will be a greater need for advanced security measures to protect them. Some of the future trends and advancements in API security and protection include:

1. AI-powered security solutions that can detect and respond to threats automatically;
2. Blockchain-based authentication and access control mechanisms;
3. Microservices-based API architectures that offer greater flexibility and scalability.

## Summary

In conclusion, WAF and API Gateway integration is a critical component of API security. By following best practices and deploying the right tools, you can create a robust security layer that protects your APIs from a wide range of attacks. With the right approach, you can ensure the availability, integrity, and confidentiality of your APIs and the data they exchange.

Tags:

[WAF](https://api7.ai/tags/waf)

[APISIX Basics](https://api7.ai/tags/apisix-basics)

Share article link

Share

Related Posts

[![Why Do Microservices Need an API Gateway](https://static.api7.ai/uploads/2023/02/16/CHqaC3Xw_Ecosystem%20%E6%A8%A1%E6%9D%BF1.png)](https://api7.ai/blog/why-do-microservices-need-an-api-gateway)

## [Why Do Microservices Need an API Gateway](https://api7.ai/blog/why-do-microservices-need-an-api-gateway)

[![Why Do You Need Apache APISIX When Using Amazon Lambda](https://static.api7.ai/uploads/2024/12/25/PGShc0sm_apisix-aws-lambda.png)](https://api7.ai/blog/why-do-you-need-apache-apisix-when-using-amazon-lambda)

## [Why Do You Need Apache APISIX When Using Amazon Lambda](https://api7.ai/blog/why-do-you-need-apache-apisix-when-using-amazon-lambda)

Table of Contents

- [Benefits of Integrating WAF and API Gateway for API Protection](#benefits-of-integrating-waf-and-api-gateway-for-api-protection)

  - [Understanding APISIX's Security Capabilities](#understanding-apisixs-security-capabilities)
  - [Limitations of API Gateway](#limitations-of-api-gateway)
  - [Benefits of WAF and API Gateway Integration](#benefits-of-waf-and-api-gateway-integration)

- [Deep Dive into the Integration Process](#deep-dive-into-the-integration-process)

  - [APISIX and Chaitin SafeLine](#apisix-and-chaitin-safeline)
  - [APISIX and Coraza-proxy-wasm](#apisix-and-coraza-proxy-wasm)

- [Best Practices for Securing APIs Using WAF and API Gateway Integration](#best-practices-for-securing-apis-using-waf-and-api-gateway-integration)

- [Future Trends and Advancements in API Security and Protection](#future-trends-and-advancements-in-api-security-and-protection)

- [Summary](#summary)

## Related Posts

[![How to Integrate Apache APISIX with open-appsec WAF](https://static.api7.ai/uploads/2024/10/18/8d1iVJWL_logo%20x%20open-appsec.png)](https://api7.ai/blog/apisix-integrates-with-open-appsec-waf)

Ecosystem

October 21, 2024

#### [How to Integrate Apache APISIX with open-appsec WAF](https://api7.ai/blog/apisix-integrates-with-open-appsec-waf)

Learn how to integrate open-appsec WAF with Apache APISIX across Linux, Docker, and Kubernetes deployment environments.

[WAF](https://api7.ai/tags/waf)

[API Security](https://api7.ai/tags/api-security)

[Data Security](https://api7.ai/tags/data-security)

[![Unlocking Tailored Solutions: Harnessing Apache APISIX and Node-Red in Harmony](https://static.api7.ai/uploads/2024/03/06/uppjizMq_APISIX-node-red-cover.png)](https://api7.ai/blog/apache-apisix-and-node-red)

Ecosystem

March 6, 2024

#### [Unlocking Tailored Solutions: Harnessing Apache APISIX and Node-Red in Harmony](https://api7.ai/blog/apache-apisix-and-node-red)

Combining APISIX and Node-Red can help visually implement a complete request-handling process without rewriting complex code or plugins.

[Node-Red](https://api7.ai/tags/node-red)

[APISIX Basics](https://api7.ai/tags/apisix-basics)

[![mTLS Everywhere: How to Configure TLS for APISIX](https://static.api7.ai/uploads/2024/12/24/DVhnbLpy_apisix-mtls.png)](https://api7.ai/blog/configure-tls-for-apisix)

Ecosystem

July 31, 2023

#### [mTLS Everywhere: How to Configure TLS for APISIX](https://api7.ai/blog/configure-tls-for-apisix)

Transport Secure Layer, aka TLS, formerly SSL, is among its many pillars of information system security. Let's learn how to configure TLS for the Apache APISIX API Gateway.

[APISIX Basics](https://api7.ai/tags/apisix-basics)

[API Security](https://api7.ai/tags/api-security)
