Skip to content

Commit 0114e11

Browse files
jsd docs refresh
1 parent 9576fad commit 0114e11

File tree

6 files changed

+107
-44
lines changed

6 files changed

+107
-44
lines changed

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

Lines changed: 51 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,78 @@ 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 are automatically enabled and cannot be disabled.
21+
22+
For Super Bot Fight Mode and Bot Management for Enterprise customers, JavaScript Detections are 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+
Use JavaScript Detections API for more granular control over when and where JavaScript Detections are injected on your website, as well as an option for callback handling (for logging or other additional actions).
37+
38+
You can explicitly add a script reference to `jsd/api.js` and your own code calling `window.cloudflare.jsd.executeOnce` on specific HTML pages of your website.
39+
40+
:::warning
41+
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 JSD API exclusively.
42+
:::
43+
44+
The following script must be added to every page that you wish to have JavaScript Detections enabled:
45+
46+
```js wrap
47+
<script>
48+
49+
function jsdOnload(){
50+
window.cloudflare.jsd.executeOnce(
51+
{
52+
callback: function(result){
53+
console.log('jsd outcome', result);
54+
}
55+
);
56+
}
57+
</script>
58+
<script src="/cdn-cgi/challenge-platform/scripts/jsd/api.js?onload=jsdOnload" async>
59+
```
60+
61+
## Considerations
62+
63+
- While JavaScript Detections further refine the [bot score](/bots/concepts/bot-score/), it is not a very significant effect.
64+
- 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.
65+
- Even if JavaScript Detections pass, 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.
2966
3067
## Limitations
3168
3269
### If you enabled Bot Management before June 2020
3370
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.
71+
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.
72+
73+
### If it is the first request to your website
74+
75+
The first request from a new client to your website or application will generally not have JSD 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.
76+
77+
Subsequent requests can include a `cf-clearance` cookie if JavaScript ran successfully.
3578
3679
### If you have a Content Security Policy (CSP)
3780
3881
<Render file="javascript-detections-csp" />
3982
4083
:::caution[Warning]
4184
42-
JavaScript detections are not supported with `nonce` set via `<meta>` tags.
85+
JavaScript Detections are not supported with `nonce` set via `<meta>` tags.
4386
:::
4487
4588
### 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 Detection](/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 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.
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** are 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 help 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 rely 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: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,25 @@
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+
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/)).
77

8-
When adding this field to WAF custom rules, use it:
9-
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).
8+
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).
139

1410
### Prerequisites
1511

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.
12+
- You must have JavaScript detections enabled on your zone.
13+
- 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.
14+
- You must not run this field on websocket endpoints.
15+
- You must use the field in a custom rules expression that expects only browser traffic.
16+
- The action should always be a managed challenge in case a legitimate user has not received the challenge for network or browser reasons.
17+
- The path specified in the rule builder should never be the first HTML page a user visits when browsing your site.
2418

2519
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.
2620

27-
```js title="Example with Workers"
21+
```js title="Example"
2822
"botManagement": {
29-
"jsDetection":
30-
31-
{ "passed": false }
32-
,
33-
},
34-
```
23+
"jsDetection": {
24+
"passed": false
25+
}
26+
}
27+
```
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 are 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 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. 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 the JavaScript Detection 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 with 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+
:::warning
25+
Enforcement against bots do **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)