mirror of
https://github.com/eigent-ai/eigent.git
synced 2026-12-05 05:34:01 +00:00
815 lines
27 KiB
Python
815 lines
27 KiB
Python
# ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
# ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
|
|
|
|
import os
|
|
import time
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app import SecurityHeadersMiddleware, api as brain_api
|
|
from app.auth.file_access import (
|
|
FileAccessScope,
|
|
issue_file_access_grant,
|
|
require_file_preview_access,
|
|
require_file_stream_access,
|
|
)
|
|
from app.auth.local_control import (
|
|
LOCAL_CONTROL_CAPABILITY_HEADER,
|
|
require_local_control_if_configured,
|
|
require_local_control_principal,
|
|
)
|
|
from app.controller import file_controller
|
|
from app.router import register_routers
|
|
|
|
CAPABILITY = {LOCAL_CONTROL_CAPABILITY_HEADER: "secret-1"}
|
|
SCOPE_PARAMS = {"project_id": "project-1", "email": "user@example.com"}
|
|
|
|
|
|
def test_brain_cors_exposes_file_preview_metadata_headers():
|
|
cors = next(
|
|
middleware
|
|
for middleware in brain_api.user_middleware
|
|
if middleware.cls is CORSMiddleware
|
|
)
|
|
exposed = {header.lower() for header in cors.kwargs["expose_headers"]}
|
|
|
|
assert {
|
|
"accept-ranges",
|
|
"content-disposition",
|
|
"content-length",
|
|
"content-range",
|
|
"last-modified",
|
|
} <= exposed
|
|
|
|
|
|
def test_resolve_project_root_prefers_user_id_root(
|
|
monkeypatch, tmp_path, caplog
|
|
):
|
|
eigent_root = tmp_path / "eigent"
|
|
user_project = eigent_root / "user_20" / "project_p1"
|
|
user_project.mkdir(parents=True)
|
|
(eigent_root / "other_user" / "project_p1").mkdir(parents=True)
|
|
|
|
monkeypatch.setattr(
|
|
file_controller, "_get_eigent_root", lambda: eigent_root
|
|
)
|
|
|
|
resolved = file_controller._resolve_project_root(
|
|
"yueming.lai@example.com", "p1", "20"
|
|
)
|
|
|
|
assert resolved == user_project
|
|
assert "Resolved project root via fallback lookup" not in caplog.text
|
|
|
|
|
|
def test_resolve_project_root_falls_back_to_legacy_email_root(
|
|
monkeypatch, tmp_path
|
|
):
|
|
eigent_root = tmp_path / "eigent"
|
|
legacy_project = eigent_root / "yueming.lai" / "project_p1"
|
|
legacy_project.mkdir(parents=True)
|
|
|
|
monkeypatch.setattr(
|
|
file_controller, "_get_eigent_root", lambda: eigent_root
|
|
)
|
|
|
|
resolved = file_controller._resolve_project_root(
|
|
"yueming.lai@example.com", "p1", "20"
|
|
)
|
|
|
|
assert resolved == legacy_project
|
|
|
|
|
|
def test_resolve_project_root_does_not_fallback_to_other_user_root(
|
|
monkeypatch, tmp_path
|
|
):
|
|
eigent_root = tmp_path / "eigent"
|
|
(eigent_root / "user_20" / "project_p1").mkdir(parents=True)
|
|
expected = eigent_root / "user_42" / "project_p1"
|
|
|
|
monkeypatch.setattr(
|
|
file_controller, "_get_eigent_root", lambda: eigent_root
|
|
)
|
|
|
|
resolved = file_controller._resolve_project_root(
|
|
"yueming.lai@example.com", "p1", "42"
|
|
)
|
|
|
|
assert resolved == expected
|
|
|
|
|
|
def test_resolve_project_root_without_user_id_stays_email_scoped(
|
|
monkeypatch, tmp_path
|
|
):
|
|
eigent_root = tmp_path / "eigent"
|
|
(eigent_root / "user_20" / "project_p1").mkdir(parents=True)
|
|
expected = eigent_root / "yueming.lai" / "project_p1"
|
|
|
|
monkeypatch.setattr(
|
|
file_controller, "_get_eigent_root", lambda: eigent_root
|
|
)
|
|
|
|
resolved = file_controller._resolve_project_root(
|
|
"yueming.lai@example.com", "p1"
|
|
)
|
|
|
|
assert resolved == expected
|
|
|
|
|
|
def test_task_changes_include_all_outputs_but_only_recent_workspace_edits(
|
|
tmp_path,
|
|
):
|
|
output_root = tmp_path / "outputs"
|
|
working_root = tmp_path / "workspace"
|
|
output_root.mkdir()
|
|
working_root.mkdir()
|
|
started_at = time.time()
|
|
|
|
copied_output = output_root / "copied-report.csv"
|
|
copied_output.write_text("output", encoding="utf-8")
|
|
old_workspace_file = working_root / "existing.md"
|
|
old_workspace_file.write_text("old", encoding="utf-8")
|
|
old_time = started_at - 60
|
|
os.utime(copied_output, (old_time, old_time))
|
|
os.utime(old_workspace_file, (old_time, old_time))
|
|
# Old files must be filtered before the 500-item result bound. Otherwise
|
|
# a large selected folder can hide a recent artifact later in the walk.
|
|
for index in range(510):
|
|
old_file = working_root / f"old-{index:03d}.txt"
|
|
old_file.write_text("old", encoding="utf-8")
|
|
os.utime(old_file, (old_time, old_time))
|
|
edited_workspace_file = working_root / "reports" / "final.md"
|
|
edited_workspace_file.parent.mkdir()
|
|
edited_workspace_file.write_text("new", encoding="utf-8")
|
|
|
|
files = file_controller._list_task_changed_files(
|
|
SimpleNamespace(
|
|
task_output_root=str(output_root),
|
|
working_directory=str(working_root),
|
|
task_start_time=started_at,
|
|
)
|
|
)
|
|
|
|
assert {item["path"] for item in files} == {
|
|
str(copied_output.resolve()),
|
|
str(edited_workspace_file.resolve()),
|
|
}
|
|
assert {item["relativePath"] for item in files} == {
|
|
"copied-report.csv",
|
|
"reports/final.md",
|
|
}
|
|
assert {item["path"]: item["changeType"] for item in files} == {
|
|
str(copied_output.resolve()): "generated",
|
|
str(edited_workspace_file.resolve()): "changed",
|
|
}
|
|
assert {item["path"]: item["size"] for item in files} == {
|
|
str(copied_output.resolve()): 6,
|
|
str(edited_workspace_file.resolve()): 3,
|
|
}
|
|
assert all(item["modifiedAt"] > 0 for item in files)
|
|
|
|
|
|
def test_task_changes_exclude_files_written_after_run_attempt(tmp_path):
|
|
output_root = tmp_path / "outputs"
|
|
working_root = tmp_path / "shared-space"
|
|
output_root.mkdir()
|
|
working_root.mkdir()
|
|
started_at = time.time() - 120
|
|
ended_at = started_at + 30
|
|
|
|
current_run_file = working_root / "bank-transfer.csv"
|
|
current_run_file.write_text("current", encoding="utf-8")
|
|
os.utime(
|
|
current_run_file,
|
|
(started_at + 10, started_at + 10),
|
|
)
|
|
future_run_file = working_root / "long-horizon" / "report.md"
|
|
future_run_file.parent.mkdir()
|
|
future_run_file.write_text("future", encoding="utf-8")
|
|
os.utime(
|
|
future_run_file,
|
|
(ended_at + 60, ended_at + 60),
|
|
)
|
|
|
|
files = file_controller._list_task_changed_files(
|
|
SimpleNamespace(
|
|
task_output_root=str(output_root),
|
|
working_directory=str(working_root),
|
|
task_start_time=started_at,
|
|
),
|
|
modification_windows=((started_at - 1, ended_at),),
|
|
)
|
|
|
|
assert [item["relativePath"] for item in files] == ["bank-transfer.csv"]
|
|
|
|
|
|
def test_task_changes_skip_file_deleted_between_walk_and_stat(
|
|
monkeypatch, tmp_path
|
|
):
|
|
output_root = tmp_path / "outputs"
|
|
output_root.mkdir()
|
|
disappearing = output_root / "ephemeral.txt"
|
|
disappearing.write_text("temporary", encoding="utf-8")
|
|
original_stat = Path.stat
|
|
target_stat_calls = 0
|
|
|
|
monkeypatch.setattr(
|
|
file_controller,
|
|
"list_files",
|
|
lambda *_args, **_kwargs: [str(disappearing)],
|
|
)
|
|
|
|
def disappearing_stat(path, *args, **kwargs):
|
|
nonlocal target_stat_calls
|
|
if path == disappearing:
|
|
target_stat_calls += 1
|
|
if target_stat_calls >= 2:
|
|
raise FileNotFoundError(path)
|
|
return original_stat(path, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(Path, "stat", disappearing_stat)
|
|
|
|
files = file_controller._list_task_changed_files(
|
|
SimpleNamespace(
|
|
task_output_root=str(output_root),
|
|
working_directory=str(output_root),
|
|
task_start_time=time.time() - 10,
|
|
)
|
|
)
|
|
|
|
assert files == []
|
|
|
|
|
|
def test_task_changes_endpoint_freezes_completed_run_manifest(
|
|
monkeypatch, tmp_path
|
|
):
|
|
monkeypatch.setenv("EIGENT_RUNTIME", "electron")
|
|
monkeypatch.setenv("EIGENT_LOCAL_CONTROL_CAPABILITY", "secret-1")
|
|
started_at = time.time() - 120
|
|
ended_at = started_at + 30
|
|
changed_file = tmp_path / "report.csv"
|
|
changed_file.write_text("report", encoding="utf-8")
|
|
os.utime(changed_file, (started_at + 10, started_at + 10))
|
|
snapshot = SimpleNamespace(
|
|
project_id="project-1",
|
|
task_output_root=str(tmp_path / "outputs"),
|
|
working_directory=str(tmp_path),
|
|
task_start_time=started_at,
|
|
artifact_manifest=None,
|
|
)
|
|
resolver = MagicMock()
|
|
resolver.store.get_snapshot.return_value = snapshot
|
|
journal = MagicMock()
|
|
journal.get_run.return_value = SimpleNamespace(
|
|
project_id="project-1",
|
|
status="completed",
|
|
created_at=started_at,
|
|
updated_at=ended_at,
|
|
)
|
|
journal.list_run_attempts.return_value = [
|
|
SimpleNamespace(started_at=started_at, ended_at=ended_at)
|
|
]
|
|
monkeypatch.setattr(
|
|
file_controller, "get_workspace_resolver", lambda: resolver
|
|
)
|
|
monkeypatch.setattr(
|
|
file_controller, "get_default_run_journal", lambda: journal
|
|
)
|
|
|
|
app = FastAPI()
|
|
app.include_router(file_controller.router)
|
|
client = TestClient(app, client=("127.0.0.1", 50000))
|
|
response = client.get(
|
|
"/files/changes",
|
|
params={
|
|
"task_id": "task-1",
|
|
"project_id": "project-1",
|
|
"email": "user@example.com",
|
|
},
|
|
headers={LOCAL_CONTROL_CAPABILITY_HEADER: "secret-1"},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert [item["relativePath"] for item in body["artifacts"]] == [
|
|
"report.csv"
|
|
]
|
|
assert body["scan_status"] == "complete"
|
|
assert body["truncated"] is False
|
|
resolver.store.freeze_artifact_manifest.assert_called_once_with(
|
|
"user@example.com", snapshot, body["artifacts"]
|
|
)
|
|
|
|
|
|
def test_task_changes_endpoint_reads_canonical_manifest_without_filesystem_scan(
|
|
monkeypatch,
|
|
):
|
|
monkeypatch.setenv("EIGENT_RUNTIME", "electron")
|
|
monkeypatch.setenv("EIGENT_LOCAL_CONTROL_CAPABILITY", "secret-1")
|
|
resolver = MagicMock()
|
|
journal = MagicMock()
|
|
journal.get_run_artifact_manifest_event.return_value = SimpleNamespace(
|
|
payload={
|
|
"scan_status": "complete",
|
|
"artifacts": [
|
|
{
|
|
"artifact_id": "art-1",
|
|
"filename": "report.csv",
|
|
"path": "/workspace/report.csv",
|
|
"relativePath": "report.csv",
|
|
"changeType": "generated",
|
|
"size": 12,
|
|
"modifiedAt": 1234,
|
|
"supportsRanges": True,
|
|
}
|
|
],
|
|
}
|
|
)
|
|
monkeypatch.setattr(
|
|
file_controller, "get_workspace_resolver", lambda: resolver
|
|
)
|
|
monkeypatch.setattr(
|
|
file_controller, "get_default_run_journal", lambda: journal
|
|
)
|
|
monkeypatch.setattr(
|
|
file_controller,
|
|
"_list_task_changed_files",
|
|
MagicMock(side_effect=AssertionError("filesystem scan must not run")),
|
|
)
|
|
|
|
app = FastAPI()
|
|
app.include_router(file_controller.router)
|
|
client = TestClient(app, client=("127.0.0.1", 50000))
|
|
response = client.get(
|
|
"/files/changes",
|
|
params={
|
|
"task_id": "task-1",
|
|
"project_id": "project-1",
|
|
"email": "user@example.com",
|
|
},
|
|
headers={LOCAL_CONTROL_CAPABILITY_HEADER: "secret-1"},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
assert response.json()["artifacts"][0]["artifact_id"] == "art-1"
|
|
assert response.json()["scan_status"] == "complete"
|
|
assert response.json()["truncated"] is False
|
|
resolver.store.get_snapshot.assert_not_called()
|
|
|
|
|
|
def test_task_changes_treats_unavailable_manifest_as_finalized_history(
|
|
monkeypatch,
|
|
):
|
|
monkeypatch.setenv("EIGENT_RUNTIME", "electron")
|
|
monkeypatch.setenv("EIGENT_LOCAL_CONTROL_CAPABILITY", "secret-1")
|
|
resolver = MagicMock()
|
|
journal = MagicMock()
|
|
journal.get_run_artifact_manifest_event.return_value = SimpleNamespace(
|
|
payload={
|
|
"scan_status": "workspace_unavailable",
|
|
"artifacts": [],
|
|
}
|
|
)
|
|
monkeypatch.setattr(
|
|
file_controller, "get_workspace_resolver", lambda: resolver
|
|
)
|
|
monkeypatch.setattr(
|
|
file_controller, "get_default_run_journal", lambda: journal
|
|
)
|
|
|
|
app = FastAPI()
|
|
app.include_router(file_controller.router)
|
|
client = TestClient(app, client=("127.0.0.1", 50000))
|
|
response = client.get(
|
|
"/files/changes",
|
|
params={
|
|
"task_id": "task-1",
|
|
"project_id": "project-1",
|
|
"email": "user@example.com",
|
|
},
|
|
headers={LOCAL_CONTROL_CAPABILITY_HEADER: "secret-1"},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
assert response.json() == {
|
|
"artifacts": [],
|
|
"scan_status": "workspace_unavailable",
|
|
"truncated": False,
|
|
}
|
|
resolver.store.get_snapshot.assert_not_called()
|
|
|
|
|
|
def test_stream_file_supports_byte_ranges(monkeypatch, tmp_path):
|
|
project_root = tmp_path / "project"
|
|
project_root.mkdir()
|
|
payload = b"0123456789"
|
|
(project_root / "report.pdf").write_bytes(payload)
|
|
monkeypatch.setattr(
|
|
file_controller,
|
|
"_resolve_file_root",
|
|
lambda *_args, **_kwargs: project_root,
|
|
)
|
|
|
|
app = FastAPI()
|
|
app.include_router(file_controller.router)
|
|
client = TestClient(app)
|
|
response = client.get(
|
|
"/files/stream",
|
|
params={
|
|
"path": "report.pdf",
|
|
"project_id": "project-1",
|
|
"email": "user@example.com",
|
|
},
|
|
headers={"Range": "bytes=2-5"},
|
|
)
|
|
|
|
assert response.status_code == 206
|
|
assert response.content == b"2345"
|
|
assert response.headers["accept-ranges"] == "bytes"
|
|
assert response.headers["content-range"] == "bytes 2-5/10"
|
|
|
|
head = client.head(
|
|
"/files/stream",
|
|
params={
|
|
"path": "report.pdf",
|
|
"project_id": "project-1",
|
|
"email": "user@example.com",
|
|
},
|
|
)
|
|
assert head.status_code == 200
|
|
assert head.content == b""
|
|
assert head.headers["content-length"] == str(len(payload))
|
|
assert head.headers["accept-ranges"] == "bytes"
|
|
|
|
|
|
@pytest.mark.parametrize("prefix", ["", "/api/v1"])
|
|
@pytest.mark.parametrize(
|
|
"route",
|
|
[
|
|
"/files/stream",
|
|
"/files/stream/display.pdf",
|
|
"/files/stream/中文 report.pdf",
|
|
],
|
|
)
|
|
def test_stream_file_can_be_framed_by_the_app_only(
|
|
monkeypatch, tmp_path, prefix, route
|
|
):
|
|
project_root = tmp_path / "project"
|
|
project_root.mkdir()
|
|
(project_root / "report.pdf").write_bytes(b"%PDF-1.4\n")
|
|
monkeypatch.setattr(
|
|
file_controller,
|
|
"_resolve_file_root",
|
|
lambda *_args, **_kwargs: project_root,
|
|
)
|
|
|
|
app = FastAPI()
|
|
app.add_middleware(SecurityHeadersMiddleware)
|
|
app.include_router(file_controller.router, prefix=prefix)
|
|
client = TestClient(app)
|
|
params = {"project_id": "project-1", "email": "user@example.com"}
|
|
|
|
# The Files PDF preview loads this URL in an iframe. X-Frame-Options: DENY
|
|
# makes Chromium replace the frame with a blank error page.
|
|
stream = client.get(
|
|
prefix + route, params={**params, "path": "report.pdf"}
|
|
)
|
|
assert stream.status_code == 200
|
|
assert "x-frame-options" not in stream.headers
|
|
policy = stream.headers["content-security-policy"]
|
|
assert policy.startswith("frame-ancestors ")
|
|
sources = set(policy.removeprefix("frame-ancestors ").rstrip(";").split())
|
|
# The dev renderer is served from localhost; the packaged one is file://.
|
|
assert {"'self'", "http://localhost:*", "file:"} <= sources
|
|
assert "*" not in sources
|
|
|
|
listing = client.get(prefix + "/files", params=params)
|
|
assert listing.status_code == 200
|
|
assert listing.headers["x-frame-options"] == "DENY"
|
|
assert "content-security-policy" not in listing.headers
|
|
|
|
for unrelated in ("/files/streaming", "/files/stream-other"):
|
|
response = client.get(prefix + unrelated, params=params)
|
|
assert response.headers["x-frame-options"] == "DENY"
|
|
assert "content-security-policy" not in response.headers
|
|
|
|
|
|
def test_task_changes_endpoint_requires_local_capability(
|
|
monkeypatch, tmp_path
|
|
):
|
|
monkeypatch.setenv("EIGENT_RUNTIME", "electron")
|
|
monkeypatch.setenv("EIGENT_LOCAL_CONTROL_CAPABILITY", "secret-1")
|
|
snapshot = SimpleNamespace(
|
|
project_id="project-1",
|
|
task_output_root=str(tmp_path),
|
|
working_directory=str(tmp_path),
|
|
task_start_time=time.time(),
|
|
)
|
|
resolver = MagicMock()
|
|
resolver.store.get_snapshot.return_value = snapshot
|
|
journal = MagicMock()
|
|
journal.get_run.return_value = None
|
|
monkeypatch.setattr(
|
|
file_controller, "get_workspace_resolver", lambda: resolver
|
|
)
|
|
monkeypatch.setattr(
|
|
file_controller, "get_default_run_journal", lambda: journal
|
|
)
|
|
|
|
app = FastAPI()
|
|
app.include_router(file_controller.router)
|
|
client = TestClient(app, client=("127.0.0.1", 50000))
|
|
params = {
|
|
"task_id": "task-1",
|
|
"project_id": "project-1",
|
|
"email": "user@example.com",
|
|
}
|
|
|
|
assert client.get("/files/changes", params=params).status_code == 401
|
|
response = client.get(
|
|
"/files/changes",
|
|
params=params,
|
|
headers={LOCAL_CONTROL_CAPABILITY_HEADER: "secret-1"},
|
|
)
|
|
assert response.status_code == 200
|
|
assert response.json() == {
|
|
"artifacts": [],
|
|
"scan_status": "complete",
|
|
"truncated": False,
|
|
}
|
|
|
|
|
|
def test_named_pdf_preview_preserves_authorized_identity_and_byte_ranges(
|
|
monkeypatch, tmp_path
|
|
):
|
|
(tmp_path / "actual.pdf").write_bytes(b"0123456789")
|
|
monkeypatch.setattr(
|
|
file_controller, "_resolve_file_root", lambda *_a, **_k: tmp_path
|
|
)
|
|
app = FastAPI()
|
|
app.add_middleware(SecurityHeadersMiddleware)
|
|
app.include_router(file_controller.router)
|
|
client = TestClient(app)
|
|
params = {
|
|
"path": "actual.pdf",
|
|
"project_id": "project-1",
|
|
"email": "user@example.com",
|
|
}
|
|
response = client.get(
|
|
"/files/stream/display.pdf",
|
|
params=params,
|
|
headers={"Range": "bytes=2-5"},
|
|
)
|
|
assert response.status_code == 206
|
|
assert response.content == b"2345"
|
|
assert "actual.pdf" in response.headers["content-disposition"]
|
|
assert response.headers["content-type"] == "application/pdf"
|
|
assert response.headers["content-range"] == "bytes 2-5/10"
|
|
assert "x-frame-options" not in response.headers
|
|
assert response.headers["content-security-policy"].startswith(
|
|
"frame-ancestors "
|
|
)
|
|
|
|
head = client.head("/files/stream/display.pdf", params=params)
|
|
assert head.status_code == 200
|
|
assert head.content == b""
|
|
assert head.headers["content-length"] == "10"
|
|
assert "actual.pdf" in head.headers["content-disposition"]
|
|
assert "x-frame-options" not in head.headers
|
|
assert (
|
|
head.headers["content-security-policy"]
|
|
== response.headers["content-security-policy"]
|
|
)
|
|
|
|
|
|
def _desktop_file_client(monkeypatch, project_root):
|
|
monkeypatch.setenv("EIGENT_RUNTIME", "electron")
|
|
monkeypatch.setenv("EIGENT_LOCAL_CONTROL_CAPABILITY", "secret-1")
|
|
roots = []
|
|
|
|
def resolve_file_root(email, project_id, space_id=None, user_id=None):
|
|
roots.append((email, project_id, space_id, user_id))
|
|
return project_root
|
|
|
|
monkeypatch.setattr(
|
|
file_controller, "_resolve_file_root", resolve_file_root
|
|
)
|
|
app = FastAPI()
|
|
register_routers(app)
|
|
return TestClient(app, client=("127.0.0.1", 50000)), roots
|
|
|
|
|
|
def test_every_file_route_declares_an_access_guard():
|
|
guards = {
|
|
require_local_control_if_configured,
|
|
require_local_control_principal,
|
|
require_file_stream_access,
|
|
require_file_preview_access,
|
|
}
|
|
assert file_controller.router.routes
|
|
for route in file_controller.router.routes:
|
|
assert any(
|
|
dependency.call in guards
|
|
for dependency in route.dependant.dependencies
|
|
), f"{sorted(route.methods)} {route.path} has no access guard"
|
|
|
|
|
|
def test_desktop_file_stream_requires_a_grant_for_the_same_root(
|
|
monkeypatch, tmp_path
|
|
):
|
|
project_root = tmp_path / "project"
|
|
project_root.mkdir()
|
|
(project_root / "report.pdf").write_bytes(b"0123456789")
|
|
client, _ = _desktop_file_client(monkeypatch, project_root)
|
|
file_params = {**SCOPE_PARAMS, "path": "report.pdf"}
|
|
|
|
assert client.get("/files/stream", params=file_params).status_code == 401
|
|
assert client.head("/files/stream", params=file_params).status_code == 401
|
|
assert client.post("/files/access", json=SCOPE_PARAMS).status_code == 401
|
|
|
|
minted = client.post(
|
|
"/files/access", json=SCOPE_PARAMS, headers=CAPABILITY
|
|
)
|
|
assert minted.status_code == 200
|
|
access = minted.json()["access"]
|
|
assert minted.json()["expires_at"] > time.time()
|
|
granted = {**file_params, "access": access}
|
|
|
|
ranged = client.get(
|
|
"/files/stream", params=granted, headers={"Range": "bytes=2-5"}
|
|
)
|
|
assert ranged.status_code == 206
|
|
assert ranged.content == b"2345"
|
|
assert client.head("/files/stream", params=granted).status_code == 200
|
|
|
|
# A grant covers only the file root it was issued for.
|
|
for other_root in (
|
|
{"project_id": "project-2"},
|
|
{"email": "other@example.com"},
|
|
{"space_id": "space-1"},
|
|
{"user_id": "7"},
|
|
):
|
|
response = client.get(
|
|
"/files/stream", params={**granted, **other_root}
|
|
)
|
|
assert response.status_code == 401
|
|
|
|
expired, _ = issue_file_access_grant(
|
|
FileAccessScope.of("user@example.com", "project-1"),
|
|
now=time.time() - 2 * 60 * 60,
|
|
)
|
|
for invalid in (expired, f"{access}x", "user@example.com"):
|
|
response = client.get(
|
|
"/files/stream", params={**file_params, "access": invalid}
|
|
)
|
|
assert response.status_code == 401
|
|
|
|
remote = TestClient(client.app, client=("203.0.113.8", 50000))
|
|
assert remote.get("/files/stream", params=granted).status_code == 403
|
|
|
|
|
|
def test_desktop_named_pdf_stream_requires_a_grant(monkeypatch, tmp_path):
|
|
"""The display-name route the PDF viewer uses is guarded like the bare one."""
|
|
|
|
project_root = tmp_path / "project"
|
|
project_root.mkdir()
|
|
(project_root / "report.pdf").write_bytes(b"0123456789")
|
|
client, _ = _desktop_file_client(monkeypatch, project_root)
|
|
file_params = {**SCOPE_PARAMS, "path": "report.pdf"}
|
|
named = "/files/stream/report.pdf"
|
|
|
|
assert client.get(named, params=file_params).status_code == 401
|
|
assert client.head(named, params=file_params).status_code == 401
|
|
other_root, _ = issue_file_access_grant(
|
|
FileAccessScope.of("user@example.com", "project-2")
|
|
)
|
|
assert (
|
|
client.get(
|
|
named, params={**file_params, "access": other_root}
|
|
).status_code
|
|
== 401
|
|
)
|
|
|
|
access = client.post(
|
|
"/files/access", json=SCOPE_PARAMS, headers=CAPABILITY
|
|
).json()["access"]
|
|
granted = {**file_params, "access": access}
|
|
ranged = client.get(named, params=granted, headers={"Range": "bytes=2-5"})
|
|
assert ranged.status_code == 206
|
|
assert ranged.content == b"2345"
|
|
assert ranged.headers["content-range"] == "bytes 2-5/10"
|
|
head = client.head(named, params=granted)
|
|
assert head.status_code == 200
|
|
assert head.headers["content-length"] == "10"
|
|
|
|
|
|
def test_desktop_file_preview_reads_the_root_named_by_its_grant(
|
|
monkeypatch, tmp_path
|
|
):
|
|
project_root = tmp_path / "project"
|
|
(project_root / "site" / "assets").mkdir(parents=True)
|
|
(project_root / "site" / "index.html").write_text("<p>report</p>")
|
|
(project_root / "site" / "assets" / "app.css").write_text("p{}")
|
|
client, roots = _desktop_file_client(monkeypatch, project_root)
|
|
access = client.post(
|
|
"/files/access",
|
|
json={**SCOPE_PARAMS, "space_id": "space-1"},
|
|
headers=CAPABILITY,
|
|
).json()["access"]
|
|
|
|
page = client.get(f"/files/preview/{access}/site/index.html")
|
|
assert page.status_code == 200
|
|
assert page.text == "<p>report</p>"
|
|
# Relative references inside the page keep the grant segment.
|
|
asset = client.get(f"/files/preview/{access}/site/assets/app.css")
|
|
assert asset.status_code == 200
|
|
assert asset.text == "p{}"
|
|
assert roots[-1] == ("user@example.com", "project-1", "space-1", None)
|
|
|
|
invalid = client.get("/files/preview/not-a-grant/site/index.html")
|
|
assert invalid.status_code == 401
|
|
legacy_url = "/files/preview/user@example.com/project-1/site/index.html"
|
|
assert client.get(legacy_url).status_code == 401
|
|
|
|
|
|
def test_desktop_file_listing_and_upload_require_the_capability(
|
|
monkeypatch, tmp_path
|
|
):
|
|
project_root = tmp_path / "project"
|
|
project_root.mkdir()
|
|
(project_root / "notes.txt").write_text("notes")
|
|
workspace_root = tmp_path / "workspace"
|
|
client, _ = _desktop_file_client(monkeypatch, project_root)
|
|
monkeypatch.setattr(
|
|
file_controller, "_get_workspace_root", lambda: workspace_root
|
|
)
|
|
|
|
assert client.get("/files", params=SCOPE_PARAMS).status_code == 401
|
|
listing = client.get("/files", params=SCOPE_PARAMS, headers=CAPABILITY)
|
|
assert listing.status_code == 200
|
|
assert [item["filename"] for item in listing.json()] == ["notes.txt"]
|
|
|
|
upload = {"file": ("note.txt", b"hello", "text/plain")}
|
|
session = {"X-Session-ID": "session-1"}
|
|
rejected = client.post("/files", files=upload, headers=session)
|
|
assert rejected.status_code == 401
|
|
assert not workspace_root.exists()
|
|
uploaded = client.post(
|
|
"/files", files=upload, headers={**session, **CAPABILITY}
|
|
)
|
|
assert uploaded.status_code == 200
|
|
|
|
|
|
def test_web_mode_keeps_streams_open_and_previews_use_grants(
|
|
monkeypatch, tmp_path
|
|
):
|
|
"""A Brain without the Desktop capability keeps router-level auth."""
|
|
|
|
monkeypatch.delenv("EIGENT_RUNTIME", raising=False)
|
|
monkeypatch.delenv("EIGENT_LOCAL_CONTROL_CAPABILITY", raising=False)
|
|
project_root = tmp_path / "project"
|
|
project_root.mkdir()
|
|
(project_root / "index.html").write_text("<p>web</p>")
|
|
monkeypatch.setattr(
|
|
file_controller,
|
|
"_resolve_file_root",
|
|
lambda *_args, **_kwargs: project_root,
|
|
)
|
|
app = FastAPI()
|
|
register_routers(app)
|
|
client = TestClient(app)
|
|
|
|
stream = client.get(
|
|
"/files/stream", params={**SCOPE_PARAMS, "path": "index.html"}
|
|
)
|
|
assert stream.status_code == 200
|
|
named = client.head(
|
|
"/files/stream/index.html",
|
|
params={**SCOPE_PARAMS, "path": "index.html"},
|
|
)
|
|
assert named.status_code == 200
|
|
assert client.get("/files", params=SCOPE_PARAMS).status_code == 200
|
|
|
|
access = client.post("/files/access", json=SCOPE_PARAMS).json()["access"]
|
|
preview = client.get(f"/files/preview/{access}/index.html")
|
|
assert preview.status_code == 200
|
|
assert preview.text == "<p>web</p>"
|
|
invalid = client.get("/files/preview/not-a-grant/index.html")
|
|
assert invalid.status_code == 401
|