Skip to content

feat(mobile): render interactive visuals in a sandboxed webview - #695

Merged
nixuuu merged 2 commits into
devfrom
feat/mobile-visual-webview
Nov 14, 2026
Merged

nixuuu merged 2 commits into
devfrom
feat/mobile-visual-webview

Conversation

@nixuuu

@nixuuu nixuuu commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

Replaces the mobile text-only visual card (#688) with an interactive renderer in react-native-webview 14.0.1 (the version SDK 58 pins). It ships in the same native build as #659. Do not merge it alone into a released build: it changes the native fingerprint.

How it works

  • Same document and CSP as the web. The app reads the spec with GetVisual (Connect), validates it again and compiles it with compileVisual in frame mode. The WebView gets source={{ html }} with no baseUrl (an opaque about:blank document). It holds a trusted shell (@canopy/domain/visual-shell) that mounts the visual in <iframe sandbox="allow-scripts" srcdoc>, like the web.
  • Shared nonce. A srcdoc frame inherits its parent's CSP on top of its own. Headless Chromium showed the kit blocked under the shell's policy until both documents used the same per-mount nonce.
  • Frame-scoped messaging. The RN bridge may be reachable from every frame: Android injects its WebMessageListener with *, and iOS exposes webkit.messageHandlers to all frames. So the shell wraps each port message in a per-mount token envelope that the frame never sees. The app drops anything without the token and anything over 8 KiB plus the envelope. The rest goes through VisualFrameInbox (new in @canopy/domain/visuals), which the web frame-host.ts now uses too: 10 msg/s, 4 heights/s, strict parser, local state checked against the spec. Host messages (theme, visibility, reduced motion) go in through postMessage with the same token. The shell accepts them only when there is no event.source, so a frame can't forge them.
  • Locked WebView (SandboxedWebView.tsx):
    • No file, content or universal access.
    • incognito, no shared or third-party cookies, no DOM storage (the kit doesn't use it), no cache.
    • No new windows: setSupportMultipleWindows={false}, a no-op onOpenWindow, javaScriptCanOpenWindowsAutomatically={false}. The shell also disables window.open, alert, confirm and prompt.
    • No media, geolocation, zoom, link previews or debugging. No injectedJavaScript.
    • originWhitelist={["*"]} sends every URL to onShouldStartLoadWithRequest. The library's default list would pass other schemes to Linking. That callback allows only the first top-frame about:blank and the child's about:srcdoc.
  • Theme comes from the app theme (light and dark), with Interface size as the scale, and updates live through host messages.
  • Budget:
    • At most 1 live WebView in the app.
    • A visual mounts lazily when it is on screen and the slot is free. "Open visual" takes the slot unless the other visual is in full screen.
    • Off screen it pauses at once and is released after 5 s. A backgrounded app releases it at once.
    • The last height stays reserved, and a new mount starts at that height.
    • Spec cache: LRU, 16 entries / 2 MiB. Control state: 64 entries / 256 KiB in Paths.cache.
  • Host chrome at parity: title, "Version N", "Open full screen" and "Visual actions". The actions are "Save HTML" (Android, chosen directory) or "Share HTML" (iOS), "Copy spec", "Cite visual" and the version list. Full screen uses the same slot, and a hardware Escape closes it.
  • Old binaries: TurboModuleRegistry.get("RNCWebViewModule") is checked before the renderer is imported (lazily), so on a build without the module the JS never touches the native component. Those builds keep feat(agents): summarize visuals on mobile until the webview renderer #688's VisualSummary card.
  • compileVisualExport now builds the "Download HTML" file for both the web BFF route and mobile.

Verified statically

  • bun install, gen:check, typecheck, lint (oxlint), format:check, check:tmp: 2974 pass, 175 skip, 0 fail, nothing left in the temp dir. Web build passes.
  • New packages/domain/src/visuals/host.test.ts:
    • The envelope rejects a wrong or missing token, oversized input and malformed JSON.
    • The inbox enforces its limits and validates state.
    • The token is never in the frame document, and the title and spec stay inert.
    • Mounting refuses a newer or revoked kit and a spec that doesn't parse.
  • Headless Chromium (Blink, the same engine as Android System WebView), not committed. From the sandboxed frame:
    • window.parent.ReactNativeWebView → SecurityError.
    • window.open → blocked (no allow-popups); alert → ignored (no allow-modals).
    • top.location → SecurityError; localStorage and document.cookie → SecurityError; origin null.
    • A forged token envelope posted to the parent was not relayed. The real ready was relayed.
  • expo export for iOS and Android passes; the shell is in the Hermes bundle.
  • expo prebuild --no-install --clean in a temp copy passes:
    • No change to Info.plist or app permissions. The library manifest only merges a non-exported RNCWebViewFileProvider and payment <queries>; we turn payment requests off and have no file inputs.
    • The webview is autolinked (rncoreAutolinkingConfig).
  • Combined native build (this PR on dev with feat(mobile): play video attachments #659 merged), re-checked on head e332d33:
    • bun install --frozen-lockfile is clean, the mobile typecheck passes, and expo export passes for iOS (3320 modules) and Android (3314 modules).

    • expo prebuild --no-install --clean in a temp copy passes, and both sides apply together:

      • Autolinking resolves expo-video, canopy-media-stream, canopy-file-intake and canopy-pinned-fetch (Expo modules) plus react-native-webview (RN community).
      • feat(mobile): play video attachments #659's media-network plugin writes res/xml/canopy_media_network.xml, and expo-video adds no UIBackgroundModes.
      • Android permissions and Info.plist keys are unchanged.
    • Fingerprint (@expo/fingerprint):

      Platform dev with feat(mobile): play video attachments #659 This PR (feat(mobile): play video attachments #659 + webview)
      iOS 5a2f1ebbf02efbeb8f57f05a91e105b8f4f7c935 138e0c6b171c82b4933e86808cdfef56da73b8c6
      Android 43b7842311f5520084c7fcfcbce1b1df23da45ec 906dc944f94eb8333fa384ed9f198834fdd30d13

      The "dev with feat(mobile): play video attachments #659" values come from a temporary worktree that shared this checkout's node_modules. Use the right-hand column for the scheduled native build.

  • expo lint on the new files reports the same rule patterns that already fail across the app (react-hooks/refs for viewport.ref, set-state-in-effect for claiming the slot). Mobile ESLint is not a CI gate and already reports 104 errors on dev.

Security review follow-ups (P3)

  • A remount (new shell and token) starts unloaded again: the loaded state is stored per mount token, so on iOS allowInitialLoad no longer blocks the new about:blank (VisualWebView.tsx).
  • Unpairing, an expired pairing and a new pairing clear the spec cache and visual-state.json, in memory and on disk (clearVisualCaches in session.tsx). A read started before a clear can't restore old entries.
  • docs/agent-visuals.md (mobile section) records the accepted residual risks: bridge flooding from frame code, and unchecked child-frame http(s) navigation on Android (the sandbox and CSP stay the boundary).
  • Re-checked: oxlint, format, bun run typecheck, mobile tsc, and expo export for iOS and Android. JS-only change, so the fingerprints are unchanged.

Needs on-device checks (the device host is broken)

  • Sandbox boundary on both platforms: the frame can't reach the native bridge. The token makes direct calls harmless, but confirm that Android WebMessageListener and iOS webkit.messageHandlers calls from the frame are dropped. Also confirm that no file or content URL loads.
  • Navigation block: on Android, shouldOverrideUrlLoading doesn't see http(s) navigations of child frames, and it allows the load if JS doesn't answer within 250 ms. The sandbox and CSP remain the boundary there; the kit never navigates.
  • Message validation end to end: heights, saved state restored after remount, Escape in full screen.
  • Layout: scrolling inside the iframe within the timeline (iOS scrollEnabled={false} on the shell, Android nestedScrollEnabled). Light and dark, theme changes while open, phone width, long titles.
  • Behaviour: memory of one WebView (target ≤64 MiB), VoiceOver/TalkBack, Save/Share HTML, and the fallback card on a build without the module.

Copy audit (docs/copy.md §8)

Text Check
Open visual / Open full screen / Visual actions Verb first, sentence case, same as web.
Save HTML (Android) / Share HTML (iOS) Verb matches what the platform does (save to a chosen folder or share), like "Save image" / "Share image".
Copy spec / Cite visual / Version N / Latest Same as web; "visual" per the glossary.
Loading visual… / Shown in full screen. Ongoing action with an ellipsis; a short status.
Couldn't open this visual. + Retry What happened; the button is the action.
Update Canopy to open this visual. One idea, says what to do.
Close another visual to open this one. Imperative, says what to do.
Couldn't save/share the HTML. Try again. / Couldn't copy the spec. Try again. / Couldn't cite this visual. Try again. What happened, then what to do.
Spec copied Short status, no period.

No new glossary terms.

Visual items on mobile now mount the same compiled document and CSP as
the web inside a trusted shell in react-native-webview 14.0.1. The shell
relays the frame's port messages in a per-mount token envelope; the app
parses them through the inbox shared with the web. One live WebView, lazy
mount, 5 s release off screen, full screen, Save/Share HTML, Copy spec,
Cite visual and the version picker. Builds without the module keep the
text card.
@nixuuu
nixuuu merged commit 93aa4db into dev Nov 14, 2026
10 checks passed
@nixuuu
nixuuu deleted the feat/mobile-visual-webview branch November 14, 2026 09:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant