|
| 1 | +import type { HostProbeSnapshot } from "./host-probe-types"; |
| 2 | + |
| 3 | +export function captureRuntime(): HostProbeSnapshot["runtime"] { |
| 4 | + let sandboxAttr: string | null = null; |
| 5 | + let allowAttr: string | null = null; |
| 6 | + let crossOriginBlocked = false; |
| 7 | + try { |
| 8 | + const fe = window.frameElement as HTMLIFrameElement | null; |
| 9 | + if (fe) { |
| 10 | + sandboxAttr = fe.getAttribute("sandbox"); |
| 11 | + allowAttr = fe.getAttribute("allow"); |
| 12 | + } |
| 13 | + } catch { |
| 14 | + crossOriginBlocked = true; |
| 15 | + } |
| 16 | + |
| 17 | + let metaCsp: string | null = null; |
| 18 | + try { |
| 19 | + metaCsp = |
| 20 | + document |
| 21 | + .querySelector('meta[http-equiv="Content-Security-Policy"]') |
| 22 | + ?.getAttribute("content") ?? null; |
| 23 | + } catch { |
| 24 | + /* ignore */ |
| 25 | + } |
| 26 | + |
| 27 | + let permissionsPolicy: string | null = null; |
| 28 | + try { |
| 29 | + const doc = document as unknown as { |
| 30 | + permissionsPolicy?: { allowedFeatures?: () => string[] }; |
| 31 | + featurePolicy?: { allowedFeatures?: () => string[] }; |
| 32 | + }; |
| 33 | + const pp = doc.permissionsPolicy ?? doc.featurePolicy; |
| 34 | + if (pp?.allowedFeatures) { |
| 35 | + permissionsPolicy = pp.allowedFeatures().join(" "); |
| 36 | + } |
| 37 | + } catch { |
| 38 | + /* ignore */ |
| 39 | + } |
| 40 | + |
| 41 | + let ancestorOriginsLength = 0; |
| 42 | + try { |
| 43 | + ancestorOriginsLength = window.location.ancestorOrigins?.length ?? 0; |
| 44 | + } catch { |
| 45 | + /* ignore */ |
| 46 | + } |
| 47 | + |
| 48 | + return { |
| 49 | + location: { |
| 50 | + origin: window.location.origin, |
| 51 | + href: window.location.href, |
| 52 | + ancestorOriginsLength, |
| 53 | + }, |
| 54 | + navigator: { |
| 55 | + userAgent: navigator.userAgent, |
| 56 | + platform: navigator.platform, |
| 57 | + languages: navigator.languages ?? [], |
| 58 | + hardwareConcurrency: navigator.hardwareConcurrency ?? 0, |
| 59 | + }, |
| 60 | + frame: { |
| 61 | + sandboxAttr, |
| 62 | + allowAttr, |
| 63 | + crossOriginBlocked, |
| 64 | + doubleIframed: window.parent !== window.top, |
| 65 | + }, |
| 66 | + policies: { |
| 67 | + metaCsp, |
| 68 | + permissionsPolicy, |
| 69 | + }, |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +export async function runCspProbes( |
| 74 | + urls: string[], |
| 75 | +): Promise<NonNullable<HostProbeSnapshot["runtime"]["cspProbes"]>> { |
| 76 | + const results: NonNullable<HostProbeSnapshot["runtime"]["cspProbes"]> = []; |
| 77 | + for (const url of urls) { |
| 78 | + try { |
| 79 | + await fetch(url, { mode: "no-cors" }); |
| 80 | + results.push({ url, ok: true, errorName: null }); |
| 81 | + } catch (e) { |
| 82 | + results.push({ |
| 83 | + url, |
| 84 | + ok: false, |
| 85 | + errorName: e instanceof Error ? e.name : String(e), |
| 86 | + }); |
| 87 | + } |
| 88 | + } |
| 89 | + return results; |
| 90 | +} |
| 91 | + |
| 92 | +// The `App` SDK exposes `getHostContext()` but not the rest of the |
| 93 | +// `ui/initialize` result. Sniff the raw envelope from a parallel |
| 94 | +// message log to recover hostInfo / hostCapabilities / protocolVersion. |
| 95 | +export function findUiInitializeResult( |
| 96 | + messages: Array<{ data: unknown }>, |
| 97 | +): { |
| 98 | + protocolVersion?: string; |
| 99 | + hostInfo?: unknown; |
| 100 | + hostCapabilities?: unknown; |
| 101 | +} | null { |
| 102 | + for (const { data } of messages) { |
| 103 | + if (!data || typeof data !== "object") continue; |
| 104 | + const result = (data as { result?: unknown }).result; |
| 105 | + if (!result || typeof result !== "object") continue; |
| 106 | + const r = result as Record<string, unknown>; |
| 107 | + if ("hostCapabilities" in r || "hostInfo" in r) { |
| 108 | + return { |
| 109 | + protocolVersion: |
| 110 | + typeof r.protocolVersion === "string" ? r.protocolVersion : undefined, |
| 111 | + hostInfo: r.hostInfo, |
| 112 | + hostCapabilities: r.hostCapabilities, |
| 113 | + }; |
| 114 | + } |
| 115 | + } |
| 116 | + return null; |
| 117 | +} |
0 commit comments