Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(cowork): keep the HTML preview sandboxed on Windows

James Dam92e12d1e8aa4
diff --git a/web-app/src/containers/CoworkPreviewPanel.tsx b/web-app/src/containers/CoworkPreviewPanel.tsx
index d01e2aa1d..db4f5ffbd 100644
--- a/web-app/src/containers/CoworkPreviewPanel.tsx
+++ b/web-app/src/containers/CoworkPreviewPanel.tsx
@@ -40,6 +40,12 @@ import {
* whose identity changes on every render. */
const TOO_LARGE = 'common:preview.tooLarge'
+/** Whether the unsandboxed `preview://` mode is offered. Not on Windows: Tauri
+ * treats the scheme's origin as the app's own, and on WebView2 wry injects the
+ * IPC init script into subframes too (elsewhere only the main frame gets it),
+ * so there only the opaque-origin srcdoc sandbox keeps the page off IPC. */
+const canUnsandbox = () => !IS_WINDOWS
+
type Props = {
/** Every place the session may have written the file: its sandbox and, when
* attached, the writable project folder. The path must resolve inside one. */
@@ -140,7 +146,7 @@ export function CoworkPreviewPanel({ roots, path, onClose }: Props) {
// flag rides on the registration, so a toggle re-registers. Closing the
// panel or leaving the mode withdraws the root. The registered root is the
// one the file resolved inside -- sandbox or attached folder.
- const live = unsandboxed && kind === 'html' && !!matchedRoot
+ const live = unsandboxed && canUnsandbox() && kind === 'html' && !!matchedRoot
useEffect(() => {
if (!live || !matchedRoot) return
void registerPreviewRoot(matchedRoot, allowNetwork).catch(() => {
@@ -191,6 +197,7 @@ export function CoworkPreviewPanel({ roots, path, onClose }: Props) {
allowNetwork
)}
{matchedRoot &&
+ canUnsandbox() &&
iconButton(
t('common:preview.unsandboxed'),
ShieldOff,
diff --git a/web-app/src/containers/__tests__/CoworkPreviewPanel.test.tsx b/web-app/src/containers/__tests__/CoworkPreviewPanel.test.tsx
index 674617085..b1f5ab0a7 100644
--- a/web-app/src/containers/__tests__/CoworkPreviewPanel.test.tsx
+++ b/web-app/src/containers/__tests__/CoworkPreviewPanel.test.tsx
@@ -240,6 +240,20 @@ describe('CoworkPreviewPanel', () => {
)
})
+ // On Windows the unsandboxed frame would reach the app's IPC, so the
+ // toggle is not offered and the page stays in the srcdoc sandbox.
+ it('offers no unsandboxed mode on Windows', async () => {
+ vi.stubGlobal('IS_WINDOWS', true)
+ respondWith('<canvas></canvas><script>run()</script>')
+ view('game.html')
+
+ const frame = (await screen.findByTitle('game.html')) as HTMLIFrameElement
+ expect(screen.queryByLabelText('common:preview.unsandboxed')).toBeNull()
+ expect(frame.getAttribute('sandbox')).toBe('allow-scripts')
+ expect(frame.getAttribute('src')).toBeNull()
+ expect(previewRegisterRoot).not.toHaveBeenCalled()
+ })
+
it('registers nothing while the sandbox is in use', async () => {
respondWith('<p>ok</p>')
view('page.html')