-
Notifications
You must be signed in to change notification settings - Fork 468
feat(js,shared): add Prosopo Procaptcha as a CAPTCHA provider #8944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
forgetso
wants to merge
8
commits into
clerk:main
Choose a base branch
from
forgetso:feat/prosopo-captcha-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a965ed8
feat(js,shared): add Prosopo Procaptcha as a CAPTCHA provider
forgetso 547f04f
refactor(js): extract shared captcha container resolver + expand tests
forgetso b784d1e
fix(js): propagate Procaptcha error-callback message
forgetso 43802a3
Merge branch 'main' into feat/prosopo-captcha-provider
forgetso 3629098
fix(js): address CodeRabbit review on containerResolver
forgetso 6ad4080
Merge branch 'main' into feat/prosopo-captcha-provider
forgetso 0e03a3f
Merge branch 'main' into feat/prosopo-captcha-provider
forgetso 721452b
Merge branch 'main' into feat/prosopo-captcha-provider
forgetso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@clerk/clerk-js': minor | ||
| '@clerk/shared': minor | ||
| --- | ||
|
|
||
| Add Prosopo Procaptcha as an alternative CAPTCHA provider alongside Cloudflare Turnstile. When `displayConfig.captchaProvider` is `'prosopo'`, clerk-js now loads the Procaptcha bundle from `js.prosopo.io` and renders an invisible or smart widget into the same containers Turnstile uses today (the auto-generated invisible div and the user-supplied `#clerk-captcha` element). Turnstile remains the default and the existing flow is unchanged for instances that have not opted into Prosopo. The public `CaptchaProvider` type now accepts `'turnstile' | 'prosopo'`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
packages/clerk-js/src/utils/captcha/__tests__/CaptchaChallenge.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| vi.mock('../getCaptchaToken', () => ({ | ||
| getCaptchaToken: vi.fn(async () => ({ captchaToken: 'mock_token', captchaWidgetType: 'invisible' })), | ||
| })); | ||
|
|
||
| import { CaptchaChallenge } from '../CaptchaChallenge'; | ||
| import { getCaptchaToken } from '../getCaptchaToken'; | ||
|
|
||
| const makeClerk = (provider: 'turnstile' | 'prosopo') => | ||
| ({ | ||
| isStandardBrowser: true, | ||
| __internal_environment: { | ||
| displayConfig: { | ||
| captchaProvider: provider, | ||
| captchaPublicKey: 'visible-key', | ||
| captchaPublicKeyInvisible: 'invisible-key', | ||
| captchaWidgetType: 'smart' as const, | ||
| }, | ||
| userSettings: { signUp: { captcha_enabled: true } }, | ||
| }, | ||
| __internal_getOption: () => undefined, | ||
| }) as any; | ||
|
|
||
| describe('CaptchaChallenge propagates displayConfig.captchaProvider', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('invisible() forwards "prosopo" when configured', async () => { | ||
| const challenge = new CaptchaChallenge(makeClerk('prosopo')); | ||
|
|
||
| await challenge.invisible(); | ||
|
|
||
| expect(getCaptchaToken).toHaveBeenCalledTimes(1); | ||
| expect((getCaptchaToken as any).mock.calls[0][0]).toMatchObject({ captchaProvider: 'prosopo' }); | ||
| }); | ||
|
|
||
| it('invisible() forwards "turnstile" when configured (default)', async () => { | ||
| const challenge = new CaptchaChallenge(makeClerk('turnstile')); | ||
|
|
||
| await challenge.invisible(); | ||
|
|
||
| expect(getCaptchaToken).toHaveBeenCalledTimes(1); | ||
| expect((getCaptchaToken as any).mock.calls[0][0]).toMatchObject({ captchaProvider: 'turnstile' }); | ||
| }); | ||
|
|
||
| it('managedOrInvisible() forwards the configured provider', async () => { | ||
| const challenge = new CaptchaChallenge(makeClerk('prosopo')); | ||
|
|
||
| await challenge.managedOrInvisible({ action: 'verify' }); | ||
|
|
||
| expect(getCaptchaToken).toHaveBeenCalledTimes(1); | ||
| expect((getCaptchaToken as any).mock.calls[0][0]).toMatchObject({ captchaProvider: 'prosopo' }); | ||
| }); | ||
| }); |
70 changes: 70 additions & 0 deletions
70
packages/clerk-js/src/utils/captcha/__tests__/containerResolver.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { CAPTCHA_INVISIBLE_CLASSNAME } from '@clerk/shared/internal/clerk-js/constants'; | ||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| vi.mock('@clerk/shared/dom', () => ({ | ||
| waitForElement: vi.fn(), | ||
| })); | ||
|
|
||
| import { waitForElement } from '@clerk/shared/dom'; | ||
|
|
||
| import { cleanupCaptchaContainer, resolveCaptchaContainer } from '../containerResolver'; | ||
|
|
||
| const baseOpts = { | ||
| captchaProvider: 'turnstile' as const, | ||
| siteKey: 'visible-key', | ||
| invisibleSiteKey: 'invisible-key', | ||
| widgetType: 'invisible' as const, | ||
| }; | ||
|
|
||
| describe('resolveCaptchaContainer', () => { | ||
| afterEach(() => { | ||
| document.body.innerHTML = ''; | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe('invisible flow — per-instance containers', () => { | ||
| it('hands out a unique selector per call so concurrent challenges do not collide', async () => { | ||
| const a = await resolveCaptchaContainer(baseOpts); | ||
| const b = await resolveCaptchaContainer(baseOpts); | ||
|
|
||
| expect(a.containerSelector).not.toBe(b.containerSelector); | ||
| expect(document.querySelectorAll(`.${CAPTCHA_INVISIBLE_CLASSNAME}`)).toHaveLength(2); | ||
| expect(document.querySelector(a.containerSelector)).not.toBeNull(); | ||
| expect(document.querySelector(b.containerSelector)).not.toBeNull(); | ||
| }); | ||
|
|
||
| it('cleanup only removes the resolved instance, leaving concurrent containers intact', async () => { | ||
| const a = await resolveCaptchaContainer(baseOpts); | ||
| const b = await resolveCaptchaContainer(baseOpts); | ||
|
|
||
| cleanupCaptchaContainer('invisible', {}, a.containerSelector); | ||
|
|
||
| expect(document.querySelector(a.containerSelector)).toBeNull(); | ||
| expect(document.querySelector(b.containerSelector)).not.toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('modal flow — timeout', () => { | ||
| it('throws modal_container_not_found when waitForElement does not resolve in time', async () => { | ||
| vi.useFakeTimers(); | ||
| // waitForElement returns a promise that never resolves. | ||
| (waitForElement as any).mockReturnValue(new Promise<Element | null>(() => {})); | ||
|
|
||
| const openModal = vi.fn(async () => undefined); | ||
| const promise = resolveCaptchaContainer({ | ||
| ...baseOpts, | ||
| modalContainerQuerySelector: '#cl-modal-captcha-container', | ||
| modalWrapperQuerySelector: '#cl-modal-captcha-wrapper', | ||
| openModal, | ||
| }); | ||
|
|
||
| const expectation = expect(promise).rejects.toMatchObject({ captchaError: 'modal_container_not_found' }); | ||
|
|
||
| // Advance past the timeout so the Promise.race resolves the null branch. | ||
| await vi.advanceTimersByTimeAsync(5001); | ||
| await expectation; | ||
|
|
||
| vi.useRealTimers(); | ||
| }); | ||
| }); | ||
| }); | ||
45 changes: 45 additions & 0 deletions
45
packages/clerk-js/src/utils/captcha/__tests__/getCaptchaToken.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { getCaptchaToken } from '../getCaptchaToken'; | ||
|
|
||
| vi.mock('../turnstile', () => ({ | ||
| getTurnstileToken: vi.fn(async () => ({ captchaToken: 'turnstile_token', captchaWidgetType: 'invisible' })), | ||
| })); | ||
| vi.mock('../prosopo', () => ({ | ||
| getProcaptchaToken: vi.fn(async () => ({ captchaToken: 'procaptcha_token', captchaWidgetType: 'invisible' })), | ||
| })); | ||
|
|
||
| import { getProcaptchaToken } from '../prosopo'; | ||
| import { getTurnstileToken } from '../turnstile'; | ||
|
|
||
| describe('getCaptchaToken provider routing', () => { | ||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('routes to Procaptcha when captchaProvider is "prosopo"', async () => { | ||
| const result = await getCaptchaToken({ | ||
| captchaProvider: 'prosopo', | ||
| siteKey: 'visible', | ||
| invisibleSiteKey: 'invisible', | ||
| widgetType: 'invisible', | ||
| }); | ||
|
|
||
| expect(getProcaptchaToken).toHaveBeenCalledTimes(1); | ||
| expect(getTurnstileToken).not.toHaveBeenCalled(); | ||
| expect(result).toEqual({ captchaToken: 'procaptcha_token', captchaWidgetType: 'invisible' }); | ||
| }); | ||
|
|
||
| it('routes to Turnstile when captchaProvider is "turnstile"', async () => { | ||
| const result = await getCaptchaToken({ | ||
| captchaProvider: 'turnstile', | ||
| siteKey: 'visible', | ||
| invisibleSiteKey: 'invisible', | ||
| widgetType: 'invisible', | ||
| }); | ||
|
|
||
| expect(getTurnstileToken).toHaveBeenCalledTimes(1); | ||
| expect(getProcaptchaToken).not.toHaveBeenCalled(); | ||
| expect(result).toEqual({ captchaToken: 'turnstile_token', captchaWidgetType: 'invisible' }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: clerk/javascript
Length of output: 5024
🌐 Web query:
Vitest vi.useFakeTimers beforeEach afterEach useRealTimers documentation💡 Result:
In Vitest, you use fake timers to gain control over time-dependent functions like setTimeout, setInterval, and Date, allowing you to run tests instantly instead of waiting for real time to pass [1][2]. Key functions and their usage: vi.useFakeTimers: Enables fake timers [3][2]. It is best practice to call this in a beforeEach hook to ensure a clean state for each test [1][4]. It intercepts calls to global timer APIs and, optionally, other platform methods like nextTick or queueMicrotask [2][5]. vi.useRealTimers: Restores the native timer implementations [3][2]. It is highly recommended to call this in an afterEach hook to avoid leaking mocked timer behavior into subsequent tests, which can cause instability or unexpected test failures [4]. Example Pattern: import { beforeEach, afterEach, describe, it, vi } from 'vitest'; describe('timer tests', => { beforeEach( => { // Enable fake timers before the test runs vi.useFakeTimers; }); afterEach( => { // Restore real timers after each test to prevent side effects vi.useRealTimers; }); it('example test', => { // Test logic here }); }); Important Considerations: - Date Mocking: vi.useFakeTimers also mocks the global Date object [3][2]. If you use vi.setSystemTime to control the date, remember that this state persists unless you manually reset it or restore real timers [6][7]. - Configuration: You can configure which APIs are faked (e.g., nextTick, requestAnimationFrame) by passing an options object to vi.useFakeTimers({ toFake: [...] }) [2][5]. - Cleanup: Always ensure cleanup occurs in afterEach, even if a test fails, to maintain a consistent environment for the rest of your test suite [4].
Citations:
Restore fake timers in
afterEach.This test enables fake timers, but
vi.useRealTimers()only runs after the awaited rejection expectation. If the awaited promise rejects before that line runs, fake timers remain enabled and can affect later tests. Movevi.useRealTimers()into the existingafterEachblock.Proposed fix
afterEach(() => { document.body.innerHTML = ''; + vi.useRealTimers(); vi.restoreAllMocks(); }); ... - vi.useRealTimers();📝 Committable suggestion
🧰 Tools
🪛 ast-grep (0.45.0)
[warning] 20-20: Direct modification of innerHTML or outerHTML properties detected. Modifying these properties with unsanitized user input can lead to XSS vulnerabilities. Use safe alternatives or sanitize content first.
Context: document.body.innerHTML = ''
Note: [CWE-79] Improper Neutralization of Input During Web Page Generation
(dom-content-modification)
🤖 Prompt for AI Agents