You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
`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`.
29
73
30
74
## Limitations
31
75
32
76
### If you enabled Bot Management before June 2020
33
77
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.
35
85
36
86
### If you have a Content Security Policy (CSP)
37
87
38
88
<Render file="javascript-detections-csp" />
39
89
40
90
:::caution[Warning]
41
91
42
-
JavaScript detections are not supported with `nonce` set via `<meta>` tags.
92
+
JavaScript Detections is not supported with `nonce` set via `<meta>` tags.
Challenge Pages and Turnstile rely on the same underlying mechanism to issue Challenges to your website or application's visitors.
21
21
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.
23
23
24
24
---
25
25
@@ -29,7 +29,7 @@ Refer to the following pages for more information on the different challenge typ
Copy file name to clipboardExpand all lines: src/content/partials/cloudflare-challenges/javascript-detections-csp.mdx
+7-4Lines changed: 7 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,8 +5,11 @@
5
5
6
6
import { GlossaryTooltip } from"~/components"
7
7
8
-
If you have a <GlossaryTooltipterm="content security policy (CSP)">Content Security Policy (CSP)</GlossaryTooltip>, you need to take additional steps to implement JavaScript detections:
8
+
If you have a <GlossaryTooltipterm="content security policy (CSP)">Content Security Policy (CSP)</GlossaryTooltip>, you need to take additional steps to implement JavaScript Detections:
9
9
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.
Copy file name to clipboardExpand all lines: src/content/partials/cloudflare-challenges/javascript-detections-definition.mdx
+2-8Lines changed: 2 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,12 +4,6 @@
4
4
5
5
import { Markdown } from"~/components"
6
6
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.
8
8
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.
Copy file name to clipboardExpand all lines: src/content/partials/cloudflare-challenges/javascript-detections-implementation.mdx
+26-21Lines changed: 26 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,32 +3,37 @@
3
3
4
4
---
5
5
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";
7
7
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/)).
9
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).
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).
13
11
14
12
### Prerequisites
15
13
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.
24
20
25
21
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.
26
22
27
-
```js title="Example with Workers"
28
-
"botManagement": {
29
-
"jsDetection":
30
-
31
-
{ "passed":false }
32
-
,
33
-
},
34
-
```
23
+
<Tabs>
24
+
<TabItemlabel="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)
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