From 9daeb70cce90027dbe059b48ddbf1e408cd76d0e Mon Sep 17 00:00:00 2001 From: Rishi Balakrishnan Date: Tue, 18 Aug 2026 16:39:15 -0400 Subject: [PATCH] fix(signup): allow the selected community PDS in the captcha webview The native captcha webview gated navigation on a static host allowlist that only covered Blacksky hosts. Once the community picker let signup target a third-party PDS, the gate page on that PDS was blocked: on iOS the initial load never rendered, and the captcha form post was blocked on both platforms. Derive the allowed host from the same serviceUrl the captcha URL is built from, so any current or future community PDS is covered. An unset or malformed serviceUrl adds nothing and leaves the static list unchanged. --- .../Signup/StepCaptcha/CaptchaWebView.tsx | 37 ++++++++++++++++--- .../__tests__/CaptchaWebView.test.ts | 36 ++++++++++++++++++ 2 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 src/screens/Signup/StepCaptcha/__tests__/CaptchaWebView.test.ts diff --git a/src/screens/Signup/StepCaptcha/CaptchaWebView.tsx b/src/screens/Signup/StepCaptcha/CaptchaWebView.tsx index 58cc745d6..7c4182057 100644 --- a/src/screens/Signup/StepCaptcha/CaptchaWebView.tsx +++ b/src/screens/Signup/StepCaptcha/CaptchaWebView.tsx @@ -1,4 +1,4 @@ -import {useEffect, useMemo, useRef} from 'react' +import {useCallback, useEffect, useMemo, useRef} from 'react' import {WebView, type WebViewNavigation} from 'react-native-webview' import {type ShouldStartLoadRequest} from 'react-native-webview/lib/WebViewTypes' @@ -16,6 +16,25 @@ const ALLOWED_HOSTS = [ 'api2.hcaptcha.com', ] +/** + * The gate page lives on whichever PDS signup is pointed at, so the static list + * can't cover community PDSes. Derive that host from the same `serviceUrl` the + * captcha URL is built from — an unset or malformed value adds nothing, leaving + * the static list as-is. + */ +export function buildAllowedHosts(serviceUrl?: string): string[] { + if (!serviceUrl) return ALLOWED_HOSTS + + try { + const {host} = new URL(serviceUrl) + return ALLOWED_HOSTS.includes(host) + ? ALLOWED_HOSTS + : [...ALLOWED_HOSTS, host] + } catch { + return ALLOWED_HOSTS + } +} + const MIN_DELAY = 3_500 export function CaptchaWebView({ @@ -55,10 +74,18 @@ export function CaptchaWebView({ const wasSuccessful = useRef(false) - const onShouldStartLoadWithRequest = (event: ShouldStartLoadRequest) => { - const urlp = new URL(event.url) - return ALLOWED_HOSTS.includes(urlp.host) - } + const allowedHosts = useMemo( + () => buildAllowedHosts(state?.serviceUrl), + [state?.serviceUrl], + ) + + const onShouldStartLoadWithRequest = useCallback( + (event: ShouldStartLoadRequest) => { + const urlp = new URL(event.url) + return allowedHosts.includes(urlp.host) + }, + [allowedHosts], + ) const onNavigationStateChange = (e: WebViewNavigation) => { if (wasSuccessful.current) return diff --git a/src/screens/Signup/StepCaptcha/__tests__/CaptchaWebView.test.ts b/src/screens/Signup/StepCaptcha/__tests__/CaptchaWebView.test.ts new file mode 100644 index 000000000..e8339d92b --- /dev/null +++ b/src/screens/Signup/StepCaptcha/__tests__/CaptchaWebView.test.ts @@ -0,0 +1,36 @@ +import {buildAllowedHosts} from '#/screens/Signup/StepCaptcha/CaptchaWebView' + +describe('buildAllowedHosts', () => { + it('allows the community PDS the signup is pointed at', () => { + expect(buildAllowedHosts('https://medsky.network')).toContain( + 'medsky.network', + ) + }) + + it('keeps the static hosts alongside the community PDS', () => { + const hosts = buildAllowedHosts('https://medsky.network') + expect(hosts).toEqual(expect.arrayContaining(['blacksky.app', 'bsky.app'])) + }) + + it('strips the port and path from the service URL', () => { + expect(buildAllowedHosts('https://pds.example.com/xrpc')).toContain( + 'pds.example.com', + ) + }) + + it('does not duplicate a host already on the static list', () => { + const hosts = buildAllowedHosts('https://blacksky.app') + expect(hosts.filter(h => h === 'blacksky.app')).toHaveLength(1) + }) + + it('falls back to the static list when serviceUrl is unset', () => { + expect(buildAllowedHosts(undefined)).toContain('blacksky.app') + expect(buildAllowedHosts(undefined)).not.toContain('medsky.network') + }) + + it('falls back to the static list when serviceUrl is malformed', () => { + const hosts = buildAllowedHosts('not a url') + expect(hosts).toContain('blacksky.app') + expect(hosts).toHaveLength(buildAllowedHosts(undefined).length) + }) +}) -- knotmirror 0.0.1