Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(cowork): make sandboxed artifact previews interactive again
Akarshan Biswas512e4295cb20
diff --git a/web-app/src/lib/__tests__/previewShim.test.ts b/web-app/src/lib/__tests__/previewShim.test.tsindex 3d5fa32a2..ac56fa49b 100644--- a/web-app/src/lib/__tests__/previewShim.test.ts+++ b/web-app/src/lib/__tests__/previewShim.test.ts@@ -4,6 +4,8 @@ import { PREVIEW_SHIM_SCRIPT, previewShimError } from '../previewShim'describe('PREVIEW_SHIM_SCRIPT', () => {const posted: unknown[] = []const sessionBefore = window.sessionStorage+ // What the wrapped history call reached the native method with.+ const historyCalls: Array<unknown[]> = []// One run for the file: the shim guards against a second install, and a// fresh install per test would stack listeners and double every report.@@ -14,6 +16,21 @@ describe('PREVIEW_SHIM_SCRIPT', () => {throw new DOMException('sandboxed', 'SecurityError')},})+ // Stand in for the sandbox: any history call carrying a URL is rejected,+ // exactly as an opaque origin rejects it. Installed before the shim so the+ // shim wraps this throwing version.+ for (const name of ['pushState', 'replaceState'] as const) {+ Object.defineProperty(window.history, name, {+ configurable: true,+ writable: true,+ value(...args: unknown[]) {+ if (args[2] != null) {+ throw new DOMException('sandboxed', 'SecurityError')+ }+ historyCalls.push(args)+ },+ })+ }// jsdom's window.parent is the window itself.vi.spyOn(window, 'postMessage').mockImplementation((data: unknown) => {posted.push(data)@@ -48,6 +65,31 @@ describe('PREVIEW_SHIM_SCRIPT', () => {expect(window.sessionStorage).toBe(sessionBefore)})+ // A deep-linked slideshow calls pushState on every slide change; a thrown+ // SecurityError there aborts the click handler, so the button looks dead.+ it('keeps a URL-bearing history call from throwing', () => {+ historyCalls.length = 0+ expect(() =>+ window.history.pushState({ slide: 3 }, '', '#slide-3')+ ).not.toThrow()+ expect(() =>+ window.history.replaceState({ slide: 4 }, '', '?s=4')+ ).not.toThrow()+ // The state change survives; only the URL the sandbox forbids is dropped.+ expect(historyCalls).toEqual([+ [{ slide: 3 }, ''],+ [{ slide: 4 }, ''],+ ])+ })++ it('passes an already-safe history call straight through', () => {+ historyCalls.length = 0+ window.history.pushState({ ok: 1 }, '')+ // The wrapper forwards its three params; a URL-less call carries undefined,+ // which the native stand-in accepts without throwing.+ expect(historyCalls).toEqual([[{ ok: 1 }, '', undefined]])+ })+it('reports uncaught errors and failed resources to the parent', () => {window.dispatchEvent(new ErrorEvent('error', { message: 'boom' }))const script = document.createElement('script')diff --git a/web-app/src/lib/previewShim.ts b/web-app/src/lib/previewShim.tsindex 70f01c7bc..8a2b1fe6c 100644--- a/web-app/src/lib/previewShim.ts+++ b/web-app/src/lib/previewShim.ts@@ -61,6 +61,39 @@ export const PREVIEW_SHIM_SCRIPT = `(function () {shimStorage('localStorage')shimStorage('sessionStorage')+ // pushState/replaceState with a URL run a same-origin check the opaque+ // sandbox origin always fails, throwing SecurityError and aborting whatever+ // called it -- a deep-linked slideshow or client router then looks dead+ // because its own click handler never gets past the navigation. Retry+ // without the URL, a state change the sandbox does allow, so the page's+ // navigation still runs; only the address it cannot touch is dropped.+ function shimHistory() {+ var h = window.history+ if (!h) return+ function wrap(name) {+ var native = h[name]+ if (typeof native !== 'function') return+ try {+ h[name] = function (state, title, url) {+ try {+ return native.call(h, state, title, url)+ } catch (e) {+ try {+ return native.call(h, state, title)+ } catch (e2) {+ /* even the URL-less form failed; swallow so the caller runs on */+ }+ }+ }+ } catch (e) {+ /* history method not writable on this engine */+ }+ }+ wrap('pushState')+ wrap('replaceState')+ }+ shimHistory()+function report(message) {try {window.parent.postMessage(