Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

iOS host: isolate each document on its own origin

nyblnet952a5541f0f8
diff --git a/ios/BentoHost/EditorViewController.swift b/ios/BentoHost/EditorViewController.swift
index d288bdba..5ea20190 100644
--- a/ios/BentoHost/EditorViewController.swift
+++ b/ios/BentoHost/EditorViewController.swift
@@ -4,12 +4,13 @@
import UIKit
import WebKit
import UniformTypeIdentifiers
+import CryptoKit
/// Hosts one open deck in a WKWebView and bridges saving to UIDocument.
///
/// TWO decisions here carry the design:
///
-/// 1. The deck is served through a CUSTOM SCHEME, never `loadFileURL`. A
+/// 1. The document is served through a CUSTOM SCHEME, never `loadFileURL`. A
/// file:// page in WKWebView gets an opaque, unstable origin, which makes
/// localStorage and IndexedDB unreliable — that would silently break the
/// autosave backstop, the per-device collab member key, and the language and
@@ -17,6 +18,17 @@ import UniformTypeIdentifiers
/// it keeps cross-origin fetches to the sync relay well-defined rather than
/// arriving as `Origin: null`.
///
+/// The host is PER DOCUMENT, derived from the file's own path, not a single
+/// shared `deck`. This app opens ANY self-contained HTML document, not only
+/// Bento's, so a shared origin would let one document read another's
+/// localStorage and IndexedDB — fine when every file is yours, a real leak
+/// between unrelated third-party apps. Derived rather than random because
+/// the origin IS the storage boundary: a fresh host per launch would wipe
+/// that storage on every open. The trade is that moving or renaming a file
+/// gives it a new origin and orphans its local state; that state is a
+/// cache-and-backstop, never the document itself, which is why this is the
+/// right way round.
+///
/// 2. The web content is the DOCUMENT'S OWN runtime. The app bundles no shell
/// for rendering and has no opinion about which version a deck carries, so a
/// deck self-updates through Bento's normal signed channel and iOS users get
@@ -35,6 +47,14 @@ final class EditorViewController: UIViewController, WKScriptMessageHandler, WKUR
private var openDocumentVended = false
private var pendingExportName: String?
+ /// Stable per-document host: a truncated SHA-256 of the file's path. Hex
+ /// only, so it is always a valid host component.
+ private lazy var originHost: String = {
+ let path = document.fileURL.standardizedFileURL.path
+ let digest = SHA256.hash(data: Data(path.utf8))
+ return digest.compactMap { String(format: "%02x", $0) }.joined().prefix(24).description
+ }()
+
init(document: BentoDocument) {
self.document = document
super.init(nibName: nil, bundle: nil)
@@ -60,7 +80,7 @@ final class EditorViewController: UIViewController, WKScriptMessageHandler, WKUR
webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
webView.allowsBackForwardNavigationGestures = false
view.addSubview(webView)
- webView.load(URLRequest(url: URL(string: "bento-app://deck/index.html")!))
+ webView.load(URLRequest(url: URL(string: "bento-app://\(originHost)/index.html")!))
}
// MARK: - serving the deck
diff --git a/ios/README.md b/ios/README.md
index e8ac0efd..a3c6ee8b 100644
--- a/ios/README.md
+++ b/ios/README.md
@@ -1,6 +1,13 @@
# bento/host — iOS
-A thin native host so decks can be **saved in place** on iOS.
+A thin native host that runs **any self-contained HTML document** and lets it
+**save itself in place** on iOS.
+
+Bento decks are the reason it exists, but nothing in the Swift is Bento-specific
+— it never parses the document, it is a courier. Any single-file HTML app that
+saves itself through the File System Access API works the same way, which on iOS
+is otherwise impossible: every browser there is WebKit and none of them ship
+that API.
## Why this exists
@@ -63,11 +70,19 @@ of this bridge did exactly that and prompted on every single save.
## Two implementation details that carry weight
-- **The deck is served through a custom scheme** (`bento-app://`), never
+- **The document is served through a custom scheme** (`bento-app://`), never
`loadFileURL`. A `file://` page in WKWebView gets an opaque, unstable origin,
which makes `localStorage` and IndexedDB unreliable — silently breaking the
autosave backstop, the per-device collab member key, and language/motion
preferences. It also keeps relay fetches from arriving as `Origin: null`.
+- **The host is PER DOCUMENT**, a truncated SHA-256 of the file's path, not a
+ shared `deck`. Since this app opens any HTML document, a shared origin would
+ let one document read another's `localStorage` and IndexedDB — fine when every
+ file is yours, a real leak between unrelated third-party apps. Derived rather
+ than random because the origin IS the storage boundary: a random host per
+ launch would wipe that storage on every open. The trade is that moving or
+ renaming a file gives it a new origin and orphans its local state — which is a
+ cache and a backstop, never the document itself.
- **`bridge.js` is injected `.atDocumentStart`.** Bento decides whether it can
save during boot; injected later, the editor has already concluded it cannot.