Skip to content

fix: hide captcha if BE is down - #7812

Merged
kernelwhisperer merged 4 commits into
developfrom
fix/hide-captcha-if-be-is-down
Jul 27, 2026
Merged

kernelwhisperer merged 4 commits into
developfrom
fix/hide-captcha-if-be-is-down

Conversation

@kernelwhisperer

@kernelwhisperer kernelwhisperer commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #7801 and #7768

If the our BE endpoint is down, it will simply hide the CF turnstile widget.

To Test

It's difficult to test, you need to bring the endpoint down or try it in an environment where it's not been deployed like prod.

Background

Why not a retry button?

image

Instead of a retry button we can do exponential backoff, why should the user click a button for us to retry.

Still, I think it's premature to optimize for this edge case. When this failure happens it will be caught by Sentry and we should react quickly to bring the endpoint back up.

Summary by CodeRabbit

  • Bug Fixes
    • Improved CAPTCHA verification handling by recording token→JWT exchange failures in a dedicated error state so the UI stays consistent after failed exchanges.
    • The CAPTCHA widget now only appears when CAPTCHA is enabled, properly configured, no JWT is already present, and there’s no recorded exchange error.
    • Normalized exchange errors are logged, and stale exchange results no longer produce the previous “stale” warning behavior.

@kernelwhisperer
kernelwhisperer requested a review from a team July 6, 2026 12:25
@kernelwhisperer kernelwhisperer self-assigned this Jul 6, 2026
@vercel

vercel Bot commented Jul 6, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
cowfi Ready Ready Preview Jul 27, 2026 4:55pm
explorer-dev Ready Ready Preview Jul 27, 2026 4:55pm
storybook Ready Ready Preview Jul 27, 2026 4:55pm
swap-dev Ready Ready Preview Jul 27, 2026 4:55pm
widget-configurator Ready Ready Preview Jul 27, 2026 4:55pm
2 Skipped Deployments
Project Deployment Actions Updated (UTC)
cosmos Ignored Ignored Jul 27, 2026 4:55pm
sdk-tools Ignored Ignored Preview Jul 27, 2026 4:55pm

Request Review

@vercel
vercel Bot temporarily deployed to Preview – explorer-dev July 6, 2026 12:25 Inactive
@vercel
vercel Bot temporarily deployed to Preview – swap-dev July 6, 2026 12:26 Inactive
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 6, 2026 •

Copy link
Copy Markdown

Deploying swap-dev with  Cloudflare Pages  Cloudflare Pages

Latest commit: 302ed98
Status: ✅  Deploy successful!
Preview URL: https://3044f8b9.swap-dev-5u6.pages.dev
Branch Preview URL: https://fix-hide-captcha-if-be-is-do.swap-dev-5u6.pages.dev

View logs

@coderabbitai

coderabbitai Bot commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: db37bf78-61d3-4dd6-9d27-c4462beec27d

📥 Commits

Reviewing files that changed from the base of the PR and between ee81458 and 6079f64.

📒 Files selected for processing (2)
  • apps/cowswap-frontend/src/modules/captcha/containers/CaptchaWidget.container.tsx
  • apps/cowswap-frontend/src/modules/captcha/state/captchaErrorAtom.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • apps/cowswap-frontend/src/modules/captcha/state/captchaErrorAtom.ts
  • apps/cowswap-frontend/src/modules/captcha/containers/CaptchaWidget.container.tsx

Walkthrough

CaptchaWidget records normalized JWT exchange failures in captchaError, suppresses Turnstile rendering after failure, and omits stale-result warnings.

Changes

Captcha Error State Handling

Layer / File(s) Summary
Captcha error atom and widget handling
apps/cowswap-frontend/src/modules/captcha/state/captchaErrorAtom.ts, apps/cowswap-frontend/src/modules/captcha/containers/CaptchaWidget.container.tsx
Adds the typed error atom, reads it in CaptchaWidget, gates Turnstile rendering on its absence, and stores normalized JWT exchange failures while logging the failure cause.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: alfetopito

Poem

A rabbit caught an error bright,
And hid the widget out of sight.
No false success can now appear,
Just honest state, both calm and clear. 🐰

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR hides the captcha on exchange failure, but issue #7801 requires showing a failed state instead. Update the captcha flow to render an explicit failed state on JWT exchange failure rather than suppressing the widget.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title matches the main change: hiding the captcha widget when the backend is unavailable.
Description check ✅ Passed The description follows the template with Summary, To Test, Background, and linked issue references.
Out of Scope Changes check ✅ Passed The changes stay within the captcha failure-handling scope and add only the supporting error atom.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/hide-captcha-if-be-is-down

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@kernelwhisperer
kernelwhisperer requested a review from a team July 6, 2026 12:31

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@apps/cowswap-frontend/src/modules/captcha/containers/CaptchaWidget.container.tsx`:
- Line 20: The CaptchaWidget.container component leaves captchaError stuck after
a failed JWT exchange, preventing Turnstile from mounting again. Update the
retry/reset flow in CaptchaWidget.container so captchaError is cleared whenever
captchaJwt is reset or a new challenge is allowed, and also ensure the
success/error handlers that trigger another attempt can reset this state. Use
the existing useState-managed captchaError and related captchaJwt reset logic as
the place to fix it.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 385459c7-5f0f-4216-b009-b713a32f13ee

📥 Commits

Reviewing files that changed from the base of the PR and between afcc4dc and cccec91.

📒 Files selected for processing (1)
  • apps/cowswap-frontend/src/modules/captcha/containers/CaptchaWidget.container.tsx

@elena-zh elena-zh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @kernelwhisperer , but it still shows 'success' state when token is failed

Image

For me it is weird. Moreover, then the question comes: why it reappears again and again on the form switching/page refresh/qupte update?

@fairlighteth fairlighteth left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ AI Review (Codex GPT-5, worked 5m): add rejected captcha exchange coverage

Finding: [NON-BLOCKING] Cover rejected JWT exchange behavior

  • Location: apps/cowswap-frontend/src/modules/captcha/containers/CaptchaWidget.container.tsx:109
  • This PR changes the linked failure path by latching captchaError after exchangeTurnstileToken() rejects, hiding Turnstile and leaving no JWT. The existing CaptchaWidget.container.test.tsx cases cover disabled/enabled/stored-JWT paths, but none call onSuccess or assert the rejected exchange path.
  • Without a focused test, a future change can reintroduce the stale success UI from #7801 or accidentally change the intentional hide behavior while unit CI still passes.

Suggested fix

  • Add a component test that mocks Turnstile enough to trigger onSuccess, makes exchangeTurnstileToken reject, and asserts the widget is hidden after the rejection and captchaJwtAtom remains null.
  • If the permanent per-mount hide is the intended product behavior, encode that in the test name so the CodeRabbit thread decision is documented in code.
Review scope and related context
  • I’m not repeating the existing CodeRabbit thread about captchaError never resetting; current code still behaves that way, but the author marked it intentional and CodeRabbit withdrew.
  • Lint, unit test, typecheck, and Agent Harness checks are green on the PR. Cypress is currently red in swap.test.ts / swapMod.test.ts waiting for #do-trade-button; I didn’t tie that failure to this captcha diff, but it still needs resolution or rerun before merge.
🤖 Prompt for AI agents
Verify the missing test against current PR code. Add only focused coverage for the CaptchaWidget JWT-exchange rejection path.

Context:
- apps/cowswap-frontend/src/modules/captcha/containers/CaptchaWidget.container.tsx:109
- onSuccess awaits exchangeTurnstileToken(token); on rejection it normalizes the error, sets captchaError, logs it, and sets captchaJwtAtom to null.
- Existing apps/cowswap-frontend/src/modules/captcha/containers/CaptchaWidget.container.test.tsx tests do not invoke onSuccess or the rejected exchange path.

Expected coverage:
- Mock Turnstile so the test can trigger onSuccess.
- Mock exchangeTurnstileToken to reject.
- Assert the widget is hidden after the rejection and captchaJwtAtom remains null.

Generated using the pr-review skill from the CoW Protocol skills repo.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 7, 2026 •

Copy link
Copy Markdown

Deploying explorer-dev with  Cloudflare Pages  Cloudflare Pages

Latest commit: 302ed98
Status: ✅  Deploy successful!
Preview URL: https://6b425161.explorer-dev-dxz.pages.dev
Branch Preview URL: https://fix-hide-captcha-if-be-is-do.explorer-dev-dxz.pages.dev

View logs

@vercel
vercel Bot temporarily deployed to Preview – storybook July 7, 2026 13:39 Inactive
@vercel
vercel Bot temporarily deployed to Preview – explorer-dev July 7, 2026 13:40 Inactive
@vercel
vercel Bot temporarily deployed to Preview – widget-configurator July 7, 2026 13:40 Inactive
@vercel
vercel Bot temporarily deployed to Preview – cowfi July 7, 2026 13:41 Inactive
@vercel
vercel Bot temporarily deployed to Preview – swap-dev July 7, 2026 13:43 Inactive

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@apps/cowswap-frontend/src/modules/captcha/state/captchaErrorAtom.ts`:
- Line 3: Add a reset path for captchaErrorAtom so the Turnstile can reappear
after a retry. Update the captcha module around captchaErrorAtom and the
CaptchaWidget flow to expose a way to clear the stored Error before
re-attempting the JWT exchange, and make sure the retry path invokes that reset
instead of leaving the atom set indefinitely.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 3ccbe980-2f3e-4227-8d3e-aa2a22f9e975

📥 Commits

Reviewing files that changed from the base of the PR and between cccec91 and 581d9c0.

📒 Files selected for processing (2)
  • apps/cowswap-frontend/src/modules/captcha/containers/CaptchaWidget.container.tsx
  • apps/cowswap-frontend/src/modules/captcha/state/captchaErrorAtom.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/cowswap-frontend/src/modules/captcha/containers/CaptchaWidget.container.tsx

@kernelwhisperer

Copy link
Copy Markdown
Contributor Author

Hey @kernelwhisperer , but it still shows 'success' state when token is failed

Image For me it is weird.

Not much we can do there, because that captcha component is controlled by cloudflare from their POV the challenge succeeded. It's the second step on our side that is failing.

Moreover, then the question comes: why it reappears again and again on the form switching/page refresh/qupte update?
I pushed a fixed, it should not reappear when switching pages, it will stay hidden until a refresh.

@vercel
vercel Bot temporarily deployed to Preview – storybook July 7, 2026 15:03 Inactive
@elena-zh

elena-zh commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Hey @kernelwhisperer , thank you, now it looks better. However, 'success' state still confuses me a bit.
Let's wait for @alfetopito opinion here.

@vercel
vercel Bot temporarily deployed to Preview – widget-configurator July 7, 2026 15:03 Inactive
@vercel
vercel Bot temporarily deployed to Preview – cowfi July 7, 2026 15:04 Inactive
@vercel
vercel Bot temporarily deployed to Preview – explorer-dev July 7, 2026 15:05 Inactive
@vercel
vercel Bot temporarily deployed to Preview – swap-dev July 7, 2026 15:08 Inactive
@vercel
vercel Bot temporarily deployed to Preview – cowfi July 9, 2026 14:15 Inactive
@vercel
vercel Bot temporarily deployed to Preview – cowfi July 9, 2026 14:48 Inactive

@azebuado azebuado left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reviewing the proposal and the issues raised by Elena, I think this is a good enough compromise.

  1. We don't expect it to be down very often, making it an unlikely edge case. We were seeing this because backend was not properly setup.
  2. Hiding it should be ok when it fails because as Anuj confirmed, when it's down it just not block anything, so the fact that the captcha shows success kind of is true in either case (if I understood it correctly).

@kernelwhisperer
kernelwhisperer force-pushed the fix/hide-captcha-if-be-is-down branch from 302ed98 to ee81458 Compare July 22, 2026 15:07
@kernelwhisperer
kernelwhisperer force-pushed the fix/hide-captcha-if-be-is-down branch from ee81458 to 6079f64 Compare July 24, 2026 07:46
@azebuado
azebuado requested a review from elena-zh July 24, 2026 17:01
@kernelwhisperer
kernelwhisperer enabled auto-merge (squash) July 27, 2026 16:52
@kernelwhisperer
kernelwhisperer merged commit 5802b03 into develop Jul 27, 2026
19 checks passed
@kernelwhisperer
kernelwhisperer deleted the fix/hide-captcha-if-be-is-down branch July 27, 2026 16:57
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 27, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Captcha shows a success state when JWT token is failed

4 participants