Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(outputs): sandbox worker HTML even when opened in a browser tab

rubenluetke10-beep27846e14d1bc
diff --git a/jarvis/ui/web/artifact_view.py b/jarvis/ui/web/artifact_view.py
index fae7deabc5..82da0a4b05 100644
--- a/jarvis/ui/web/artifact_view.py
+++ b/jarvis/ui/web/artifact_view.py
@@ -25,17 +25,29 @@ _MARKDOWN_EXT = (".md", ".markdown")
# XSS from artifact content rendered in the app origin.
VIEW_CSP = "default-src 'none'; style-src 'unsafe-inline'; img-src data:;"
+# VIEW_CSP as a response HEADER. The ``sandbox`` directive is honoured only in
+# a header (a <meta> CSP ignores it), and it puts the document in an opaque
+# origin even when it is opened top-level in a browser tab rather than inside
+# the app's sandboxed iframe — so a worker-written file can never act as the
+# app origin, whatever the script-src says.
+VIEW_HEADER_CSP = VIEW_CSP + " sandbox;"
+
# The artifact-page CSP (``/files/{path}/page`` in outputs_routes): an artifact
# is a self-contained page a worker wrote to be LOOKED AT and used — tabs,
# filters, a chart drawn on canvas — so inline scripts run. What stays shut is
# every way out: no network of any kind (no fetch, no remote script, font,
-# image or frame), no forms posting anywhere, no navigation. The frontend
-# frames such a page with ``sandbox="allow-scripts"`` and WITHOUT
-# ``allow-same-origin``, so the script runs in an opaque origin that cannot
-# reach the app's cookies, storage or API — the same model Claude artifacts
-# use. ``/view`` and inline downloads keep VIEW_CSP: those are arbitrary
-# worker files, not artifacts.
+# image or frame), no forms posting anywhere, no navigation. The script runs in
+# an opaque origin that cannot reach the app's cookies, storage or API — the
+# same model Claude artifacts use. The frontend frames the page with
+# ``sandbox="allow-scripts"``, but "Open in browser" loads the same URL as a
+# top-level tab, where no iframe sandbox exists: there the ``sandbox
+# allow-scripts`` directive below is the ONLY thing keeping the page out of
+# the app origin (without it, one ``window.open('/')`` hands the page a
+# same-origin window whose ``fetch`` reaches every API route). It also blocks
+# popups, forms and top-level navigation. ``/view`` and inline downloads keep
+# VIEW_HEADER_CSP: those are arbitrary worker files, not artifacts.
ARTIFACT_PAGE_CSP = (
+ "sandbox allow-scripts; "
"default-src 'none'; "
"script-src 'unsafe-inline'; "
"style-src 'unsafe-inline'; "
@@ -118,4 +130,4 @@ def render_artifact_html(filename: str, text: str, *, theme: str | None = None)
return _shell(filename, f"<pre>{html.escape(text)}</pre>", theme)
-__all__ = ["ARTIFACT_PAGE_CSP", "VIEW_CSP", "render_artifact_html"]
+__all__ = ["ARTIFACT_PAGE_CSP", "VIEW_CSP", "VIEW_HEADER_CSP", "render_artifact_html"]
diff --git a/jarvis/ui/web/outputs_routes.py b/jarvis/ui/web/outputs_routes.py
index dc2b83ed4d..2faa41b524 100644
--- a/jarvis/ui/web/outputs_routes.py
+++ b/jarvis/ui/web/outputs_routes.py
@@ -38,7 +38,11 @@ from jarvis.missions.standalone_run import read_marker as read_standalone_marker
from jarvis.missions.state_machine import MissionState, is_terminal
from jarvis.missions.stream_evidence import clean_request_body
from jarvis.platform import detect_platform
-from jarvis.ui.web.artifact_view import ARTIFACT_PAGE_CSP, VIEW_CSP, render_artifact_html
+from jarvis.ui.web.artifact_view import (
+ ARTIFACT_PAGE_CSP,
+ VIEW_HEADER_CSP,
+ render_artifact_html,
+)
from jarvis.ui.web.mission_graph import build_mission_graph, render_mission_graph_html
from jarvis.ui.web.run_plan import build_run_plan
@@ -807,7 +811,7 @@ async def get_output_graph(slug: str, request: Request) -> HTMLResponse:
return HTMLResponse(
render_mission_graph_html(data),
headers={
- "Content-Security-Policy": VIEW_CSP,
+ "Content-Security-Policy": VIEW_HEADER_CSP,
"X-Content-Type-Options": "nosniff",
},
)
@@ -1069,7 +1073,7 @@ async def download_output_artifact(
# worker-authored artifact cannot execute code against the app.
active_document_types = {"text/html", "image/svg+xml"}
if disposition == "inline" and media_type in active_document_types:
- headers["Content-Security-Policy"] = VIEW_CSP
+ headers["Content-Security-Policy"] = VIEW_HEADER_CSP
return FileResponse(
target,
media_type=media_type or "application/octet-stream",
@@ -1141,7 +1145,7 @@ async def view_output_artifact(slug: str, path: str, request: Request) -> HTMLRe
# the app's theme rather than the OS's; absent = OS preference.
render_artifact_html(target.name, text, theme=request.query_params.get("theme")),
headers={
- "Content-Security-Policy": VIEW_CSP,
+ "Content-Security-Policy": VIEW_HEADER_CSP,
"X-Content-Type-Options": "nosniff",
},
)
diff --git a/tests/unit/ui/web/test_outputs_routes.py b/tests/unit/ui/web/test_outputs_routes.py
index 2ed9ab6610..019a47b1a2 100644
--- a/tests/unit/ui/web/test_outputs_routes.py
+++ b/tests/unit/ui/web/test_outputs_routes.py
@@ -1379,6 +1379,42 @@ def test_artifact_page_allows_inline_scripts_but_no_network(app):
assert "document.title" in r.text
+def test_artifact_page_is_sandboxed_even_when_opened_top_level(app):
+ """"Open in browser" loads ``/page`` as a top-level tab, where the app's
+ iframe sandbox does not exist. The header CSP must carry the sandbox itself
+ (scripts only, no same-origin, no popups), or the page runs as the app
+ origin and one ``window.open('/')`` reaches every API route."""
+ root = Path(app.state.outputs_root)
+ slug = "mission_019ed2dfd0fab"
+ rel = _make_deliverable(
+ root, "019ed2dfd0fab1234", "escape.html", "<script>window.open('/')</script>"
+ )
+ client = TestClient(app)
+ r = client.get(f"/api/outputs/{slug}/files/{rel}/page")
+ assert r.status_code == 200
+ directives = [d.strip() for d in r.headers["content-security-policy"].split(";")]
+ sandbox = [d for d in directives if d.split(" ")[0] == "sandbox"]
+ assert sandbox == ["sandbox allow-scripts"]
+
+
+ "route",
+ ["view", "download?disposition=inline"],
+)
+def test_worker_html_rendered_inline_is_sandboxed(app, route):
+ """Every inline rendering of a worker file gets an opaque origin, not just
+ a script ban: a top-level tab must never act as the app origin."""
+ root = Path(app.state.outputs_root)
+ slug = "mission_019ed2dfd0fab"
+ name = "notes.md" if route == "view" else "page.html"
+ rel = _make_deliverable(root, "019ed2dfd0fab1234", name, "<b>x</b>")
+ client = TestClient(app)
+ r = client.get(f"/api/outputs/{slug}/files/{rel}/{route}")
+ assert r.status_code == 200
+ directives = [d.strip() for d in r.headers["content-security-policy"].split(";")]
+ assert "sandbox" in directives
+
+
def test_artifact_page_answers_head_probe(app):
"""The frontend probes ``/page`` with HEAD before framing it. A 405 here
made the probe fail on every backend, so the sandboxed page never got its