fix(webui): enforce workspace authority across queued inputs

Keep workspace changes at the authenticated owner boundary. Queue follow-up inputs separately when their tool scope differs from the active run.

Co-authored-by: Tenable Research <152317477+tenbbughunters@users.noreply.github.com>
This commit is contained in:
Xubin RenandTenable Research committed 2026-11-04 23:33:49 +08:00
1 parent 4dd3fbecb6
commit 219dd13fd8
12 files changed
+381 -36

No files matched your search

@@ -17,6 +17,7 @@ from nanobot.cron.session_turns import CRON_DEFER_UNTIL_IDLE_META, CRON_TRIGGER_
from nanobot.goal.assembly import install_goal
from nanobot.providers.base import GenerationSettings, LLMResponse, LLMUsage, ToolCallRequest
from nanobot.runtime_context import public_history_messages
from nanobot.security.workspace_access import current_workspace_scope
from nanobot.session.automation_turns import AUTOMATION_HISTORY_META
from nanobot.session.goal_state import GOAL_STATE_KEY
from nanobot.session.history_visibility import is_hidden_history_message
@@ -48,6 +49,49 @@ async def loop(tmp_path):
await agent.aclose()
@pytest.mark.parametrize("followup_scope", ["same", "restricted", "other-project"])
async def test_unified_followup_keeps_its_workspace_authority(loop, tmp_path, followup_scope):
loop._unified_session = True
project = tmp_path / "other-project"
project.mkdir()
initial_scope = {"project_path": str(tmp_path), "access_mode": "full"}
next_scope = {
"project_path": str(project if followup_scope == "other-project" else tmp_path),
"access_mode": "restricted" if followup_scope == "restricted" else "full",
}
observed = []
async def chat(**kwargs):
scope = current_workspace_scope()
observed.append(scope.metadata())
if len(observed) == 1:
await loop.bus.publish_inbound(InboundMessage(
channel="websocket", sender_id="client", chat_id="followup", content="second",
metadata={"workspace_scope": next_scope},
))
async with asyncio.timeout(3):
while loop._pending_queues["unified:default"].empty():
await asyncio.sleep(0)
return LLMResponse(content="done")
loop.provider.chat_stream_with_retry = chat
loop.runner.run = AsyncMock(wraps=loop.runner.run)
worker = asyncio.create_task(loop.run())
try:
await loop.bus.publish_inbound(InboundMessage(
channel="websocket", sender_id="owner", chat_id="initial", content="first",
metadata={"workspace_scope": initial_scope},
))
async with asyncio.timeout(5):
while not observed or "unified:default" in loop._pending_queues:
await asyncio.sleep(0.01)
assert observed == [initial_scope, next_scope]
assert loop.runner.run.await_count == (1 if followup_scope == "same" else 2)
finally:
loop.stop()
await asyncio.wait_for(worker, timeout=3)
@pytest.mark.parametrize("rejection", ["stop", "followup", "replaced-followup", "idle"])
async def test_rejected_input_does_not_stop_other_sessions(loop, monkeypatch, rejection):
state = loop.sessions.state
+12 -4
View File
@@ -500,7 +500,8 @@ async def test_access_changes_refresh_only_new_message_and_preserve_prefix(tmp_p
@pytest.mark.asyncio
async def test_injected_channel_input_reports_active_tool_scope(tmp_path: Path) -> None:
@pytest.mark.parametrize("same_scope", [True, False])
async def test_followup_only_joins_matching_tool_scope(tmp_path: Path, same_scope: bool) -> None:
project = tmp_path / "project"
project.mkdir()
(project / "marker.txt").write_text("active project", encoding="utf-8")
@@ -517,7 +518,8 @@ async def test_injected_channel_input_reports_active_tool_scope(tmp_path: Path)
session.add_message("user", "Start")
await loop.sessions.state.prepare_input(session)
pending = asyncio.Queue()
await pending.put(InboundMessage("telegram", "user", "chat", "Read marker.txt"))
followup = InboundMessage("websocket" if same_scope else "telegram", "user", "chat", "Read marker.txt")
await pending.put(followup)
await loop._run_agent_loop(
TranscriptInput(history=session.get_history(), current_message=None),
runtime=loop.llm_runtime(), session=session, pending_queue=pending,
@@ -525,8 +527,14 @@ async def test_injected_channel_input_reports_active_tool_scope(tmp_path: Path)
)
calls = loop.provider.chat_stream_with_retry.await_args_list
context = calls[0].kwargs["messages"][-1]["content"]
assert json.dumps(str(project.resolve())) in context
assert "File access: project and tool-specific allowed paths only." in context
if same_scope:
assert json.dumps(str(project.resolve())) in context
assert "File access: project and tool-specific allowed paths only." in context
assert pending.empty()
else:
assert context == "Start"
assert pending.get_nowait() is followup
assert pending.empty()
assert any(message.get("role") == "tool" and "active project" in message["content"]
for message in calls[1].kwargs["messages"])
+30
View File
@@ -112,6 +112,36 @@ async def test_webui_default_access_applies_to_unscoped_old_sessions(tmp_path, m
assert new_scope.access_mode == "full"
@pytest.mark.parametrize("scope_present", [False, True])
async def test_old_session_scope_defaults_respect_connection_authority(
tmp_path, monkeypatch, scope_present: bool,
) -> None:
monkeypatch.setattr("nanobot.webui.workspaces.get_webui_dir", lambda: tmp_path / "webui")
default = tmp_path / "default"
default.mkdir()
sessions = SessionManager(tmp_path / "sessions")
session = sessions.get_or_create("websocket:legacy")
if scope_present:
session.metadata[WORKSPACE_SCOPE_METADATA_KEY] = None
sessions.save(session)
write_webui_default_access_mode("full")
controller = WebUIWorkspaceController(
session_manager=sessions, default_workspace=default, default_restrict_to_workspace=True,
)
client_scope = await controller.scope_for_message(
{}, chat_id="legacy", chat_running=False, can_change_project=False,
)
owner_scope = await controller.scope_for_message(
{}, chat_id="legacy", chat_running=False, can_change_project=True,
)
assert client_scope.project_path == default.resolve()
assert client_scope.access_mode == "restricted"
assert owner_scope.access_mode == ("restricted" if scope_present else "full")
assert sessions.read_session_metadata("websocket:legacy")["metadata"] == session.metadata
def test_indexed_scope_preserves_missing_and_explicit_null_semantics(tmp_path, monkeypatch) -> None:
monkeypatch.setattr("nanobot.webui.workspaces.get_webui_dir", lambda: tmp_path / "webui")
default = tmp_path / "default"