Files
Genarrative/apps/mobile-shell/src/mobileShellNavigation.ts
T
kdletters ee49c26868 收紧移动壳外链协议白名单
Expo WebView 外域导航只允许安全外链协议交给系统

阻断危险协议避免外链页面保留宿主桥能力

补充移动壳导航测试和宿主壳文档
2026-06-18 00:35:14 +08:00

52 lines
1.0 KiB
TypeScript

import { normalizeHostBridgeExternalUrl } from '../../../packages/shared/src/contracts/hostBridge';
export function shouldOpenInMobileShellWebView(
rawUrl: string,
allowedOrigin: string,
) {
if (rawUrl === 'about:blank') {
return true;
}
if (
!rawUrl.startsWith('/') &&
!rawUrl.startsWith('#') &&
!/^[a-z][a-z0-9+.-]*:/i.test(rawUrl)
) {
return false;
}
try {
const url = new URL(rawUrl, allowedOrigin);
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
return false;
}
return url.origin === allowedOrigin;
} catch {
return false;
}
}
export function resolveMobileShellExternalUrl(rawUrl: string) {
return normalizeHostBridgeExternalUrl(rawUrl);
}
export function resolveMobileShellWebViewUrl(
rawUrl: string,
allowedOrigin: string,
) {
if (
rawUrl === 'about:blank' ||
!shouldOpenInMobileShellWebView(rawUrl, allowedOrigin)
) {
return null;
}
try {
return new URL(rawUrl, allowedOrigin).toString();
} catch {
return null;
}
}