mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-06 04:09:56 +08:00
A video lesson's turn directory holds the model's source next to the built page. Only artifacts/lesson.html is added to the public-output allowlist; Lesson.tsx and anything else in the directory stay private. The page runs model-written script, so it is served with "Content-Security-Policy: sandbox allow-scripts allow-popups". It then runs in an opaque origin even when opened in its own tab, without the API's cookies or same-origin access. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
52 lines
2.1 KiB
Python
52 lines
2.1 KiB
Python
"""Only the lesson page is public, and it is served sandboxed."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from deeptutor.services.path_service import PathService
|
|
|
|
|
|
def test_only_the_lesson_page_is_public(tmp_path: Path) -> None:
|
|
service = PathService(workspace_root=tmp_path / "data")
|
|
turn = service.get_agent_dir("video_lesson") / "turn-1"
|
|
page = turn / "artifacts" / "lesson.html"
|
|
source = turn / "source" / "Lesson.tsx"
|
|
stray = turn / "artifacts" / "notes.html"
|
|
for path in (page, source, stray):
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text("x", encoding="utf-8")
|
|
|
|
assert service.resolve_public_output_path(page) == page.resolve()
|
|
assert service.resolve_public_output_path(source) is None
|
|
assert service.resolve_public_output_path(stray) is None
|
|
|
|
|
|
def test_lesson_page_is_served_sandboxed(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
|
|
from deeptutor.api.routers import auth as auth_router
|
|
from deeptutor.api.routers import outputs
|
|
from deeptutor.multi_user import paths as multi_user_paths
|
|
|
|
admin_root = tmp_path / "data"
|
|
monkeypatch.setattr(multi_user_paths, "ADMIN_WORKSPACE_ROOT", admin_root)
|
|
monkeypatch.setattr(multi_user_paths, "USERS_ROOT", admin_root / "users")
|
|
monkeypatch.setattr(multi_user_paths, "_path_services", {})
|
|
monkeypatch.setattr(auth_router, "AUTH_ENABLED", False)
|
|
service = PathService(workspace_root=admin_root)
|
|
page = service.get_agent_dir("video_lesson") / "turn-1" / "artifacts" / "lesson.html"
|
|
page.parent.mkdir(parents=True)
|
|
page.write_text("<!doctype html><script>1</script>", encoding="utf-8")
|
|
|
|
app = FastAPI()
|
|
app.include_router(outputs.router, prefix="/files/outputs")
|
|
rel = page.relative_to(service.get_public_outputs_root()).as_posix()
|
|
response = TestClient(app).get(f"/files/outputs/{rel}")
|
|
|
|
assert response.status_code == 200
|
|
assert response.headers["content-security-policy"].startswith("sandbox allow-scripts")
|