| 5 |
| JavaScript bundles (sandbox shim, trusted validator, renderer with the `webkit` |
| 6 | transport) are byte-for-byte the ones iOS ships. WebKit's API is the same on |
| 7 | both platforms; what differs is the view layer, and that is a handful of |
| 8 | `#if os(macOS)` branches in three files. |
| 9 |
| 10 | ``` |
| 11 | macos/ |
| 12 | DemoApp/ |
| 13 | project.yml xcodegen project: SwiftUI app for macOS 13+, sources and corpus from apps/ios/DemoApp |
| 14 | Info.plist generated by xcodegen (local networking for the canary) |
| 15 | packages/ios/Sources/WidgetSandbox/ |
| 16 | Platform.swift PlatformView = UIView | NSView, screen scale |
| 17 | RendererWebView.swift WKWebView knobs per platform (see below) |
| 18 | WidgetHost.swift WidgetContainerView: UIView (didMoveToWindow) | NSView (window + occlusion) |
| 19 | WidgetView.swift UIViewRepresentable | NSViewRepresentable, shared WidgetModel |
| 20 | packages/ios/Tests/WidgetSandboxTests/ |
| 21 | TestWindow.swift UIWindow | NSWindow helper for the WKWebView tests |
| 22 | apps/ios/DemoApp/Sources/ |
| 23 | ReferenceWebView.swift reference mode (dev tooling): the same file rendered natively, no sandbox |
| 24 | NativeRenderer.swift Blitz / litehtml renderer prototypes (dev tooling), AppKit branch: mouse → one pointer, 60 Hz timer |
| 25 | CompareRunnerView.swift sandbox vs reference timing over compat + generated (--autorun compare) |
| 26 | ``` |
| 27 |
| 28 | ## Build and run |
| 29 |
| 30 | ```bash |
| 31 | corepack pnpm build:macos # = build:ios: bundles → packages/ios/Sources/WidgetSandbox/WidgetJS, corpus → apps/ios/DemoApp/Corpus |
| 32 | cd apps/macos/DemoApp && xcodegen generate # regenerates TwidgetDemoMac.xcodeproj (brew install xcodegen) |
| 33 | open apps/macos/DemoApp/TwidgetDemoMac.xcodeproj |
| 34 | ``` |
| 35 |
| 36 | Package tests natively (no simulator): |
| 37 |
| 38 | ```bash |
| 39 | xcodebuild test -scheme WidgetSandbox -destination 'platform=macOS' |
| 40 | ``` |
| 41 |
| 42 | The Blitz and litehtml renderer prototypes are frozen (`packages/ios/README.md`, "Native renderer prototypes"): like |
| 43 | the iOS demo, this one builds and links them only with `TW_NATIVE_RENDERERS=YES` on `xcodebuild`, and then the |
| 44 | pre-build script phases need rustup's `cargo` and Homebrew's `cmake` + `ninja`. |
| 45 | The demo is ad-hoc signed, not sandboxed (it must reach the canary itself) |
| 46 | and never distributed. Its runners can be driven from a shell with a launch |
| 47 | argument, like the Android demo's `--es autorun`; each writes a JSON report to |
| 48 | `~/Library/Application Support/org.telegram.widgetsandbox.TwidgetDemoMac/` and |
| 49 | quits: |
| 50 |
| 51 | ```bash |
| 52 | cd apps/macos/DemoApp && xcodebuild build -project TwidgetDemoMac.xcodeproj -scheme TwidgetDemoMac -destination 'platform=macOS' -derivedDataPath /tmp/twidget-mac |
| 53 | APP=/tmp/twidget-mac/Build/Products/Debug/TwidgetDemoMac.app/Contents/MacOS/TwidgetDemoMac |
| 54 | corepack pnpm canary & # the attack runner asks it for hits |
| 55 | "$APP" --autorun attacks # → attack-report.json (hits must be 0) |
| 56 | "$APP" --autorun generated # → generated-report.json (40 widgets) |
| 57 | "$APP" --autorun compat # → compat-report.json (21 widgets) |
| 58 | "$APP" --autorun htmlbubbles # → htmlbubbles-report.json (116 Telegram demos ported to classic widgets; build with -configuration Release, the Debug QuickJS is too slow for their boot under the 500 ms wall-clock slice) |
| 59 | "$APP" --autorun pause # → pause-report.json (window minimised ⇒ frames stop) |
| 60 | "$APP" --autorun compare # → compare-report.json (sandbox vs native WebKit, wall time to loaded) |
| 61 | "$APP" --autorun generated --blitz # the same on the Blitz renderer prototype (a TW_NATIVE_RENDERERS=YES build); --litehtml for litehtml, --webview back (the choice is persisted) |
| 62 | ``` |
| 63 |
| 64 | ## Reference mode (dev tooling, never a product path) |
| 65 |
| 66 | Like the web prototype's `?reference=1`, the corpus browser has a |
| 67 | **Reference** toggle: the same file is rendered a second time natively by |
| 68 | WebKit — no QuickJS, no validator, no renderer protocol — so output and speed |
| 69 | can be compared against what the engine does on its own. The demo implements it |
| 70 | in `ReferenceWebView.swift`, outside the `WidgetSandbox` package: a doctype is |
| 71 | added (no library is bundled, so a CDN script fails as in the sandbox), and the page is served from a private `tgref://` scheme that refuses |
| 72 | every other URL, with the web dev server's `REFERENCE_CSP` (`connect-src |
| 73 | 'none'`, `script-src 'self' 'unsafe-inline' 'unsafe-eval'`), the http(s)/ws(s) |
| 74 | content rule list and every navigation cancelled. The toggle is hidden for |
| 75 | `attacks/`, and `--autorun compare` never loads that corpus: a reference page |
| 76 | runs the widget's own scripts with the full DOM, there is no sandbox to test. |
| 77 | Both bubbles show "loaded in N ms" with the same definition — wall clock from |
| 78 | creating the host (`WidgetHost`, or the reference's `WKWebView`) to `loaded` / |
| 79 | the `load` event — so the two can be compared at a glance. The reference bubble |
| 80 | adds the page's own clock (`loadEventStart` and DOMContentLoaded from navigation |
| 81 | start), which excludes creating the web view and its process and is therefore |
| 82 | much smaller, plus uncaught errors. |
| 83 |
| 84 | `--autorun compare` loads every compat and generated widget both ways and |
| 85 | writes `compare-report.json` (per widget: sandbox wall time to `loaded` and |
| 86 | the engine's JS time; reference wall time to the `load` event and the page's |
| 87 | own `loadEventStart`). Debug build (quickjs-ng at `-O0`), one run, medians: |
| 88 |
| 89 | | Corpus | Sandbox: wall to loaded | Sandbox: JS time | Reference: wall to load | Reference: page's own load event | Ratio | |
| 90 | |---|---|---|---|---|---| |
| 91 | | compat (21) | 215 ms (max 484) | 19 ms | 105 ms (max 156) | 6 ms | 2.3× | |
| 92 | | generated, with profile (20) | 240 ms (max 325) | 33 ms | 107 ms (max 151) | 21 ms | 2.3× | |
| 93 | | generated, without profile (20) | 236 ms (max 365) | 32 ms | 107 ms (max 221) | 22 ms | 2.2× | |
| 94 |
| 95 | Reading it: both wall times include creating a WKWebView and its web content |
| 96 | process (~100 ms, the whole reference number); the sandbox adds the QuickJS |
| 97 | boot (evaluating ~500 KB of sandbox bundle from source, the bulk of the |
| 98 | difference; the precompiled `.qbc` bundles have since halved it), the validator |
| 99 | round trip and the renderer handshake. The widget's own script work is 20–35 ms |
| 100 | in QuickJS against 7–21 ms natively. The numbers are from a debug build and |
| 101 | should be re-measured in release before being quoted. |
| 102 |
| 103 | One widget fails in reference mode and passes in the sandbox: `d3-bar` |
| 104 | calls `getBBox()` on an SVG `<text>` it has just appended, synchronously while |
| 105 | the document is still being parsed, and WebKit throws `NotFoundError: The |
| 106 | object can not be found here.` there (a bare WKWebView reproduces it; the same |
| 107 | call succeeds once the page has loaded, and Chromium — the web reference — |
| 108 | never throws). The sandboxed widget is unaffected because its layout queries |
| 109 | are answered by the renderer against a laid-out document after the ops flush. |
| 110 |
| 111 | ## What differs from iOS |
| 112 |
| 113 | | iOS | macOS | |
| 114 | |---|---| |
| 115 | | `UIView` container, `didMoveToWindow` ⇒ visible | `NSView` container; visible = in a window **and** `NSWindow.occlusionState` contains `.visible` (minimised, fully covered or on another Space counts as hidden, like a background tab). `occlusionState` is trusted only once the window server has reported on the window; before that `isVisible` decides — an xctest process never finishes launching `NSApplication` and would otherwise count every window as hidden | |
| 116 | | `overrideUserInterfaceStyle` for the theme | `webView.appearance = NSAppearance(.aqua / .darkAqua)` drives `prefers-color-scheme` | |
| 117 | | `isOpaque = false`, clear `backgroundColor`, scroll view disabled | `drawsBackground = false` (KVC; the public `underPageBackgroundColor` only tints past the document's end); no scroll view: the host sizes the view to the content, and wheel events over a non-scrolling document reach the enclosing scroll view | |
| 118 | | — | `RendererWKWebView` removes the context menu (`willOpenMenu`): Reload, Open in New Window, Download, Inspect Element are navigations we cancel anyway or tooling a bubble must not expose | |
| 119 | | — | `runOpenPanelWith` answers `nil`: `<input type=file>` never opens a file dialog, the widget gets nothing from the disk | |
| 120 | | `UIScreen.main.scale` | `NSScreen.main.backingScaleFactor` as the initial `devicePixelRatio`; the renderer reports the real one with its first `size` | |
| 121 | | `allowsInlineMediaPlayback` | iOS-only setting, skipped | |
| 122 |
| 123 | Everything security-relevant is unchanged: the custom scheme handler serving |
| 124 | exactly one document, nonce CSP as header and `<meta>`, the content rule list |
| 125 | blocking `http(s)`/`ws(s)`, non-persistent data store, every navigation |
| 126 | cancelled in `decidePolicyFor`, `window.open` → nil, the document-start script |
| 127 | deleting the network APIs, the trusted validator between engine and renderer, |
| 128 | and QuickJS on its own `JSThread` with the 8 MB stack. The macOS demo is not |
| 129 | App-Sandboxed; a host app that is, without `com.apple.security.network.client`, |
| 130 | would add one more platform barrier in front of WebKit's networking process, |
| 131 | but nothing here relies on it. |
| 132 |
| 133 | ## Verified (macOS 26.5, Xcode 26.6, Apple silicon) |
| 134 |
| 135 | - Package tests natively: 21 tests green (engine, QuickJS thread pinning, |
| 136 | trusted validator, renderer in a real WKWebView, WidgetHost end-to-end: |
| 137 | validator diagnostics, CSSOM attacks reported as blocked, killed widget keeps |
| 138 | its last render). `testRemovingTheViewFromTheWindowPausesTheWidget` is iOS |
| 139 | only: under `xcodebuild test` the xctest process gets no window-server |
| 140 | occlusion events, so WebKit keeps the page `hidden` (no animation frames, |
| 141 | timers at 1 Hz — checked with a bare WKWebView in the same kind of process); |
| 142 | the same scenario runs in the demo as `--autorun pause`. |
| 143 | - The same 22 tests still pass on the iPhone 17 simulator (iOS 26.5) after the |
| 144 | AppKit changes. |
| 145 | - Demo runners (debug build, quickjs-ng at `-O0`): |
| 146 |
| 147 | | Runner | Result | |
| 148 | |---|---| |
| 149 | | attacks (89 files) | canary hits **0**; 237 blocked diagnostics; one widget dead by design (`fallback-at-boot`, CPU limit) | |
| 150 | | generated (40) | 40/40 load without errors; JS time median 97 ms, max 875 ms | |
| 151 | | compat (21) | 21/21; median 19 ms, max 91 ms | |
| 152 | | pause | see `pause-report.json` after `--autorun pause`: frames flow with the window visible, stop while it is minimised, resume after restore; the widget sees `visible,hidden,visible,` | |
| 153 |
| 154 | ## Not done / next |
| 155 |
| 156 | - Same open items as iOS: `Intl.formatToParts`, snapshot-and-drop of hidden |
| 157 | runtimes, pentest of the WebKit blockers beyond the attack corpus. |
| 158 | - Keyboard focus and the Tab order between native chat chrome and a widget's |
| 159 | form controls have not been designed; WKWebView takes focus like any NSView. |
| 160 | - A chat list with many bubbles: the demo shows one widget per screen; the |
| 161 | product's scroll-visibility reporting (`setVisible`) is the host's job as on |
| 162 | iOS and Android. |
| 163 |