Skip to content

Commit 71adc13

Browse files
patriciasantaanathomasgauvin
authored andcommitted
[Challenges] JSD docs refresh (#23543)
* jsd docs refresh * fix warning * typo * feedback * edits * cf_clearance * api feedback * pcx feedback
1 parent dd773a0 commit 71adc13

File tree

6 files changed

+126
-44
lines changed

6 files changed

+126
-44
lines changed

src/content/docs/cloudflare-challenges/challenge-types/javascript-detections.mdx

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
---
33
type: overview
44
pcx_content_type: reference
5-
title: JavaScript detections
5+
title: JavaScript Detections
66
sidebar:
77
order: 3
88
---
@@ -11,35 +11,85 @@ import { Render, Tabs, TabItem } from "~/components"
1111

1212
<Render file="javascript-detections-definition" />
1313

14-
## Enable JavaScript detections
14+
<Render file="javascript-detections-process" />
1515

16-
For Bot Fight Mode customers, JavaScript detections are automatically enabled and cannot be disabled.
16+
Refer to the steps below to enable and enforce JavaScript Detections.
1717

18-
For Super Bot Fight Mode and Bot Management for Enterprise customers, JavaScript detections are optional.
18+
## 1. Enable JavaScript Detections
19+
20+
For Bot Fight Mode customers, JavaScript Detections is automatically enabled and cannot be disabled.
21+
22+
For Super Bot Fight Mode and Bot Management for Enterprise customers, JavaScript Detections is optional.
1923

2024
<Render file="javascript-detections-enable" />
2125

2226
For more details on how to set up bot protection, refer to the [Bots documentation](/bots/get-started/).
2327

24-
## Enforcing execution of JavaScript detections
28+
## 2. Enforce execution of JavaScript Detections
2529

2630
<Render file="javascript-detections-implementation" />
2731

28-
<Render file="cf-clearance-cookie" />
32+
Refer to the [WAF documentation](/waf/custom-rules/create-dashboard/) for more information on creating a custom rule.
33+
34+
## API
35+
36+
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.
37+
38+
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).
39+
40+
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.
41+
42+
:::caution
43+
It is not recommended to combine both approaches (zone-wide toggle and the manual injection). If you want to selectively deploy JavaScript Detections only on certain pages, disable JavaScript Detections via the Cloudflare dashboard and use the JavaScript Detections API exclusively.
44+
:::
45+
46+
The following script must be added to every page that you wish to have JavaScript Detections enabled:
47+
48+
```js wrap
49+
<script>
50+
51+
function jsdOnload(){
52+
window.cloudflare.jsd.executeOnce(
53+
{
54+
callback: function(result){
55+
console.log('jsd outcome', result);
56+
}
57+
);
58+
}
59+
</script>
60+
<script src="/cdn-cgi/challenge-platform/scripts/jsd/api.js?onload=jsdOnload" async>
61+
```
62+
63+
:::note
64+
`result` = `success` or `error` only refers to the execution of JavaScript Detections. It does not indicate whether a visitor is a human or a bot.
65+
:::
66+
67+
## Considerations
68+
69+
JavaScript Detections does not guarantee a specific bot score.
70+
71+
- If the JavaScript Detections injection or execution fails and `cf.bot_management.js_detection.passed` = `false`, a separate Bot Management heuristic can still yield a `1` or higher bot score, independent of JavaScript Detections.
72+
- If the JavaScript Detections passes, the final bot score may still be `1` due to other detection heuristics (for example, known malicious IP, signature detection, and more), resulting in `js_detection.passed` = `true`, but `score` = `1`.
2973
3074
## Limitations
3175
3276
### If you enabled Bot Management before June 2020
3377
34-
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.
78+
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.
79+
80+
### If it is the first request to your website
81+
82+
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.
83+
84+
Subsequent requests can include a `cf_clearance` cookie if JavaScript ran successfully.
3585
3686
### If you have a Content Security Policy (CSP)
3787
3888
<Render file="javascript-detections-csp" />
3989
4090
:::caution[Warning]
4191
42-
JavaScript detections are not supported with `nonce` set via `<meta>` tags.
92+
JavaScript Detections is not supported with `nonce` set via `<meta>` tags.
4393
:::
4494
4595
### If you have ETags

src/content/docs/cloudflare-challenges/concepts/how-challenges-work.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ Challenges can be issued in three primary ways depending on which Cloudflare pro
1111
| Product | Challenge type(s) |
1212
| --- | --- |
1313
| [WAF](/waf/) ([custom rules](/waf/custom-rules/), [rate limiting rules](/waf/rate-limiting-rules/), [IP access rules](/waf/tools/ip-access-rules/)) | [Interstitial Challenge Page](/cloudflare-challenges/challenge-types/challenge-pages/) |
14-
| [Bot Management](/bots/get-started/bot-management/) | [JavaScript detection](/bots/additional-configurations/javascript-detections/) |
14+
| [Bot Management](/bots/get-started/bot-management/) | [JavaScript Detections](/bots/additional-configurations/javascript-detections/) |
1515
| [Bot Fight Mode](/bots/get-started/bot-fight-mode/), [Super Bot Fight Mode](/bots/get-started/super-bot-fight-mode/) | [Interstitial Challenge Page](/cloudflare-challenges/challenge-types/challenge-pages/) |
1616
| [Turnstile](/turnstile/) | Embedded widget |
1717
| [HTTP DDoS attack protection](/ddos-protection/managed-rulesets/http/) | Any Challenge |
1818
| [Under Attack Mode](/fundamentals/reference/under-attack-mode/) | [Managed Challenge](/cloudflare-challenges/challenge-types/challenge-pages/#managed-challenge-recommended) |
1919

2020
Challenge Pages and Turnstile rely on the same underlying mechanism to issue Challenges to your website or application's visitors.
2121

22-
JavaScript detections support Cloudflare's Enterprise Bot Management. While it still relies on client-side detections, JavaScript detections function using a more performant challenge logic than Challenge Pages or Turnstile.
22+
JavaScript Detections supports Cloudflare's Enterprise Bot Management. While it still relies on client-side detections, JavaScript Detections function using a more performant challenge logic than Challenge Pages or Turnstile.
2323

2424
---
2525

@@ -29,7 +29,7 @@ Refer to the following pages for more information on the different challenge typ
2929

3030
- [Interstitial Challenge Pages](/cloudflare-challenges/challenge-types/challenge-pages/)
3131
- [Turnstile](/cloudflare-challenges/challenge-types/turnstile/)
32-
- [JavaScript detections](/cloudflare-challenges/challenge-types/javascript-detections/)
32+
- [JavaScript Detections](/cloudflare-challenges/challenge-types/javascript-detections/)
3333

3434
---
3535

src/content/partials/cloudflare-challenges/javascript-detections-csp.mdx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66
import { GlossaryTooltip } from "~/components"
77

8-
If you have a <GlossaryTooltip term="content security policy (CSP)">Content Security Policy (CSP)</GlossaryTooltip>, you need to take additional steps to implement JavaScript detections:
8+
If you have a <GlossaryTooltip term="content security policy (CSP)">Content Security Policy (CSP)</GlossaryTooltip>, you need to take additional steps to implement JavaScript Detections:
99

10-
* Ensure that anything under `/cdn-cgi/challenge-platform/` is allowed. Your CSP should allow scripts served from your origin domain (`script-src self`).
11-
* If your CSP uses a `nonce` for script tags, Cloudflare will add these nonces to the scripts it injects by parsing your CSP response header.
12-
* If your CSP does not use `nonce` for script tags and **JavaScript Detection** is enabled, you may see a console error such as `Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-b123b8a70+4jEj+d6gWI9U6IilUJIrlnRJbRR/uQl2Jc='), or a nonce ('nonce-...') is required to enable inline execution.` We highly discourage the use of `unsafe-inline` and instead recommend the use CSP `nonces` in script tags which we parse and support in our CDN.
10+
- Ensure that anything under `/cdn-cgi/challenge-platform/` is allowed. Your CSP should allow scripts served from your origin domain (`script-src self`).
11+
- For `nonce` script tags:
12+
13+
- If your CSP uses a `nonce` for script tags, Cloudflare will add these nonces to the scripts it injects by parsing your CSP response header.
14+
15+
- If your CSP does not use `nonce` for script tags and **JavaScript Detections** is enabled, you may see a console error such as `Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-b123b8a70+4jEj+d6gWI9U6IilUJIrlnRJbRR/uQl2Jc='), or a nonce ('nonce-...') is required to enable inline execution.` We highly discourage the use of `unsafe-inline` and instead recommend the use CSP `nonces` in script tags which we parse and support in our CDN.

src/content/partials/cloudflare-challenges/javascript-detections-definition.mdx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44

55
import { Markdown } from "~/components"
66

7-
JavaScript detections are another method that help Cloudflare identify bot requests.
7+
JavaScript Detections is a type of Challenge separate from Cloudflare’s Challenge Pages or Turnstile. Javascript Detections helps Cloudflare's [bot solutions](/bots/) identify automated requests.
88

9-
These detections are implemented via a lightweight, invisible JavaScript code snippet that follows Cloudflare’s [privacy standards](https://www.cloudflare.com/privacypolicy/). JavaScript is injected only in response to requests for HTML pages or page views, excluding AJAX calls. API and mobile app traffic is unaffected. JavaScript detections have 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.
10-
11-
The snippets of JavaScript will contain a source pointing to the challenge platform, with paths that start with `/cdn-cgi/challenge-platform/...`
12-
13-
:::note
14-
The information in JavaScript detections which populates `js_detection.passed` is stored in the `cf_clearance` cookie.
15-
:::
9+
While Challenge Pages and Turnstile rely on client-side signals to determine the authenticity of a request, Bot Management’s JavaScript Detections relies on network-side signals and run on every single request made to your website.

src/content/partials/cloudflare-challenges/javascript-detections-implementation.mdx

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,37 @@
33

44
---
55

6-
Once you enable JavaScript detections, you can use the `cf.bot_management.js_detection.passed` field in [WAF custom rules](/waf/custom-rules/) (or the `request.cf.botManagement.jsDetection.passed` variable in [Workers](/workers/)).
6+
import { Tabs, TabItem } from "~/components";
77

8-
When adding this field to WAF custom rules, use it:
8+
Once you enable JavaScript detections, you must use the `cf.bot_management.js_detection.passed` field to create [WAF custom rules](/waf/custom-rules/) (or the `request.cf.botManagement.jsDetection.passed` variable in [Workers](/workers/)).
99

10-
* On endpoints expecting browser traffic (avoiding native mobile applications or websocket endpoints).
11-
* After a user's first request to your application (Cloudflare needs at least one HTML request before injecting JavaScript detection).
12-
* With the [Managed Challenge action](/cloudflare-challenges/challenge-types/challenge-pages/#managed-challenge-recommended), 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 apps).
10+
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).
1311

1412
### Prerequisites
1513

16-
* You must have JavaScript detections enabled on your zone.
17-
* You must have [updated your Content Security Policy headers](/cloudflare-challenges/challenge-types/javascript-detections/#if-you-have-a-content-security-policy-csp) for JavaScript detections.
18-
* You must not run this field on websocket endpoints.
19-
* You must use the field in a custom rules expression that expects only browser traffic.
20-
* The action should always be a managed challenge in case a legitimate user has not received the challenge for network or browser reasons.
21-
* The path specified in the rule builder should never be the first HTML page a user visits when browsing your site.
22-
23-
`cf.bot_management.js_detection.passed` is used to indicate that a request has a Bot Management cookie present with a JavaScript detection value indicating it submitted the JavaScript detection test, and received a likely human scoring result.
14+
- You must have JavaScript Detections enabled on your zone.
15+
- You must have [updated your Content Security Policy headers](/cloudflare-challenges/challenge-types/javascript-detections/#if-you-have-a-content-security-policy-csp) for JavaScript detections.
16+
- You must not run this field on websocket endpoints.
17+
- You must use the field in a custom rules expression that expects only browser traffic.
18+
- The action should always be a managed challenge in case a legitimate user has not received the challenge for network or browser reasons.
19+
- The path specified in the rule builder should never be the first HTML page a user visits when browsing your site.
2420

2521
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.
2622

27-
```js title="Example with Workers"
28-
"botManagement": {
29-
"jsDetection":
30-
31-
{ "passed": false }
32-
,
33-
},
34-
```
23+
<Tabs>
24+
<TabItem label="WAF rule example">
25+
```txt wrap
26+
(http.request.uri.path eq "/api/v4/user/create" and http.request.method eq "POST" and not cf.bot_management.verified_bot)
27+
and (cf.bot_management.score lt 30 or !cf.bot_management.js_detection.passed)
28+
```
29+
</TabItem>
30+
<TabItem label="Workers example">
31+
```js
32+
"botManagement": {
33+
"jsDetection": {
34+
"passed": false
35+
}
36+
}
37+
```
38+
</TabItem>
39+
</Tabs>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
{}
3+
---
4+
5+
import { Markdown } from "~/components";
6+
7+
## Process
8+
9+
JavaScript Detections is implemented on your website via a lightweight, invisible JavaScript code snippet that follows Cloudflare's [privacy standards](https://www.cloudflare.com/privacypolicy/).
10+
11+
JavaScript is injected only in response to requests for HTML pages or page views, excluding AJAX calls. API and mobile application traffic is unaffected.
12+
13+
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/…`
14+
15+
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`.
16+
17+
- If the visitor is verified and a `cf_clearance` cookie is issued, it will contain the outcome: `cf.bot_management.js.detection.passed` = `true`
18+
- If the verification fails, the cookie will contain the outcome: `cf.bot_management.js.detection.passed` = `false`
19+
20+
:::note
21+
The `cf_clearance` cookie cannot exceed the maximum size of 4096 bytes.
22+
:::
23+
24+
:::caution
25+
Enforcement against bots does **not** occur even if the cookie is flagged false.
26+
27+
You must enable JavaScript Detections and then create a custom WAF rule using the `cf.bot_management.js.detection.passed` field to block or challenge a failed request.
28+
:::
29+
30+
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.

0 commit comments

Comments
 (0)