Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

4.0 KB
Raw Blame
import type { CapabilityRunContext } from "@/lib/capabilities/tool";
import { activeWorkflowForActor } from "@/lib/workflow";
import os from "node:os";
import path from "node:path";
import { promises as fs } from "node:fs";

function absolute(value: string): string {
  if (value === "~") return os.homedir();
  if (value.startsWith("~/")) return path.join(os.homedir(), value.slice(2));
  return path.resolve(value);
}

function inside(root: string, candidate: string): boolean {
  const rel = path.relative(root, candidate);
  return rel === "" || (!rel.startsWith("..") && !path.isAbsolute(rel));
}

async function canonicalWorkspaceTarget(root: string, target: string): Promise<void> {
  const canonicalRoot = await fs.realpath(root);
  let current = root;
  for (const part of path.relative(root, target).split(path.sep).filter(Boolean)) {
    current = path.join(current, part);
    const stat = await fs.lstat(current).catch((error: NodeJS.ErrnoException) => { if (error.code === "ENOENT") return null; throw error; });
    if (!stat) break;
    if (stat.isSymbolicLink() || !inside(canonicalRoot, await fs.realpath(current))) throw new Error("source-isolated workflow cannot traverse workspace symlinks");
  }
}

async function isolatedWorkspace(context: CapabilityRunContext) {
  const actor = context.workflowActor ?? context.actor;
  if (!actor || !context.workflowId) return null;
  const workflow = await activeWorkflowForActor(actor, context.workflowId);
  if (!workflow?.orchestration || workflow.orchestration.isolation !== "isolated-worktree") return null;
  const project = workflow.project ? absolute(workflow.project) : null;
  const workspace = workflow.orchestration.workspacePath ? absolute(workflow.orchestration.workspacePath) : null;
  if (!project || !workspace) throw new Error("isolated source workflow has no verified workspace; cancel/restart it before mutation");
  return { project, workspace, workflowId: workflow.id };
}

/** Mutating filesystem tools are workspace-bound once workflow_start selected
 * isolated source work. The guard is deliberately stricter than the generic FS
 * jail: source tasks do not get to write elsewhere on the host just because an
 * Owner token technically could. */
export async function requireWorkflowMutationPath(context: CapabilityRunContext, rawPath: string): Promise<void> {
  const isolated = await isolatedWorkspace(context);
  if (!isolated || isolated.workspace === isolated.project) return;
  const target = absolute(rawPath);
  if (!inside(isolated.workspace, target)) {
    throw new Error(`workflow ${isolated.workflowId} is source-isolated; mutate only inside ${isolated.workspace}`);
  }
  await canonicalWorkspaceTarget(isolated.workspace, target);
}

/** Shell is the widest authority surface. During isolated source work it must
 * have an explicit cwd inside the task worktree; host/production commands belong
 * in a separate reviewed workflow. */
export async function requireWorkflowExecCwd(context: CapabilityRunContext, rawCwd?: string): Promise<void> {
  const isolated = await isolatedWorkspace(context);
  if (!isolated || isolated.workspace === isolated.project) return;
  if (!rawCwd) throw new Error(`workflow ${isolated.workflowId} is source-isolated; exec requires cwd inside ${isolated.workspace}`);
  const cwd = absolute(rawCwd);
  if (!inside(isolated.workspace, cwd)) {
    throw new Error(`workflow ${isolated.workflowId} is source-isolated; exec cwd must stay inside ${isolated.workspace}`);
  }
  await canonicalWorkspaceTarget(isolated.workspace, cwd);
}

export async function requireWorkflowProjectTarget(context: CapabilityRunContext, projectPath: string): Promise<void> {
  const isolated = await isolatedWorkspace(context);
  if (!isolated || isolated.workspace === isolated.project) return;
  if (!inside(isolated.workspace, absolute(projectPath))) {
    throw new Error(`workflow ${isolated.workflowId} is source-isolated; project execution must target ${isolated.workspace}`);
  }
  await canonicalWorkspaceTarget(isolated.workspace, absolute(projectPath));
}