Skip to content

feat(webview): offer unsandboxed mode when a tab's storage is blocked - #828

Open
ArizeSky wants to merge 8 commits into
Ark0N:masterfrom
ArizeSky:feat/webview-sandbox-notice
Open

ArizeSky wants to merge 8 commits into
Ark0N:masterfrom
ArizeSky:feat/webview-sandbox-notice

Conversation

@ArizeSky

Copy link
Copy Markdown
Contributor

What happens now

A proxied tab runs in an iframe without allow-same-origin, so its origin is opaque and localStorage/sessionStorage throw on access. Apps that keep a session there — SvelteKit among them — die halfway through boot and leave their own loading screen up, with nothing in Codeman to explain it.

The tab now names the cause and offers a way out, once the page actually touches storage.

Behaviour

  • The injected shim wraps the localStorage and sessionStorage getters instead of probing them. The first access calls the original getter, rethrows the original SecurityError, and posts one message to the parent.
  • The tab accepts that message only from the frame it mounted (event.source); the payload alone proves nothing.
  • The notice explains that the dashboard tried to use storage, and that unsandboxed mode shares Codeman's origin, so the dashboard could read the page and call its API. It should only be used for a dashboard you control.
  • Two actions: Reload unsandboxed, which sets trusted for that tab and remounts it, and Dismiss, which only closes the notice.
  • Both buttons are neutral, so the UI does not steer anyone toward the risky action. A click on Reload unsandboxed is the only thing that changes trusted.
  • In an unsandboxed frame the getters work, so nothing is posted. A page that never touches storage never sees the notice.

Tests

11 jsdom tests and 3 in a real Chromium:

  • a storage-free page posts nothing and shows no notice;
  • the first storage access posts once and rethrows the original SecurityError;
  • an unsandboxed frame gets the real Storage object and no report;
  • the notice only accepts messages from a mounted frame;
  • Reload unsandboxed PATCHes trusted and remounts; Dismiss leaves the setting alone;
  • the buttons read from skin tokens and neither is accented.

Also in this PR

The notice uses --bg-card; the old --bg fallback was a hardcoded dark and fought light skins. Its paragraph is one source line so the zh-CN dictionary matches it. The failure panels' btn-secondary, a class no stylesheet defines, now uses the shell's surface, border and accent tokens. docs/web-tabs.md describes when the notice fires. One patch changeset.

A proxied frame runs in an opaque origin by default, so `localStorage` throws on
first touch. A dashboard that keeps a token or a theme there dies mid-boot and
paints its own loading screen forever — the Halo WebUI tab that started this work
was a spinner with nothing in the log to explain it.

The injected runtime script now reports the blocked access once per load
(`codeman:webview-needs-origin`), and the tab turns it into a named choice: the
loading screen is replaced by the reason plus **Reload unsandboxed**, which flips
that one setting and remounts, and **Dismiss**. The frame is identified by
`event.source` against the iframes this tab mounted — a sandboxed frame's origin
is `null`, so the message itself proves nothing — and a tab that is already
unsandboxed ignores it.

The notice also owns the failure panel until dismissed or remounted: the frame's
`load` handler used to clear that overlay after the notice had been written,
which hid the one thing that explained the dead page.

Verified in a real sandboxed iframe against Halo WebUI: notice visible, one click
flips `trusted` to true, the frame remounts with `allow-same-origin` and lands on
the sign-in page.
The panel borrowed the failure overlay's `background: var(--bg, #0f1319)`, and
`--bg` exists in no skin, so the hardcoded dark fallback won every time while the
text kept following the theme: a light skin drew dark text on a dark plate. Use
`--bg-card`, the surface that steps with the skin, and set the text colour
explicitly so the contrast cannot depend on what the iframe painted.

The paragraph also wrapped across source lines, so its text node never matched a
dictionary key and stayed English under zh-CN. It is one line now, with the four
strings added to ZH_CN (the existing `Dismiss` key is reused, not duplicated).

Verified by computed styles in daylight-blue and paper-gray plus zh-CN:
dark #1b222c/#f3f6fa, light #ffffff/#1f2328, and 这个面板需要非沙盒模式.
Both panels asked for `btn-secondary`, a class no stylesheet defines, so the
actions fell through to the browser's default button: system chrome sitting on
a themed surface, with Reload and Dismiss carrying identical weight. Paint them
from the same tokens as the rest of the shell (surface, border, text) and give
the one action worth taking the accent.

Verified by computed styles in daylight-blue and paper-gray — #202833/#2a323d
and #eef1f4/#e4e9ef surfaces, accent #38b6f0 and #0969da — with the notice
translated under zh-CN.
The injected shim probed `window.localStorage` at boot. In a frame without
`allow-same-origin` that getter always throws, so every sandboxed dashboard
posted `codeman:webview-needs-origin`, including a page that never touched
storage and never ran any of its own script. The tab then covered that page
with the unsandboxed-mode notice.

Wrap the `localStorage` and `sessionStorage` getters instead. Page code that
reads either one hits the original getter; a throw is rethrown unchanged, so
the page sees the same SecurityError, and the failure is reported to the parent
once per load. A working accessor returns the real Storage object and posts
nothing, so an unsandboxed frame sees no change. Defining the wrapper is
guarded: a context where defineProperty refuses simply keeps the native
accessor and reports nothing.

The jsdom tests boot the shim in a frame with throwing storage getters: no
message on load, one after the first page read, and the original error still
surfaces. A working frame returns its Storage object and stays silent.
jsdom cannot reproduce the opaque origin a sandboxed iframe gets: its
`localStorage` is a normal object, so the boot-probe bug could not be seen
there. Drive the rewritten proxy pages in real Chromium instead.

The parent page mounts its iframe with the same sandbox tokens the tab
uses (no `allow-same-origin`). A page whose body is a single <h1> must not
produce `codeman:webview-needs-origin`; a page that reads `localStorage`
must produce exactly one and still catch the original SecurityError. Both
pages are served by request interception, so no Codeman server is needed.

Use a cached Chromium build when the revision this Playwright release pins is
absent; the browser suite excludes this file like every other Playwright test.
The docs and the changeset said the report fires once per load, which was the
old boot-probe behaviour. Say what the shim does now: it reports the first time
page code touches `localStorage` or `sessionStorage`, and a page that never
does stays quiet.

The notice's copy called unsandboxed mode 'the same trust a proxied dashboard
already relies on'. It is not: a trusted frame shares Codeman's origin and can
read the page and call its API. Say that plainly, and update the zh-CN entry
with the new English key.

Prettier also normalises docs/web-tabs.md (table widths and emphasis markers).
The first button in the failure panel carried the accent, which made
'Reload unsandboxed' the visually preferred action in a panel whose other
choice is to stay sandboxed. Unsandboxing is a security change, not an
upgrade, so paint both actions from the same surface and border tokens.

Reload stays first and Dismiss second; the test now pins the neutral rule and
the absence of a first-child accent instead of asserting it.
The previous docs commit ran Prettier over two markdown files the repo does not
format: table column widths and emphasis markers were rewritten across
docs/web-tabs.md, and the changeset frontmatter changed quotes. That churn hides
the actual wording change.

Restore both files to their pre-formatting branch state, then re-apply only the
intended edits by hand: the storage-trigger paragraph in docs/web-tabs.md and the
two body paragraphs of the changeset. The frontmatter stays "aicodeman" with
double quotes.
@Ark0N

Ark0N commented Nov 16, 2026

Copy link
Copy Markdown
Owner

Thanks for this, and for the tests. The real-Chromium check that a plain page reports nothing is the right test to have. The PR makes a web tab explain why a sandboxed dashboard is stuck when its page hits blocked localStorage/sessionStorage, and offers a one-click switch to unsandboxed mode.

There is one open design question on my side. Two smaller fixes apply to any version of the notice that stays.

Open question: should the page itself be able to raise the unsandbox offer?

The listener at src/web/public/webview-tabs.js:301 checks event.source. That proves the message came from the mounted frame, but the frame is the untrusted party. A page that never touches storage can post {type: 'codeman:webview-needs-origin'} itself (I checked this in Chromium). Codeman then draws its own notice, and its first button sets trusted: true (webview-tabs.js:341) and remounts the frame with allow-same-origin. From then on the page has Codeman's origin, so it can read the Codeman page and call the API that starts agents. Today that switch is in the tab's settings, next to a warning that names the agent API. The offer also reaches tabs created automatically from loopback links in agent output (openUrlInWebTab), which the code at webview-tabs.js:144-150 already treats as agent-controlled content. It is still open whether Codeman should show a page-triggered offer at all, and if it does, whether the button may flip the setting in one click or should open the tab's settings instead. Please hold any rework of that part until it is decided.

1. The notice covers dashboards that work (src/web/public/webview-tabs.js:319, src/web/public/styles.css:16136)

The shim reports the first storage access that throws, whether or not the page catches it. Many dashboards probe storage in a try/catch (theme toggles, language detectors, analytics and error-reporting SDKs) and run fine sandboxed. The notice reuses the failure panel, which is position: absolute; inset: 0 on an opaque background, so a dashboard like that disappears behind "This dashboard needs unsandboxed mode". In Chromium, a page that caught the error and rendered "Dashboard rendered (dark)" was completely hidden by the notice. STORAGE_SOURCE in your own browser test has the same shape. Dismiss only lasts for one load, so the notice comes back on every reload or remount. Please render it as a strip that leaves the dashboard visible and usable instead of the full-frame failure panel.

2. The notice outlives an in-place navigation (src/web/public/webview-tabs.js:525 and :563)

Only Dismiss or a remount clears wrap.dataset.webviewNeedsOrigin. When _mountWebviewFrame navigates an existing frame (a deep link from an agent's loopback link, or the lost-frame recovery), the new document loads under the old notice, and the load handler keeps it up even if the new page never touches storage. Please clear the flag and the webview-frame--failed class in the navigate branch; the next document reports again if it needs to. On Dismiss (webview-tabs.js:360), also put _webviewFailureHtml(id) back into the panel. Otherwise a later no-load timeout shows the storage notice instead of "This URL did not load".

Two smaller things:

  • The notice says the dashboard could "call its API", but the settings checkbox says "call the API that starts agents". Please use the stronger wording in the notice and in its zh-CN entry.
  • docs/architecture-invariants.md:612 (Web tabs, "Sandbox default") documents the shim and the codeman:webview-lost message. It needs a sentence on codeman:webview-needs-origin and the event.source rule.

The theming fix for the failure panel (--bg-card, buttons painted from skin tokens) is good as it is. Typecheck, lint, format and all the webview tests pass locally.

The open question decides the final shape of this PR. Items 1 and 2 apply to any version of the notice that stays, so you can start on them now.

@Ark0N

Ark0N commented Nov 27, 2026

Copy link
Copy Markdown
Owner

The open question is decided: the notice may keep its one-click switch, with one exception.

A web tab that Codeman opened by itself from a link in agent output (openUrlInWebTab, the records webview-tabs.js:144-150 already treats as agent-controlled) never gets the switch. There the notice only explains what is blocked and points to the tab's settings. For a tab the user saved themselves, the button stays one click, as you built it.

Since that button is then the only place the user reads the consequence before switching, please use the settings checkbox's wording in the notice and in its zh-CN entry: the page will be able to "call the API that starts agents".

The rest of the review stands as written:

  1. The notice becomes a strip that leaves the dashboard visible and usable, instead of the full-frame failure panel.
  2. The navigate branch clears the notice state and the webview-frame--failed class, and Dismiss puts _webviewFailureHtml(id) back into the panel.
  3. One sentence in docs/architecture-invariants.md (Web tabs, "Sandbox default") on the codeman:webview-needs-origin message and the event.source rule.

Please add a test that a tab created from an agent link shows the notice without the switch. With these in, the PR goes through its final review. Thanks for waiting on this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants