IMPORTANT: Our original WAF offering is now a legacy product. It was superseded by a new version, including a new interface and API, on July 13, 2020. The legacy version will continue to be supported for all existing users. The new version is available to all customers and is the default version for new customers as of July 13, 2020. Customers of the legacy WAF can contact support@fastly.com or their Fastly account team to upgrade.
You can create a custom HTML error page that will be presented to users who are blocked by the Fastly WAF response object. The attributes of the response object include the HTTP status code, the HTTP response text, the content type, and the returned content.
For this example, we'll:
- use a dynamic VCL snippet to create a custom
req.http.x-request-id
HTTP header, - use that header as a global variable to store the transaction ID of the request so that it can be used in both the request and WAF logs, and
- create a synthetic response to present the user with an HTML response.
The error page will display the transaction ID, something that might be useful if, for example, the user decides to contact your support team.
Creating a dynamic VCL Snippet
To create a dynamic VCL Snippet for the transaction ID, make the following API call in a terminal application:
1
curl -X POST -s https://api.fastly.com/service/<Service ID>/version/<Editable Version Number>/snippet -H "Fastly-Key:FASTLY_API_TOKEN" -H 'Content-Type: application/x-www-form-urlencoded' --data $'name=my_dynamic_snippet_name&type=recv&dynamic=1&content=if (!req.http.x-request-id) {\n set req.http.x-request-id = digest.hash_sha256(now randomstr(64) req.http.host req.url req.http.Fastly-Client-IP server.identity);\n}'
Creating a synthetic response
To create a synthetic response for the custom HTML error page, follow the steps below:
- Log in to the Fastly web interface and click the Configure link.
- From the service menu, select the appropriate service.
- Click the Edit configuration button and then select Clone active. The Domains page appears.
- Click the Content link. The Content page appears.
-
Click the Set up advanced response button. The Create a synthetic response page appears.
- Fill out the Create a synthetic response fields as follows:
- In the Name field, type
WAF_Response
. - From the Status menu, select
403 Forbidden
. - In the MIME Type field, specify the Content-Type of the response (e.g.,
text/html
). -
In the Response field, enter the following HTML. This response will display the value of
req.http.x-request-id
.1 2 3 4 5 6 7 8 9 10 11
<html> <head> <title>403 Forbidden</title> </head> <body> <p>The requested URL was rejected.</p> <p>For additional information, please contact support and provide this reference ID:</p> <p>"} req.http.x-request-id {"</p> <p><button onclick='history.back();'>Go Back</button></p> </body> </html>
- In the Name field, type
- Click the Create button. Your new response appears in the list of responses.
- Click the Activate button to deploy your configuration changes.
Additional notes
- You can change the composition of the transaction ID, but care should be taken to minimize the probability that multiple requests within a specified window of time (e.g., per day) have the same transaction ID value.
- A VCL Snippet was used to simplify the example presented and is not explicitly required for a custom WAF error page. As an alternative, you can use custom VCL to create the transaction ID.
- It's useful to include the transaction ID in the request and WAF logging formats to allow multiple messages generated for the same request to be correlated.