INTEGRITY Cloudflare Docs

JavaScript Detections

JavaScript Detections is a type of challenge separate from Cloudflare’s Challenge Pages or Turnstile. JavaScript Detections helps Cloudflare's bot solutions identify automated requests.

While Challenge Pages and Turnstile rely on client-side signals to determine the authenticity of a request, Bot Management’s JavaScript Detections relies on client-side signals and runs on every single request made to your website.

Process

JavaScript Detections is implemented on your website via a lightweight, invisible JavaScript code snippet that follows Cloudflare's privacy standards.

JavaScript is injected only in response to requests for HTML pages or page views, excluding AJAX calls. API and mobile application traffic is unaffected.

JavaScript Detections has a lifespan of 15 minutes. However, the code is injected again before the session expires. After page load, the script is deferred and utilizes a separate thread (where available) to ensure that performance impact is minimal. The snippets of JavaScript will contain a source pointing to the Challenge Platform, with paths that start with /cdn-cgi/challenge-platform/…

Once JavaScript Detections is injected on the HTML page, the visitor's browser will run the JavaScript code snippet and a cf_clearance cookie is issued to the visitor. The information in JavaScript Detections is stored in the cf_clearance cookie and is used to populate js_detection.passed.

When the visitor encounters a WAF custom rule on your website, the rule will check the outcome of the cf_clearance cookie. The outcome of the cf_clearance cookie determines whether the request passes, or is blocked or challenged.

Refer to the steps below to enable and enforce JavaScript Detections.

1. Enable JavaScript Detections

For Bot Fight Mode customers, JavaScript Detections is automatically enabled and cannot be disabled.

For Super Bot Fight Mode and Bot Management for Enterprise customers, JavaScript Detections is optional.

  1. In the Cloudflare dashboard, go to the Security Settings page.

    Go to Settings ↗
  2. Under your bot traffic plan configurations, select the edit icon for JS detections and turn JavaScript Detections on.

For more details on how to set up bot protection, refer to the Bots documentation.

2. Enforce execution of JavaScript Detections

Once you enable JavaScript detections, you must use the cf.bot_management.js_detection.passed field to create WAF custom rules (or the request.cf.botManagement.jsDetection.passed variable in Workers).

When adding this field to WAF custom rules, it is used on endpoints expecting browser traffic (avoiding native mobile applications or websocket endpoints), after a user's first request to your application (Cloudflare needs at least one HTML request before injecting JavaScript detection), and with the Managed Challenge action, because there are legitimate reasons a user might not have passed a JavaScript Detection challenge (network issues, ad blockers, disabled JavaScript in browser, native mobile applications).

Prerequisites

The cf.bot_management.js_detection.passed field should never be used in a WAF custom rule that matches a visitor's first request to a site. It is necessary to have at least one HTML request before Cloudflare can inject JavaScript detection.

(http.request.uri.path eq "/api/v4/user/create" and http.request.method eq "POST" and not cf.bot_management.verified_bot)
and (cf.bot_management.score lt 30 or !cf.bot_management.js_detection.passed)
"botManagement": {
"jsDetection": {
    "passed": false
}
}

Refer to the WAF documentation for more information on creating a custom rule.

API

If you enable JavaScript Detections via the dashboard, Cloudflare will insert a script tag in all HTML pages served on your website. If you would prefer to limit where JavaScript Detections is served, you can do so with the JavaScript Detections API script.

The JavaScript Detections API allows you more granular control over when and where JavaScript Detections is injected on your website, as well as an option for callback handling (for logging or other additional actions).

You can explicitly add a script reference to /cdn-cgi/challenge-platform/scripts/jsd/api.js and your own code calling window.cloudflare.jsd.executeOnce on specific HTML pages of your website.

The following script must be added to every page that you wish to have JavaScript Detections enabled:

<script>

function jsdOnload(){
  window.cloudflare.jsd.executeOnce(
    {
      callback: function(result){
        console.log('jsd outcome', result);
      }
    }
  );
}
</script>
<script src="/cdn-cgi/challenge-platform/scripts/jsd/api.js?onload=jsdOnload" async>

Considerations

JavaScript Detections does not guarantee a specific bot score.

Limitations

If you enabled Bot Management before June 2020

Customers who enabled Enterprise Bot Management before June 2020 do not have JavaScript Detections enabled by default (unless specifically requested). These customers can still enable the feature in the Cloudflare dashboard.

If it is the first request to your website

The first request from a new client to your website or application will generally not have JavaScript Detections data (cf.bot_management.js_detection.passed = false). This is because Cloudflare needs at least one HTML request before injecting JavaScript Detection and issuing the cf_clearance cookie.

Subsequent requests can include a cf_clearance cookie if JavaScript ran successfully.

If you have a Content Security Policy (CSP)

If you have a Content Security Policy (CSP), you need to take additional steps to implement JavaScript Detections:

If you have ETags

Enabling JavaScript Detections (JSD) will strip ETags from HTML responses where JSD is injected.

If your origin sends a no-transform header

If the origin response includes a Cache-Control: no-transform directive, Cloudflare does not inject the JavaScript Detections script. The cf.bot_management.js_detection.passed field will show as missing for these requests.

To use JavaScript Detections, remove the no-transform directive from Cache-Control response headers on pages where you want JavaScript Detections to run. For more information, refer to Cache-Control directives.