diff --git a/AGENTS.md b/AGENTS.md index 9653fbef..d1bf5219 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -109,7 +109,7 @@ The HTTP server connections and the WebSocket read/write loops are coroutines (` Filesystems implement `open(std::filesystem::path) -> std::optional` and are composed with `fs::Multi<>`; the first provider returning a file wins. `NativeDebugFS` is development-only and must not be presented as a production sandbox. -Bridge calls are asynchronous, and the two directions differ. `webFront.cppFunction(name)(...)` always returns a JavaScript Promise (also for `void` callbacks), so tests and examples must await or handle it. On the C++ side, only the untyped `ui.jsFunction(name)(...)` is fire-and-forget; `ui.jsFunction(name)(...)` returns a `std::future`. `ui.jsFunctionSender(name)(...)` owns encoded arguments and dispatches at sender start; co_await propagates the receiver stop token. Cancellation completes stopped locally and discards a late reply; JavaScript continues running. See `docs/ASYNC_EXECUTION.md` for lifetime and completion-thread rules. On missing functions, callback exceptions, malformed returns, and disconnects, a C++ future completes exceptionally (`get()` throws) and a JavaScript Promise rejects. Result types are limited to the bridge's scalar, string, tuple, numeric typed-array, packed `std::bitset` and list types. A list is a `std::vector` of strings, tuples, typed arrays or bitsets (JavaScript: `webFront.list(items)` of strings, typed arrays or tuples, received as an `Array`; numbers go as typed arrays; a plain short array is a tuple that a `std::vector` destination also accepts); elements are homogeneous and lists do not nest. Bitsets use `Uint8Array` with exactly `ceil(N / 8)` bytes, least-significant bit first in each byte, and zero high padding bits; decoding rejects wrong types, sizes and padding. JavaScript must know the logical bit count from the application contract. Strings of 65,536 bytes or more use the `longString` code (32-bit length), chosen by the encoder; the only bound is the buffers policy's `maxMessageSize` (64 MiB by default, JavaScript's `maxMessageBytes`): beyond it C++ throws `std::length_error` when it encodes, which rejects the Promise when it is a callback's result, and JavaScript throws a `RangeError` before sending. A `cppFunction` callback may return a sender (a `webfront::ResultTask` coroutine): the Promise settles when it completes, the browser's disconnection stops it and drops its answer, and shutdown joins it. Callback signatures are deduced for ordinary lambdas, function pointers, and `std::function`; generic lambdas and overloaded call operators need explicit `cppFunction`. Do not write tests or documentation that present JavaScript calls as fire-and-forget, imply an untyped C++ call returns a value, or suggest that richer objects cross the bridge (only the types above do). Registered callback objects must outlive their registration through owned storage; never capture a forwarding-reference parameter by reference in a stored handler. +Bridge calls are asynchronous, and the two directions differ. `webFront.cppFunction(name)(...)` always returns a JavaScript Promise (also for `void` callbacks), so tests and examples must await or handle it. On the C++ side, only the untyped `ui.jsFunction(name)(...)` is fire-and-forget; `ui.jsFunction(name)(...)` returns a `std::future`. `ui.jsFunctionSender(name)(...)` owns encoded arguments and dispatches at sender start; co_await propagates the receiver stop token. Cancellation completes stopped locally and discards a late reply; JavaScript continues running. See `docs/ASYNC_EXECUTION.md` for lifetime and completion-thread rules. On missing functions, callback exceptions, malformed returns, and disconnects, a C++ future completes exceptionally (`get()` throws) and a JavaScript Promise rejects. Result types are limited to the bridge's scalar, string, tuple, numeric typed-array, packed `std::bitset` and list types. A list is a `std::vector` of strings, tuples, typed arrays or bitsets (JavaScript: `webFront.list(items)` of strings, typed arrays or tuples, received as an `Array`; numbers go as typed arrays; a plain short array is a tuple that a `std::vector` destination also accepts); elements are homogeneous and lists do not nest. Bitsets use `Uint8Array` with exactly `ceil(N / 8)` bytes, least-significant bit first in each byte, and zero high padding bits; decoding rejects wrong types, sizes and padding. JavaScript must know the logical bit count from the application contract. Strings of 65,536 bytes or more use the `longString` code (32-bit length), chosen by the encoder; the only bound is the buffers policy's `maxMessageSize` (64 MiB by default, JavaScript's `maxMessageBytes`): beyond it C++ throws `std::length_error` when it encodes, which rejects the Promise when it is a callback's result, and JavaScript throws a `RangeError` before sending. A `cppFunction` callback may return a sender (a `webfront::ResultTask` coroutine): the Promise settles when it completes, the browser's disconnection stops it and drops its answer, and shutdown joins it. Callback signatures are deduced for ordinary lambdas, function pointers, and `std::function`; generic lambdas and overloaded call operators need explicit `cppFunction`. Do not write tests or documentation that present JavaScript calls as fire-and-forget, imply an untyped C++ call returns a value, or suggest that richer objects cross the bridge (only the types above do). Registered callback objects must outlive their registration through owned storage; never capture a forwarding-reference parameter by reference in a stored handler. Names starting with `webfront.inspector.` are reserved to the inspectors (ADR-027). WebFront guards only its own reserved functions: an example or test callback that writes the project or what the server serves, starts a process or exposes a sensitive state takes the calling `UI` and checks `ui.isLocalClient()`, and documentation must not present the application's callbacks as guarded by WebFront. The embedded `WebFront.js` wire format and C++ `Messages.hpp` implementation must evolve together. Any protocol change requires encoding/decoding unit tests and a real-browser regression test. diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 08901374..b4d0162a 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -13,6 +13,7 @@ these entries stays in the GitHub releases and the Git history. ### Breaking - A custom frontend whose `open()` takes `const ViewLaunch&` declares `static bool providesViewProof()`, `true` when it gives the proof to its view and `false` otherwise, or the developer environment no longer compiles with it: undeclared, its proven view would leave project writes and C++ commands unguarded (#680, ADR-027, `docs/FRONTENDS.md`). - `cppFunction` (and `Space::cppFunction`) refuses the names starting with `webfront.inspector.`, reserved to the inspectors, with `std::invalid_argument`: rename such a function, and its `webFront.cppFunction(...)` calls in the page, to a name of your own (#680, ADR-027). +- An external tool that writes the project (`webfront.developer.*` writes) or starts terminal commands (`webfront.commands.start`, `cancel`) through the bridge is refused with `webfront.inspector.denied` when the frontend proves its view: run it in that view, use MCP automation, or set `trustLoopbackClients` knowingly (#680, ADR-027, `docs/DEVELOPER_ENVIRONMENT.md`). ### Added - Open the inspectors from the WebFront tab's menu of the developer environment: each panel shows in a floating window of the page and can be detached to a native secondary window (WebKitGTK, WKWebView, WebView2) or, with `trustLoopbackClients`, a tab of the system browser; the choice and the place are kept per project (#680, ADR-027, `docs/DEVELOPER_ENVIRONMENT.md`). @@ -41,7 +42,8 @@ these entries stays in the GitHub releases and the Git history. ### Changed - Destroying the handle of a watch (`inspect::Watch`) waits for an operation of the inspectors in progress on another thread: destroy it outside every lock its source takes, or declare the watch with the state it owns (#682, ADR-027, `docs/WATCHES.md`). -- The inspectors show where each `cppFunction` was registered; existing calls of `cppFunction` and `Space::cppFunction` compile unchanged (#681, ADR-027). +- `cppFunction` and `Space::cppFunction` take a last, defaulted `std::source_location`, which the inspectors show; existing calls compile unchanged, the explicit `cppFunction` form included (#681, ADR-027). +- Document the security contract of the application's callbacks: one that writes the project or what the server serves, starts a process or exposes a sensitive state takes the calling `UI` and checks `ui.isLocalClient()`, since WebFront guards only its own functions (#686, ADR-027, `docs/DEVELOPING_WITH_AGENTS.md`). - `WebFront.js` opens its WebSocket on the directory of the page (`ws://host/dir/` instead of `ws://host`), which tells the server a page's space (#671) and lets an application be deployed under a sub-path of a site (#672). - A request whose path holds a control character (`%00`...) is refused with 400, as a `..` already was, instead of being looked up as a file (#672). - A `+` in the path of a file request is a literal `+`, as for the routes, the spaces and `ui.path()`: `/a+b.html` serves `a+b.html`, no longer `a b.html`, which `/a%20b.html` names (#672). diff --git a/docs/DEVELOPER_ENVIRONMENT.md b/docs/DEVELOPER_ENVIRONMENT.md index cb550ae3..d0a1e623 100644 --- a/docs/DEVELOPER_ENVIRONMENT.md +++ b/docs/DEVELOPER_ENVIRONMENT.md @@ -450,13 +450,30 @@ environment's `mount`, not to the embedded bootstrap page): with the mouse or with the arrow keys on its title bar), whose frame opens no connection of its own and borrows the page's bridge, since the adapters give the proof of the view to the main frame only. **Detach** moves the panel to a window of its own, and the next opening of that panel goes there again; **Attach**, in that window, brings it back to the floating - window, where it then opens: + window, where it then opens. The containers follow the access each frontend actually gives, never its name alone: - | Frontend of the page | Detached container | - | --- | --- | - | WebKitGTK, WKWebView, WebView2 | a native secondary window (`webfront.inspector.openWindow`, `openSecondaryWindow`), which carries the proof of the view; opening the panel again brings it to the front, and closing the main window closes it | - | System browser with `trustLoopbackClients` | a tab of the system browser (`window.open`), with its own loopback connection; when the pop-up blocker refuses it, the panel stays in the floating window and a message offers to try again; the tab closes when the page that opened it is gone | - | CEF, or an embedded view without secondary windows | none: Detach is greyed, with the reason in its tooltip (`window.open` there would open a frame without the proof) | + | Frontend of the page | Inspectors | Panel opens in | Detached container | + | --- | --- | --- | --- | + | WebKitGTK, WKWebView, WebView2 | admitted: the view proves itself | the floating window | a native secondary window (`webfront.inspector.openWindow`, `openSecondaryWindow`), which carries the proof of the view; opening the panel again brings it to the front, and closing the main window closes it | + | CEF | admitted: the view proves itself | the floating window | none: CEF opens no secondary window, and `window.open` there would open a frame without the proof; Detach is greyed, with the reason in its tooltip | + | System browser (default of `webfront::WebFront`) | **refused by default**: the entries are greyed and say how to enable them | none | none | + | System browser with `trustLoopbackClients` | admitted: any loopback page | the floating window | a tab of the system browser (`window.open`), with its own loopback connection; when the pop-up blocker refuses it, the panel stays in the floating window and a message offers to try again; the tab closes when the page that opened it is gone | + + `NativeOrBrowserFrontend` behaves as its native engine while it opens a native window, and as the system browser once it + falls back to it. A custom frontend gives the proof only if it implements the `open(..., const ViewLaunch&)` overload and + declares `providesViewProof()` ([frontends](FRONTENDS.md#view-association)). + + **Enabling the inspectors with the system browser.** Prefer an embedded frontend (`webfront::NativeApp`, or + `WebFrontWithFrontend<...>`): its view proves itself and nothing else is admitted. Otherwise, set `trustLoopbackClients` + in a development build only, on a machine with one user and no local proxy or tunnel: + + ```cpp + webfront::BasicDeveloperApp<> app{"my-project", {.trustLoopbackClients = true}}; // system browser + app.bootstrap("developer"); + ``` + + Every loopback connection is then a local client: it reads the inspectors, and it also passes the `ui.isLocalClient()` + check of the application's own callbacks (below). The option is the application's choice; the environment never sets it. Attach brings the panel back to the page that detached it only, even when other pages of the application are open on the same origin. A page keeps its identifier across a reload through Web Locks; in a browser without them, a reloaded @@ -522,7 +539,10 @@ environment's `mount`, not to the embedded bootstrap page): The inspectors' own calls are not shown. Attached, the panel reads the page directly; detached, its window does not see the page, so the page publishes the same state to C++ every 250 ms while the panel reads, and stops within fifteen seconds of its last read (`webfront.inspector.bridgeSource`, `publishBridge` and `bridge`, admitted pages - only). + only). The panel detaches like the others, to a native secondary window with WebKitGTK, WKWebView and WebView2. C++ + knows 256 connected pages at most, and keeps the published state of 16 of them: beyond 256 connected pages, a page's + announcement is refused with `webfront.inspector.unavailable`, and its detached panel says that the page is not + connected. The tab is a button (`aria-expanded`, `aria-haspopup="menu"`, 44 px target), its menu a `menu` of `menuitem`s of 44 px, and the window a non-modal dialog. The interface stays in the preview @@ -532,6 +552,39 @@ or the tab first). This is a layout, not a sandbox (see [limitations](#dependencies-storage-and-limitations)). A page that mounts the terminal itself (`runtime.mount`) with a preview frame gets the same layout; `workspace: false` keeps the page as it is. +### The inspectors and the application's callbacks + +The inspectors exist only in the developer environment: `bootstrap("developer")` registers the reserved +`webfront.inspector.*` functions, an application without it has none, and a distribution build +(`WEBFRONT_DISTRIBUTION=ON`) compiles none of them; `app.watch` then returns an empty handle. They read what the application +shows them and write only the [watched values](WATCHES.md#writing-a-value) that the application made writable. They are +not a sandbox: an admitted page shares the authority of the bridge. + +When the frontend proves its view, WebFront guards its own entry points that change what the view runs or what the server +serves: the project writes (`webfront.developer.begin`, `beginNew`, `write`, `commit`, `commitGroup`, `abort`) and +starting or cancelling a terminal command (`webfront.commands.start`, `cancel`) require an admitted page, like the +inspectors, and are refused with `webfront.inspector.denied` otherwise. The reads of the project and +`webfront.commands.list` keep the origin check. With the system browser no view is proven, so these operations stay open +to every connection that the [content policy](CONTENT_POLICY.md) admits. + +**The callbacks of the application are its own contract** (ADR-027, section 2). WebFront does not know what a +`cppFunction` does and does not guard it. A callback that writes the project or what the server serves, starts a process, +or exposes a state the application would not show to an unproven client takes the calling `UI` as its first parameter and +checks it before acting: + +```cpp +app.cppFunction("app.saveScript", [&](webfront::NativeApp::UI ui, std::string text) { + if (!ui.isLocalClient()) // not the application's view, nor a trusted loopback page + throw std::runtime_error("app.saveScript requires the application's view"); // the Promise rejects + saveScript(text); +}); +``` + +Without that check, such a callback opens again the door the guard above closes: a local process could call it, then let +the proven view load what it wrote. The **Caller** column of the Functions panel shows which callbacks receive the `UI`, +and so can make the check. An agent reads the same state through MCP, with the `state` grant, and never writes it +([Developing with an agent](DEVELOPING_WITH_AGENTS.md#reading-the-running-application-webfrontstate)). + ## The terminal and the application's commands The terminal of the environment (developer distribution 0.5.0, #308) is the only WebFront terminal; it is served diff --git a/docs/DEVELOPING_WITH_AGENTS.md b/docs/DEVELOPING_WITH_AGENTS.md index b08281a6..0bd8c509 100644 --- a/docs/DEVELOPING_WITH_AGENTS.md +++ b/docs/DEVELOPING_WITH_AGENTS.md @@ -36,6 +36,29 @@ not how the interface is shipped. An application that serves a document root wit line on stderr at its first `show()` saying so. Only [scalars, strings, tuples, typed arrays, bitsets and lists](../README.md#contracts-and-limitations) cross the bridge. +### A callback that acts checks its caller + +WebFront guards its own functions, not the application's: it does not know what a `cppFunction` does. **A callback that +writes the project or what the server serves, starts a process, or exposes a state the application would not show to any +client takes the calling `UI` as its first parameter and checks `ui.isLocalClient()` before acting** (ADR-027, section 2). +`isLocalClient()` is true for the view of an embedded frontend, which proves itself, and for a loopback page only when the +application sets `trustLoopbackClients`. + +```cpp +app.cppFunction("app.saveScript", [&](webfront::NativeApp::UI ui, std::string text) { + if (!ui.isLocalClient()) + throw std::runtime_error("app.saveScript requires the application's view"); // the Promise rejects + saveScript(text); +}); +``` + +The calling `UI` is not a parameter on the JavaScript side: the page still calls `webFront.cppFunction('app.saveScript')(text)`. +A callback that only computes and returns a value needs no check. Without it, a callback that acts can be called by a +local process that forges the page's origin (the origin check alone cannot tell it from the page), then the proven view +may load what it wrote. The Functions panel of the +[inspectors](DEVELOPER_ENVIRONMENT.md#the-inspectors-and-the-applications-callbacks) shows which callbacks receive the +caller. + ## The development loop `wf create ` takes the name of a **starter**, not the name of your project: `app` is a minimal TypeScript diff --git a/docs/PROJECT_STATUS.md b/docs/PROJECT_STATUS.md index 836903ad..a85f2036 100644 --- a/docs/PROJECT_STATUS.md +++ b/docs/PROJECT_STATUS.md @@ -42,6 +42,12 @@ WebFront is a header-only C++23 HTTP/WebSocket core plus a standalone developer `webfront::Commands`. From environment 0.9.1, a project that has a preview opens on its interface over the whole window, with the terminal in a floating window behind a tab on the window's edge. See the [developer reference](DEVELOPER_ENVIRONMENT.md). +- **State inspectors (development only).** The menu of the environment's tab opens the inspectors of the running + application ([ADR-027](adr/ADR-027-inspecteurs-de-l-etat-de-l-application.md)): the registered C++ functions, the + connected browsers and tasks, the [watches](WATCHES.md) the application declares with `app.watch`, the locked packages + and the state of the bridge in the page. A panel opens in a floating window of the page and detaches to a native + secondary window with WebKitGTK, WKWebView and WebView2, or to a tab of the system browser with `trustLoopbackClients`. + See [the inspectors](DEVELOPER_ENVIRONMENT.md#the-page-interface-and-floating-terminal). - **Distribution.** `wf embed` generates the application's compressed C++ provider. An interface kept as a plain directory, with no build step, is embedded from CMake alone by `webfront_embed_directory()` (identity encoded; see [embedded assets](EMBEDDED_ASSETS.md#embedding-an-interface-directory-from-cmake)). @@ -178,6 +184,18 @@ are retained for investigation; diagnostics alone do not establish a correction. restricts browser origins and navigation. The optional [TLS listener](TLS.md) adds HTTPS and WSS with TLS 1.3 and ChaCha20-Poly1305. Each listener admits 16 concurrent handshakes on the I/O thread. Client authentication remains an application responsibility. +- The state inspectors and their reserved `webfront.inspector.*` functions exist only in the developer environment; a + distribution build compiles none of them, and `app.watch` returns an empty handle there. They answer only an admitted + page (`ui.isLocalClient()`): the view of an embedded frontend, which proves itself, and, only when the application sets + `trustLoopbackClients`, any loopback page. With the system browser they are therefore refused by default. When the + frontend proves its view, the environment's project writes and the start or cancellation of terminal commands are + reserved to admitted pages too; with the system browser, which proves no view, they stay open to every client the origin + check admits. The application's own callbacks are not guarded: one that acts must check `ui.isLocalClient()` itself + ([ADR-027](adr/ADR-027-inspecteurs-de-l-etat-de-l-application.md), section 2). An inspector writes only a value the + application made writable; a write is refused as stale unless the target and its ancestors still show what was + displayed, which cannot detect a move between two elements without keys that are identical in every visible way. A late + page of a large container without random access (`std::list`, `std::map`) is reached by a linear walk, under the + application's lock; a `PagedSource` pages itself. See [Watches](WATCHES.md). - `fs::NativeStorage` confines writable storage to a declared root while resolving each path, but on POSIX it cannot prevent the effective user or the superuser from moving a directory out of the root during an operation: it detects such a move before a commit and after each operation, at diff --git a/docs/WATCHES.md b/docs/WATCHES.md index c5a81155..7e73b8d3 100644 --- a/docs/WATCHES.md +++ b/docs/WATCHES.md @@ -33,7 +33,9 @@ call, which the panel shows. This is a development tool, not a sandbox. Only a page that is admitted reads or writes a watch: the view of an embedded frontend, or a loopback page when the application chose `trustLoopbackClients` (see -[the menu and the inspectors](DEVELOPER_ENVIRONMENT.md)). +[the menu and the inspectors](DEVELOPER_ENVIRONMENT.md#the-page-interface-and-floating-terminal)). A watch is +read-only unless its source writes, and only the panel writes it: the [MCP reads](#reading-a-watch-from-an-agent) never do. +The application's own callbacks follow [their own contract](DEVELOPER_ENVIRONMENT.md#the-inspectors-and-the-applications-callbacks). ## Sources: one operation, one access diff --git a/js/developer/environment/bundles.json b/js/developer/environment/bundles.json index d562aa12..fc819621 100644 --- a/js/developer/environment/bundles.json +++ b/js/developer/environment/bundles.json @@ -50,7 +50,7 @@ "js/developer/environment/embed.mjs": "3d3b5036c34c23a1fa0512c1eea42f26d5f30e054545b61fe83a5c378b74e09e", "examples/life/ui/main.tsx": "7b1ff285d2a6b8f897ef12df872886e61ef4b24e2358a9423b76b38ab75c85d3", "examples/life/ui/style.css": "ac13886f0aea2adfdca21c487467a8f6e647feffb88c51d81766e628584ea5cd", - "js/developer/starters/agent-guide.md": "6c75cb4434360ab91bb857b19ee3f750305d06a10a01276854123f47e896eaaa", + "js/developer/starters/agent-guide.md": "46b1f85589564ae08f37487184422e0634c11ea97cbca9d840315a0bf93c378c", "js/developer/starters/app/main.ts": "b4f43fd3cd9136d86dd3e6e89e29092ddf3b67066c2cc08d78078d15a445054e", "js/developer/starters/app/style.css": "6a042eff15ee9b1603c95d3e4e974a03876e1c365389553e84eb7c472aded180", "js/developer/tools/distribution.json": "06c5e81e5fbbe3407488840646fb8dd1b36b540fca43dd85dcd5d7342261a36a", diff --git a/js/developer/starters/agent-guide.md b/js/developer/starters/agent-guide.md index 0307f925..e3c86ed2 100644 --- a/js/developer/starters/agent-guide.md +++ b/js/developer/starters/agent-guide.md @@ -13,6 +13,10 @@ backend. own window. `wf` installs npm packages, compiles TS/TSX, type-checks and runs Jasmine specs: no Node.js, no package manager. - Hand-written HTML with classic `