Repository navigation
HTML attachments: a page's own script cannot move the Inbox; no network for pages from outside the project (port of #2511) - #3469
Open
backnotprop wants to merge 7 commits into
Open
backnotprop wants to merge 7 commits into
backnotprop wants to merge 7 commits into
Conversation
…rk for pages from outside the project Port of #2511 onto today's viewer. - Link clicks (a link, or a pageNavigation GET form's address) travel only on a private MessagePort the viewer hands the bridge after each ready, in srcdoc and served frames alike. A link-click posted on the window, which a page's own script can do, is ignored. BRIDGE_PROTOCOL_VERSION 1 -> 2. - A document opened outside the session's roots is marked by /api/doc (HTML_NO_NETWORK_MARKER, both runtimes) and the viewer puts connect-src 'none' right after its doctype. - inbox-links-in-place: the fixme for a page's own script is a real test.
Review fixes for the port of #2511. - A link guard runs first in the frame's document (before the sandbox shim and any page script, <head> included). It creates the channel, offers one end to the host with this load's key, and sends a link only while a person's click is being dispatched, for the clicked link's own href. - The host takes one offer per load it started, only with that load's key (new for every srcdoc, served address and frame.reload; a served frame's key rides its address as __pnk and the guard removes it). It never posts a port to the frame: a forged ready, a second offer, a page that moved its frame to the bare asset copy or rewrote itself with document.open get nothing. - On the bridgeScriptUrl path the asset runs the guard at the end of <head>; the limit for a <head> script there is documented. - Error path: the no-network marker goes after a leading doctype, in both runtimes. - Tests: the guard in a frame; one offer per load, keys, forged ready and second offers in htmlLinkProtocol; e2e hostile pages (bare asset copy, document.open, <head> script) in inbox-links-in-place.
| // a page's own head script included: it owns the only port the host takes | ||
| // links on, and a script before it could take that port. | ||
| describe("link guard placement", () => { | ||
| const KEY = "0123456789abcdef0123456789abcdef"; |
…ot at a web address
… without a DOM; pages-only e2e reads the keyed address
…attached page is really served
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Supersedes #2511. It brings that fix onto today's viewer by hand instead of merging the old branch. After review, the link channel was redesigned (see "Design after review").
The hole
An HTML attachment in the Inbox can move the window to another thread by its own script, with no click. The bridge sent a followed link to the parent as a window-wide
postMessage, and the parent accepted anylink-clickwhosesourcewas the frame's window. The page's own script runs in that same window, so it can post the same message. In the Inbox,onOpenLinkfollows thread links in place, so the page could move the window to any thread. On main this test is markedtest.fixme('its own script, with no click, does not move the window').The second hole: since 0.28.41, an HTML document outside the session's roots gets
connect-src 'none'only when the asset route serves it by URL. When the viewer renders the same document as a srcdoc, it has no response header, and the viewer removes the page's own CSP tags. So its scripts couldfetch, open an XHR or a WebSocket, or send a beacon.What #2511 decided
link-clickonly on a privateMessagePort, and ignores alink-clickposted on the window. Link clicks still go out only for a trusted click.BRIDGE_PROTOCOL_VERSIONgoes from 1 to 2. A stale cachedbridge-script.asset.jsshows the existing version-mismatch banner instead of silently losing links.rewriteHtmlAssetReferencesgetsnoNetwork, which adds<meta name="plannotator-network" content="none">(HTML_NO_NETWORK_MARKER,packages/shared/html-assets.ts). The Bun registry and Pi's mirror set it for an outside-root document, including on the rewrite's error path. WhenrawHtmlcarries the marker, the viewer puts<meta http-equiv="Content-Security-Policy" content="connect-src 'none'">right after the doctype, before every page script. The policy has noscript-src.Design after review
#2511 handed a fresh port to the frame after every
ready. Review showed a body script can make sure its own code receives that port: move the frame to the asset route's bare copy of the page (no bridge there) or calldocument.open()(which drops the bridge's listeners), then forgeready. A script in<head>could also take the port first. This PR now uses a different shape:packages/ui/components/html-viewer/linkGuard.ts). It is injected before the sandbox shim and before every script of the page,<head>included. It creates theMessageChannel, keeps one end in its closure, and offers the other end to the host in a singleplannotator-bridge-link-offermessage with this load's key.armLinkOfferinuseHtmlAnnotation, armed byHtmlViewer). It refuses and closes every later offer. It never posts a port to the frame, so a forgedreadygets nothing.frameUrl, the Inbox, and everyframe.reload) rides its address as__pnk, which the guard reads and removes withreplaceStatebefore any page script runs, solocation.searchis still the page's own.href(neverjavascript:). For a form, the click must be on a button or input, and the address must be a query with no fragment. The bridge hands clicks towindow.__plannotatorLinkGuard.follow(frozen, not replaceable). The built-ins it uses are captured before any page script runs.Why this is sounder than one offer per load alone: with only "one offer per load", the old document can post an offer at the moment the host re-arms. A page can trigger that moment itself (a forged "page gone" message makes the Inbox re-serve the frame), or it happens after a reload a person caused. A per-load key that the old document cannot know closes that race: the new key is only in the new srcdoc text or the new address. What each hostile case gets:
document.open(): the same; its new offer is a second offer.ready: nothing, because the host never sends ports.<head>script: it runs after the guard. It cannot get the port, and it cannot send anything without a person's click.Limit, stated in the README, HANDOFF and guide. A page script can still act during a person's click:
#thread=.On the
bridgeScriptUrlpath (an opt-in host option that Plannotator does not use), the bridge asset runs the guard at the end of<head>. There, a script that the page puts earlier in<head>can take the guard's place.How it works with #3162's link clicks
#3162 (owner approval
html-link-click) made a person's click do more: a link that is only a query (?variant=2) reloads the served page in place, the prototype window follows links, and withpageNavigationa GET form is reported as<action>?<fields>. All of these reach the host as the samelink-click, so all of them now go through the guard's port:link-clicksenders, the link interceptor andsubmitPageForm, hand the click to the guard. Both are still registered only outside live mode, so Inbox: let people click and navigate inside an agent's prototype page #3162's guard test (two senders, both inside!LIVE) holds as it was.frame.reload(search, hash)arms a new key and puts it in the new address. The page still sees exactly the query it was reloaded with.onOpenLink(href, frame)andframe.reloadare unchanged. The host still calls them, now from the port's handler.isInsideRoots, so its attachments never get the no-network marker. Their network policy (INBOX_PAGE_CSP) is unchanged.What changed from the original PR
ready". Thelink-portmessage is gone.prependNoNetworkMarker), in both runtimes. The page no longer drops into quirks mode.submitPageForm(new in Inbox: let people click and navigate inside an agent's prototype page #3162) goes through the guard.bridgePageNavigation.test.tspasses its sender to the form handler it runs.HtmlViewer, the CSP goes on afterinjectSandboxShim,injectPrintDefaultsand the guard, so it is still the first thing after the doctype.linkGuard.tsnext to the bridge script (apps/pi-extension/vendor.sh), as it does the sandbox shim.packages/ui/HANDOFF.mdentry for the protocol bump, next to Inbox: let people click and navigate inside an agent's prototype page #3162's entry.packages/ui surface changes
BRIDGE_PROTOCOL_VERSIONis 2.plannotator-bridge-link-offer{key}with one transferred port. The host no longer posts any message or port to the frame.link-clickis ignored.onOpenLinkandonLinkClickfire only from the accepted port.<script>comes first in<head>.__pnk, which the guard removes.window.__plannotatorLinkGuardexists in the frame.bridgeScriptUrltag gainsdata-plannotator-link-key.connect-src 'none'.index.tsor the exports map):linkGuard.ts.srcdoc.tshelpersinjectLinkGuard,hasNoNetworkMarker,NO_NETWORK_CSP_METAandinjectNoNetworkCsp.linkKeyoption onbuildBridgeScriptTagandbuildSrcdocInjection.armLinkOffer.test-setup/bridgeLinkPort.ts.HTML_NO_NETWORK_MARKER,RewriteHtmlAssetOptions.noNetworkandprependNoNetworkMarker.Tests
tests/e2e/inbox-links-in-place.spec.ts, each in a real browser with no click unless stated:fixme.readyand offers ports with every key it could read (its load address from the navigation entry, the inline key, and a made-up key), plus a windowlink-click.document.open(), then forgesreadyand offers again, several times.<head>script that steals any posted port, calls the guard with no click, forges offers and readys, and, during a person's click, asks the guard for another thread. It then clicks the page's own link, which still goes to that link's thread.readyand offer.tests/e2e/annotate-added-folder.spec.ts: an HTML page in a folder outside the project, opened from the Files sidebar, has itsfetchrefused with aconnect-srcviolation.linkGuard.test.ts(new, DOM): the guard's shipped string, run in a window with a fake parent. It checks:a.click()send nothing;javascript:link sends nothing;withLinkKeykeeps the page's query.htmlLinkProtocol.test.tsx:link-clickis ignored;ready(twice) gets no port;readygets a fresh port" is removed.srcdoc.test.ts:<head>, with no head, and after the CSP;HtmlViewer.bridgeAsset.test.tsx: the URL-path tag carries the key.HtmlViewer.sandbox.test.tsx: no-network placement.packages/shared/html-assets.test.ts: the error-path marker keeps the doctype first.doc-any-path.scenarios.ts: marker checks, run in both runtimes.App.htmlLinkDraft.test.tsxandpackages/inbox/htmlLinks.test.tsxsimulate the guard's offer throughtest-setup/bridgeLinkPort.ts.Run locally, each file by itself (DOM ones with
DOM_TESTS=1 --isolate), all passed:linkGuard(11)htmlLinkProtocol(20)srcdoc(142)HtmlViewer.bridgeAsset(15)bridgeAsset(9)bridgePageNavigation(6)HtmlViewer.sandbox(8)HtmlViewer.frameUrl(5)htmlLiveProtocol(39)htmlPinpointProtocol(80)prototypeWindow(5)App.htmlLinkDraft(3)HtmlViewer.*and bridge DOM filespackages/shared/html-assets.test.ts(29)tsc is clean for shared, server, ui, ui strict-consumer and inbox.
e2e: tests/e2e/inbox-links-in-place.spec.ts, tests/e2e/annotate-added-folder.spec.ts, tests/e2e/inbox-html-links.spec.ts, tests/e2e/annotate-html-links.spec.ts, tests/e2e/inbox-prototype-navigation.spec.ts, tests/e2e/inbox-pages-only.spec.ts, tests/e2e/inbox-html-phone.spec.ts, tests/e2e/inbox-html-fit.spec.ts, tests/e2e/inbox-attachments.spec.ts, tests/e2e/surface.spec.ts
Not verified locally
ci:e2e).packages/inbox/htmlLinks.test.tsxstarts an Inbox test server, so it was not run locally.apps/pi-extension/server/doc-any-path.test.tsneeds the vendoredgenerated/folder, so it was not run locally. The Pi change is the same as the Bun change, line for line.bridgeScriptUrlpath's guard (from the asset) is covered by unit tests only. No e2e uses that path.