Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

home/ios: hide document content in the app switcher while sealed (#786)

nyblnet0e79825cb87c
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 3537a4ac..1aff7e00 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -2765,6 +2765,28 @@ jobs:
if grep -q 'SKIPPED' "$RUNNER_TEMP/rig.out"; then echo "::error::$rig skipped on macOS"; exit 1; fi
done
+ home-ios-simulator:
+ # Builds the iOS app and drives it on a simulator: the app-switcher snapshot
+ # iOS writes to disk shows nothing of a sealed document. The rig reads back
+ # the snapshots iOS itself wrote; a skip fails here too.
+ runs-on: macos-latest
+ timeout-minutes: 30
+ steps:
+ - uses: actions/checkout@v4
+ - uses: actions/setup-node@v4
+ with:
+ node-version: 24
+ - name: Toolchain
+ run: |
+ brew install xcodegen
+ sw_vers && xcodebuild -version && xcrun simctl list runtimes
+ - name: Snapshot cover (a skip fails)
+ shell: bash
+ run: |
+ set -o pipefail
+ node scripts/test-home-snapshot-cover.mjs | tee "$RUNNER_TEMP/rig.out"
+ if grep -q 'SKIPPED' "$RUNNER_TEMP/rig.out"; then echo "::error::snapshot cover skipped on macOS"; exit 1; fi
+
home-origin-probe-browser:
# The probe that decides whether a file opened for the first time may keep
# the storage origin it had before origins followed the file. It runs in
diff --git a/home/ios/EditorViewController.swift b/home/ios/EditorViewController.swift
index 00f87048..d242b4e0 100644
--- a/home/ios/EditorViewController.swift
+++ b/home/ios/EditorViewController.swift
@@ -37,6 +37,8 @@ import CryptoKit
final class EditorViewController: UIViewController, WKScriptMessageHandler, WKURLSchemeHandler,
WKNavigationDelegate, WKDownloadDelegate, WKUIDelegate {
private let document: BentoDocument
+ /// the open document's saved body is sealed (bento/enc): the app switcher must not show it
+ var isSealed: Bool { SnapshotCover.isSealed(document.html) }
private var webView: WKWebView!
/// Has the open document been handed to the web app yet? Bento only reaches
diff --git a/home/ios/SceneDelegate.swift b/home/ios/SceneDelegate.swift
index aa71526f..67ff4e38 100644
--- a/home/ios/SceneDelegate.swift
+++ b/home/ios/SceneDelegate.swift
@@ -26,7 +26,36 @@ final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
// The index is only as good as its last pass, and documents change
// outside this app — that is the point of opening them in place.
LibraryIndex.shared.reindex()
+ #if DEBUG
+ // scripts/test-home-snapshot-cover.mjs: open this file at launch
+ if let path = ProcessInfo.processInfo.environment["BENTO_RIG_OPEN"] {
+ DispatchQueue.main.async { [weak self] in self?.open(URL(fileURLWithPath: path)) }
+ }
+ #endif
+ }
+
+ // The app-switcher snapshot is written to disk: a sealed document is covered
+ // before it's taken (see SnapshotCover). Not on the Mac, where window
+ // previews aren't saved and a scene resigns active whenever focus moves.
+ func sceneWillResignActive(_ scene: UIScene) {
+ #if !targetEnvironment(macCatalyst)
+ if let w = window, SnapshotCover.needed(in: w) { SnapshotCover.show(over: w) }
+ #endif
+ }
+
+ func sceneDidBecomeActive(_ scene: UIScene) {
+ if let w = window { SnapshotCover.hide(from: w) }
+ }
+
+ #if DEBUG
+ /// scripts/test-home-snapshot-cover.mjs: what the window shows as it goes to
+ /// the background, the moment the system takes its snapshot
+ func sceneDidEnterBackground(_ scene: UIScene) {
+ guard let path = ProcessInfo.processInfo.environment["BENTO_RIG_SNAPSHOT"], let w = window else { return }
+ let image = UIGraphicsImageRenderer(bounds: w.bounds).image { _ in _ = w.drawHierarchy(in: w.bounds, afterScreenUpdates: false) }
+ try? image.pngData()?.write(to: URL(fileURLWithPath: path))
}
+ #endif
/// A result tapped in system Spotlight. The identifier is the one donated in
/// `LibraryIndex.donate`, so it resolves through the index rather than being
diff --git a/home/ios/SnapshotCover.swift b/home/ios/SnapshotCover.swift
new file mode 100644
index 00000000..ce9b9b39
--- /dev/null
+++ b/home/ios/SnapshotCover.swift
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: MIT
+// Copyright (c) 2026 The Bento authors
+
+import UIKit
+
+/// Hide document content in the app switcher while sealed.
+///
+/// iOS saves the app-switcher snapshot to disk, so while a sealed or password
+/// document is open (its saved body is a bento/enc envelope), a plain cover goes
+/// over the window as the scene resigns active and comes off when it's active
+/// again. Unsealed documents are left alone.
+///
+/// Not on the Mac: Catalyst window previews aren't saved, and a scene there
+/// resigns active every time another window takes focus.
+enum SnapshotCover {
+ /// the saved body is sealed: the same check the library index makes
+ static func isSealed(_ html: Data) -> Bool {
+ BentoIndex.isEncrypted(Array(String(decoding: html.prefix(BentoIndex.headBytes), as: UTF8.self).utf16))
+ }
+
+ private static let tag = 0x5EA1_0C0
+
+ static func show(over window: UIWindow) {
+ guard window.viewWithTag(tag) == nil else { return }
+ let cover = UIView(frame: window.bounds)
+ cover.tag = tag
+ cover.autoresizingMask = [.flexibleWidth, .flexibleHeight]
+ cover.backgroundColor = .systemBackground
+ let lock = UIImageView(image: UIImage(systemName: "lock.fill"))
+ lock.tintColor = .secondaryLabel
+ lock.translatesAutoresizingMaskIntoConstraints = false
+ cover.addSubview(lock)
+ NSLayoutConstraint.activate([
+ lock.centerXAnchor.constraint(equalTo: cover.centerXAnchor),
+ lock.centerYAnchor.constraint(equalTo: cover.centerYAnchor),
+ lock.widthAnchor.constraint(equalToConstant: 44),
+ lock.heightAnchor.constraint(equalToConstant: 44),
+ ])
+ window.addSubview(cover)
+ }
+
+ static func hide(from window: UIWindow) {
+ window.viewWithTag(tag)?.removeFromSuperview()
+ }
+
+ /// Every editor anywhere under `root`: presented, or a child of a container.
+ static func editors(in root: UIViewController?) -> [EditorViewController] {
+ guard let root else { return [] }
+ var out: [EditorViewController] = []
+ if let e = root as? EditorViewController { out.append(e) }
+ for child in root.children { out += editors(in: child) }
+ if let p = root.presentedViewController { out += editors(in: p) }
+ return out
+ }
+
+ /// Whether this window shows a sealed document right now.
+ static func needed(in window: UIWindow) -> Bool {
+ editors(in: window.rootViewController).contains { $0.isSealed }
+ }
+}
diff --git a/scripts/test-home-snapshot-cover.mjs b/scripts/test-home-snapshot-cover.mjs
new file mode 100644
index 00000000..97080601
--- /dev/null
+++ b/scripts/test-home-snapshot-cover.mjs
@@ -0,0 +1,152 @@
+#!/usr/bin/env node
+// SPDX-License-Identifier: MIT
+// Copyright (c) 2026 The Bento authors
+/**
+ * bento/home on iOS: the app-switcher snapshot of a sealed document shows none
+ * of it (home/ios/SnapshotCover.swift).
+ *
+ * node scripts/test-home-snapshot-cover.mjs
+ *
+ * iOS writes the app-switcher snapshot to disk, under the app's
+ * Library/SplashBoard, as KTX images. This builds the app for the simulator,
+ * opens a document, sends the app to the background, and reads back the
+ * snapshots iOS itself wrote. ImageIO reads KTX. The document is one solid red
+ * page, so the question per snapshot is how much of it is that red.
+ * - sealed (its body is a bento/enc envelope): no red in any snapshot
+ * - plain (the control): red in the snapshot, so the measurement can see
+ * content when it's there
+ * - back in the foreground, the sealed document shows again: the cover comes off
+ * The sealed file is red on screen too, so "no red" can only come from the cover.
+ * With the cover disabled, this measured 93% red.
+ *
+ * Needs Xcode and an iOS simulator; skips elsewhere. Takes a few minutes.
+ */
+import { execFileSync } from 'node:child_process'
+import { mkdtempSync, writeFileSync, rmSync, readdirSync, statSync, existsSync, mkdirSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join, resolve, dirname } from 'node:path'
+import { fileURLToPath } from 'node:url'
+
+const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..')
+const BUNDLE = 'page.bento.home'
+const run = (cmd, args, opts = {}) => execFileSync(cmd, args, { stdio: 'pipe', maxBuffer: 1 << 26, ...opts }).toString()
+try { run('xcrun', ['simctl', 'help']) } catch { console.log('home snapshot cover: SKIPPED — needs Xcode with iOS simulators.'); process.exit(0) }
+
+const work = mkdtempSync(join(tmpdir(), 'bento-snapcover-'))
+const sleep = (s) => run('sleep', [String(s)])
+let checks = 0, failures = 0
+const ok = (c, m) => { checks++; if (!c) failures++; console.log(` ${c ? 'ok ' : 'FAIL'} ${m}`) }
+
+// an iPhone simulator: the first available one, booted
+const devices = JSON.parse(run('xcrun', ['simctl', 'list', 'devices', 'available', '-j'])).devices
+const phone = Object.entries(devices).filter(([rt]) => rt.includes('iOS')).flatMap(([, ds]) => ds).find((d) => d.name.startsWith('iPhone'))
+if (!phone) { console.error('no available iPhone simulator'); process.exit(2) }
+const DEV = phone.udid
+console.log(`simulator: ${phone.name} (${DEV})`)
+if (phone.state !== 'Booted') { try { run('xcrun', ['simctl', 'boot', DEV]) } catch {} }
+run('xcrun', ['simctl', 'bootstatus', DEV, '-b'], { timeout: 600000 })
+
+// build and install (Debug: the rig's launch hooks exist only there)
+const dd = join(work, 'dd')
+const ios = join(ROOT, 'home/ios')
+try { run('xcodegen', [], { cwd: ios }) } catch (e) { console.error('xcodegen failed (is it installed?): ' + (e.stderr?.toString() || e.message)); process.exit(2) }
+try {
+ run('xcodebuild', ['-project', 'BentoTray.xcodeproj', '-scheme', 'BentoTray', '-configuration', 'Debug',
+ '-destination', `platform=iOS Simulator,id=${DEV}`, '-derivedDataPath', dd, 'CODE_SIGN_IDENTITY=-', 'build'],
+ { cwd: ios, timeout: 1200000 })
+} catch (e) { console.error('build failed:\n' + (e.stdout?.toString().split('\n').filter((l) => /error:/.test(l)).join('\n') || e.message)); process.exit(2) }
+const prod = join(dd, 'Build/Products', readdirSync(join(dd, 'Build/Products')).find((d) => d.endsWith('-iphonesimulator')))
+run('xcrun', ['simctl', 'install', DEV, join(prod, 'BentoTray.app')])
+const DATA = run('xcrun', ['simctl', 'get_app_container', DEV, BUNDLE, 'data']).trim()
+mkdirSync(join(DATA, 'Documents'), { recursive: true })
+
+// the measuring tool: fraction of a picture that is the document's red
+const tool = join(work, 'red')
+writeFileSync(join(work, 'red.swift'), String.raw`
+import Foundation
+import ImageIO
+import CoreGraphics
+for p in CommandLine.arguments.dropFirst() {
+ guard let src = CGImageSourceCreateWithURL(URL(fileURLWithPath: p) as CFURL, nil), let img = CGImageSourceCreateImageAtIndex(src, 0, nil) else { print("-1"); continue }
+ let w = img.width, h = img.height
+ var px = [UInt8](repeating: 0, count: w * h * 4)
+ let ctx = CGContext(data: &px, width: w, height: h, bitsPerComponent: 8, bytesPerRow: w * 4, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!
+ ctx.draw(img, in: CGRect(x: 0, y: 0, width: w, height: h))
+ var red = 0
+ for i in stride(from: 0, to: px.count, by: 4) where px[i] > 200 && px[i+1] < 70 && px[i+2] < 70 { red += 1 }
+ print(Double(red) / Double(w * h))
+}
+`)
+run('swiftc', ['-O', '-o', tool, join(work, 'red.swift')])
+const redOf = (files) => files.length ? run(tool, files).trim().split('\n').map(Number) : []
+
+function doc(name, format) {
+ const p = join(DATA, 'Documents', `${name}.bento.html`)
+ writeFileSync(p, `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Rig ${name}</title>
+<script type="application/json" id="bento-doc">{"format":"${format}","docId":"rig-${name}"}</script></head>
+<body style="margin:0;background:#ff0000;min-height:100vh"></body></html>`)
+ return p
+}
+
+/** the scene snapshots iOS wrote after `since` (full size; the launch-image group is a different thing) */
+function sceneSnapshots(since) {
+ const out = []
+ const walk = (d) => { for (const e of readdirSync(d, { withFileTypes: true })) {
+ const p = join(d, e.name)
+ if (e.isDirectory()) { if (e.name !== 'downscaled') walk(p) } else if (p.endsWith('.ktx') && p.includes('sceneID:') && statSync(p).mtimeMs > since) out.push(p)
+ } }
+ const root = join(DATA, 'Library/SplashBoard')
+ if (existsSync(root)) walk(root)
+ return out
+}
+
+/** Wait (up to a minute) until the document is on screen: a cold simulator can
+ * take far longer than a warm one, and backgrounding before it shows would make
+ * a clean snapshot mean nothing. Returns how much of the screen is its red. */
+function untilOnScreen(tag) {
+ let red = 0
+ for (let i = 0; i < 30 && red <= 0.5; i++) {
+ sleep(2)
+ const shot = join(work, `fg-${tag}-${i}.png`)
+ run('xcrun', ['simctl', 'io', DEV, 'screenshot', shot])
+ red = redOf([shot])[0]
+ }
+ return red
+}
+
+function background(path) {
+ try { run('xcrun', ['simctl', 'terminate', DEV, BUNDLE]) } catch {}
+ rmSync(join(DATA, 'Library/SplashBoard'), { recursive: true, force: true })
+ const since = Date.now()
+ run('xcrun', ['simctl', 'launch', DEV, BUNDLE], { env: { ...process.env, SIMCTL_CHILD_BENTO_RIG_OPEN: path } })
+ const onScreen = untilOnScreen(since)
+ run('xcrun', ['simctl', 'launch', DEV, 'com.apple.Preferences'])
+ sleep(5)
+ return { onScreen, snapshots: redOf(sceneSnapshots(since)) }
+}
+
+try {
+ const sealedPath = doc('sealed', 'bento/enc'), plainPath = doc('plain', 'bento/slides')
+ console.log('a sealed document:')
+ const s = background(sealedPath)
+ ok(s.onScreen > 0.5, `it is on screen while open (${(s.onScreen * 100).toFixed(0)}% red), so a clean snapshot can only come from the cover`)
+ ok(s.snapshots.length > 0, `iOS wrote app-switcher snapshots (${s.snapshots.length})`)
+ ok(s.snapshots.length > 0 && s.snapshots.every((r) => r === 0), `none of them shows the document (${s.snapshots.map((r) => (r * 100).toFixed(1) + '%').join(', ')} red)`)
+ console.log('the control, an unsealed document:')
+ const p = background(plainPath)
+ ok(p.onScreen > 0.5, `it is on screen while open (${(p.onScreen * 100).toFixed(0)}% red)`)
+ ok(p.snapshots.some((r) => r > 0.5), `its snapshot shows it (${p.snapshots.map((r) => (r * 100).toFixed(1) + '%').join(', ')} red), so the measurement sees content when it is there`)
+ console.log('back in the foreground:')
+ run('xcrun', ['simctl', 'terminate', DEV, BUNDLE])
+ run('xcrun', ['simctl', 'launch', DEV, BUNDLE], { env: { ...process.env, SIMCTL_CHILD_BENTO_RIG_OPEN: sealedPath } })
+ untilOnScreen('again')
+ run('xcrun', ['simctl', 'launch', DEV, 'com.apple.Preferences']); sleep(3)
+ run('xcrun', ['simctl', 'launch', DEV, BUNDLE])
+ const b = untilOnScreen('back')
+ ok(b > 0.5, `the sealed document shows again once the app is active (${(b * 100).toFixed(0)}% red): the cover comes off`)
+} finally {
+ try { run('xcrun', ['simctl', 'terminate', DEV, BUNDLE]) } catch {}
+ rmSync(work, { recursive: true, force: true })
+}
+console.log(`\n${checks - failures}/${checks} checks passed`)
+process.exit(failures ? 1 : 0)