Protect an API with Firewall and WAF
Put a Firewall with a WAF rule set in front of an API path, and watch it block a SQL injection while normal requests pass.
In this tutorial, you will protect an API with a Firewall that applies a WAF rule set to every path that starts with /api. You will publish a small API, create the WAF rule set and the Firewall, deploy both on one Workload, and send a SQL injection that WAF blocks.
Every resource the tutorial creates links to one created before it:
- The Function is the API: it answers every request with the path and the query string it received.
- The Application runs the Function on every path.
- The WAF rule set sets which threat families WAF scores, and how sensitive it is to each one.
- The Firewall has WAF turned on, and a rule that applies the rule set in Blocking mode to the paths that start with
/api. - The Workload holds the domain, and its deployment sets the Firewall that inspects each request and the Application that answers it.
The tutorial uses Azion Console. To create a Firewall and its rules with the Azion CLI or the API, refer to Firewall quickstart and Create and apply a WAF rule set.
Prerequisites
- An Azion account. To create one, refer to Create an account.
- Access to Azion Console. To sign in, refer to Access Azion Console.
curl, to send requests to your API.
1. Write the API
Create a directory for the tutorial, go to it, and create an index.js file with this code:
The Function echoes the path and the query string of each request as JSON, so every response shows what reached it.
2. Create the Function
To create the Function in Azion Console:
- Access Azion Console > Functions.
- Select + Function.
- In Name, enter
protected-api. - Keep Application selected in Execution Environment.
- Select the Code tab.
- Replace the sample code in the editor with the content of
index.js. - Select Save.
The Console shows the message Your function has been created, and the account holds the Function.
3. Create the Application
The Application takes three parts: Functions turned on, a Function Instance of the Function, and a rule that runs it on every path.
To create the Application:
- Access Azion Console > Applications.
- Select + Application.
- In Name, enter
protected-api. - In Modules, turn on Functions.
- Select Create.
To attach the Function:
- Select the Functions Instances tab.
- Select + Function.
- In Name, enter
protected-api. - In Function, select
protected-api. - Select Save.
To run the Function on every request:
- Select the Rules Engine tab.
- Select + Rule.
- In Name, enter
Run protected-api. - Keep Request Phase selected.
- In Criteria, keep
${uri}and starts with, and enter/as the argument. - In Behaviors, replace Deliver with Run Function.
- In the field that Run Function adds, select the
protected-apiFunction Instance. - Select Save.
The Application runs the Function on every request it receives.
4. Create the WAF rule set
The rule set scores eight threat families, each at medium sensitivity, which is what Azion Console proposes for a new rule set.
To create the rule set:
- Access Azion Console > WAF Rules.
- Select + WAF Rule.
- In Name, enter
protected-api. - In Threat Type Configuration, keep the eight threat families turned on, each with Sensitivity Medium.
- Select Save.
The Console shows the message Your WAF rule has been created. The account holds the rule set, and it scores nothing until a Firewall rule applies it.
5. Create the Firewall
The Firewall needs WAF turned on and a rule that applies the rule set to the paths that start with /api, in Blocking mode. Requests to other paths reach the Application without WAF inspection.
To create the Firewall:
- Access Azion Console > Firewalls.
- Select + Firewall.
- In Name, enter
protected-api. - In Modules, turn on Web Application Firewall.
- Select Create.
The Console opens the new Firewall on its Main Settings tab.
To apply the rule set to /api:
- Select the Rules Engine tab.
- Select + Rule.
- In Name, enter
Apply the WAF to /api. - In Criteria, keep Request Uri and starts with, and enter
/apias the argument. - In Behaviors, select Set WAF.
- In the WAF field that Set WAF adds, select
protected-api. - In Select a WAF mode, select Blocking.
- Select Save.
The rule appears in the Rules Engine tab of the Firewall, which applies the rule set to /api in Blocking mode.
6. Create the Workload
The deployment of the Workload sets both the Firewall and the Application, so every request passes the Firewall before it reaches the Application.
To create the Workload and its deployment:
- Access Azion Console > Workloads.
- Select + Workload.
- In Name, enter
protected-api. - In Deployment Settings, select
protected-apiin Application. - In Firewall, select
protected-api. - Select Save.
The list of Workloads shows the domain in the Workload Domain column, in the format xxxxxxxxxxx.map.azionedge.net.
7. Test the protection
A new Workload answers several minutes after its deployment, once the change propagates across Azion’s distributed infrastructure. In tests, it took 6 to 9 minutes, and no duration is guaranteed. Until then, the domain answers 404 with an HTML page, and the answers can alternate between 404 and the responses below.
Set DOMAIN to the domain of your Workload:
Send a normal request to the API:
Repeat it every minute until the API answers three times in a row:
Send a SQL injection in the same parameter, and print the status code:
WAF blocked the request before it reached the Function. The body is Azion’s default error page, headed Bad Request.
Send the same injection to /, which the Firewall rule does not cover:
The request reached the Function, because only the paths that start with /api pass through the rule set.
8. Delete the resources
The protected API keeps running on your domain after the tutorial, so this stage is optional. To remove the resources, delete the Workload first, because its deployment uses the Firewall and the Application. Then delete the Firewall before the rule set it applies.
To delete the Workload:
- Access Azion Console > Workloads.
- In the
protected-apirow, open the actions menu and select Delete. - In the Delete Workload dialog, enter
protected-api. - Select Delete.
Repeat the steps in this order: Firewalls for the Firewall, WAF Rules for the rule set, Applications for the Application, and Functions for the Function. Each dialog asks for the name of the resource.
The account no longer holds the resources of the tutorial.