mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-09 12:40:59 +00:00
e1ac5e0fc8
* Document multi-tenant workspace architecture * Add OSS and commercial workspace boundaries * docs: redesign multi-tenant workspace architecture * feat(tenancy): implement workspace isolation * docs(tenancy): record verification evidence * docs(tenancy): revise single-instance SaaS topology * docs(tenancy): refine architecture options * docs: finalize cloud v2 multi-tenant decisions * feat(tenancy): establish cloud isolation foundations * feat(tenancy): harden shared cloud runtime boundaries * docs(tenancy): record final isolation verification * fix(tenancy): close isolation and permission gaps * docs(tenancy): record final isolation verification * feat(tenancy): connect cloud workspace control plane * fix(build): install git for pinned SDK * docs(cloud): update control plane verification * chore: update multi-tenant SDK pin * fix(cloud): skip legacy model sync during startup * test(cloud): preserve minimal model manager fixtures * fix(cloud): preserve authenticated account context * fix(cloud): reuse authenticated account for user info * feat(cloud): complete Workspace settings navigation * test(web): cover Workspace dropdown menu * feat(web): place workspace controls in sidebar * refactor(web): streamline workspace controls * style(web): format workspace layout test * fix(cloud): surface runtime and workspace plan status * fix(plugin): keep runtime identity stable across restarts * fix(ui): widen and center workspace switcher * fix(ui): hide roles from workspace switcher * fix(ui): align workspace switcher with sidebar entries * feat(workspace): add in-product collaboration and direct Cloud launch * style: format collaboration changes * fix(workspace): bind collaboration APIs to tenant UoW * fix(cloud): preserve Core-owned collaboration state * test(cloud): require Space identity for invite registration * feat(cloud): complete secure invitation experience * style(web): format invitation flows * fix(cloud): recover box runtime without unscoped skill reload * feat(oss): enforce invitation account and owner billing flows * style: format OSS account service * test(oss): cover invitation logout handoff * fix(oss): resolve workspace owner in scoped session * feat(cloud): harden multi-tenant runtime resources * fix(cloud): bound runtime restart storms * fix(cloud): eliminate periodic runtime CPU spikes * fix(cloud): enforce instance capacity ceilings * fix(cloud): scope public login capability discovery * fix(cloud): bound tenant maintenance and monitoring work * fix(runtime): bound tenant resource amplification * fix(deps): pin green multi-tenant plugin SDK * fix(cloud): handle unavailable skill capability * fix(security): require authentication for image file endpoint (H-2) - Changed /api/v1/files/image from AuthType.NONE to USER_TOKEN_OR_API_KEY - Added Permission.RESOURCE_VIEW requirement - Prevents unauthenticated cross-tenant file access via leaked keys - Fixes HIGH severity finding from multi-tenant security review docs: add comprehensive database migration guide - Complete migration steps for OSS → multi-tenant - Backup, execution, verification procedures - Rollback scenarios and recovery plans - Performance tuning recommendations * test: add comprehensive cross-tenant isolation tests Added 7 critical test scenarios for multi-tenant boundaries: - Cross-tenant bot access prevention - Viewer role read-only enforcement - Removed member immediate access revocation - Model provider credential isolation - WebSocket message isolation - Invitation token workspace scoping - Multi-workspace context validation These tests address P0-2 coverage gaps for: - workspaces.py (membership & invitation flows) - user.py (authentication & authorization) - websocket_chat.py (real-time isolation) - plugins.py (resource access control) docs: finalize database migration guide * fix(security): resolve M-1, M-2, M-3 security findings M-1: WebSocket authorization TOCTOU race (FIXED) - Changed _revalidate_websocket_authorization to return RequestContext - Ensures validated context is used immediately without race window - Prevents removed members from sending messages during revalidation gap M-2: Model Manager cache workspace isolation (VERIFIED) - Confirmed _CacheKey already uses 4-tuple: (instance, workspace, generation, resource) - Cache is properly scoped per workspace, no cross-tenant leakage possible - No code change needed, documented as working correctly M-3: Invitation lock workspace scoping (FIXED) - Changed lock key from token_digest to workspace_uuid:token_digest - Prevents DoS where attacker locks token in Workspace A to block Workspace B - Locks now isolated per workspace All MEDIUM severity findings from security review now resolved. * fix(cloud): unblock tenant CI and enforce knowledge quotas * fix(tenancy): scope rerank model sync --------- Co-authored-by: dadachann <185672915+dadachann@users.noreply.github.com>
215 lines
7.3 KiB
Python
215 lines
7.3 KiB
Python
from __future__ import annotations
|
|
|
|
import re
|
|
import typing
|
|
|
|
from ....box import workspace as box_workspace
|
|
from ....api.http.context import ExecutionContext
|
|
|
|
if typing.TYPE_CHECKING:
|
|
from ....core import app
|
|
from langbot_plugin.api.entities.events import pipeline_query
|
|
|
|
ACTIVATED_SKILLS_KEY = '_activated_skills'
|
|
PIPELINE_BOUND_SKILLS_KEY = '_pipeline_bound_skills'
|
|
SKILL_MOUNT_PREFIX = '/workspace/.skills'
|
|
_SKILL_MOUNT_PATTERN = re.compile(r'/workspace/\.skills/([A-Za-z0-9_-]+)')
|
|
|
|
|
|
def get_virtual_skill_mount_path(skill_name: str) -> str:
|
|
return f'{SKILL_MOUNT_PREFIX}/{skill_name}'
|
|
|
|
|
|
def get_bound_skill_names(query: pipeline_query.Query) -> list[str] | None:
|
|
if query.variables is None:
|
|
return None
|
|
|
|
bound_skills = query.variables.get(PIPELINE_BOUND_SKILLS_KEY)
|
|
if bound_skills is None:
|
|
return None
|
|
if isinstance(bound_skills, list):
|
|
return [str(item) for item in bound_skills]
|
|
return None
|
|
|
|
|
|
def get_visible_skills(ap: app.Application, query: pipeline_query.Query) -> dict[str, dict]:
|
|
skill_mgr = getattr(ap, 'skill_mgr', None)
|
|
if skill_mgr is None:
|
|
return {}
|
|
|
|
execution_context = ExecutionContext(
|
|
instance_uuid=str(getattr(query, 'instance_uuid', '') or ''),
|
|
workspace_uuid=str(getattr(query, 'workspace_uuid', '') or ''),
|
|
placement_generation=getattr(query, 'placement_generation', 0) or 0,
|
|
bot_uuid=getattr(query, 'bot_uuid', None),
|
|
pipeline_uuid=getattr(query, 'pipeline_uuid', None),
|
|
query_uuid=getattr(query, 'query_uuid', None),
|
|
)
|
|
visible_skills = skill_mgr.get_skills(execution_context)
|
|
bound_skills = get_bound_skill_names(query)
|
|
if bound_skills is None:
|
|
return visible_skills
|
|
|
|
return {skill_name: skill_data for skill_name, skill_data in visible_skills.items() if skill_name in bound_skills}
|
|
|
|
|
|
def get_visible_skill(ap: app.Application, query: pipeline_query.Query, skill_name: str) -> dict | None:
|
|
return get_visible_skills(ap, query).get(skill_name)
|
|
|
|
|
|
def get_activated_skills(query: pipeline_query.Query) -> dict[str, dict]:
|
|
if query.variables is None:
|
|
return {}
|
|
|
|
activated = query.variables.get(ACTIVATED_SKILLS_KEY, {})
|
|
if not isinstance(activated, dict):
|
|
return {}
|
|
return activated
|
|
|
|
|
|
def get_activated_skill(query: pipeline_query.Query, skill_name: str) -> dict | None:
|
|
return get_activated_skills(query).get(skill_name)
|
|
|
|
|
|
def register_activated_skill(query: pipeline_query.Query, skill_data: dict) -> None:
|
|
if query.variables is None:
|
|
query.variables = {}
|
|
|
|
activated = query.variables.setdefault(ACTIVATED_SKILLS_KEY, {})
|
|
skill_name = str(skill_data.get('name', '') or '').strip()
|
|
if skill_name and skill_name not in activated:
|
|
activated[skill_name] = skill_data
|
|
|
|
|
|
def normalize_skill_names(value: typing.Any) -> list[str]:
|
|
"""Return a de-duplicated list of non-empty skill names."""
|
|
if not isinstance(value, list):
|
|
return []
|
|
|
|
names: list[str] = []
|
|
for item in value:
|
|
skill_name = str(item or '').strip()
|
|
if skill_name and skill_name not in names:
|
|
names.append(skill_name)
|
|
return names
|
|
|
|
|
|
def get_activated_skill_names(query: pipeline_query.Query) -> list[str]:
|
|
"""Return activated skill names for callers that own persistence policy."""
|
|
return normalize_skill_names(list(get_activated_skills(query).keys()))
|
|
|
|
|
|
def restore_activated_skills(
|
|
ap: app.Application,
|
|
query: pipeline_query.Query,
|
|
skill_names: typing.Any,
|
|
) -> list[str]:
|
|
"""Restore caller-provided activated skill names into Query variables.
|
|
|
|
Persistence and state scope ownership belong to higher-level flows. This
|
|
helper only rebuilds current Query state from pipeline-visible skills, so
|
|
removed or unbound skills stay unavailable to native exec/write/edit.
|
|
"""
|
|
restored: list[str] = []
|
|
for skill_name in normalize_skill_names(skill_names):
|
|
skill_data = get_visible_skill(ap, query, skill_name)
|
|
if skill_data is None:
|
|
continue
|
|
register_activated_skill(query, skill_data)
|
|
restored.append(skill_name)
|
|
return restored
|
|
|
|
|
|
def parse_skill_mount_path(sandbox_path: str) -> tuple[str | None, str]:
|
|
normalized_path = str(sandbox_path or '/workspace').strip() or '/workspace'
|
|
if normalized_path == SKILL_MOUNT_PREFIX:
|
|
raise ValueError(f'Path must include a skill name under {SKILL_MOUNT_PREFIX}/<skill-name>.')
|
|
prefix = f'{SKILL_MOUNT_PREFIX}/'
|
|
if not normalized_path.startswith(prefix):
|
|
return None, normalized_path
|
|
|
|
remainder = normalized_path[len(prefix) :]
|
|
skill_name, separator, tail = remainder.partition('/')
|
|
if not skill_name:
|
|
raise ValueError(f'Path must include a skill name under {SKILL_MOUNT_PREFIX}/<skill-name>.')
|
|
|
|
rewritten_path = '/workspace'
|
|
if separator:
|
|
rewritten_path = f'/workspace/{tail}'
|
|
return skill_name, rewritten_path
|
|
|
|
|
|
def resolve_virtual_skill_path(
|
|
ap: app.Application,
|
|
query: pipeline_query.Query,
|
|
sandbox_path: str,
|
|
*,
|
|
include_visible: bool,
|
|
include_activated: bool,
|
|
) -> tuple[dict | None, str]:
|
|
skill_name, rewritten_path = parse_skill_mount_path(sandbox_path)
|
|
if skill_name is None:
|
|
return None, rewritten_path
|
|
|
|
if include_activated:
|
|
activated_skill = get_activated_skill(query, skill_name)
|
|
if activated_skill is not None:
|
|
return activated_skill, rewritten_path
|
|
|
|
if include_visible:
|
|
visible_skill = get_visible_skill(ap, query, skill_name)
|
|
if visible_skill is not None:
|
|
return visible_skill, rewritten_path
|
|
|
|
activated_names = ', '.join(sorted(get_activated_skills(query).keys())) or 'none'
|
|
visible_names = ', '.join(sorted(get_visible_skills(ap, query).keys())) or 'none'
|
|
raise ValueError(
|
|
f'Skill "{skill_name}" is not available at this path. '
|
|
f'Activated skills: {activated_names}. Visible skills: {visible_names}.'
|
|
)
|
|
|
|
|
|
def find_referenced_skill_names(text: str) -> list[str]:
|
|
if not text:
|
|
return []
|
|
|
|
seen: list[str] = []
|
|
for match in _SKILL_MOUNT_PATTERN.findall(text):
|
|
if match not in seen:
|
|
seen.append(match)
|
|
return seen
|
|
|
|
|
|
def rewrite_command_for_skill_mount(command: str, skill_name: str) -> str:
|
|
virtual_root = get_virtual_skill_mount_path(skill_name)
|
|
rewritten = command.replace(f'{virtual_root}/', '/workspace/')
|
|
return rewritten.replace(virtual_root, '/workspace')
|
|
|
|
|
|
def build_skill_session_id(skill_data: dict, query: pipeline_query.Query) -> str:
|
|
skill_identifier = str(skill_data.get('name', 'unknown') or 'unknown')
|
|
launcher_type = getattr(query, 'launcher_type', None)
|
|
launcher_id = getattr(query, 'launcher_id', None)
|
|
query_id = getattr(query, 'query_id', 'unknown')
|
|
|
|
if launcher_type is not None and launcher_id is not None:
|
|
return f'skill-{launcher_type}_{launcher_id}-{skill_identifier}'
|
|
return f'skill-{query_id}-{skill_identifier}'
|
|
|
|
|
|
def should_prepare_skill_python_env(package_root: str | None) -> bool:
|
|
return box_workspace.should_prepare_python_env(package_root)
|
|
|
|
|
|
def wrap_skill_command_with_python_env(
|
|
command: str,
|
|
*,
|
|
mount_path: str = '/workspace',
|
|
state_path: str | None = None,
|
|
) -> str:
|
|
return box_workspace.wrap_python_command_with_env(
|
|
command,
|
|
mount_path=mount_path,
|
|
state_path=state_path,
|
|
).rstrip()
|