Skip to content

feat: captcha feature flag - #7591

Merged
kernelwhisperer merged 4 commits into
release/2026-05-27from
feat/captcha-feature-flag
Jun 2, 2026
Merged

kernelwhisperer merged 4 commits into
release/2026-05-27from
feat/captcha-feature-flag

Conversation

@kernelwhisperer

@kernelwhisperer kernelwhisperer commented Jun 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds a frontend feature flag gate for captcha while the backend /auth/turnstile endpoint is not ready.

When isCaptchaEnabled is missing or false:

  • Turnstile does not render
  • captcha token exchange is not triggered
  • existing orderbook bearer token is cleared
  • stored captcha JWT is cleared

Also adds focused tests for disabled, enabled, and cleanup behavior.

To Test

  1. Verify flag-off behavior
  • With isCaptchaEnabled missing or false, captcha is not rendered in the trade widget
  • No request is made to /auth/turnstile
  • Existing captcha JWT/bearer token state is cleared
  1. Verify flag-on behavior

Background

The captcha backend endpoint is not deployed yet, so captcha must default off and only activate after LaunchDarkly enables isCaptchaEnabled.

Summary by CodeRabbit

  • New Features

    • Introduced a captcha feature flag to control captcha widget visibility and functionality.
  • Documentation

    • Updated captcha documentation to explain feature flag behavior.
  • Tests

    • Added comprehensive test coverage for captcha feature flag functionality.

@kernelwhisperer
kernelwhisperer requested a review from a team June 2, 2026 11:55
@kernelwhisperer kernelwhisperer self-assigned this Jun 2, 2026
@coderabbitai

coderabbitai Bot commented Jun 2, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 147714b4-00b6-48a9-b7cd-69ac47c1f8e0

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Walkthrough

This PR adds an isCaptchaEnabled feature flag to conditionally gate Captcha functionality. When the flag is missing or false, the widget is disabled, token exchange is blocked, and stored JWT state is cleared. The implementation includes multiple effects for state cleanup and an updated render condition, with comprehensive test coverage validating all flag states.

Changes

Captcha Feature Flag Gating

Layer / File(s) Summary
Feature flag integration and state management
apps/cowswap-frontend/src/modules/captcha/containers/CaptchaWidget.container.tsx
CaptchaWidget reads isCaptchaEnabled from featureFlagsAtom, gates initial captcha setup with early return when disabled, clears bearer token and stored JWT when flag is toggled off, and prevents Turnstile rendering when captcha is disabled.
Comprehensive test coverage for feature flag behavior
apps/cowswap-frontend/src/modules/captcha/containers/CaptchaWidget.container.test.tsx
Test suite verifies Turnstile and token exchange are disabled when feature flag is missing or false, confirms state cleanup (JWT cleared and bearer token reset to null) when flag toggles off, and validates normal flow when flag is enabled.
Feature flag documentation
apps/cowswap-frontend/src/modules/captcha/README.md
README documents the isCaptchaEnabled flag behavior: widget disabling, token exchange blocking, and JWT clearing when the flag is missing or false.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • cowprotocol/cowswap#7030: Both PRs center on the Captcha/Turnstile flow: the retrieved PR adds the Turnstile token widget/state and injects the token into quote requests, while the main PR gates whether Turnstile and the subsequent token/JWT exchange behavior run via isCaptchaEnabled.

Suggested reviewers

  • alfetopito

Poem

🐰 A flag to guard the captcha gate,
When enabled, tokens integrate,
But disabled, we clean and reset,
Bearer tokens—null, you bet!
Tests confirm each branch's fate. 🔐

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat: captcha feature flag' clearly and concisely summarizes the main change: adding a feature flag for controlling captcha functionality.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed PR description covers all required template sections: Summary explains the feature flag gate, To Test provides clear verification steps with checkboxes, and Background provides context on the deployment status.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/captcha-feature-flag

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 and usage tips.

@vercel

vercel Bot commented Jun 2, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
cowfi Building Building Preview Jun 2, 2026 3:13pm
explorer-dev Building Building Preview Jun 2, 2026 3:13pm
storybook Building Building Preview Jun 2, 2026 3:13pm
swap-dev Building Building Preview Jun 2, 2026 3:13pm
widget-configurator Building Building Preview Jun 2, 2026 3:13pm
2 Skipped Deployments
Project Deployment Actions Updated (UTC)
cosmos Ignored Ignored Jun 2, 2026 3:13pm
sdk-tools Ignored Ignored Preview Jun 2, 2026 3:13pm

Request Review

@kernelwhisperer

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 2, 2026 •

Copy link
Copy Markdown

Deploying swap-dev with  Cloudflare Pages  Cloudflare Pages

Latest commit: 00f6b65
Status: ✅  Deploy successful!
Preview URL: https://7cb774c9.swap-dev-5u6.pages.dev
Branch Preview URL: https://feat-captcha-feature-flag.swap-dev-5u6.pages.dev

View logs

@vercel
vercel Bot temporarily deployed to Preview – storybook June 2, 2026 12:00 Inactive
@vercel
vercel Bot temporarily deployed to Preview – widget-configurator June 2, 2026 12:00 Inactive
@vercel
vercel Bot temporarily deployed to Preview – explorer-dev June 2, 2026 12:00 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.

🧹 Nitpick comments (1)
apps/cowswap-frontend/src/modules/captcha/containers/CaptchaWidget.container.test.tsx (1)

73-88: ⚡ Quick win

Cover stale JWT cleanup on initial disabled render.

These cases only assert that Turnstile stays hidden. The new contract is stronger: when the flag is missing or false, a previously stored captcha JWT should also be cleared on first render. Without seeding captchaJwtAtom here, a startup regression in that cleanup path would slip through.

🤖 Prompt for 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.

In
`@apps/cowswap-frontend/src/modules/captcha/containers/CaptchaWidget.container.test.tsx`
around lines 73 - 88, The tests for initial disabled/missing captcha flag don't
seed captchaJwtAtom so they miss verifying JWT cleanup; update both test cases
(the one using renderWithStore() and the one that creates a store and sets
featureFlagsAtom) to pre-seed captchaJwtAtom with a non-null token (e.g.,
set(captchaJwtAtom, 'seeded-token') on the store or via createStore helper)
before calling renderWithStore, then assert that the cleanup occurs by expecting
setBearerTokenMock toHaveBeenCalledWith(null) (and/or that captchaJwtAtom is
cleared) after render; reference captchaJwtAtom, renderWithStore, createStore,
featureFlagsAtom, and setBearerTokenMock to locate where to change the tests.
🤖 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.

Nitpick comments:
In
`@apps/cowswap-frontend/src/modules/captcha/containers/CaptchaWidget.container.test.tsx`:
- Around line 73-88: The tests for initial disabled/missing captcha flag don't
seed captchaJwtAtom so they miss verifying JWT cleanup; update both test cases
(the one using renderWithStore() and the one that creates a store and sets
featureFlagsAtom) to pre-seed captchaJwtAtom with a non-null token (e.g.,
set(captchaJwtAtom, 'seeded-token') on the store or via createStore helper)
before calling renderWithStore, then assert that the cleanup occurs by expecting
setBearerTokenMock toHaveBeenCalledWith(null) (and/or that captchaJwtAtom is
cleared) after render; reference captchaJwtAtom, renderWithStore, createStore,
featureFlagsAtom, and setBearerTokenMock to locate where to change the tests.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: c10f35cd-5511-402e-9291-bfe2a5d6c4bc

📥 Commits

Reviewing files that changed from the base of the PR and between 804ca02 and 3e5d783.

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

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 2, 2026 •

Copy link
Copy Markdown

Deploying explorer-dev with  Cloudflare Pages  Cloudflare Pages

Latest commit: 00f6b65
Status: ✅  Deploy successful!
Preview URL: https://fce7914d.explorer-dev-dxz.pages.dev
Branch Preview URL: https://feat-captcha-feature-flag.explorer-dev-dxz.pages.dev

View logs

@vercel
vercel Bot temporarily deployed to Preview – cowfi June 2, 2026 12:01 Inactive
@vercel
vercel Bot temporarily deployed to Preview – swap-dev June 2, 2026 12:02 Inactive
@vercel
vercel Bot temporarily deployed to Preview – storybook June 2, 2026 12:05 Inactive
@vercel
vercel Bot temporarily deployed to Preview – widget-configurator June 2, 2026 12:05 Inactive
@vercel
vercel Bot temporarily deployed to Preview – explorer-dev June 2, 2026 12:06 Inactive
@vercel
vercel Bot temporarily deployed to Preview – cowfi June 2, 2026 12:06 Inactive
@vercel
vercel Bot temporarily deployed to Preview – swap-dev June 2, 2026 12:07 Inactive
import { useCaptchaDebugControls } from '../hooks/useCaptchaDebugControls'
import { logCaptcha } from '../logger'

export function CaptchaWidget(): ReactNode {

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.

Maybe it will be simplier:

{isCaptchaEnabled && <CaptchaWidget />}

@fairlighteth
fairlighteth self-requested a review June 2, 2026 13:42

@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 4m28s): gate captcha cleanup on hydrated feature flags

Finding: Gate captcha cleanup on hydrated feature flags

  • Location: apps/cowswap-frontend/src/modules/captcha/containers/CaptchaWidget.container.tsx:47
  • featureFlagsAtom starts as {}, and FeatureFlagsUpdater copies LaunchDarkly flags into it from a layout effect.
  • On initial app mount, this cleanup effect treats the not-yet-hydrated flag as disabled. If LaunchDarkly already has isCaptchaEnabled=true and a valid captcha JWT is persisted, the widget can clear the bearer token/JWT before it observes the real enabled flag.
  • Impact: users with captcha enabled can lose a valid stored captcha token on reload and be forced through Turnstile again.

Suggested fix

  • Distinguish “flags not hydrated yet” from “flag missing/false”. For example, read featureFlagsHydratedAtom and skip destructive cleanup until hydration is complete; once hydrated, missing/false can still clear as intended.
  • Add coverage for a startup case where a valid captchaJwtAtom exists, flags are not hydrated on first render, then hydrate to { isCaptchaEnabled: true }; the JWT/bearer token should not be cleared.
Review scope and related context

Existing comments already cover:

  • CodeRabbit’s nit about adding initial disabled/missing-flag cleanup coverage in the test file.
  • @shoom3301’s open suggestion to simplify render gating; this finding is separate because moving the gate outside the widget must still preserve the JWT/bearer cleanup behavior.

I did not run the targeted Jest test because this checkout has no node_modules installed.

🤖 Prompt for AI agents
Verify this finding against current code. Fix only if still valid, keep the change minimal, and validate with targeted tests.

Context:
- CaptchaWidget reads featureFlagsAtom directly at apps/cowswap-frontend/src/modules/captcha/containers/CaptchaWidget.container.tsx:20.
- featureFlagsAtom defaults to {} and featureFlagsHydratedAtom defaults to false in apps/cowswap-frontend/src/common/state/featureFlagsState.ts.
- FeatureFlagsUpdater hydrates the atom in useLayoutEffect.
- The cleanup effect at CaptchaWidget.container.tsx:47 currently treats undefined isCaptchaEnabled as disabled and clears bearer/JWT state.

Expected fix/test:
- Do not clear captchaJwtAtom or bearer token while feature flags are still unhydrated.
- Still clear them once flags are hydrated and isCaptchaEnabled is missing or false.
</details>
</details>

useEffect(() => {
if (isCaptchaEnabled) return

exchangeRequestIdRef.current += 1

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.

What is this tracking exactly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The idea is to discard stale requests (for exchanging a turnstile token to a jwt), so we assign an incrementing integer which we compare when the result comes back.
This protects against race conditions, but... if we remove useCaptchaDebugControls we have little reason to keep it.

@alfetopito alfetopito 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.

One question about a ref whose name is confusing. Code and behaviour looks good otherwise.

@kernelwhisperer

Copy link
Copy Markdown
Contributor Author

⚠️ AI Review (Codex GPT-5, worked 4m28s): gate captcha cleanup on hydrated feature flags
Finding: Gate captcha cleanup on hydrated feature flags

  • Location: apps/cowswap-frontend/src/modules/captcha/containers/CaptchaWidget.container.tsx:47
  • featureFlagsAtom starts as {}, and FeatureFlagsUpdater copies LaunchDarkly flags into it from a layout effect.
  • On initial app mount, this cleanup effect treats the not-yet-hydrated flag as disabled. If LaunchDarkly already has isCaptchaEnabled=true and a valid captcha JWT is persisted, the widget can clear the bearer token/JWT before it observes the real enabled flag.
  • Impact: users with captcha enabled can lose a valid stored captcha token on reload and be forced through Turnstile again.

Suggested fix

  • Distinguish “flags not hydrated yet” from “flag missing/false”. For example, read featureFlagsHydratedAtom and skip destructive cleanup until hydration is complete; once hydrated, missing/false can still clear as intended.
  • Add coverage for a startup case where a valid captchaJwtAtom exists, flags are not hydrated on first render, then hydrate to { isCaptchaEnabled: true }; the JWT/bearer token should not be cleared.

Review scope and related context
Existing comments already cover:

  • CodeRabbit’s nit about adding initial disabled/missing-flag cleanup coverage in the test file.
  • @shoom3301’s open suggestion to simplify render gating; this finding is separate because moving the gate outside the widget must still preserve the JWT/bearer cleanup behavior.

I did not run the targeted Jest test because this checkout has no node_modules installed.

🤖 Prompt for AI agents

Fixed in c0cf8e8

@kernelwhisperer
kernelwhisperer merged commit 1ad26ea into release/2026-05-27 Jun 2, 2026
8 of 14 checks passed
@kernelwhisperer
kernelwhisperer deleted the feat/captcha-feature-flag branch June 2, 2026 15:13
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 2, 2026
@vercel
vercel Bot temporarily deployed to Preview – storybook June 2, 2026 15:14 Inactive
@vercel
vercel Bot temporarily deployed to Preview – widget-configurator June 2, 2026 15:14 Inactive
@vercel
vercel Bot temporarily deployed to Preview – explorer-dev June 2, 2026 15:14 Inactive
@vercel
vercel Bot temporarily deployed to Preview – cowfi June 2, 2026 15:14 Inactive
@vercel
vercel Bot temporarily deployed to Preview – swap-dev June 2, 2026 15:15 Inactive
@fairlighteth

Copy link
Copy Markdown
Contributor
⚠️ AI Review (Codex GPT-5, worked 38m45s): follow-up still incomplete for hydrated missing flag

Rechecked

  • Author response: @kernelwhisperer said the prior hydrated feature flag cleanup finding was fixed in c0cf8e885c5e2548f86c08bbe395ff364dd8ac46.
  • Code path: apps/cowswap-frontend/src/modules/captcha/containers/CaptchaWidget.container.tsx:47.
  • Test coverage: CaptchaWidget.container.test.tsx:73 now asserts a missing flag preserves the stored JWT.

Result: Partially fixed. The c0cf change prevents the unhydrated {} state from clearing a valid JWT before isCaptchaEnabled=true arrives, but a hydrated flag set that omits isCaptchaEnabled no longer clears captchaJwtAtom or calls setBearerToken(null).

Suggested next step

  • Read featureFlagsHydratedAtom, skip destructive cleanup while hydration is false, then clear once hydrated when isCaptchaEnabled is missing or false.
  • Add focused tests for unhydrated -> enabled preserving JWT, hydrated missing clearing, and explicit false clearing.
Review scope and related context

Existing comments already cover:

  • CodeRabbit’s earlier test-coverage note around initial disabled/missing-flag cleanup.
  • @shoom3301’s render-gating suggestion and @alfetopito’s ref naming question.

This follow-up only checks the author’s c0cf8e8 fix against the prior AI review and current PR contract.

🤖 Prompt for AI agents
Verify this follow-up finding against current code. Fix only if still valid, keep the change minimal, and validate with targeted tests.

Context:
- CaptchaWidget currently clears only when isCaptchaEnabled === false.
- featureFlagsAtom defaults to {}, while featureFlagsHydratedAtom defaults to false and is set true by FeatureFlagsUpdater.
- The PR contract says missing or false disables captcha and clears stored JWT/bearer state.

Expected fix/test:
- Skip cleanup while featureFlagsHydratedAtom is false.
- Once hydrated, clear captchaJwtAtom and setBearerToken(null) when isCaptchaEnabled is missing or false.
- Preserve JWT through an unhydrated initial render followed by isCaptchaEnabled=true.
</details>
</details>

This branch was previously deployed

5 inactive deployments
Preview – swap-dev — c0cf8e88 Deployed Jun 2, 2026 by vercel[bot]
Preview – cowfi — c0cf8e88 Deployed Jun 2, 2026 by vercel[bot]
Preview – explorer-dev — c0cf8e88 Deployed Jun 2, 2026 by vercel[bot]
Preview – widget-configurator — c0cf8e88 Deployed Jun 2, 2026 by vercel[bot]
Preview – storybook — c0cf8e88 Deployed Jun 2, 2026 by vercel[bot]
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.

4 participants