* fix(session): move persistence off event loop * refactor(session): isolate async persistence boundary * refactor(session): keep a single session manager * refactor(session): use stateless async adapters * fix(session): serialize local persistence without file-lock contention * refactor(session): own async persistence and snapshot commits * refactor(session): narrow persistence changes to async adapters * refactor(session): remove redundant async forwarding helpers * fix(command): offload compact session persistence * fix(session): adapt idle compaction to current main * fix(webui): offload session metadata persistence * refactor(session): centralize state ownership in SQLite * fix(session): skip storage for non-recoverable followups * test(webui): honor the full gateway event deadline * perf(session): persist incremental changes and reuse owner snapshots * test(agent): allow durable worker shutdown in burst test * fix(gateway): drain session storage on Windows console shutdown * fix(session): enforce bounded ownership and lifecycle-safe persistence * fix: reject unresolved home paths and stabilize platform tests * fix(session): preserve request context compatibility and owner-backed tests * docs: focus configuration guide on user-facing behavior * fix(session): adapt goal admission to async ownership after rebase * fix(session): integrate current gateway and subagent contracts * fix(session): align rebased workspace admission and async tests * fix(session): support compatible SQLite schema evolution * test(agent): use session owner in workspace context regressions * test: stabilize Windows lifecycle and browser checks * refactor(session): remove premature JSONL file export * ci: bound Windows full-suite worker concurrency * chore: remove session state refactor notes * fix(session): queue writes and retain worker connections * perf(webui): read detached metadata for session and task lists * perf(agent): bound prompt preparation and local token counting * docs: remove internal session storage guide * perf(session): skip archived transcripts during idle scans * fix(session): coordinate turns and isolate legacy session data Bind WebUI input generations to the execution session and serialize idle and SDK compaction with turns. Keep turn drafts through preparation so the latest channel route persists. Move imported workspace JSONL and checkpoints to durable private backups before gateway admission, resuming cleanup after interruptions. Remove committed child sessions when task creation is cancelled. Validation: focused Python and frontend regressions, Ruff, BasedPyright, import boundaries, WebUI build, and isolated gateway/browser flows. * fix(agent): keep cancellation hooks independent of prompt preparation * fix(session): preserve admission, delivery, and migration contracts * fix(agent): preserve cancellation settlement after rebase * fix(session): preserve request attribution through async ownership
157 lines
6.1 KiB
Python
157 lines
6.1 KiB
Python
import base64
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from nanobot.security.workspace_access import default_workspace_scope
|
|
from nanobot.webui.file_preview import (
|
|
WebUIFilePreviewError,
|
|
file_preview_availability_payload,
|
|
file_preview_payload,
|
|
file_reference_payload,
|
|
)
|
|
|
|
PNG = base64.b64decode(
|
|
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/l9sAAAAASUVORK5CYII="
|
|
)
|
|
|
|
|
|
def test_file_reference_metadata_does_not_read_contents(tmp_path, monkeypatch) -> None:
|
|
source = tmp_path / "notes with space.bin"
|
|
source.write_bytes(b"\0binary")
|
|
scope = default_workspace_scope(tmp_path, restrict_to_workspace=True)
|
|
monkeypatch.setattr("builtins.open", lambda *args, **kwargs: pytest.fail("read contents"))
|
|
assert file_reference_payload("notes%20with%20space.bin:12", scope=scope) == {
|
|
"path": str(source.resolve()), "relative_path": "notes with space.bin",
|
|
}
|
|
|
|
|
|
def test_file_reference_outside_project_has_no_relative_path(tmp_path) -> None:
|
|
workspace = tmp_path / "workspace"
|
|
workspace.mkdir()
|
|
outside = tmp_path / "elsewhere.txt"
|
|
outside.write_text("example")
|
|
scope = default_workspace_scope(workspace, restrict_to_workspace=False)
|
|
assert file_reference_payload(str(outside), scope=scope) == {
|
|
"path": str(outside.resolve()), "relative_path": None,
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize("target", ["../elsewhere.txt", "linked.txt"])
|
|
def test_file_reference_rejects_traversal_and_symlink_escape(
|
|
tmp_path, target, create_symlink,
|
|
) -> None:
|
|
workspace = tmp_path / "workspace"
|
|
workspace.mkdir()
|
|
outside = tmp_path / "elsewhere.txt"
|
|
outside.write_text("example")
|
|
if target == "linked.txt":
|
|
create_symlink(workspace / "linked.txt", outside)
|
|
scope = default_workspace_scope(workspace, restrict_to_workspace=True)
|
|
with pytest.raises(WebUIFilePreviewError) as error:
|
|
file_reference_payload(target, scope=scope)
|
|
assert error.value.status == 403
|
|
|
|
|
|
@pytest.mark.parametrize("target", ["missing.txt", "."])
|
|
def test_file_reference_rejects_missing_files_and_directories(tmp_path, target) -> None:
|
|
with pytest.raises(WebUIFilePreviewError) as error:
|
|
file_reference_payload(target, scope=default_workspace_scope(tmp_path, True))
|
|
assert error.value.status == 404
|
|
|
|
|
|
@pytest.mark.parametrize(("data", "mime"), [
|
|
(PNG, "image/png"),
|
|
(b"\xff\xd8\xff\xe0\0example", "image/jpeg"),
|
|
(b"GIF89a\0example", "image/gif"),
|
|
(b"RIFF\0\0\0\0WEBPexample", "image/webp"),
|
|
])
|
|
def test_raster_preview_uses_content_signature(tmp_path: Path, data: bytes, mime: str) -> None:
|
|
# Extension must not determine the rendered MIME type.
|
|
image = tmp_path / "figure.bin"
|
|
image.write_bytes(data)
|
|
scope = default_workspace_scope(tmp_path, restrict_to_workspace=True)
|
|
assert file_preview_availability_payload("figure.bin", scope=scope) == {"available": True}
|
|
result = file_preview_payload("figure.bin", scope=scope)
|
|
assert result["kind"] == "image"
|
|
assert result["mime_type"] == mime
|
|
assert base64.b64decode(result["data_url"].split(",", 1)[1]) == data
|
|
assert result["size"] == len(data)
|
|
|
|
|
|
def test_large_raster_is_rejected_without_truncating(tmp_path: Path, monkeypatch) -> None:
|
|
monkeypatch.setattr("nanobot.webui.file_preview.MAX_IMAGE_PREVIEW_BYTES", 32)
|
|
(tmp_path / "large.png").write_bytes(PNG)
|
|
scope = default_workspace_scope(tmp_path, restrict_to_workspace=True)
|
|
for read in (file_preview_payload, file_preview_availability_payload):
|
|
with pytest.raises(WebUIFilePreviewError) as error:
|
|
read("large.png", scope=scope)
|
|
assert error.value.status == 413
|
|
|
|
|
|
@pytest.mark.parametrize("filename", ["page.html", "drawing.svg", "disguised.png"])
|
|
def test_active_content_is_only_source(tmp_path: Path, filename: str) -> None:
|
|
content = "<script>window.example = true</script>"
|
|
(tmp_path / filename).write_text(content)
|
|
result = file_preview_payload(
|
|
filename, scope=default_workspace_scope(tmp_path, restrict_to_workspace=True),
|
|
)
|
|
assert result["kind"] == "text"
|
|
assert result["content"] == content
|
|
|
|
|
|
def test_text_truncation_is_unchanged(tmp_path: Path) -> None:
|
|
(tmp_path / "notes.txt").write_text("abcdefghijklm")
|
|
result = file_preview_payload(
|
|
"notes.txt", scope=default_workspace_scope(tmp_path, restrict_to_workspace=True), max_bytes=5,
|
|
)
|
|
assert result["content"] == "abcde"
|
|
assert result["truncated"] is True
|
|
|
|
|
|
def test_raster_symlink_cannot_escape_workspace(tmp_path: Path, create_symlink) -> None:
|
|
workspace = tmp_path / "workspace"
|
|
workspace.mkdir()
|
|
image = tmp_path / "outside.png"
|
|
image.write_bytes(PNG)
|
|
create_symlink(workspace / "linked.png", image)
|
|
with pytest.raises(WebUIFilePreviewError) as error:
|
|
file_preview_payload(
|
|
"linked.png", scope=default_workspace_scope(workspace, restrict_to_workspace=True),
|
|
)
|
|
assert error.value.status == 403
|
|
|
|
|
|
def test_restricted_preview_allows_media_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
workspace = tmp_path / "workspace"
|
|
workspace.mkdir()
|
|
media = tmp_path / "media"
|
|
media.mkdir()
|
|
uploaded = media / "upload.txt"
|
|
uploaded.write_text("uploaded", encoding="utf-8")
|
|
monkeypatch.setattr("nanobot.webui.file_preview.get_media_dir", lambda: media)
|
|
|
|
scope = default_workspace_scope(workspace, restrict_to_workspace=True)
|
|
|
|
payload = file_preview_payload(str(uploaded), scope=scope)
|
|
|
|
assert payload["content"] == "uploaded"
|
|
assert Path(payload["path"]) == uploaded.resolve()
|
|
|
|
|
|
def test_restricted_preview_rejects_other_root(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
workspace = tmp_path / "workspace"
|
|
workspace.mkdir()
|
|
media = tmp_path / "media"
|
|
media.mkdir()
|
|
outside = tmp_path / "outside.txt"
|
|
outside.write_text("secret", encoding="utf-8")
|
|
monkeypatch.setattr("nanobot.webui.file_preview.get_media_dir", lambda: media)
|
|
|
|
scope = default_workspace_scope(workspace, restrict_to_workspace=True)
|
|
|
|
with pytest.raises(WebUIFilePreviewError, match="outside the current workspace") as exc_info:
|
|
file_preview_payload(str(outside), scope=scope)
|
|
|
|
assert exc_info.value.status == 403
|