Skip to content

Commit 79d1aa7

Browse files
dariyaanisdariyaanis
authored andcommitted
fix: support user agent for Turnstile Challenge
1 parent fe318e3 commit 79d1aa7

3 files changed

Lines changed: 45 additions & 10 deletions

File tree

‎README.md‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,23 +330,29 @@ that the target page expects in `cf-turnstile-response`.
330330
| `action` | no | Value of the widget's `data-action` attribute, if set. |
331331
| `data` | no | Custom payload from the widget's `data-cdata` attribute, if set. |
332332
| `pagedata` | no | Value of the `chlPageData` parameter, needed for some Cloudflare challenge pages beyond the basic widget. |
333-
| `userAgent` | no | User-Agent to solve with -- the returned token is tied to it, submit with the same one. |
333+
| `userAgent` | no | Current browser User-Agent. Pass it for Cloudflare Challenge pages; it is not needed for a standalone widget. |
334334

335335
**Response:** `token` -- submit as `cf-turnstile-response`. For Cloudflare
336-
Challenge pages, the solution also includes `userAgent`; use that User-Agent
337-
when submitting the token. Pass `action`, `data`, and `pagedata` when the page
338-
provides them. The API field is spelled `pagedata`, all lowercase.
336+
Challenge pages, pass `action`, `data`, `pagedata`, and the current browser
337+
`userAgent` in the task. The solution also contains `userAgent`; switch the
338+
browser or HTTP client to this returned value before invoking the callback with
339+
the token. The API field is spelled `pagedata`, all lowercase.
339340

340341
```python
341342
from captcha_solver_api import CaptchaClient
342343
from captcha_solver_api.tasks import TurnstileTaskProxyless
343344
client = CaptchaClient("your_api_key")
344345
task = TurnstileTaskProxyless(
345346
websiteURL="https://example.com/login",
346-
websiteKey="YOUR_WEBSITE_KEY"
347+
websiteKey="YOUR_WEBSITE_KEY",
348+
action="managed",
349+
data="INTERCEPTED_CDATA",
350+
pagedata="INTERCEPTED_CHL_PAGE_DATA",
351+
userAgent="CURRENT_BROWSER_USER_AGENT",
347352
)
348353
result = client.solve(task)
349354
print(result["token"])
355+
print(result.get("userAgent"))
350356
```
351357

352358
With proxy, use `TurnstileTask` (same proxy fields as reCAPTCHA v2).
@@ -712,6 +718,7 @@ Full API reference: https://captcha-solver.com/en/docs/captcha-types
712718

713719
## Useful Links
714720

721+
- [Cloudflare Turnstile Puppeteer Demo](https://github.com/captcha-solver-api/cloudflare-turnstile-puppeteer-demo) — a working browser automation example for Cloudflare Turnstile.
715722
- [How to Automate Tencent CAPTCHA](https://captcha-solver.com/en/blog/how-to-automate-tencent-captcha) — a step-by-step guide with Python and JavaScript SDK examples.
716723

717724
## License

‎captcha_solver_api/tasks.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,14 @@ class TurnstileTaskProxyless(BaseTask):
303303
challenge pages beyond the basic widget. Named lowercase (not `pageData`)
304304
to match the API field name exactly -- Cloudflare-specific fields are
305305
the one place this API doesn't camelCase.
306-
userAgent: User-Agent to solve with. The returned token is tied to it --
307-
submit it with the same User-Agent.
306+
userAgent: User-Agent of the browser that loaded the challenge. Pass it
307+
together with `action`, `data`, and `pagedata` for Cloudflare
308+
Challenge pages. It is not needed for a standalone widget.
308309
309310
Returns (`solution` from `solve()`):
310311
`token` -- the value to submit as `cf-turnstile-response`.
312+
`userAgent` -- for Cloudflare Challenge pages, switch the browser or HTTP
313+
client to this returned User-Agent before invoking the callback.
311314
"""
312315

313316
type = "TurnstileTaskProxyless"
@@ -345,10 +348,14 @@ class TurnstileTask(BaseTask, ProxyMixin):
345348
pagedata: Value of the `chlPageData` parameter, needed for some Cloudflare
346349
challenge pages beyond the basic widget. Named lowercase (not `pageData`)
347350
to match the API field name exactly.
348-
userAgent: User-Agent to solve with. The returned token is tied to it.
351+
userAgent: User-Agent of the browser that loaded the challenge. Pass it
352+
for Cloudflare Challenge pages; it is not needed for a standalone
353+
widget.
349354
350355
Returns (`solution` from `solve()`):
351356
`token` -- the value to submit as `cf-turnstile-response`.
357+
`userAgent` -- for Cloudflare Challenge pages, switch the browser or HTTP
358+
client to this returned User-Agent before invoking the callback.
352359
"""
353360

354361
type = "TurnstileTask"

‎tests/sync/test_turnstile.py‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99

1010

1111
class TestTurnstile:
12+
def test_proxyless_includes_challenge_user_agent(self):
13+
task = TurnstileTaskProxyless(
14+
websiteURL="https://example.com",
15+
websiteKey="test_key",
16+
action="managed",
17+
data="cdata",
18+
pagedata="page-data",
19+
userAgent="Mozilla/5.0",
20+
)
21+
22+
assert task.to_dict()["userAgent"] == "Mozilla/5.0"
23+
1224
def test_proxyless_to_dict(self):
1325
task = TurnstileTaskProxyless(
1426
websiteURL="https://example.com",
@@ -25,12 +37,14 @@ def test_task_with_proxy_to_dict(self):
2537
proxyType="http",
2638
proxyAddress="1.2.3.4",
2739
proxyPort=8080,
40+
userAgent="Mozilla/5.0",
2841
)
2942
result = task.to_dict()
3043

3144
assert result["type"] == "TurnstileTask"
3245
assert result["proxyType"] == "http"
3346
assert result["proxyAddress"] == "1.2.3.4"
47+
assert result["userAgent"] == "Mozilla/5.0"
3448

3549
def test_solve(self):
3650
client = CaptchaClient("test_key", polling_interval=0.1)
@@ -39,8 +53,15 @@ def test_solve(self):
3953
with patch.object(client, "_request") as mock_request:
4054
mock_request.side_effect = [
4155
{"errorId": 0, "taskId": 103},
42-
{"errorId": 0, "status": "ready", "solution": {"token": "turnstile_token"}},
56+
{
57+
"errorId": 0,
58+
"status": "ready",
59+
"solution": {
60+
"token": "turnstile_token",
61+
"userAgent": "returned-agent",
62+
},
63+
},
4364
]
4465
result = client.solve(task)
4566

46-
assert result == {"token": "turnstile_token"}
67+
assert result == {"token": "turnstile_token", "userAgent": "returned-agent"}

0 commit comments

Comments
 (0)