Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

home/ios: only the open document may use the bridge (#601)

nyblnetc9f80d3f3609
diff --git a/home/README.md b/home/README.md
index 8a56d6d1..8d985ac6 100644
--- a/home/README.md
+++ b/home/README.md
@@ -460,7 +460,7 @@ here rather than discovered later.
| per-document origin | `bento-tray://<sha24>` custom scheme | `https://<sha24>.bento-tray.invalid`, intercepted | same |
| document served from memory, never parsed | ✓ | ✓ | same |
| bridge injected at document start | `WKUserScript(.atDocumentStart)` | `addDocumentStartJavaScript` | same |
-| bridge reachable only from the document | `forMainFrameOnly` | `allowedOriginRules` + `isMainFrame` | same |
+| bridge reachable only from the document | every message checked: main frame + own origin (`BridgeSender`) | `allowedOriginRules` + `isMainFrame` | same |
| first `begin` = open document, later = export | ✓ | ✓ | same |
| an export can never address the open file | `exportName` + `targetsOpenDocument` | identical logic | same |
| page-supplied filenames sanitised | `safeFileName` | `safeFileName` | same |
diff --git a/home/ios/BridgeSender.swift b/home/ios/BridgeSender.swift
new file mode 100644
index 00000000..7ff7b60f
--- /dev/null
+++ b/home/ios/BridgeSender.swift
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: MIT
+// Copyright (c) 2026 The Bento authors
+
+import Foundation
+
+/// Whether a bridge message came from the open document itself.
+///
+/// Every op is checked, `begin` included: the top frame of this editor, served
+/// from this document's own `bento-tray://<originHost>`. Anything else is
+/// ignored without a reply. Android's equivalent is `allowedOriginRules` plus
+/// `isMainFrame`.
+///
+/// Foundation-only, so the rule can be checked standalone with `swiftc`.
+/// EditorViewController passes in what WebKit reports about the sender
+/// (`message.frameInfo`); it decides nothing itself.
+enum BridgeSender {
+ static func accepts(isMainFrame: Bool, scheme: String, host: String, documentHost: String) -> Bool {
+ guard isMainFrame else { return false }
+ guard scheme == "bento-tray", host == documentHost, !documentHost.isEmpty else { return false }
+ return true
+ }
+}
diff --git a/home/ios/EditorViewController.swift b/home/ios/EditorViewController.swift
index 46fde0b2..b43cf0be 100644
--- a/home/ios/EditorViewController.swift
+++ b/home/ios/EditorViewController.swift
@@ -437,6 +437,12 @@ final class EditorViewController: UIViewController, WKScriptMessageHandler, WKUR
// MARK: - the save bridge
func userContentController(_ c: WKUserContentController, didReceive message: WKScriptMessage) {
+ // Only the open document may use the bridge. Checked before anything in
+ // the message is read; see BridgeSender.
+ let sender = message.frameInfo.securityOrigin
+ guard BridgeSender.accepts(isMainFrame: message.frameInfo.isMainFrame,
+ scheme: sender.protocol, host: sender.host,
+ documentHost: originHost) else { return }
guard let m = message.body as? [String: Any],
let id = m["id"] as? Int, let op = m["op"] as? String else { return }