Attaching the token when your client doesn't attach the aws-waf-token cookie by default AwsWafIntegration.getToken() retrieves a valid token and stores it as a cookie, but not all client calls attach this cookie by default. For example, calls made across host domains don't attach the cookie. The fetch wrapper handles these cases automatically, but if you aren't able to use the fetch wrapper, you can handle this by using a custom x-aws-waf-token header. AWS WAF reads tokens from this header, in addition to reading them from the aws-waf-token cookie. The following code shows an example of setting the header. const token = await AwsWafIntegration.getToken(); const result = await fetch('/url', { headers: { 'x-aws-waf-token': token, Client application integrations 469 AWS WAF, AWS Firewall Manager, and AWS Shield Advanced Developer Guide }, }); By default, AWS WAF only accepts tokens that contain the same domain as the requested host domain. Any cross-domain tokens require corresponding entries in the web ACL token domain list. For more information, see AWS WAF web ACL token domain list configuration. For additional information about cross-domain token use, see aws-samples/aws-waf-bot-control- api-protection-with-captcha. Using the CAPTCHA JavaScript API This section provides instructions for using the CAPTCHA integration API. The CAPTCHA JavaScript API allows you to configure the CAPTCHA puzzle and place it where you want in your client application. This API leverages the features of the intelligent threat JavaScript APIs to acquire and use AWS WAF tokens after an end user successfully completes a CAPTCHA puzzle. Implement the JavaScript integration first in a test environment, then in production. For additional coding guidance, see the sections that follow. To use the CAPTCHA integration API 1. Install the API a. Sign in to the AWS Management Console and open the AWS WAF console at https:// console.aws.amazon.com/wafv2/. b. In the navigation pane, choose Application integration. On the Application integration page, you can see tabbed options. c. Select CAPTCHA integration. d. Copy the listed JavaScript integration script tag for use in your integration. e. In your application page code, in the
section, insert the script tag that you copied. This inclusion makes the CAPTCHA puzzle available for configuration and use.
Example configuration settings The following example listing shows the renderCaptcha with non-default settings for the width and the title options. Client application integrations 476 AWS WAF, AWS Firewall Manager, and AWS Shield Advanced Developer Guide AwsWafCaptcha.renderCaptcha(container, { apiKey: \"...API key goes here...\", onSuccess: captchaExampleSuccessFunction, onError: captchaExampleErrorFunction, dynamicWidth: true, skipTitle: true }); For full information about the configuration options, see CAPTCHA JavaScript API specification. Handling a CAPTCHA response from AWS WAF This section provides an example of handling a CAPTCHA response. An AWS WAF rule with a CAPTCHA action terminates the evaluation of a matching web request if the request doesn't have a token with a valid CAPTCHA timestamp. If the request is a GET text/ html call, the CAPTCHA action then serves the client an interstitial with a CAPTCHA puzzle. When you don't integrate the CAPTCHA JavaScript API, the interstitial runs the puzzle and, if the end user successfully solves it, automatically resubmits the request. When you integrate the CAPTCHA JavaScript API and customize your CAPTCHA handling, you need to detect the terminating CAPTCHA response, serve your custom CAPTCHA, and then if the end user successfully solves the puzzle, resubmit the client's web request. The following code example shows how to do this. Note The AWS WAF CAPTCHA action response has a status code of HTTP 405, which we use to recognize the CAPTCHA response"}}},{"rowIdx":104871,"cells":{"id":{"kind":"string","value":"waf-dg-165"},"source":{"kind":"string","value":"waf-dg.pdf"},"chunk_id":{"kind":"number","value":165,"string":"165"},"text":{"kind":"string","value":"interstitial with a CAPTCHA puzzle. When you don't integrate the CAPTCHA JavaScript API, the interstitial runs the puzzle and, if the end user successfully solves it, automatically resubmits the request. When you integrate the CAPTCHA JavaScript API and customize your CAPTCHA handling, you need to detect the terminating CAPTCHA response, serve your custom CAPTCHA, and then if the end user successfully solves the puzzle, resubmit the client's web request. The following code example shows how to do this. Note The AWS WAF CAPTCHA action response has a status code of HTTP 405, which we use to recognize the CAPTCHA response in this code. If your protected endpoint uses an HTTP 405 status code to communicate any other type of response for the same call, this example code will render a CAPTCHA puzzle for those responses as well.