Overview
You can use this document to learn how to implement an IP firewall rule.
Before you begin, you must enable serverless scripting for your stack. To learn more, see Enable Serverless Scripting and Create a Script.
View a sample script
In the following example, the route is: *
This script will apply the firewall to your entire site.
If you want to block a specific part of your site, then you can change the route to be more specific.
Review the following script:
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request))
})
async function fetchAndApply(request) {
//Create an Array of IP's to block
var blacklist = ["192.168.1.1", "192.168.1.2"];
//Check if IP is blacklisted by getting the client IP from the x-sp-client-ip header
if (blacklist.includes(request.headers.get('x-sp-client-ip'))) {
return new Response('Sorry, this page is not available.',
{ status: 403, statusText: 'Forbidden' })
}
return fetch(request)
}
Access your script
You can access your script with any of the delivery domains on your account.
Based on the above example, you can access the script with: https://sitehash.stackpathcdn.com/hello-world