/** * 外链白名单(审查 C6):渲染成 a[href] / window.open 之前过滤协议。 * * 结果文件地址、task_data 里的 file_url 等都是客户端/服务端原样透传的开放字段 * (见 shared/bridges/pywebview.ts 对 task_data 的说明),页面直接绑到 :href 上时, * `javascript:` / `data:` 这类伪协议会在当前文档上下文里执行。这里只放行 http/https。 */ const ALLOWED_PROTOCOLS = new Set(['http:', 'https:']) /** 非 http(s) 或无法解析的地址一律返回 '#',其余原样返回(相对路径按当前页面补全后判定)。 */ export function safeHttpUrl(raw?: string | null): string { const value = typeof raw === 'string' ? raw.trim() : '' if (!value) return '#' const base = typeof window !== 'undefined' && window.location?.href ? window.location.href : 'http://localhost/' let parsed: URL try { parsed = new URL(value, base) } catch { return '#' } return ALLOWED_PROTOCOLS.has(parsed.protocol) ? value : '#' }