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) + }) +})